go-by-test/sum/sum_test.go

51 lines
952 B
Go

package sum
import (
"slices"
"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)
exp := 15
if got != exp {
t.Errorf("got %d, expected %d, given %v", got, exp, numbers)
}
})
}
func TestSumAll(t *testing.T) {
got := SumAll([]int{1, 2}, []int{0, 9})
exp := []int{3, 9}
if !slices.Equal(got, exp) {
t.Errorf("got %d, expected %d", got, exp)
}
}
func TestSumAllTails(t *testing.T) {
checkSums := func(t *testing.T, got, exp []int) {
if !slices.Equal(got, exp) {
t.Errorf("got %d, expected %d", got, exp)
}
}
t.Run("make the sums of some slices", func(t *testing.T) {
got := SumAllTails([]int{1, 2}, []int{0, 9})
exp := []int{2, 9}
checkSums(t, got, exp)
})
t.Run("safely sum empty slices", func(t *testing.T) {
got := SumAllTails([]int{}, []int{3, 4, 5})
exp := []int{0, 9}
checkSums(t, got, exp)
})
}