Add package arrays
This commit is contained in:
parent
cb26678c66
commit
3569bcd485
9
arrays/sum.go
Normal file
9
arrays/sum.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func Sum(numbers []int) int {
|
||||||
|
sum := 0
|
||||||
|
for _, n := range numbers {
|
||||||
|
sum += n
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
29
arrays/sum_test.go
Normal file
29
arrays/sum_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSum(t *testing.T) {
|
||||||
|
|
||||||
|
t.Run("collection of 5 numbers", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3, 4, 5}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
want := 15
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("collection of any size", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
want := 6
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user