This repository was archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquaternion.c
90 lines (72 loc) · 2.4 KB
/
quaternion.c
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
#include <time.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <cml/math.h>
#include <cml/quaternion.h>
#include "include/test.h"
#define QUAT_TEST_EQUAL_S(_q, _w, _x, _y, _z) do { \
EXPECT_FLOAT_EQ(_q.w, _w); \
EXPECT_FLOAT_EQ(_q.x, _x); \
EXPECT_FLOAT_EQ(_q.y, _y); \
EXPECT_FLOAT_EQ(_q.z, _z); \
} while(0);
#define QUAT_TEST_EQUAL(_q, _w) do { \
EXPECT_TRUE(cml_quaternion_equal(_q, _w)); \
} while(0);
int cml_count_tests = 0;
int cml_count_failedtests = 0;
int cml_count_failures = 0;
int
run_quaternion_tests()
{
CATEGORY_BEGIN(Quaternion)
{
TEST_BEGIN(Identity)
{
cml_quaternion_t q = cml_quaternion_identity();
QUAT_TEST_EQUAL_S(q, 1.0, 0.0, 0.0, 0.0);
EXPECT_FLOAT_EQ(cml_quaternion_norm(q), 1.0);
EXPECT_FLOAT_EQ(cml_quaternion_abs(q), 1.0);
}
TEST_END()
TEST_BEGIN(Conjugate)
{
cml_quaternion_t qA = cml_quaternion_from_axis_anglef3(M_TAU / 4.0, 1.0, 1.0, 1.0),
qB = cml_quaternion_from_axis_anglef3(M_TAU / 4.0, -1.0, -1.0, -1.0);
QUAT_TEST_EQUAL(qA, cml_quaternion_conj(qB));
}
TEST_END()
TEST_BEGIN(Multipy Identity)
{
cml_quaternion_t qA, qB, qI = cml_quaternion_identity();
qA.x = 1;
qA.y = 2;
qA.z = 3;
qA.w = 4;
qB = cml_quaternion_multiply(qA, qI);
QUAT_TEST_EQUAL(qB, qA);
}
TEST_END()
}
CATEGORY_END()
return 0;
}
int
main()
{
clock_t cl = clock();
run_quaternion_tests();
(cml_count_failedtests > 0) ?
printf(RED) :
printf(GREEN);
printf("\n%d/%d tests passed overall, %d failures\n" RESET "%Lg%s\n",
cml_count_tests - cml_count_failedtests,
cml_count_tests,
cml_count_failures,
(long double) (clock()-cl)/CLOCKS_PER_SEC,
"s"
);
return (cml_count_failedtests > 0);
}