-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmedian_test.go
121 lines (112 loc) · 2.53 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright 2020 Binh Nguyen
Licensed under terms of MIT license (see LICENSE)
*/
package tago
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
)
func TestNewMedian(t *testing.T) {
tests := map[string]struct {
input int
want *Median
wantErr error
}{
"negative n": {input: -3, want: nil, wantErr: ErrInvalidParameters},
"zero n": {input: 0, want: nil, wantErr: ErrInvalidParameters},
"positive n": {input: 9, want: &Median{n: 9, data: make([]float64, 0, 9)}, wantErr: nil},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
gotSD, gotErr := NewMedian(tc.input)
if tc.wantErr != nil { // only check error returned if expecting one
assert.EqualError(t, gotErr, tc.wantErr.Error(), "must return the correct error")
}
assert.Equal(t, tc.want, gotSD, "must return the correct value")
})
}
}
func TestMedianNextOddLength(t *testing.T) {
sd, _ := NewMedian(3)
tests := []struct {
input float64
want float64
}{
{input: 10., want: 10.},
{input: 20., want: 15.},
{input: 30., want: 20.},
{input: 15., want: 20.},
{input: 40., want: 30.},
{input: 25., want: 25.},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
}
func TestMedianNextEvenLength(t *testing.T) {
sd, _ := NewMedian(4)
tests := []struct {
input float64
want float64
}{
{input: 10., want: 10.},
{input: 20., want: 15.},
{input: 30., want: 20.},
{input: 15., want: 17.5},
{input: 40., want: 25.},
{input: 25., want: 27.5},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
}
func TestMedianReset(t *testing.T) {
sd, _ := NewMedian(4)
tests := []struct {
input float64
want float64
}{
{input: 10., want: 10.},
{input: 20., want: 15.},
{input: 30., want: 20.},
{input: 15., want: 17.5},
{input: 40., want: 25.},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
got := sd.Next(tc.input)
diff := cmp.Diff(tc.want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
})
}
sd.Reset()
diff := cmp.Diff(25., sd.Next(25.), floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}
func TestMedianString(t *testing.T) {
sd, _ := NewMedian(4)
want := "Median(4)"
got := sd.String()
diff := cmp.Diff(want, got, floatComparer)
if diff != "" {
t.Fatalf(diff)
}
}