master
1package main
2
3import "reflect"
4import "testing"
5
6func TestSum(test *testing.T) {
7 test.Run("collection of 5 numbers", func(test *testing.T) {
8 numbers := []int{1, 2, 3, 4, 5}
9 got := Sum(numbers)
10 want := 15
11
12 if got != want {
13 test.Errorf("got %d want %d given, %v", got, want, numbers)
14 }
15 })
16
17 test.Run("collection of any size", func(test *testing.T) {
18 numbers := []int{1, 2, 3}
19 got := Sum(numbers)
20 want := 6
21
22 if got != want {
23 test.Errorf("got %d want %d given, %v", got, want, numbers)
24 }
25 })
26}
27
28func TestSumAll(t *testing.T) {
29 got := SumAll([]int{1, 2}, []int{0, 9})
30 want := []int{3, 9}
31
32 if !reflect.DeepEqual(got, want) {
33 t.Errorf("got %v want %v", got, want)
34 }
35}
36
37func TestSumAllTails(t *testing.T) {
38 checkSums := func(t *testing.T, got, want []int) {
39 t.Helper()
40 if !reflect.DeepEqual(got, want) {
41 t.Errorf("got %v want %v", got, want)
42 }
43 }
44
45 t.Run("make the sums of some slices", func(t *testing.T) {
46 got := SumAllTails([]int{1, 2}, []int{0, 9})
47 want := []int{2, 9}
48
49 checkSums(t, got, want)
50 })
51
52 t.Run("safely sum empty slices", func(t *testing.T) {
53 got := SumAllTails([]int{}, []int{3, 4, 5})
54 want := []int{0, 9}
55
56 checkSums(t, got, want)
57 })
58}