-
Notifications
You must be signed in to change notification settings - Fork 7
/
median_test.go
66 lines (60 loc) · 1.19 KB
/
median_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
package statistics
import (
"fmt"
"testing"
)
func ExampleMedian() {
median := Median(7, 5, 1, 4, 3, 6, 2)
fmt.Printf("The median of [7 5 1 4 3 6 2] is %v.\n", median)
// Output:
// The median of [7 5 1 4 3 6 2] is 4.
}
func TestMedian(t *testing.T) {
cases := []testArrayCase{
{
name: "no input",
input: []uint{},
expected: "float64 NaN",
},
{
name: "uints",
input: []uint{5, 15},
expected: "float64 10",
},
{
name: "float32s",
input: []float32{5, 15, 7.5, 11},
expected: "float64 9.25",
},
{
name: "ints",
input: []int{2, 1, 3},
expected: "float64 2",
},
}
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 = Median(c.input.([]uint)...)
case []int:
actual = Median(c.input.([]int)...)
case []float32:
actual = Median(c.input.([]float32)...)
}
r := fmt.Sprintf("%T %v", actual, actual)
if r != c.expected {
t.Logf("expected %v, got %v", c.expected, r)
t.FailNow()
}
})
}
}
func BenchmarkMedian(b *testing.B) {
for i := 0; i < b.N; i++ {
Median(1, -5, 8, 3, -13)
}
}