forked from go-gl/mathgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec_test.go
220 lines (161 loc) · 5.13 KB
/
vec_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
package mathgl
import (
"math/rand"
"testing"
"time"
)
/* Only floats are tested because the double versions are simply a find->replace on floats */
func Test2DVecAdd(t *testing.T) {
v1 := Vec2f{1.0, 2.5}
v2 := Vec2f{0.0, 1.0}
v3 := v1.Add(v2)
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 3.5) {
t.Errorf("Add not adding properly")
}
v4 := v2.Add(v1)
if !FloatEqual32(v3[0], v4[0]) || !FloatEqual32(v3[0], v4[0]) {
t.Errorf("Addition is somehow not commutative")
}
}
func Test3DVecAdd(t *testing.T) {
v1 := Vec3f{1.0, 2.5, 1.1}
v2 := Vec3f{0.0, 1.0, 9.9}
v3 := v1.Add(v2)
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 3.5) || !FloatEqual32(v3[2], 11.0) {
t.Errorf("Add not adding properly")
}
v4 := v2.Add(v1)
if !FloatEqual32(v3[0], v4[0]) || !FloatEqual32(v3[0], v4[0]) || !FloatEqual32(v3[2], v4[2]) {
t.Errorf("Addition is somehow not commutative")
}
}
func Test4DVecAdd(t *testing.T) {
v1 := Vec4f{1.0, 2.5, 1.1, 2.0}
v2 := Vec4f{0.0, 1.0, 9.9, 100.0}
v3 := v1.Add(v2)
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 3.5) || !FloatEqual32(v3[2], 11.0) || !FloatEqual32(v3[3], 102.0) {
t.Errorf("Add not adding properly")
}
v4 := v2.Add(v1)
if !FloatEqual32(v3[0], v4[0]) || !FloatEqual32(v3[0], v4[0]) || !FloatEqual32(v3[2], v4[2]) || !FloatEqual32(v3[3], v4[3]) {
t.Errorf("Addition is somehow not commutative")
}
}
func Test2DVecSub(t *testing.T) {
v1 := Vec2f{1.0, 2.5}
v2 := Vec2f{0.0, 1.0}
v3 := v1.Sub(v2)
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 1.5) {
t.Errorf("Sub not subtracting properly [%f, %f]", v3[0], v3[1])
}
}
func Test3DVecSub(t *testing.T) {
v1 := Vec3f{1.0, 2.5, 1.1}
v2 := Vec3f{0.0, 1.0, 9.9}
v3 := v1.Sub(v2)
// 1.1-9.9 does stupid things to floats, so we need threshold
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 1.5) || !FloatEqualThreshold32(v3[2], -8.8, 1e-5) {
t.Errorf("Sub not subtracting properly [%f, %f, %f]", v3[0], v3[1], v3[2])
}
}
func Test4DVecSub(t *testing.T) {
v1 := Vec4f{1.0, 2.5, 1.1, 2.0}
v2 := Vec4f{0.0, 1.0, 9.9, 100.0}
v3 := v1.Sub(v2)
// 1.1-9.9 does stupid things to floats, so we need a more tolerant threshold
if !FloatEqual32(v3[0], 1.0) || !FloatEqual32(v3[1], 1.5) || !FloatEqualThreshold32(v3[2], -8.8, 1e-5) || !FloatEqual32(v3[3], -98.0) {
t.Errorf("Sub not subtracting properly [%f, %f, %f, %f]", v3[0], v3[1], v3[2], v3[3])
}
}
func TestVecScale(t *testing.T) {
v := Vec2f{1.0, 0.0}
v = v.Mul(15.0)
if !FloatEqual32(v[0], 15.0) || !FloatEqual32(v[1], 0.0) {
t.Errorf("Vec mul does something weird [%f, %f]", v[0], v[1])
}
v2 := Vec3f{1.0, 0.0, 100.1}
v2 = v2.Mul(15.0)
if !FloatEqual32(v2[0], 15.0) || !FloatEqual32(v2[1], 0.0) || !FloatEqual32(v2[2], 1501.5) {
t.Errorf("Vec mul does something weird [%f, %f, %f]", v2[0], v2[1], v2[2])
}
v3 := Vec4f{1.0, 0.0, 100.1, -1.0}
v3 = v3.Mul(15.0)
if !FloatEqual32(v3[0], 15.0) || !FloatEqual32(v3[1], 0.0) || !FloatEqual32(v3[2], 1501.5) || !FloatEqual32(v3[3], -15.0) {
t.Errorf("Vec mul does something weird [%f, %f, %f, %f]", v3[0], v3[1], v3[2], v3[3])
}
}
func BenchmarkVec4Add(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
v2 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Add(v2)
}
}
func BenchmarkVec4Sub(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
v2 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Sub(v2)
}
}
func BenchmarkVec4Scale(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
c := r.Float32()
b.StartTimer()
v1.Mul(c)
}
}
func BenchmarkVec4Dot(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
v2 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Dot(v2)
}
}
func BenchmarkVec4Len(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Len()
}
}
func BenchmarkVec4Normalize(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec4f{r.Float32(), r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Normalize()
}
}
func BenchmarkVecCross(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
for i := 0; i < b.N; i++ {
b.StopTimer()
v1 := Vec3f{r.Float32(), r.Float32(), r.Float32()}
v2 := Vec3f{r.Float32(), r.Float32(), r.Float32()}
b.StartTimer()
v1.Cross(v2)
}
}