-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
median_test.go
59 lines (52 loc) · 1.16 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
package stats_test
import (
"fmt"
"reflect"
"testing"
"github.com/montanaflynn/stats"
)
func ExampleMedian() {
data := []float64{1.0, 2.1, 3.2, 4.823, 4.1, 5.8}
median, _ := stats.Median(data)
fmt.Println(median)
// Output: 3.65
}
func TestMedian(t *testing.T) {
for _, c := range []struct {
in []float64
out float64
}{
{[]float64{5, 3, 4, 2, 1}, 3.0},
{[]float64{6, 3, 2, 4, 5, 1}, 3.5},
{[]float64{1}, 1.0},
} {
got, _ := stats.Median(c.in)
if got != c.out {
t.Errorf("Median(%.1f) => %.1f != %.1f", c.in, got, c.out)
}
}
_, err := stats.Median([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func BenchmarkMedianSmallFloatSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = stats.Median(makeFloatSlice(5))
}
}
func BenchmarkMedianLargeFloatSlice(b *testing.B) {
lf := makeFloatSlice(100000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = stats.Median(lf)
}
}
func TestMedianSortSideEffects(t *testing.T) {
s := []float64{0.1, 0.3, 0.2, 0.4, 0.5}
a := []float64{0.1, 0.3, 0.2, 0.4, 0.5}
_, _ = stats.Median(s)
if !reflect.DeepEqual(s, a) {
t.Errorf("%.1f != %.1f", s, a)
}
}