-
Notifications
You must be signed in to change notification settings - Fork 7
/
mean_test.go
67 lines (60 loc) · 1.14 KB
/
mean_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package statistics
import (
"fmt"
"testing"
)
func ExampleMean() {
mean := Mean(1, 1000)
fmt.Printf("The mean of [1 1000] is %0.1f.\n", mean)
// Output:
// The mean of [1 1000] is 500.5.
}
func TestMean(t *testing.T) {
cases := []testArrayCase{
{
name: "no numbers",
input: []uint{},
expected: "NaN",
},
{
name: "two numbers",
input: []uint{5, 15},
expected: "10",
},
{
name: "zero",
input: []int{-5, 5},
expected: "0",
},
{
name: "floats",
input: []float32{2.5, 4.5},
expected: "3.5",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
var actual float64
switch c.input.(type) {
case []uint:
actual = Mean(c.input.([]uint)...)
case []int:
actual = Mean(c.input.([]int)...)
case []float32:
actual = Mean(c.input.([]float32)...)
}
if fmt.Sprintf("%v", actual) != c.expected {
t.Logf("expected %v, got %v", c.expected, actual)
t.FailNow()
}
})
}
}
var testMeanVals []uint = make([]uint, 1000)
func BenchmarkMean(b *testing.B) {
for i := 0; i < b.N; i++ {
Mean(testMeanVals...)
}
}