Explorar el Código

Rewrote Matrix Test

Thomas Buck hace 10 años
padre
commit
6f8897005c
Se han modificado 3 ficheros con 164 adiciones y 226 borrados
  1. 3
    1
      ChangeLog
  2. 56
    1
      src/Matrix.cpp
  3. 105
    224
      test/Matrix.cpp

+ 3
- 1
ChangeLog Ver fichero

@@ -6,7 +6,9 @@
6 6
  OpenRaider (0.1.2) xythobuz <xythobuz@xythobuz.de>
7 7
 
8 8
 	[ 20140111 ]
9
-	* Documented MatMath, fixed bug in helRandomNum()
9
+	* Rewrote Matrix Unit Test now using greatest
10
+	* Documented MatMath, fixed bug in helRandomNum(),
11
+	  added better Unit Test
10 12
 	* Only one way of conversion between Deg and Rad in MatMath
11 13
 	* Use the same style of include guards in all headers
12 14
 	* Added memory test to SkeletalModel. Adding to OpenGLMesh causes

+ 56
- 1
src/Matrix.cpp Ver fichero

@@ -442,7 +442,62 @@ void Matrix::copy(matrix_t source, matrix_t dest)
442 442
 
443 443
 void Matrix::multiply(const matrix_t a, const matrix_t b, matrix_t result)
444 444
 {
445
-	/* Generated code for matrix mult */
445
+	/* Generated code for matrix mult
446
+     * Code used:
447
+
448
+    // char order is argument
449
+	int i, j, k;
450
+	if (order == 'r')
451
+	{
452
+		printf("// Row order\n");
453
+	}
454
+	else
455
+	{
456
+		printf("// Column order\n");
457
+	}
458
+	for (i = 0; i < 4; ++i)
459
+	{
460
+		for (j = 0; j < 4; ++j)
461
+		{
462
+			if (order == 'r')
463
+			{
464
+				printf("result[%2i] = ", j+i*4);
465
+			}
466
+			else
467
+			{
468
+				printf("result[%2i] = ", j+i*4);
469
+			}
470
+			for (k = 0; k < 4; ++k)
471
+			{
472
+				if (order == 'r')
473
+				{
474
+					printf("a[%2i] * b[%2i]%s",
475
+							  k+i*4, j+k*4, (k == 3) ? ";\n" : " + ");
476
+				}
477
+				else
478
+				{
479
+					printf("a[%2i] * b[%2i]%s",
480
+							 i+k*4, k+j*4, (k == 3) ? ";\n" : " + ");
481
+				}
482
+				//sum+=(elements[i+k*4]*m.elements[k+j*4]);
483
+			}
484
+			//result.elements[i+j*4]=sum;
485
+		}
486
+		printf("\n");
487
+	}
488
+	printf("\n");
489
+	printf("// Transpose\n");
490
+	for(i = 0; i < 4; ++i)
491
+	{
492
+		for (j = 0; j < 4; ++j)
493
+		{
494
+			printf("a[%2i] = b[%2i]%s",
495
+					 j+i*4, i+j*4, (j == 3) ? ";\n" : "; ");
496
+		}
497
+	}
498
+
499
+     * was in test/Matrix.cpp
500
+     */
446 501
 #ifdef COLUMN_ORDER
447 502
 	/* Column order */
448 503
 	result[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3];

+ 105
- 224
test/Matrix.cpp Ver fichero

@@ -1,220 +1,101 @@
1
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
2
-/*================================================================
1
+/*!
2
+ * \file test/Matrix.cpp
3
+ * \brief Matrix Unit Test
3 4
  *
4
- * Project : Freyja
5
- * Author  : Terry 'Mongoose' Hendrix II
6
- * Website : http://www.westga.edu/~stu7440/
7
- * Email   : stu7440@westga.edu
8
- * Object  : Matrix
9
- * License : No use w/o permission (C) 2002 Mongoose
10
- * Comments: 3d Matrix class
11
- *
12
- *
13
- *           This file was generated using Mongoose's C++
14
- *           template generator script.  <stu7440@westga.edu>
15
- *
16
- *-- History -------------------------------------------------
17
- *
18
- * 2002.05.11:
19
- * Mongoose - Created
20
- =================================================================*/
21
-
5
+ * \author Mongoose
6
+ * \author xythobuz
7
+ */
22 8
 #include <stdio.h>
23 9
 #include <math.h>
24 10
 #include <strings.h>
25
-
26 11
 #include <Matrix.h>
27
-
28
-
29
-/* <Order> is (r)ow or (c)ol */
30
-void generateMatrixSourceTest(char order)
31
-{
32
-	int i, j, k;
33
-
34
-
35
-	if (order == 'r')
36
-	{
37
-		printf("/* Row order */\n");
38
-	}
39
-	else
40
-	{
41
-		printf("/* Column order */\n");
42
-	}
43
-
44
-	for (i = 0; i < 4; ++i)
45
-	{
46
-		for (j = 0; j < 4; ++j)
47
-		{
48
-			if (order == 'r')
49
-			{
50
-				printf("result[%2i] = ", j+i*4);
51
-			}
52
-			else
53
-			{
54
-				printf("result[%2i] = ", j+i*4);
55
-			}
56
-
57
-			for (k = 0; k < 4; ++k)
58
-			{
59
-				if (order == 'r')
60
-				{
61
-					printf("a[%2i] * b[%2i]%s",
62
-							  k+i*4, j+k*4, (k == 3) ? ";\n" : " + ");
63
-				}
64
-				else
65
-				{
66
-					printf("a[%2i] * b[%2i]%s",
67
-							 i+k*4, k+j*4, (k == 3) ? ";\n" : " + ");
68
-				}
69
-
70
-				//sum+=(elements[i+k*4]*m.elements[k+j*4]);
71
-			}
72
-
73
-			//result.elements[i+j*4]=sum;
74
-		}
75
-
76
-		printf("\n");
77
-	}
78
-
79
-	printf("\n");
80
-
81
-	printf("/* Transpose */\n");
82
-	for(i = 0; i < 4; ++i)
83
-	{
84
-		for (j = 0; j < 4; ++j)
85
-		{
86
-			printf("a[%2i] = b[%2i]%s",
87
-					 j+i*4, i+j*4, (j == 3) ? ";\n" : "; ");
88
-		}
89
-	}
90
-}
91
-
92
-
93
-int runMatrixUnitTest()
94
-{
95
-	unsigned int i, errs;
96
-	Matrix a, b, c;
97
-	matrix_t m;
98
-
99
-
100
-	// Test 3 cases of identity use
101
-	for (errs = 0, i = 0; i < 3; ++i)
102
-	{
103
-		// Fill A matrix with garbage
104
-		m[ 0] = m[ 1] = m[ 2] = m[ 3] = 45.0f;
105
-		m[ 4] = m[ 5] = m[ 6] = m[ 7] = 90.0f;
106
-		m[ 8] = m[ 9] = m[10] = m[11] = 180.0f;
107
-		m[12] = m[13] = m[14] = m[15] = 270.0f;
108
-		a.setMatrix(m);
109
-
110
-		switch (i)
111
-		{
112
-		case 0:
113
-			printf("Set to Identity");
114
-			a.setIdentity();
115
-			break;
116
-		case 1:
117
-			printf("Identity * Identity");
118
-			c.setIdentity();
119
-			b.setIdentity();
120
-			a = c * b;
121
-			break;
122
-		case 2:
123
-			printf("Identity *= Identity");
124
-			a.setIdentity();
125
-			b.setIdentity();
126
-			a = a * b;
127
-			break;
128
-		}
129
-
130
-		if (a.isIdentity())
131
-		{
132
-			printf(" \t[ Passed ]\n");
133
-		}
134
-		else
135
-		{
136
-			++errs;
137
-			printf(" \t[ Failed ]\a\n"); // beep
138
-			a.print();
139
-		}
140
-
141
-		printf("\n");
142
-	}
143
-
144
-	/* 2003.06.18, Mongoose - These tests are weak and
145
-		only spot check some of the matrix */
146
-
147
-
148
-	/* Cheap translation test */
149
-	a.setIdentity();
150
-	printf("I -> Translate (10, 20, 30)\n");
151
-	a.translate(10, 20, 30);
12
+#include "greatest.h"
152 13
 
153 14
 #ifdef COLUMN_ORDER
154
-	unsigned char i0  = 0, i1  = 4, i2  =  8, i3  = 12;
155
-	unsigned char i4  = 1, i5  = 5, i6  =  9, i7  = 13;
156
-	unsigned char i8  = 2, i9  = 6, i10 = 10, i11 = 14;
157
-	unsigned char i12 = 3, i13 = 7, i14 = 11, i15 = 15;
15
+unsigned char i0  =  0, i1  =  4, i2  =  8; // i3  = 12
16
+unsigned char i4  =  1, i6  =  9; // i5  =  5, i7  = 13
17
+unsigned char i8  =  2, i9  =  6, i10 = 10; // i11 = 14
18
+unsigned char i12 =  3, i13 =  7, i14 = 11, i15 = 15;
158 19
 #else
159
-	unsigned char i0  =  0, i1  =  1, i2  =  2; // i3  =  3
160
-	unsigned char i4  =  4, i6  =  6; // i5  =  5, i7  =  7
161
-	unsigned char i8  =  8, i9  =  9, i10 = 10; // i11 = 11
162
-	unsigned char i12 = 12, i13 = 13, i14 = 14, i15 = 15;
20
+unsigned char i0  =  0, i1  =  1, i2  =  2; // i3  =  3
21
+unsigned char i4  =  4, i6  =  6; // i5  =  5, i7  =  7
22
+unsigned char i8  =  8, i9  =  9, i10 = 10; // i11 = 11
23
+unsigned char i12 = 12, i13 = 13, i14 = 14, i15 = 15;
163 24
 #endif
164 25
 
165
-	if (a.mMatrix[i12] != 10 ||
166
-		 a.mMatrix[i13] != 20 ||
167
-		 a.mMatrix[i14] != 30)
168
-	{
169
-			++errs;
170
-			printf(" \t[ Failed ]\a\n"); // beep
171
-			a.print();
172
-	}
173
-
174
-	/* Cheap X rotation test */
175
-	a.setIdentity();
176
-	printf("I -> Rotate (90 degrees, 0, 0)\n");
177
-	a.rotate(90*0.01745329251994329f, 0, 0);
178
-
179
-	if (a.mMatrix[i0] != 1 || a.mMatrix[i15] != 1 ||
180
-		 a.mMatrix[i9] != -1 || a.mMatrix[i6] != 1)
181
-	{
182
-			++errs;
183
-			printf(" \t[ Failed ]\a\n"); // beep
184
-			a.print();
185
-	}
186
-
187
-	/* Cheap Y rotation test */
188
-	a.setIdentity();
189
-	printf("I -> Rotate (0, 90 degrees, 0)\n");
190
-	a.rotate(0, 90*0.01745329251994329f, 0);
191
-
192
-	if (a.mMatrix[i8] != 1 || a.mMatrix[i2] != -1 ||
193
-		 a.mMatrix[i15] != 1)
194
-	{
195
-			++errs;
196
-			printf(" \t[ Failed ]\a\n"); // beep
197
-			a.print();
198
-	}
199
-
200
-	/* Cheap Z rotation test */
201
-	a.setIdentity();
202
-	printf("I -> Rotate (0, 0, 90 degrees)\n");
203
-	a.rotate(0, 0, 90*0.01745329251994329f);
26
+vec_t initialValues[][4] = {
27
+    { 45.0f, 90.0f, 180.0f, 270.0f },
28
+    { 10.0, 20.0, 30.0, 40.0 }
29
+};
30
+
31
+TEST identity(vec_t init[4], int mode) {
32
+    Matrix a, b, c;
33
+    matrix_t m;
34
+
35
+    // Fill A matrix with garbage
36
+    m[ 0] = m[ 1] = m[ 2] = m[ 3] = init[0];
37
+    m[ 4] = m[ 5] = m[ 6] = m[ 7] = init[1];
38
+    m[ 8] = m[ 9] = m[10] = m[11] = init[2];
39
+    m[12] = m[13] = m[14] = m[15] = init[3];
40
+    a.setMatrix(m);
41
+
42
+    switch (mode) {
43
+    case 0:
44
+        a.setIdentity();
45
+        break;
46
+    case 1:
47
+        c.setIdentity();
48
+        b.setIdentity();
49
+        a = c * b;
50
+        break;
51
+    case 2:
52
+        a.setIdentity();
53
+        b.setIdentity();
54
+        a = a * b;
55
+        break;
56
+    }
57
+    ASSERT(a.isIdentity());
58
+    PASS();
59
+}
204 60
 
205
-	if (a.mMatrix[i4] != -1 || a.mMatrix[i15] != 1 ||
206
-		 a.mMatrix[i1] != 1 || a.mMatrix[i10] != 1)
207
-	{
208
-			++errs;
209
-			printf(" \t[ Failed ]\a\n"); // beep
210
-			a.print();
211
-	}
61
+TEST translation() {
62
+    Matrix a;
63
+    a.setIdentity();
64
+    a.translate(10, 20, 30);
65
+    ASSERT(a.mMatrix[i12] == 10);
66
+    ASSERT(a.mMatrix[i13] == 20);
67
+    ASSERT(a.mMatrix[i14] == 30);
68
+    PASS();
69
+}
212 70
 
213
-	printf("\n%i errors\n", errs);
214
-	printf("\n");
71
+TEST rotation(int axis) {
72
+    Matrix a;
73
+    a.setIdentity();
74
+    vec_t rot = 90 * 0.01745329251994329f;
75
+    a.rotate((axis == 0) ? rot : 0, (axis == 1) ? rot : 0, (axis == 2) ? rot : 0);
76
+    if (axis == 0) {
77
+        ASSERT(a.mMatrix[i0] == 1);
78
+        ASSERT(a.mMatrix[i15] == 1);
79
+        ASSERT(a.mMatrix[i9] == -1);
80
+        ASSERT(a.mMatrix[i6] == 1);
81
+    } else if (axis == 1) {
82
+        ASSERT(a.mMatrix[i8] == 1);
83
+        ASSERT(a.mMatrix[i2] == -1);
84
+        ASSERT(a.mMatrix[i15] == 1);
85
+    } else if (axis == 2) {
86
+        ASSERT(a.mMatrix[i4] == -1);
87
+        ASSERT(a.mMatrix[i15] == 1);
88
+        ASSERT(a.mMatrix[i1] == 1);
89
+        ASSERT(a.mMatrix[i10] == 1);
90
+    } else {
91
+        FAIL();
92
+    }
93
+    PASS();
94
+}
215 95
 
96
+TEST precision() {
97
+    Matrix a;
216 98
 	printf("Prescision test...\n");
217
-	printf("I ->\n");
218 99
 	a.setIdentity();
219 100
 	printf(" -> Rotate (0, 0, 90 degrees)\n");
220 101
 	a.rotate(0, 0, 90*0.01745329251994329f);
@@ -223,37 +104,37 @@ int runMatrixUnitTest()
223 104
 	printf(" -> scale (10, 10, 10)\n");
224 105
 	a.scale(10, 10, 10);
225 106
 	a.print();
226
-
227
-	printf("\n");
228
-
229
-	printf(" -> scale (0.1, 0.1, 0.1)\n");
107
+	printf("\n -> scale (0.1, 0.1, 0.1)\n");
230 108
 	a.scale(0.1, 0.1, 0.1);
231 109
 	printf(" -> Translate (-10, -20, -30)\n");
232 110
 	a.translate(-10, -20, -30);
233 111
 	printf(" -> Rotate (0, 0, -90 degrees)\n");
234 112
 	a.rotate(0, 0, -90*0.01745329251994329f);
235 113
 	a.print();
236
-
237
-	printf("\n%i errors\n", errs);
238
-
239
-	return errs;
114
+    PASS();
240 115
 }
241 116
 
117
+SUITE(matrixSuite) {
118
+    for (int i = 0; i < sizeof(initialValues) / sizeof(initialValues[0]); i++) {
119
+        for (int mode = 0; mode < 3; mode++) {
120
+            RUN_TESTp(identity, initialValues[i], mode);
121
+        }
122
+    }
123
+
124
+    //! \fixme These tests are weak and only spot check some of the matrix -- Mongoose, 2003.06.18
125
+    RUN_TEST(translation);
126
+    RUN_TESTp(rotation, 0);
127
+    RUN_TESTp(rotation, 1);
128
+    RUN_TESTp(rotation, 2);
242 129
 
243
-int main(int argc, char *argv[])
244
-{
245
-	if (argc > 2)
246
-	{
247
-		if (strcmp(argv[1], "-src") == 0)
248
-		{
249
-			generateMatrixSourceTest(argv[2][0]);
250
-			return 0;
251
-		}
252
-	}
130
+    RUN_TEST(precision);
131
+}
253 132
 
254
-	printf("[Matrix class test]\n");
255
-	runMatrixUnitTest();
133
+GREATEST_MAIN_DEFS();
256 134
 
257
-	return 0;
135
+int main(int argc, char *argv[]) {
136
+    GREATEST_MAIN_BEGIN();
137
+    RUN_SUITE(matrixSuite);
138
+    GREATEST_MAIN_END();
258 139
 }
259 140
 

Loading…
Cancelar
Guardar