sum: sumAll

This commit is contained in:
vinchent 2024-09-10 21:34:27 +02:00
parent fc419ec3be
commit e905c600f3
2 changed files with 16 additions and 12 deletions

View File

@ -8,3 +8,7 @@ func Sum(numbers []int) int {
} }
return sum return sum
} }
func SumAll(x []int, y []int) []int {
return []int{Sum(x), Sum(y)}
}

View File

@ -1,6 +1,9 @@
package sum package sum
import "testing" import (
"slices"
"testing"
)
func TestSum(t *testing.T) { func TestSum(t *testing.T) {
t.Run("collection of 5 numbers", func(t *testing.T) { t.Run("collection of 5 numbers", func(t *testing.T) {
@ -13,15 +16,12 @@ func TestSum(t *testing.T) {
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} func TestSumAll(t *testing.T) {
got := SumAll([]int{1, 2}, []int{0, 9})
got := Sum(numbers) exp := []int{3, 9}
exp := 6 if slices.Compare(got, exp) != 0 {
t.Errorf("got %d, expected %d", got, exp)
if got != exp { }
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
}
})
} }