-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_test.go
47 lines (42 loc) · 876 Bytes
/
sum_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package vector
import "testing"
func TestSum(t *testing.T) {
array := [8]int16{1, 1, 2, 3, 5, 8, 13, 21}
for i := 0; i <= len(array); i++ {
slice := array[:i]
want := int16(0)
for _, v := range slice {
want += v
}
got := Sum(slice)
if want != got {
t.Fatalf("Sum: wanted %v, got %v", want, got)
}
}
}
func TestSumGeneric(t *testing.T) {
array := [8]int16{1, 1, 2, 3, 5, 8, 13, 21}
for i := 0; i <= len(array); i++ {
slice := array[:i]
want := int16(0)
for _, v := range slice {
want += v
}
got := SumGeneric(slice)
if want != got {
t.Fatalf("SumGeneric: wanted %v, got %v", want, got)
}
}
}
func BenchmarkSum(b *testing.B) {
slice := make([]int16, 4096)
for i := 0; i < b.N; i++ {
Sum(slice)
}
}
func BenchmarkSumGeneric(b *testing.B) {
slice := make([]int16, 4096)
for i := 0; i < b.N; i++ {
SumGeneric(slice)
}
}