forked from montanaflynn/stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_test.go
43 lines (39 loc) · 783 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
package stats
import (
"reflect"
"testing"
)
func TestSum(t *testing.T) {
for _, c := range []struct {
in []float64
out float64
}{
{[]float64{1, 2, 3}, 6},
{[]float64{1.0, 1.1, 1.2, 2.2}, 5.5},
{[]float64{1, -1, 2, -3}, -1},
} {
got, err := Sum(c.in)
if err != nil {
t.Errorf("Returned an error")
}
if !reflect.DeepEqual(c.out, got) {
t.Errorf("Sum(%.1f) => %.1f != %.1f", c.in, got, c.out)
}
}
_, err := Sum([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func BenchmarkSumSmallFloatSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
Sum(makeFloatSlice(5))
}
}
func BenchmarkSumLargeFloatSlice(b *testing.B) {
lf := makeFloatSlice(100000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum(lf)
}
}