summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_quaternion.c
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2015-05-29 17:18:23 +0200
committerCedric BAIL <cedric@osg.samsung.com>2015-05-29 17:20:29 +0200
commit0acf23857fccc300727c32aa5ef0583953528d5d (patch)
tree34104bab144076975df7829952a65fc5809cec73 /src/tests/eina/eina_test_quaternion.c
parentc937aab8c071102710bf0856f0197c18a96ced35 (diff)
eina: beginning of a generic quaternion API.
Diffstat (limited to '')
-rw-r--r--src/tests/eina/eina_test_quaternion.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/tests/eina/eina_test_quaternion.c b/src/tests/eina/eina_test_quaternion.c
new file mode 100644
index 0000000000..55e1c9fd4d
--- /dev/null
+++ b/src/tests/eina/eina_test_quaternion.c
@@ -0,0 +1,133 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2015 Cedric Bail
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <math.h>
24#include <float.h>
25#include <limits.h>
26
27#include "eina_suite.h"
28#include "Eina.h"
29
30#define FLOAT_CMP(a, b) (fabs(a - b) <= DBL_MIN)
31
32static inline Eina_Bool
33eina_quaternion_cmp(const Eina_Quaternion *a, const Eina_Quaternion *b)
34{
35 if (FLOAT_CMP(a->x, b->x) &&
36 FLOAT_CMP(a->y, b->y) &&
37 FLOAT_CMP(a->z, b->z) &&
38 FLOAT_CMP(a->w, b->w))
39 return EINA_TRUE;
40 return EINA_FALSE;
41}
42
43static inline Eina_Bool
44eina_matrix3_cmp(const Eina_Matrix3 *a, const Eina_Matrix3 *b)
45{
46 if (FLOAT_CMP(a->xx, b->xx) &&
47 FLOAT_CMP(a->xy, b->xy) &&
48 FLOAT_CMP(a->xz, b->xz) &&
49 FLOAT_CMP(a->yx, b->yx) &&
50 FLOAT_CMP(a->yy, b->yy) &&
51 FLOAT_CMP(a->yz, b->yz) &&
52 FLOAT_CMP(a->zx, b->zx) &&
53 FLOAT_CMP(a->zy, b->zy) &&
54 FLOAT_CMP(a->zz, b->zz))
55 return EINA_TRUE;
56 return EINA_FALSE;
57}
58
59START_TEST(eina_test_quaternion_norm)
60{
61 static const Eina_Quaternion q = { 1, 3, 4, 5 };
62
63 eina_init();
64
65 fail_if(FLOAT_CMP(eina_quaternion_norm(&q), sqrt(51)));
66
67 eina_shutdown();
68}
69END_TEST
70
71START_TEST(eina_test_quaternion_conjugate)
72{
73 static const Eina_Quaternion q1 = { 1, -1, -1, 3 }, q2 = { 1, 3, 4, 3 };
74 static const Eina_Quaternion r1 = { 1, 1, 1, -3 }, r2 = { 1, -3, -4, -3 };
75 Eina_Quaternion t1, t2;
76
77 eina_init();
78
79 eina_quaternion_conjugate(&t1, &q1);
80 eina_quaternion_conjugate(&t2, &q2);
81
82 fail_if(!eina_quaternion_cmp(&t1, &r1));
83 fail_if(!eina_quaternion_cmp(&t2, &r2));
84
85 eina_shutdown();
86}
87END_TEST
88
89START_TEST(eina_test_quaternion_matrix)
90{
91 Eina_Quaternion q = { 7, 9, 5, 1 };
92 Eina_Matrix3 m = {
93 104, 76, 8,
94 104, -8, -116,
95 -52, 136, -56
96 };
97 Eina_Quaternion tq;
98 Eina_Matrix3 tm;
99
100 eina_init();
101
102 eina_quaternion_rotation_matrix3_get(&tm, &q);
103 fail_if(!eina_matrix3_cmp(&tm, &m));
104
105 eina_shutdown();
106}
107END_TEST
108
109START_TEST(eina_test_quaternion_op)
110{
111 Eina_Quaternion q = { 7, 9, 5, 1 };
112 Eina_Quaternion z = { 0, 0, 0, 0 };
113 Eina_Quaternion neg, r;
114
115 eina_init();
116
117 eina_quaternion_negative(&neg, &q);
118 eina_quaternion_add(&r, &q, &neg);
119
120 fail_if(!eina_quaternion_cmp(&z, &r));
121
122 eina_shutdown();
123}
124END_TEST
125
126void
127eina_test_quaternion(TCase *tc)
128{
129 tcase_add_test(tc, eina_test_quaternion_norm);
130 tcase_add_test(tc, eina_test_quaternion_conjugate);
131 tcase_add_test(tc, eina_test_quaternion_matrix);
132 tcase_add_test(tc, eina_test_quaternion_op);
133}