Repository navigation
Expand file tree
/
Copy pathnumeric_test.go
More file actions
224 lines (192 loc) · 5.66 KB
/
Copy pathnumeric_test.go
File metadata and controls
224 lines (192 loc) · 5.66 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package linq
import (
"cmp"
"math"
"slices"
"testing"
)
func TestSum(t *testing.T) {
tests := []struct {
input []int
want int
}{
{[]int{1, 2, 2, 3, 1}, 9},
{[]int{1}, 1},
{[]int{}, 0},
}
for _, test := range tests {
if r := Sum(FromSlice(test.input)); r != test.want {
t.Errorf("Sum(FromSlice(%v))=%v expected %v", test.input, r, test.want)
}
}
if r := Sum(FromSlice([]uint{1, 2, 2, 3, 1})); r != 9 {
t.Errorf("Sum(uints)=%v expected 9", r)
}
if r := Sum(FromSlice([]float64{1., 2., 2., 3., 1.})); r != 9. {
t.Errorf("Sum(floats)=%v expected 9.", r)
}
}
func TestSumBy(t *testing.T) {
input := []string{"apple", "banana", "fig"}
want := 14
if r := FromSlice(input).SumBy(func(s string) int {
return len(s)
}); r != want {
t.Errorf("FromSlice(%v).SumBy()=%v expected %v", input, r, want)
}
}
func TestAverage(t *testing.T) {
if r := Average(FromSlice([]int{1, 2, 2, 3, 1})); r != 1.8 {
t.Errorf("Average(ints)=%v expected 1.8", r)
}
if r := Average(FromSlice([]uint{1, 2, 5, 7, 10})); r != 5. {
t.Errorf("Average(uints)=%v expected 5.", r)
}
if r := Average(FromSlice([]float32{1., 1.})); r != 1. {
t.Errorf("Average(floats)=%v expected 1.", r)
}
}
func TestAverage_LargeIntegerCancellation(t *testing.T) {
const float64PrecisionLimit = int64(1 << 53)
input := []int64{float64PrecisionLimit + 1, -float64PrecisionLimit}
if got := Average(FromSlice(input)); got != 0.5 {
t.Errorf("Average(%v)=%v; want 0.5", input, got)
}
}
func TestAverage_LargeUnsignedPrecision(t *testing.T) {
// Each element is 2^53+1, which is not representable in float64: converting
// elements one by one silently drops the +1 three times, while summing in
// uint64 first keeps the sum exact.
v := uint64(1<<53) + 1
input := []uint64{v, v, v}
want := float64(v+v+v) / 3
if got := Average(FromSlice(input)); got != want {
t.Errorf("Average(%v)=%v; want %v", input, got, want)
}
}
func TestAverageForNaN(t *testing.T) {
if r := Average(FromSlice([]int{})); !math.IsNaN(r) {
t.Errorf("Average(FromSlice([]int{}))=%v expected NaN", r)
}
}
func TestAverageBy(t *testing.T) {
input := []string{"apple", "banana", "fig"}
want := 14. / 3.
if r := FromSlice(input).AverageBy(func(s string) int {
return len(s)
}); r != want {
t.Errorf("FromSlice(%v).AverageBy()=%v expected %v", input, r, want)
}
if r := FromSlice([]string{}).AverageBy(func(s string) int {
return len(s)
}); !math.IsNaN(r) {
t.Errorf("AverageBy()=%v expected NaN", r)
}
}
func TestMax(t *testing.T) {
tests := []struct {
input []int
want int
wantOk bool
}{
{[]int{1, 2, 2, 3, 1}, 3, true},
{[]int{1}, 1, true},
{[]int{}, 0, false},
}
for _, test := range tests {
if r, ok := Max(FromSlice(test.input)); ok != test.wantOk || r != test.want {
t.Errorf("Max(FromSlice(%v))=%v,%v expected %v,%v", test.input, r, ok, test.want, test.wantOk)
}
}
}
func TestMin(t *testing.T) {
tests := []struct {
input []int
want int
wantOk bool
}{
{[]int{1, 2, 2, 3, 0}, 0, true},
{[]int{1}, 1, true},
{[]int{}, 0, false},
}
for _, test := range tests {
if r, ok := Min(FromSlice(test.input)); ok != test.wantOk || r != test.want {
t.Errorf("Min(FromSlice(%v))=%v,%v expected %v,%v", test.input, r, ok, test.want, test.wantOk)
}
}
}
func TestMaxBy(t *testing.T) {
input := []string{"apple", "banana", "fig"}
if r, ok := FromSlice(input).MaxBy(func(s string) int {
return len(s)
}); !ok || r != "banana" {
t.Errorf("MaxBy()=%v,%v expected banana,true", r, ok)
}
if r, ok := FromSlice([]string{}).MaxBy(func(s string) int {
return len(s)
}); ok || r != "" {
t.Errorf("MaxBy()=%v,%v expected \"\",false", r, ok)
}
}
func TestMinBy(t *testing.T) {
input := []string{"apple", "banana", "fig"}
if r, ok := FromSlice(input).MinBy(func(s string) int {
return len(s)
}); !ok || r != "fig" {
t.Errorf("MinBy()=%v,%v expected fig,true", r, ok)
}
if r, ok := FromSlice([]string{}).MinBy(func(s string) int {
return len(s)
}); ok || r != "" {
t.Errorf("MinBy()=%v,%v expected \"\",false", r, ok)
}
}
func TestMinMaxWith(t *testing.T) {
type score struct {
name string
value int
}
input := []score{{"first", 10}, {"low", 2}, {"second", 10}}
compare := func(a, b score) int { return cmp.Compare(a.value, b.value) }
if got, ok := FromSlice(input).MaxWith(compare); !ok || got != input[0] {
t.Errorf("MaxWith()=%v,%v expected %v,true", got, ok, input[0])
}
if got, ok := FromSlice(input).MinWith(compare); !ok || got != input[1] {
t.Errorf("MinWith()=%v,%v expected %v,true", got, ok, input[1])
}
if got, ok := FromSlice([]score{}).MaxWith(compare); ok || got != (score{}) {
t.Errorf("empty MaxWith()=%v,%v expected zero,false", got, ok)
}
if got, ok := FromSlice([]score{}).MinWith(compare); ok || got != (score{}) {
t.Errorf("empty MinWith()=%v,%v expected zero,false", got, ok)
}
}
func TestMinMaxByWith(t *testing.T) {
type item struct {
name string
key []int
}
input := []item{
{"first max", []int{2}},
{"min", []int{1}},
{"second max", []int{2}},
}
selectorCalls := 0
selector := func(item item) []int {
selectorCalls++
return item.key
}
if got, ok := FromSlice(input).MaxByWith(selector, slices.Compare); !ok || got.name != "first max" {
t.Errorf("MaxByWith()=%v,%v expected first max,true", got, ok)
}
if selectorCalls != len(input) {
t.Errorf("MaxByWith selector called %d times expected %d", selectorCalls, len(input))
}
selectorCalls = 0
if got, ok := FromSlice(input).MinByWith(selector, slices.Compare); !ok || got.name != "min" {
t.Errorf("MinByWith()=%v,%v expected min,true", got, ok)
}
if selectorCalls != len(input) {
t.Errorf("MinByWith selector called %d times expected %d", selectorCalls, len(input))
}
}