-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil_test.go
338 lines (282 loc) · 8.29 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
package glm
import (
"math/rand"
"testing"
"time"
)
func TestEqual(t *testing.T) {
t.Parallel()
var a float32 = 1.5
var b float32 = 1.0 + .5
if !FloatEqual(a, a) {
t.Errorf("Float Equal fails on comparing a number with itself")
}
if !FloatEqual(a, b) {
t.Errorf("Float Equal fails to compare two equivalent numbers with minimal drift")
} else if !FloatEqual(b, a) {
t.Errorf("Float Equal is not symmetric for some reason")
}
if !FloatEqual(0.0, 0.0) {
t.Errorf("Float Equal fails to compare zero values correctly")
}
if FloatEqual(1.5, 1.51) {
t.Errorf("Float Equal gives false positive on large difference")
}
if FloatEqual(1.5, 1.5000001) {
t.Errorf("Float Equal gives false positive on small difference")
}
if FloatEqual(1.5, 0.0) {
t.Errorf("Float Equal gives false positive comparing with zero")
}
}
func TestEqualThreshold(t *testing.T) {
t.Parallel()
// |1.0 - 1.01| < .1
if !FloatEqualThreshold(1.0, 1.01, 1e-1) {
t.Errorf("Thresholded equal returns negative on threshold")
}
// Comes out to |1.0 - 1.01| < .0001
if FloatEqualThreshold(1.0, 1.01, 1e-3) {
t.Errorf("Thresholded equal returns false positive on tolerant threshold")
}
}
func TestEqualThresholdTable(t *testing.T) {
t.Parallel()
// http://floating-point-gui.de/errors/NearlyEqualsTest.java
tests := []struct {
A, B, Ep float32
Expected bool
}{
{1.0, 1.01, 1e-1, true},
{1.0, 1.01, 1e-3, false},
// Regular large numbers
{1000000.0, 1000001.0, 0.00001, true},
{1000001.0, 1000000.0, 0.00001, true},
{10000.0, 10001.0, 0.00001, false},
{10001.0, 10000.0, 0.00001, false},
// Negative large numbers
{-1000000.0, -1000001.0, 0.00001, true},
{-1000001.0, -1000000.0, 0.00001, true},
{-10000.0, -10001.0, 0.00001, false},
{-10001.0, -10000.0, 0.00001, false},
// Numbers around 1
{1.0000001, 1.0000002, 0.00001, true},
{1.0000002, 1.0000001, 0.00001, true},
{1.0002, 1.0001, 0.00001, false},
{1.0001, 1.0002, 0.00001, false},
// Numbers around -1
{-1.000001, -1.000002, 0.00001, true},
{-1.000002, -1.000001, 0.00001, true},
{-1.0001, -1.0002, 0.00001, false},
{-1.0002, -1.0001, 0.00001, false},
// Numbers between 1 and 0
{0.000000001000001, 0.000000001000002, 0.00001, true},
{0.000000001000002, 0.000000001000001, 0.00001, true},
{0.000000000001002, 0.000000000001001, 0.00001, false},
{0.000000000001001, 0.000000000001002, 0.00001, false},
// Numbers between -1 and 0
{-0.000000001000001, -0.000000001000002, 0.00001, true},
{-0.000000001000002, -0.000000001000001, 0.00001, true},
{-0.000000000001002, -0.000000000001001, 0.00001, false},
{-0.000000000001001, -0.000000000001002, 0.00001, false},
// Comparisons involving zero
{0.0, 0.0, 0.00001, true},
{0.0, -0.0, 0.00001, true},
{-0.0, -0.0, 0.00001, true},
{0.00000001, 0.0, 0.00001, false},
{0.0, 0.00000001, 0.00001, false},
{-0.00000001, 0.0, 0.00001, false},
{0.0, -0.00000001, 0.00001, false},
// Comparisons involving infinities
{InfPos, InfPos, 0.00001, true},
{InfNeg, InfNeg, 0.00001, true},
{InfNeg, InfPos, 0.00001, false},
{InfPos, MaxValue, 0.00001, false},
{InfNeg, -MaxValue, 0.00001, false},
// Comparisons involving NaN values
{NaN, NaN, 0.00001, false},
{0.0, NaN, 0.00001, false},
{NaN, 0.0, 0.00001, false},
{-0.0, NaN, 0.00001, false},
{NaN, -0.0, 0.00001, false},
{NaN, InfPos, 0.00001, false},
{InfPos, NaN, 0.00001, false},
{NaN, InfNeg, 0.00001, false},
{InfNeg, NaN, 0.00001, false},
{NaN, MaxValue, 0.00001, false},
{MaxValue, NaN, 0.00001, false},
{NaN, -MaxValue, 0.00001, false},
{-MaxValue, NaN, 0.00001, false},
{NaN, MinValue, 0.00001, false},
{MinValue, NaN, 0.00001, false},
{NaN, -MinValue, 0.00001, false},
{-MinValue, NaN, 0.00001, false},
// Comparisons of numbers on opposite sides of 0
{1.000000001, -1.0, 0.00001, false},
{-1.0, 1.000000001, 0.00001, false},
{-1.000000001, 1.0, 0.00001, false},
{1.0, -1.000000001, 0.00001, false},
{10 * MinValue, 10 * -MinValue, 0.00001, true},
{10000 * MinValue, 10000 * -MinValue, 0.00001, true},
// Comparisons of numbers very close to zero
{MinValue, -MinValue, 0.00001, true},
{-MinValue, MinValue, 0.00001, true},
{MinValue, 0, 0.00001, true},
{0, MinValue, 0.00001, true},
{-MinValue, 0, 0.00001, true},
{0, -MinValue, 0.00001, true},
{0.000000001, -MinValue, 0.00001, false},
{0.000000001, MinValue, 0.00001, false},
{MinValue, 0.000000001, 0.00001, false},
{-MinValue, 0.000000001, 0.00001, false},
}
for _, c := range tests {
if r := FloatEqualThreshold(c.A, c.B, c.Ep); r != c.Expected {
t.Errorf("FloatEqualThreshold(%v, %v, %v) != %v (got %v)", c.A, c.B, c.Ep, c.Expected, r)
}
}
}
func TestEqual32(t *testing.T) {
t.Parallel()
a := float32(1.5)
b := float32(1.0 + .5)
if !FloatEqual(a, a) {
t.Errorf("Float Equal fails on comparing a number with itself")
}
if !FloatEqual(a, b) {
t.Errorf("Float Equal fails to compare two equivalent numbers with minimal drift")
} else if !FloatEqual(b, a) {
t.Errorf("Float Equal is not symmetric for some reason")
}
if !FloatEqual(0.0, 0.0) {
t.Errorf("Float Equal fails to compare zero values correctly")
}
if FloatEqual(1.5, 1.51) {
t.Errorf("Float Equal gives false positive on large difference")
}
if FloatEqual(1.5, 0.0) {
t.Errorf("Float Equal gives false positive comparing with zero")
}
}
func TestClampf(t *testing.T) {
t.Parallel()
if !FloatEqual(Clamp(-1.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp returns incorrect value for below threshold")
}
if !FloatEqual(Clamp(0.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp does something weird when value is at threshold")
}
if !FloatEqual(Clamp(.14, 0.0, 1.0), .14) {
t.Errorf("Clamp fails to return correct value when value is within threshold")
}
if !FloatEqual(Clamp(1.1, 0.0, 1.0), 1.0) {
t.Errorf("Clamp fails to return max threshold when appropriate")
}
}
func TestIsClamped(t *testing.T) {
t.Parallel()
if IsClamped(-1.0, 0.0, 1.0) {
t.Errorf("Test below min is considered clamped")
}
if !IsClamped(.15, 0.0, 1.0) {
t.Errorf("Test in threshold returns false")
}
if IsClamped(1.5, 0.0, 1.0) {
t.Errorf("Test above max threshold returns false positive")
}
if IsClamped(1.5, 0.0, 1.0) {
t.Errorf("Test above max threshold returns false positive")
}
}
/* These benchmarks probably aren't very interesting, there's not really many ways to optimize the functions they're benchmarking */
func BenchmarkEqual(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
f1 := r.Float32()
f2 := r.Float32()
b.StartTimer()
FloatEqual(f1, f2)
}
}
// Here just to get a baseline of how much worse the safer equal is
func BenchmarkBuiltinEqual(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
f1 := r.Float32()
f2 := r.Float32()
b.StartTimer()
_ = f1 == f2
}
}
func BenchmarkClampf(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
a := r.Float32()
t1 := r.Float32()
t2 := r.Float32()
b.StartTimer()
Clamp(a, t1, t2)
}
}
func TestRound(t *testing.T) {
t.Parallel()
tests := []struct {
Value float32
Precision int
Expected float32
}{
{0.5, 0, 1},
{0.123, 2, 0.12},
{9.99999999, 6, 10},
{-9.99999999, 6, -10},
{-0.000099, 4, -0.0001},
}
for _, c := range tests {
if r := Round(c.Value, c.Precision); r != c.Expected {
t.Errorf("Round(%v, %v) != %v (got %v)", c.Value, c.Precision, c.Expected, r)
}
}
}
func TestSetMax(t *testing.T) {
t.Parallel()
a, b := float32(0), float32(5)
SetMax(&a, &b)
if a != 5 {
t.Errorf("SetMax failed: %f %f", a, b)
}
a, b = float32(1), float32(0)
SetMax(&a, &b)
if a != 1 {
t.Errorf("SetMax failed: %f %f", a, b)
}
}
func TestSetMin(t *testing.T) {
t.Parallel()
a, b := float32(0), float32(5)
SetMin(&a, &b)
if a != 0 {
t.Errorf("SetMin failed: %f %f", a, b)
}
a, b = float32(1), float32(0)
SetMin(&a, &b)
if a != 0 {
t.Errorf("SetMin failed: %f %f", a, b)
}
}
func BenchmarkRound(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v := r.Float32()
p := r.Intn(10)
b.StartTimer()
Round(v, p)
}
}