go-by-test/sum/sum_test.go

51 lines
952 B
Go
Raw Normal View History

2024-09-10 19:15:12 +00:00
package sum
2024-09-10 19:34:27 +00:00
import (
2024-09-10 19:43:55 +00:00
"slices"
2024-09-10 19:34:27 +00:00
"testing"
)
2024-09-10 19:15:12 +00:00
func TestSum(t *testing.T) {
2024-09-10 19:27:13 +00:00
t.Run("collection of 5 numbers", func(t *testing.T) {
numbers := []int{1, 2, 3, 4, 5}
2024-09-10 19:15:12 +00:00
2024-09-10 19:27:13 +00:00
got := Sum(numbers)
exp := 15
2024-09-10 19:15:12 +00:00
2024-09-10 19:27:13 +00:00
if got != exp {
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
}
})
2024-09-10 19:34:27 +00:00
}
2024-09-10 19:27:13 +00:00
2024-09-10 19:34:27 +00:00
func TestSumAll(t *testing.T) {
got := SumAll([]int{1, 2}, []int{0, 9})
exp := []int{3, 9}
2024-09-10 19:40:56 +00:00
2024-09-10 19:43:55 +00:00
if !slices.Equal(got, exp) {
2024-09-10 19:34:27 +00:00
t.Errorf("got %d, expected %d", got, exp)
}
2024-09-10 19:15:12 +00:00
}
2024-09-10 19:51:21 +00:00
func TestSumAllTails(t *testing.T) {
2024-09-10 19:56:18 +00:00
checkSums := func(t *testing.T, got, exp []int) {
if !slices.Equal(got, exp) {
t.Errorf("got %d, expected %d", got, exp)
}
}
2024-09-10 19:51:21 +00:00
t.Run("make the sums of some slices", func(t *testing.T) {
got := SumAllTails([]int{1, 2}, []int{0, 9})
exp := []int{2, 9}
2024-09-10 19:56:18 +00:00
checkSums(t, got, exp)
2024-09-10 19:51:21 +00:00
})
t.Run("safely sum empty slices", func(t *testing.T) {
got := SumAllTails([]int{}, []int{3, 4, 5})
exp := []int{0, 9}
2024-09-10 19:56:18 +00:00
checkSums(t, got, exp)
2024-09-10 19:51:21 +00:00
})
}