Sfoglia il codice sorgente

Wrote MatMath Unit Test using greatest

Thomas Buck 11 anni fa
parent
commit
7bff7d30ca
2 ha cambiato i file con 92 aggiunte e 20 eliminazioni
  1. 9
    9
      Makefile
  2. 83
    11
      test/MatMath.cpp

+ 9
- 9
Makefile Vedi File

314
 #################################################################
314
 #################################################################
315
 
315
 
316
 Hel.test: Quaternion.test Matrix.test Math.test
316
 Hel.test: Quaternion.test Matrix.test Math.test
317
+	@-echo "================================================="
318
+	@-echo "Running Matrix unit test"
319
+	$(BUILD_TEST_DIR)/Matrix.test
320
+	@-echo "================================================="
321
+	$(BUILD_TEST_DIR)/Math.test
322
+	@-echo "Running hel Math unit test"
323
+	@-echo "================================================="
324
+	@-echo "Running Quaternion unit test"
325
+	$(BUILD_TEST_DIR)/Quaternion.test
317
 
326
 
318
 Matrix.test:
327
 Matrix.test:
319
 	@-echo "Building Matrix unit test"
328
 	@-echo "Building Matrix unit test"
321
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
330
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
322
 	src/Matrix.cpp src/Quaternion.cpp src/Vector3d.cpp \
331
 	src/Matrix.cpp src/Quaternion.cpp src/Vector3d.cpp \
323
 	test/Matrix.cpp -o $(BUILD_TEST_DIR)/Matrix.test
332
 	test/Matrix.cpp -o $(BUILD_TEST_DIR)/Matrix.test
324
-	@-echo "================================================="
325
-	@-echo "Running Matrix unit test"
326
-	$(BUILD_TEST_DIR)/Matrix.test
327
 
333
 
328
 Quaternion.test:
334
 Quaternion.test:
329
 	@-echo "Building Quaternion unit test"
335
 	@-echo "Building Quaternion unit test"
330
 	mkdir -p $(BUILD_TEST_DIR)
336
 	mkdir -p $(BUILD_TEST_DIR)
331
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
337
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
332
 	src/Quaternion.cpp test/Quaternion.cpp -o $(BUILD_TEST_DIR)/Quaternion.test
338
 	src/Quaternion.cpp test/Quaternion.cpp -o $(BUILD_TEST_DIR)/Quaternion.test
333
-	@-echo "================================================="
334
-	@-echo "Running Quaternion unit test"
335
-	$(BUILD_TEST_DIR)/Quaternion.test
336
 
339
 
337
 Math.test:
340
 Math.test:
338
 	@-echo "Building Math unit test"
341
 	@-echo "Building Math unit test"
339
 	mkdir -p $(BUILD_TEST_DIR)
342
 	mkdir -p $(BUILD_TEST_DIR)
340
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
343
 	$(CC) -Wall -g -lm -lstdc++ -Iinclude \
341
 	src/MatMath.cpp src/Vector3d.cpp test/MatMath.cpp -o $(BUILD_TEST_DIR)/Math.test
344
 	src/MatMath.cpp src/Vector3d.cpp test/MatMath.cpp -o $(BUILD_TEST_DIR)/Math.test
342
-	@-echo "================================================="
343
-	@-echo "Running hel unit test"
344
-	$(BUILD_TEST_DIR)/Math.test
345
 
345
 
346
 #################################################################
346
 #################################################################
347
 
347
 

+ 83
- 11
test/MatMath.cpp Vedi File

1
+/*!
2
+ * \file test/MatMath.cpp
3
+ * \brief Mat Math Unit Test
4
+ *
5
+ * \todo Also test the geometric methods (intersection, distance, midpoint)
6
+ *
7
+ * \author xythobuz
8
+ */
1
 #include <stdio.h>
9
 #include <stdio.h>
2
 #include <math.h>
10
 #include <math.h>
3
-
4
 #include <MatMath.h>
11
 #include <MatMath.h>
12
+#include "greatest.h"
13
+
14
+vec_t testDegrees[] = { 0.0, 30.0, 45.0, 60.0, 90.0, 360.0, -100.0 };
15
+vec_t testRads[] = { 0.0, 0.5236, 0.7854, 1.0472, 1.5708, 6.2832, -1.7453 };
16
+vec_t testBounds[][2] = {
17
+    { 0.0, 1.0 },
18
+    { -5.0, 5.0 },
19
+    { 1.0, 1.0 },
20
+};
21
+
22
+TEST constants() {
23
+    ASSERT_EQm("Pi not correct!", HEL_PI, M_PI);
24
+    ASSERT_EQm("Pi/2 not correct!", HEL_PI_OVER_2, (M_PI / 2.0));
25
+    ASSERT_EQm("2Pi not correct!", HEL_2_PI, (M_PI * 2.0));
26
+    ASSERT_EQm("Pi/4 not correct!", HEL_PI_OVER_4, (M_PI / 4.0));
27
+    ASSERT_EQm("Pi/180 not correct!", HEL_PI_OVER_180, (M_PI / 180.0));
28
+    ASSERT_EQm("180/Pi not correct!", HEL_180_OVER_PI, (180.0 / M_PI));
29
+    PASS();
30
+}
31
+
32
+TEST types() {
33
+    ASSERT_EQ(sizeof(vec2_t), (2 * sizeof(vec_t)));
34
+    ASSERT_EQ(sizeof(vec3_t), (3 * sizeof(vec_t)));
35
+    ASSERT_EQ(sizeof(vec4_t), (4 * sizeof(vec_t)));
36
+    ASSERT_EQ(sizeof(matrix_t), (16 * sizeof(vec_t)));
37
+    PASS();
38
+}
5
 
39
 
6
-void helMathTest()
7
-{
8
-	printf("180/PI: %f, %f, %f\n",
9
-			 HEL_180_OVER_PI,
10
-			 180.0f / HEL_PI,
11
-			 180.0 / M_PI);
40
+TEST conversionToRad(vec_t deg) {
41
+    vec_t conv = HEL_DEG_TO_RAD(deg);
42
+    vec_t hand = (deg * M_PI / 180.0);
43
+    if (conv != hand) {
44
+        printf("Degree to Radian conversion failed: %f != %f\n", conv, hand);
45
+        FAIL();
46
+    } else {
47
+        PASS();
48
+    }
12
 }
49
 }
13
 
50
 
51
+TEST conversionToDeg(vec_t rad) {
52
+    vec_t conv = HEL_RAD_TO_DEG(rad);
53
+    vec_t hand = (rad * 180.0 / M_PI);
54
+    if (conv != hand) {
55
+        printf("Radian to Degree conversion failed: %f != %f\n", conv, hand);
56
+        FAIL();
57
+    } else {
58
+        PASS();
59
+    }
60
+}
61
+
62
+#define TESTNUMS 100
63
+TEST random(vec_t bound[2]) {
64
+    for (int i = 0; i < TESTNUMS; i++) {
65
+        vec_t number = helRandomNum(bound[0], bound[1]);
66
+        ASSERT(number >= bound[0]);
67
+        ASSERT(number <= bound[1]);
68
+    }
69
+    PASS();
70
+}
71
+
72
+SUITE(mathSuite) {
73
+    RUN_TEST(constants);
74
+    RUN_TEST(types);
75
+    for (int i = 0; i < (sizeof(testDegrees) / sizeof(testDegrees[0])); i++) {
76
+        RUN_TESTp(conversionToRad, testDegrees[i]);
77
+    }
78
+    for (int i = 0; i < (sizeof(testRads) / sizeof(testRads[0])); i++) {
79
+        RUN_TESTp(conversionToDeg, testRads[i]);
80
+    }
81
+    for (int i = 0; i < sizeof(testBounds) / sizeof(testBounds[0]); i++) {
82
+        RUN_TESTp(random, testBounds[i]);
83
+    }
84
+}
14
 
85
 
15
-int main(int argc, char *argv[])
16
-{
17
-	helMathTest();
86
+GREATEST_MAIN_DEFS();
18
 
87
 
19
-	return 0;
88
+int main(int argc, char *argv[]) {
89
+    GREATEST_MAIN_BEGIN();
90
+    RUN_SUITE(mathSuite);
91
+    GREATEST_MAIN_END();
20
 }
92
 }
21
 
93
 

Loading…
Annulla
Salva