forked from go-gl/mathgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
170 lines (136 loc) · 4.03 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
package mathgl
import (
"math/rand"
"testing"
"time"
)
func TestEqual(t *testing.T) {
a := 1.5
b := 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) {
// |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 TestEqual32(t *testing.T) {
a := float32(1.5)
b := float32(1.0 + .5)
if !FloatEqual32(a, a) {
t.Errorf("Float Equal fails on comparing a number with itself")
}
if !FloatEqual32(a, b) {
t.Errorf("Float Equal fails to compare two equivalent numbers with minimal drift")
} else if !FloatEqual32(b, a) {
t.Errorf("Float Equal is not symmetric for some reason")
}
if !FloatEqual32(0.0, 0.0) {
t.Errorf("Float Equal fails to compare zero values correctly")
}
if FloatEqual32(1.5, 1.51) {
t.Errorf("Float Equal gives false positive on large difference")
}
if FloatEqual32(1.5, 0.0) {
t.Errorf("Float Equal gives false positive comparing with zero")
}
}
func TestClampf(t *testing.T) {
if !FloatEqual32(Clampf(-1.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp returns incorrect value for below threshold")
}
if !FloatEqual32(Clampf(0.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp does something weird when value is at threshold")
}
if !FloatEqual32(Clampf(.14, 0.0, 1.0), .14) {
t.Errorf("Clamp fails to return correct value when value is within threshold")
}
if !FloatEqual32(Clampf(1.1, 0.0, 1.0), 1.0) {
t.Errorf("Clamp fails to return max threshold when appropriate")
}
}
func TestClampd(t *testing.T) {
if !FloatEqual(Clampd(-1.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp returns incorrect value for below threshold")
}
if !FloatEqual(Clampd(0.0, 0.0, 1.0), 0.0) {
t.Errorf("Clamp does something weird when value is at threshold")
}
if !FloatEqual(Clampd(.14, 0.0, 1.0), .14) {
t.Errorf("Clamp fails to return correct value when value is within threshold")
}
if !FloatEqual(Clampd(1.1, 0.0, 1.0), 1.0) {
t.Errorf("Clamp fails to return max threshold when appropriate")
}
}
func TestIsClamped(t *testing.T) {
if IsClampedf(-1.0, 0.0, 1.0) {
t.Errorf("Test below min is considered clamped")
}
if !IsClampedf(.15, 0.0, 1.0) {
t.Errorf("Test in threshold returns false")
}
if IsClampedf(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()
FloatEqual32(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()
Clampf(a, t1, t2)
}
}