sum: slice
This commit is contained in:
parent
b427b8fbfb
commit
fc419ec3be
@ -1,6 +1,6 @@
|
|||||||
package sum
|
package sum
|
||||||
|
|
||||||
func Sum(numbers [5]int) int {
|
func Sum(numbers []int) int {
|
||||||
sum := 0
|
sum := 0
|
||||||
|
|
||||||
for _, number := range numbers {
|
for _, number := range numbers {
|
||||||
|
@ -3,12 +3,25 @@ package sum
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestSum(t *testing.T) {
|
func TestSum(t *testing.T) {
|
||||||
numbers := [5]int{1, 2, 3, 4, 5}
|
t.Run("collection of 5 numbers", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
got := Sum(numbers)
|
got := Sum(numbers)
|
||||||
exp := 15
|
exp := 15
|
||||||
|
|
||||||
if got != exp {
|
if got != exp {
|
||||||
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
|
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("collection of any size", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
exp := 6
|
||||||
|
|
||||||
|
if got != exp {
|
||||||
|
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user