Skip to content

Commit bac5b17

Browse files
author
ANDO Yasushi
committed
test for mat2d
1 parent 0880a99 commit bac5b17

File tree

1 file changed

+293
-0
lines changed

1 file changed

+293
-0
lines changed

mat2d_test.go

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
package glmatrix
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
var mat2dA = []float64{
9+
1, 2,
10+
3, 4,
11+
5, 6,
12+
}
13+
14+
var mat2dB = []float64{
15+
7, 8,
16+
9, 10,
17+
11, 12,
18+
}
19+
20+
var out2d = []float64{
21+
0, 0,
22+
0, 0,
23+
0, 0,
24+
}
25+
26+
var identity2d = []float64{
27+
1, 0,
28+
0, 1,
29+
0, 0,
30+
}
31+
32+
func TestMat2dCreate(t *testing.T) {
33+
actual := Mat2dCreate()
34+
if !testSlice(actual, identity2d) {
35+
t.Errorf("create: %v", actual)
36+
}
37+
}
38+
39+
func TestMat2dClone(t *testing.T) {
40+
actual := Mat2dClone(mat2dA)
41+
expect := mat2dA
42+
if !testSlice(actual, expect) {
43+
t.Errorf("clone: %v", actual)
44+
}
45+
}
46+
47+
func TestMat2dCopy(t *testing.T) {
48+
actual := Mat2dCreate()
49+
Mat2dCopy(actual, mat2dA)
50+
expect := mat2dA
51+
if !testSlice(actual, expect) {
52+
t.Errorf("copy: %v", actual)
53+
}
54+
}
55+
56+
func TestMat2dIdentity(t *testing.T) {
57+
actual := Mat2dCreate()
58+
Mat2dIdentity(actual)
59+
expect := identity2d
60+
if !testSlice(actual, expect) {
61+
t.Errorf("identity: %v", actual)
62+
}
63+
}
64+
65+
func TestMat2dInvert(t *testing.T) {
66+
actual := Mat2dCreate()
67+
Mat2dInvert(actual, mat2dA)
68+
expect := []float64{
69+
-2, 1,
70+
1.5, -0.5,
71+
1, -2,
72+
}
73+
if !testSlice(actual, expect) {
74+
t.Errorf("invert: %v", actual)
75+
}
76+
}
77+
78+
func TestMat2dDeterminant(t *testing.T) {
79+
actual := Mat2dDeterminant(mat2dA)
80+
expect := -2.
81+
if actual != expect {
82+
t.Errorf("determinant: %v", actual)
83+
}
84+
}
85+
86+
func TestMat2dMultiply(t *testing.T) {
87+
actual := Mat2dCreate()
88+
Mat2dMultiply(actual, mat2dA, mat2dB)
89+
expect := []float64{
90+
31, 46,
91+
39, 58,
92+
52, 76,
93+
}
94+
if !testSlice(actual, expect) {
95+
t.Errorf("multiply: %v", actual)
96+
}
97+
}
98+
99+
func TestMat2dTranslate(t *testing.T) {
100+
actual := Mat2dCreate()
101+
Mat2dTranslate(actual, mat2dA, []float64{2, 3})
102+
expect := []float64{
103+
1, 2,
104+
3, 4,
105+
16, 22,
106+
}
107+
if !testSlice(actual, expect) {
108+
t.Errorf("translate: %v", actual)
109+
}
110+
}
111+
112+
func TestMat2dScale(t *testing.T) {
113+
actual := Mat2dCreate()
114+
Mat2dScale(actual, mat2dA, []float64{2, 3})
115+
expect := []float64{
116+
2, 4,
117+
9, 12,
118+
5, 6,
119+
}
120+
if !testSlice(actual, expect) {
121+
t.Errorf("scale: %v", actual)
122+
}
123+
}
124+
125+
func TestMat2dRotate(t *testing.T) {
126+
rad := math.Pi * 0.5
127+
actual := Mat2dCreate()
128+
Mat2dRotate(actual, mat2dA, rad)
129+
expect := []float64{
130+
3, 4,
131+
-1, -2,
132+
5, 6,
133+
}
134+
if !testSlice(actual, expect) {
135+
t.Errorf("rotate: %v", actual)
136+
}
137+
}
138+
139+
func TestMat2dStr(t *testing.T) {
140+
actual := Mat2dStr(mat2dA)
141+
expect := "mat2d(1, 2, 3, 4, 5, 6)"
142+
if actual != expect {
143+
t.Errorf("str: %v", actual)
144+
}
145+
}
146+
147+
func TestMat2dFrob(t *testing.T) {
148+
actual := Mat2dFrob(mat2dA)
149+
expect := math.Sqrt(math.Pow(1, 2) +
150+
math.Pow(2, 2) +
151+
math.Pow(3, 2) +
152+
math.Pow(4, 2) +
153+
math.Pow(5, 2) +
154+
math.Pow(6, 2) + 1)
155+
if actual != expect {
156+
t.Errorf("frob: %v", actual)
157+
}
158+
}
159+
160+
var Mat2dOp1 = []float64{
161+
1, 2, 3, 4,
162+
5, 6, 7, 8,
163+
9, 10, 11, 12,
164+
13, 14, 15, 16,
165+
}
166+
167+
var Mat2dOp2 = []float64{
168+
17, 18, 19, 20,
169+
21, 22, 23, 24,
170+
25, 26, 27, 28,
171+
29, 30, 31, 32,
172+
}
173+
174+
func TestMat2dAdd(t *testing.T) {
175+
actual := Mat2dAdd(Mat2dCreate(), mat2dA, mat2dB)
176+
expect := []float64{
177+
8, 10,
178+
12, 14,
179+
16, 18,
180+
}
181+
if !testSlice(actual, expect) {
182+
t.Errorf("add: %v", actual)
183+
}
184+
}
185+
186+
func TestMat2dSubtract(t *testing.T) {
187+
actual := Mat2dSubtract(Mat2dCreate(), mat2dA, mat2dB)
188+
expect := []float64{
189+
-6, -6,
190+
-6, -6,
191+
-6, -6,
192+
}
193+
if !testSlice(actual, expect) {
194+
t.Errorf("subtract: %v", actual)
195+
}
196+
}
197+
198+
func TestMat2dFromValues(t *testing.T) {
199+
actual := Mat2dFromValues(1, 2, 3, 4, 5, 6)
200+
expect := []float64{
201+
1, 2,
202+
3, 4,
203+
5, 6,
204+
}
205+
if !testSlice(actual, expect) {
206+
t.Errorf("from values: %v", actual)
207+
}
208+
}
209+
210+
func TestMat2dSet(t *testing.T) {
211+
actual := Mat2dCreate()
212+
Mat2dSet(actual, 1, 2, 3, 4, 5, 6)
213+
expect := []float64{
214+
1, 2,
215+
3, 4,
216+
5, 6,
217+
}
218+
if !testSlice(actual, expect) {
219+
t.Errorf("set: %v", actual)
220+
}
221+
}
222+
223+
func TestMat2dMultiplyScalar(t *testing.T) {
224+
actual := Mat2dMultiplyScalar(Mat2dCreate(), Mat2dOp1, 2)
225+
expect := []float64{
226+
2, 4,
227+
6, 8,
228+
10, 12,
229+
}
230+
if !testSlice(actual, expect) {
231+
t.Errorf("multiply scalar: %v", actual)
232+
}
233+
}
234+
235+
func TestMat2dMultiplyScalarAndAdd(t *testing.T) {
236+
actual := Mat2dMultiplyScalarAndAdd(Mat2dCreate(), Mat2dOp1, Mat2dOp2, 0.5)
237+
expect := []float64{
238+
9.5, 11,
239+
12.5, 14,
240+
15.5, 17,
241+
}
242+
if !testSlice(actual, expect) {
243+
t.Errorf("multiply scalar and add: %v", actual)
244+
}
245+
}
246+
247+
func TestMat2dExactEquals(t *testing.T) {
248+
mat2dA := []float64{
249+
1, 1,
250+
1, 1,
251+
1, 1,
252+
}
253+
mat2dB := []float64{
254+
1, 1,
255+
1, 1,
256+
1, 1,
257+
}
258+
matC := []float64{
259+
1, 1,
260+
1, 1,
261+
1, 1 + 1e-10,
262+
}
263+
if !Mat2dExactEquals(mat2dA, mat2dB) {
264+
t.Errorf("exact equal")
265+
}
266+
if Mat2dExactEquals(mat2dA, matC) {
267+
t.Errorf("exact equal")
268+
}
269+
}
270+
271+
func TestMat2dEquals(t *testing.T) {
272+
mat2dA := []float64{
273+
1, 1,
274+
1, 1,
275+
1, 1,
276+
}
277+
mat2dB := []float64{
278+
1, 1,
279+
1, 1,
280+
1, 1,
281+
}
282+
matC := []float64{
283+
1, 1,
284+
1, 1,
285+
1, 1 + 1e-10,
286+
}
287+
if !Mat2dEquals(mat2dA, mat2dB) {
288+
t.Errorf("equal")
289+
}
290+
if !Mat2dEquals(mat2dA, matC) {
291+
t.Errorf("equal")
292+
}
293+
}

0 commit comments

Comments
 (0)