-
Notifications
You must be signed in to change notification settings - Fork 96
/
util_test.go
163 lines (153 loc) · 3.28 KB
/
util_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
163
package eaopt
import (
"testing"
)
func TestMin(t *testing.T) {
var testCases = [][]int{
{1, 2},
{2, 2},
{2, 3},
}
for _, test := range testCases {
if minInt(test[0], test[1]) != test[0] {
t.Error("min didn't find the smallest integer")
}
}
}
func TestSumFloat64s(t *testing.T) {
var testCases = []struct {
floats []float64
total float64
}{
{[]float64{1, 2, 3}, 6},
{[]float64{-1, 1}, 0},
{[]float64{1.42, 42.1}, 43.52},
}
for _, test := range testCases {
if sumFloat64s(test.floats) != test.total {
t.Error("sumFloat64s didn't work as expected")
}
}
}
func TestMinFloat64s(t *testing.T) {
var testCases = []struct {
floats []float64
min float64
}{
{[]float64{1.0}, 1.0},
{[]float64{1.0, 2.0}, 1.0},
{[]float64{-1.0, 1.0}, -1.0},
}
for _, test := range testCases {
if minFloat64s(test.floats) != test.min {
t.Error("meanFloat64s didn't work as expected")
}
}
}
func TestMaxFloat64s(t *testing.T) {
var testCases = []struct {
floats []float64
max float64
}{
{[]float64{1.0}, 1.0},
{[]float64{1.0, 2.0}, 2.0},
{[]float64{-1.0, 1.0}, 1.0},
}
for _, test := range testCases {
if maxFloat64s(test.floats) != test.max {
t.Error("maxFloat64s didn't work as expected")
}
}
}
func TestMeanFloat64s(t *testing.T) {
var testCases = []struct {
floats []float64
mean float64
}{
{[]float64{1.0}, 1.0},
{[]float64{1.0, 2.0}, 1.5},
{[]float64{-1.0, 1.0}, 0.0},
}
for _, test := range testCases {
if meanFloat64s(test.floats) != test.mean {
t.Error("meanFloat64s didn't work as expected")
}
}
}
func TestVarianceFloat64s(t *testing.T) {
var testCases = []struct {
floats []float64
variance float64
}{
{[]float64{1.0}, 0.0},
{[]float64{-1.0, 1.0}, 1.0},
{[]float64{-2.0, 2.0}, 4.0},
}
for _, test := range testCases {
if varianceFloat64s(test.floats) != test.variance {
t.Error("varianceFloat64s didn't work as expected")
}
}
}
func TestDivide(t *testing.T) {
var testCases = []struct {
floats []float64
value float64
divided []float64
}{
{[]float64{1, 1}, 1, []float64{1, 1}},
{[]float64{1, 1}, 2, []float64{0.5, 0.5}},
{[]float64{42, -42}, 21, []float64{2, -2}},
}
for _, test := range testCases {
var divided = divide(test.floats, test.value)
for i := range divided {
if divided[i] != test.divided[i] {
t.Error("divided didn't work as expected")
}
}
}
}
func TestCumsum(t *testing.T) {
var testCases = []struct {
floats []float64
summed []float64
}{
{[]float64{1, 2, 3, 4}, []float64{1, 3, 6, 10}},
{[]float64{-1, 0, 1}, []float64{-1, -1, 0}},
}
for _, test := range testCases {
var summed = cumsum(test.floats)
for i := range summed {
if summed[i] != test.summed[i] {
t.Error("cumsum didn't work as expected")
}
}
}
}
func TestUnion(t *testing.T) {
var testCases = []struct {
x set
y set
u set
}{
{
x: set{1: true, 2: true, 3: true},
y: set{4: true, 5: true, 6: true},
u: set{1: true, 2: true, 3: true, 4: true, 5: true, 6: true},
},
{
x: set{1: true, 2: true, 3: true},
y: set{2: true, 3: true, 4: true},
u: set{1: true, 2: true, 3: true, 4: true},
},
}
for _, test := range testCases {
var u = union(test.x, test.y)
for i := range u {
if !test.u[i] {
t.Error("union didn't work as expected")
}
}
}
}