-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumeric_test.go
162 lines (150 loc) · 2.56 KB
/
numeric_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package collections
import (
"testing"
)
func TestSum(t *testing.T) {
testCases := []struct {
description string
sut []int
sum int
}{
{
"calling sum with empty slice",
[]int{},
0,
},
{
"calling sum with slice with values",
[]int{1, 2, 3, 4},
10,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
sum := Sum(tc.sut)
if sum != tc.sum {
t.Errorf("expected sum to be %d. got %d", tc.sum, sum)
}
})
}
}
func TestAverage(t *testing.T) {
testCases := []struct {
description string
sut []int
avg int
}{
{
"calling Average with empty slice",
[]int{},
0,
},
{
"calling Average with slice with values",
[]int{2, 2, 2, 2},
2,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
if avg := Average(tc.sut); avg != tc.avg {
t.Errorf("expected average to be %d. got %d", tc.avg, avg)
}
})
}
}
func TestMin(t *testing.T) {
testCases := []struct {
description string
sut []int
min int
}{
{
"calling min with empty slice",
[]int{},
0,
},
{
"calling min with slice with values",
[]int{4, 1, 3, 2},
1,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
if min := Min(tc.sut); min != tc.min {
t.Errorf("expected min value to be %d. got %d", tc.min, min)
}
})
}
}
func TestMax(t *testing.T) {
testCases := []struct {
description string
sut []int
max int
}{
{
"calling max with empty slice",
[]int{},
0,
},
{
"calling max with slice with values",
[]int{1, 4, 3, 2},
4,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
if max := Max(tc.sut); max != tc.max {
t.Errorf("expected max value to be %d. got %d", tc.max, max)
}
})
}
}
func TestMedian(t *testing.T) {
testCases := []struct {
description string
sut []int
median float64
}{
{
"1 and 2",
[]int{1, 2},
1.5,
},
{
"1 through 3",
[]int{1, 2, 3},
2.0,
},
{
"1 through 5",
[]int{1, 2, 3, 4, 5},
3.0,
},
{
"1 through 6",
[]int{1, 2, 3, 4, 5, 6},
3.5,
},
{
"Wikipedia example 1",
[]int{1, 3, 3, 6, 7, 8, 9},
6.0,
},
{
"Wikipedia example 2",
[]int{1, 2, 3, 4, 5, 7, 8, 9},
4.5,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
if median := Median(tc.sut); median != tc.median {
t.Errorf("expected median to be %f. Got %f", tc.median, median)
}
})
}
}