forked from mmp/vice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
441 lines (390 loc) · 11.2 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// util_test.go
// Copyright(c) 2022 Matt Pharr, licensed under the GNU Public License, Version 3.
// SPDX: GPL-3.0-only
package main
import (
"testing"
"time"
)
func TestWrapText(t *testing.T) {
input := "this is a test_with_a_long_line of stuff"
expected := "this is \n a \n test_with_a_long_line \n of \n stuff"
wrap, lines := wrapText(input, 8, 2, false)
if wrap != expected {
t.Errorf("wrapping gave %q; expected %q", wrap, expected)
}
if lines != 5 {
t.Errorf("wrapping returned %d lines, expected 5", lines)
}
}
func TestStopShouting(t *testing.T) {
input := "UNITED AIRLINES (North America)"
expected := "United Airlines (North America)"
ss := stopShouting(input)
if ss != expected {
t.Errorf("Got %q, expected %q", ss, expected)
}
}
func TestArgmin(t *testing.T) {
if argmin(1) != 0 {
t.Errorf("argmin single failed: %d", argmin(1))
}
if argmin(1, 2) != 0 {
t.Errorf("argmin 1,2 failed: %d", argmin(1, 2))
}
if argmin(2, 1) != 1 {
t.Errorf("argmin 2,1 failed: %d", argmin(2, 1))
}
if argmin(1, -3, 1, 10) != 1 {
t.Errorf("argmin 1,-3,1,10 failed: %d", argmin(1, -3, 1, 10))
}
}
func TestCompass(t *testing.T) {
type ch struct {
h float32
dir string
short string
hour int
}
for _, c := range []ch{ch{0, "North", "N", 12}, ch{22, "North", "N", 1}, ch{338, "North", "N", 11},
ch{337, "Northwest", "NW", 11}, ch{95, "East", "E", 3}, ch{47, "Northeast", "NE", 2},
ch{140, "Southeast", "SE", 5}, ch{170, "South", "S", 6}, ch{205, "Southwest", "SW", 7},
ch{260, "West", "W", 9}} {
if compass(c.h) != c.dir {
t.Errorf("compass gave %s for %f; expected %s", compass(c.h), c.h, c.dir)
}
if shortCompass(c.h) != c.short {
t.Errorf("shortCompass gave %s for %f; expected %s", shortCompass(c.h), c.h, c.short)
}
if headingAsHour(c.h) != c.hour {
t.Errorf("headingAsHour gave %d for %f; expected %d", headingAsHour(c.h), c.h, c.hour)
}
}
}
func TestHeadingDifference(t *testing.T) {
type hd struct {
a, b, d float32
}
for _, h := range []hd{hd{10, 90, 80}, hd{350, 12, 22}, hd{340, 120, 140}, hd{-90, 80, 170},
hd{40, 181, 141}, hd{-170, 160, 30}, hd{-120, -150, 30}} {
if headingDifference(h.a, h.b) != h.d {
t.Errorf("headingDifference(%f, %f) -> %f, expected %f", h.a, h.b,
headingDifference(h.a, h.b), h.d)
}
if headingDifference(h.b, h.a) != h.d {
t.Errorf("headingDifference(%f, %f) -> %f, expected %f", h.b, h.a,
headingDifference(h.b, h.a), h.d)
}
}
}
func TestTransientMap(t *testing.T) {
ts := NewTransientMap[int, int]()
ts.Add(1, 10, 250*time.Millisecond)
ts.Add(2, 20, 750*time.Millisecond)
// Should have both
if v, ok := ts.Get(1); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 10 {
t.Errorf("transient set didn't return expected value")
}
if v, ok := ts.Get(2); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 20 {
t.Errorf("transient set didn't return expected value")
}
// Note that after this point this test has the potential to be flaky,
// if the thread is not scheduled for ~250+ms; it's possible that more
// time will elapse than we think and thence some of the checks may not
// add up...
time.Sleep(500 * time.Millisecond)
// Should just have 2
if _, ok := ts.Get(1); ok {
t.Errorf("transient set still has value that it shouldn't")
}
if v, ok := ts.Get(2); !ok {
t.Errorf("transient set doesn't have expected entry")
} else if v != 20 {
t.Errorf("transient set didn't return expected value")
}
time.Sleep(250 * time.Millisecond)
if _, ok := ts.Get(1); ok {
t.Errorf("transient set still has value that it shouldn't")
}
if _, ok := ts.Get(2); ok {
t.Errorf("transient set still has value that it shouldn't")
}
}
func TestSliceEqual(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := []int{1, 2, 3, 4, 5}
if !SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
b = append(b, 6)
if SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
a = append(a, 6)
if !SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
a = a[1:]
if SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
a = nil
if SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
b = nil
if !SliceEqual(a, b) {
t.Errorf("SliceEqual incorrect")
}
}
func TestMapSlice(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := MapSlice[int, float32](a, func(i int) float32 { return 2 * float32(i) })
if len(a) != len(b) {
t.Errorf("lengths mismatch: %d vs %d", len(a), len(b))
}
for i := range a {
if float32(2*a[i]) != b[i] {
t.Errorf("value %d mismatch %f vs %f", i, float32(2*a[i]), b[i])
}
}
}
func TestDeleteSliceElement(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
a = DeleteSliceElement(a, 2)
if !SliceEqual(a, []int{1, 2, 4, 5}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 3)
if !SliceEqual(a, []int{1, 2, 4}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 0)
if !SliceEqual(a, []int{2, 4}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 1)
if !SliceEqual(a, []int{2}) {
t.Errorf("Slice element delete incorrect")
}
a = DeleteSliceElement(a, 0)
if !SliceEqual(a, nil) {
t.Errorf("Slice element delete incorrect")
}
}
func TestInsertSliceElement(t *testing.T) {
a := []int{1, 2, 4, 5}
a = InsertSliceElement(a, 2, 3)
if !SliceEqual(a, []int{1, 2, 3, 4, 5}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
a = InsertSliceElement(a, 0, 0)
if !SliceEqual(a, []int{0, 1, 2, 3, 4, 5}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
a = InsertSliceElement(a, 6, 6)
if !SliceEqual(a, []int{0, 1, 2, 3, 4, 5, 6}) {
t.Errorf("Slice insert incorrect: %+v", a)
}
}
func TestFilterSlice(t *testing.T) {
a := []int{1, 2, 3, 4, 5}
b := FilterSlice(a, func(i int) bool { return i%2 == 0 })
if len(b) != 2 || b[0] != 2 || b[1] != 4 {
t.Errorf("filter evens failed: %+v", b)
}
c := FilterSlice(a, func(i int) bool { return i >= 3 })
if len(c) != 3 || c[0] != 3 || c[1] != 4 || c[2] != 5 {
t.Errorf("filter >=3 failed: %+v", c)
}
}
func TestFindSlice(t *testing.T) {
a := []int{0, 1, 2, 3, 4, 5}
for i := 0; i < 5; i++ {
if Find(a, i) != i {
t.Errorf("find %d returned %d", i, Find(a, i))
}
}
if Find(a, 8) != -1 {
t.Errorf("find of nonexistent didn't return -1")
}
}
func TestRingBuffer(t *testing.T) {
rb := NewRingBuffer[int](10)
if rb.Size() != 0 {
t.Errorf("empty should have zero size")
}
rb.Add(0, 1, 2, 3, 4)
if rb.Size() != 5 {
t.Errorf("expected size 5; got %d", rb.Size())
}
for i := 0; i < 5; i++ {
if rb.Get(i) != i {
t.Errorf("returned unexpected value")
}
}
for i := 5; i < 18; i++ {
rb.Add(i)
}
if rb.Size() != 10 {
t.Errorf("expected size 10")
}
for i := 0; i < 10; i++ {
if rb.Get(i) != 8+i {
t.Errorf("after filling, at %d got %d, expected %d", i, rb.Get(i), 8+i)
}
}
}
func TestReduceMap(t *testing.T) {
m := map[int]string{
0: "hello",
16: "foobar",
2: "greets",
7: "x",
}
reduce := func(k int, v string, length int) int {
return length + len(v)
}
length := ReduceMap[int, string, int](m, reduce, 5)
if length != 5+5+6+6+1 {
t.Errorf("Expected %d from ReduceMap; got %d", 5+5+6+6+1, length)
}
}
func TestParseLatLong(t *testing.T) {
type LL struct {
str string
pos Point2LL
}
latlongs := []LL{
LL{str: "N40.37.58.400, W073.46.17.000", pos: Point2LL{-73.771385, 40.6328888}}, // JFK VOR
LL{str: "N40.37.58.4,W073.46.17.000", pos: Point2LL{-73.771385, 40.6328888}}, // JFK VOR
LL{str: "40.6328888, -73.771385", pos: Point2LL{-73.771385, 40.6328888}}, // JFK VOR
LL{str: "+403758.400-0734617.000", pos: Point2LL{-73.7713928, 40.632885}}} // JFK VOR
for _, ll := range latlongs {
p, err := ParseLatLong([]byte(ll.str))
if err != nil {
t.Errorf("%s: unexpected error: %v", ll.str, err)
}
if p[0] != ll.pos[0] {
t.Errorf("%s: got %.9g for latitude, expected %.9g", ll.str, p[0], ll.pos[0])
}
if p[1] != ll.pos[1] {
t.Errorf("%s: got %.9g for longitude, expected %.9g", ll.str, p[1], ll.pos[1])
}
}
for _, invalid := range []string{
"E40.37.58.400, W073.46.17.000",
"40.37.58.400, W073.46.17.000",
"N40.37.58.400, -73.22",
"N40.37.58.400, W073.46.17",
} {
if _, err := ParseLatLong([]byte(invalid)); err == nil {
t.Errorf("%s: no error was returned for invalid latlong string!", invalid)
}
}
}
func TestSampleFiltered(t *testing.T) {
if SampleFiltered([]int{}, func(int) bool { return true }) != -1 {
t.Errorf("Returned non-zero for empty slice")
}
if SampleFiltered([]int{0, 1, 2, 3, 4}, func(int) bool { return false }) != -1 {
t.Errorf("Returned non-zero for fully filtered")
}
if idx := SampleFiltered([]int{0, 1, 2, 3, 4}, func(v int) bool { return v == 3 }); idx != 3 {
t.Errorf("Returned %d rather than 3 for filtered slice", idx)
}
var counts [5]int
for i := 0; i < 9000; i++ {
idx := SampleFiltered([]int{0, 1, 2, 3, 4}, func(v int) bool { return v&1 == 0 })
counts[idx]++
}
if counts[1] != 0 || counts[3] != 0 {
t.Errorf("Incorrectly sampled odd items. Counts: %+v", counts)
}
slop := 100
if counts[0] < 3000-slop || counts[0] > 3000+slop ||
counts[2] < 3000-slop || counts[2] > 3000+slop ||
counts[4] < 3000-slop || counts[4] > 3000+slop {
t.Errorf("Didn't find roughly 3000 samples for the even items. Counts: %+v", counts)
}
}
func TestSampleWeighted(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 0, 10, 13}
counts := make([]int, len(a))
n := 100000
for i := 0; i < n; i++ {
idx := SampleWeighted(a, func(v int) int { return v })
counts[idx]++
}
sum := 0
for _, v := range a {
sum += v
}
for i, c := range counts {
expected := a[i] * n / sum
if a[0] == 0 && c != 0 {
t.Errorf("Expected 0 samples for a[%d]. Got %d", i, c)
} else if c < expected-300 || c > expected+300 {
t.Errorf("Expected roughly %d samples for a[%d]=%d. Got %d", expected, i, a[i], c)
}
}
}
func TestPointInPolygon(t *testing.T) {
type testCase struct {
name string
point Point2LL
polygon []Point2LL
expected bool
}
testCases := []testCase{
{
name: "PointInsideSimpleSquare",
point: Point2LL{1, 1},
polygon: []Point2LL{{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}},
expected: true,
},
{
name: "PointOutsideSimpleSquare",
point: Point2LL{3, 3},
polygon: []Point2LL{{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}},
expected: false,
},
{
name: "PointByVertex",
point: Point2LL{-0.001, 0},
polygon: []Point2LL{{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}},
expected: false,
},
{
name: "PointOnEdge",
point: Point2LL{1, -.001},
polygon: []Point2LL{{0, 0}, {0, 2}, {2, 2}, {2, 0}, {0, 0}},
expected: false,
},
{
name: "PointInsideComplexPolygon",
point: Point2LL{3, 3},
polygon: []Point2LL{{0, 0}, {0, 6}, {6, 6}, {6, 0}, {3, 3}, {0, 0}},
expected: true,
},
{
name: "PointOutsideComplexPolygon",
point: Point2LL{7, 7},
polygon: []Point2LL{{0, 0}, {0, 6}, {6, 6}, {6, 0}, {3, 3}, {0, 0}},
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := PointInPolygon(tc.point, tc.polygon)
if result != tc.expected {
t.Errorf("Expected %v, got %v for point %v and polygon %v",
tc.expected, result, tc.point, tc.polygon)
}
})
}
}