1 /*
2  * Copyright (c) 2019 Kevin Townsend (KTOWN)
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zsl/zsl.h>
9 #include <zsl/matrices.h>
10 #include <zsl/vectors.h>
11 #include <zsl/physics/rotation.h>
12 #include "floatcheck.h"
13 
ZTEST(zsl_tests,test_phy_rot_angle)14 ZTEST(zsl_tests, test_phy_rot_angle)
15 {
16 	int rc;
17 	zsl_real_t phi;
18 
19 	rc = zsl_phy_rot_angle(1.1, 3.1, 0.6, &phi);
20 	zassert_true(rc == 0, NULL);
21 	zassert_true(val_is_equal(phi, 6.293, 1E-6), NULL);
22 
23 	/* Example for negative time. */
24 	rc = zsl_phy_rot_angle(1.1, -3.1, 0.6, &phi);
25 	zassert_true(rc == -EINVAL, NULL);
26 	/* IEEE standard states that x != x is true only for NAN values. */
27 	zassert_true(phi != phi, NULL);
28 }
29 
ZTEST(zsl_tests,test_phy_rot_dist)30 ZTEST(zsl_tests, test_phy_rot_dist)
31 {
32 	int rc;
33 	zsl_real_t dist;
34 
35 	rc = zsl_phy_rot_dist(1.1, 6.0, &dist);
36 	zassert_true(rc == 0, NULL);
37 	zassert_true(val_is_equal(dist, 6.6, 1E-6), NULL);
38 
39 	/* Example for negative radius. */
40 	rc = zsl_phy_rot_dist(1.1, -6.0, &dist);
41 	zassert_true(rc == -EINVAL, NULL);
42 	/* IEEE standard states that x != x is true only for NAN values. */
43 	zassert_true(dist != dist, NULL);
44 }
45 
ZTEST(zsl_tests,test_phy_rot_turn)46 ZTEST(zsl_tests, test_phy_rot_turn)
47 {
48 	int rc;
49 	zsl_real_t turn;
50 
51 	rc = zsl_phy_rot_turn(1.1, &turn);
52 	zassert_true(rc == 0, NULL);
53 	zassert_true(val_is_equal(turn, 0.1750704374, 1E-6), NULL);
54 }
55 
ZTEST(zsl_tests,test_phy_rot_time)56 ZTEST(zsl_tests, test_phy_rot_time)
57 {
58 	int rc;
59 	zsl_real_t time;
60 
61 	rc = zsl_phy_rot_time(3.1, 5.7, 2.0, &time);
62 	zassert_true(rc == 0, NULL);
63 	zassert_true(val_is_equal(time, 1.3, 1E-6), NULL);
64 
65 	/* Example for negative angular acceleration. */
66 	rc = zsl_phy_rot_time(4.1, 3.1, -2.0, &time);
67 	zassert_true(rc == 0, NULL);
68 	zassert_true(val_is_equal(time, 0.5, 1E-6), NULL);
69 
70 	/* Example for zero angular acceleration. */
71 	rc = zsl_phy_rot_time(4.1, 3.1, 0.0, &time);
72 	zassert_true(rc == -EINVAL, NULL);
73 	/* IEEE standard states that x != x is true only for NAN values. */
74 	zassert_true(time != time, NULL);
75 
76 	/* Example for negative time. */
77 	rc = zsl_phy_rot_time(4.1, 3.1, 2.3, &time);
78 	zassert_true(rc == -EINVAL, NULL);
79 	/* IEEE standard states that x != x is true only for NAN values. */
80 	zassert_true(time != time, NULL);
81 }
82 
ZTEST(zsl_tests,test_phy_rot_omega)83 ZTEST(zsl_tests, test_phy_rot_omega)
84 {
85 	int rc;
86 	zsl_real_t of;
87 
88 	rc = zsl_phy_rot_omega(3.0, 5.0, 2.5, &of);
89 	zassert_true(rc == 0, NULL);
90 	zassert_true(val_is_equal(of, 15.5, 1E-6), NULL);
91 
92 	/* Example for negative time. */
93 	rc = zsl_phy_rot_omega(3.0, -5.0, 2.5, &of);
94 	zassert_true(rc == -EINVAL, NULL);
95 	/* IEEE standard states that x != x is true only for NAN values. */
96 	zassert_true(of != of, NULL);
97 }
98 
ZTEST(zsl_tests,test_phy_rot_omega2)99 ZTEST(zsl_tests, test_phy_rot_omega2)
100 {
101 	int rc;
102 	zsl_real_t of;
103 
104 	rc = zsl_phy_rot_omega2(3.0, 5.0, 2.5, &of);
105 	zassert_true(rc == 0, NULL);
106 	zassert_true(val_is_equal(of, 5.8309518948, 1E-6), NULL);
107 
108 	/* Example for complex final angular velocity. */
109 	rc = zsl_phy_rot_omega2(3.0, 5.0, -2.5, &of);
110 	zassert_true(rc == -EINVAL, NULL);
111 	/* IEEE standard states that x != x is true only for NAN values. */
112 	zassert_true(of != of, NULL);
113 }
114 
ZTEST(zsl_tests,test_phy_rot_av_omega)115 ZTEST(zsl_tests, test_phy_rot_av_omega)
116 {
117 	int rc;
118 	zsl_real_t omega;
119 
120 	rc = zsl_phy_rot_av_omega(1.0, 2.0, &omega);
121 	zassert_true(rc == 0, NULL);
122 	zassert_true(val_is_equal(omega, 0.5, 1E-6), NULL);
123 
124 	/* Example for zero time. */
125 	rc = zsl_phy_rot_av_omega(1.0, 0.0, &omega);
126 	zassert_true(rc == -EINVAL, NULL);
127 	/* IEEE standard states that x != x is true only for NAN values. */
128 	zassert_true(omega != omega, NULL);
129 
130 	/* Example for negative time. */
131 	rc = zsl_phy_rot_av_omega(1.0, -2.0, &omega);
132 	zassert_true(rc == -EINVAL, NULL);
133 	/* IEEE standard states that x != x is true only for NAN values. */
134 	zassert_true(omega != omega, NULL);
135 }
136 
ZTEST(zsl_tests,test_phy_rot_vel)137 ZTEST(zsl_tests, test_phy_rot_vel)
138 {
139 	int rc;
140 	zsl_real_t vel;
141 
142 	rc = zsl_phy_rot_vel(3.2, 6.0, &vel);
143 	zassert_true(rc == 0, NULL);
144 	zassert_true(val_is_equal(vel, 19.2, 1E-6), NULL);
145 
146 	/* Example for negative radius. */
147 	rc = zsl_phy_rot_vel(3.2, -6.0, &vel);
148 	zassert_true(rc == -EINVAL, NULL);
149 	/* IEEE standard states that x != x is true only for NAN values. */
150 	zassert_true(vel != vel, NULL);
151 }
152 
ZTEST(zsl_tests,test_phy_rot_ang_accel)153 ZTEST(zsl_tests, test_phy_rot_ang_accel)
154 {
155 	int rc;
156 	zsl_real_t a;
157 
158 	rc = zsl_phy_rot_ang_accel(1.0, 2.0, 4.0, &a);
159 	zassert_true(rc == 0, NULL);
160 	zassert_true(val_is_equal(a, 0.25, 1E-6), NULL);
161 
162 	/* Example for negative angular acceleration. */
163 	rc = zsl_phy_rot_ang_accel(4.5, 2.1, 1.2, &a);
164 	zassert_true(rc == 0, NULL);
165 	zassert_true(val_is_equal(a, -2.0, 1E-6), NULL);
166 
167 	/* Example for zero time. */
168 	rc = zsl_phy_rot_ang_accel(1.0, 2.0, 0.0, &a);
169 	zassert_true(rc == -EINVAL, NULL);
170 	/* IEEE standard states that x != x is true only for NAN values. */
171 	zassert_true(a != a, NULL);
172 
173 	/* Example for negative time. */
174 	rc = zsl_phy_rot_ang_accel(1.0, 2.0, -4.0, &a);
175 	zassert_true(rc == -EINVAL, NULL);
176 	/* IEEE standard states that x != x is true only for NAN values. */
177 	zassert_true(a != a, NULL);
178 }
179 
ZTEST(zsl_tests,test_phy_rot_accel)180 ZTEST(zsl_tests, test_phy_rot_accel)
181 {
182 	int rc;
183 	zsl_real_t accel;
184 
185 	rc = zsl_phy_rot_accel(1.5, 6.0, &accel);
186 	zassert_true(rc == 0, NULL);
187 	zassert_true(val_is_equal(accel, 9.0, 1E-6), NULL);
188 
189 	/* Example for negative radius. */
190 	rc = zsl_phy_rot_accel(1.5, -6.0, &accel);
191 	zassert_true(rc == -EINVAL, NULL);
192 	/* IEEE standard states that x != x is true only for NAN values. */
193 	zassert_true(accel != accel, NULL);
194 }
195 
ZTEST(zsl_tests,test_phy_rot_ener)196 ZTEST(zsl_tests, test_phy_rot_ener)
197 {
198 	int rc;
199 	zsl_real_t rke;
200 
201 	rc = zsl_phy_rot_ener(6.2, 4.1, &rke);
202 	zassert_true(rc == 0, NULL);
203 #ifdef CONFIG_ZSL_SINGLE_PRECISION
204  	zassert_true(val_is_equal(rke, 78.802, 1E-5), NULL);
205 #else
206         zassert_true(val_is_equal(rke, 78.802, 1E-8), NULL);
207 #endif
208 
209 	/* Example for zero moment of inertia. */
210 	rc = zsl_phy_rot_ener(6.2, 0.0, &rke);
211 	zassert_true(rc == 0, NULL);
212 	zassert_true(val_is_equal(rke, 0.0, 1E-6), NULL);
213 
214 	/* Example for negative moment of inertia. */
215 	rc = zsl_phy_rot_ener(6.2, -4.1, &rke);
216 	zassert_true(rc == -EINVAL, NULL);
217 	/* IEEE standard states that x != x is true only for NAN values. */
218 	zassert_true(rke != rke, NULL);
219 }
220 
ZTEST(zsl_tests,test_phy_rot_period)221 ZTEST(zsl_tests, test_phy_rot_period)
222 {
223 	int rc;
224 	zsl_real_t t;
225 
226 	rc = zsl_phy_rot_period(6.2, &t);
227 	zassert_true(rc == 0, NULL);
228 	zassert_true(val_is_equal(t, 1.0134169850, 1E-6), NULL);
229 
230 	/* Example for zero angular velocity. */
231 	rc = zsl_phy_rot_period(0.0, &t);
232 	zassert_true(rc == -EINVAL, NULL);
233 	/* IEEE standard states that x != x is true only for NAN values. */
234 	zassert_true(t != t, NULL);
235 
236 	/* Example for negative angular velocity. */
237 	rc = zsl_phy_rot_period(-6.2, &t);
238 	zassert_true(rc == -EINVAL, NULL);
239 	/* IEEE standard states that x != x is true only for NAN values. */
240 	zassert_true(t != t, NULL);
241 }
242 
ZTEST(zsl_tests,test_phy_rot_frequency)243 ZTEST(zsl_tests, test_phy_rot_frequency)
244 {
245 	int rc;
246 	zsl_real_t f;
247 
248 	rc = zsl_phy_rot_frequency(6.2, &f);
249 	zassert_true(rc == 0, NULL);
250 	zassert_true(val_is_equal(f, 0.9867606472, 1E-6), NULL);
251 
252 	/* Example for zero angular velocity. */
253 	rc = zsl_phy_rot_frequency(0.0, &f);
254 	zassert_true(rc == 0, NULL);
255 	zassert_true(val_is_equal(f, 0.0, 1E-6), NULL);
256 
257 	/* Example for negative angular velocity. */
258 	rc = zsl_phy_rot_frequency(-6.2, &f);
259 	zassert_true(rc == -EINVAL, NULL);
260 	/* IEEE standard states that x != x is true only for NAN values. */
261 	zassert_true(f != f, NULL);
262 }
263 
ZTEST(zsl_tests,test_phy_rot_cent_accel)264 ZTEST(zsl_tests, test_phy_rot_cent_accel)
265 {
266 	int rc;
267 	zsl_real_t ca;
268 
269 	rc = zsl_phy_rot_cent_accel(6.1, 4.2, &ca);
270 	zassert_true(rc == 0, NULL);
271 	zassert_true(val_is_equal(ca, 8.8595238095, 1E-6), NULL);
272 
273 	/* Example for zero tangencial velocity. */
274 	rc = zsl_phy_rot_cent_accel(0.0, 4.2, &ca);
275 	zassert_true(rc == 0, NULL);
276 	zassert_true(val_is_equal(ca, 0.0, 1E-6), NULL);
277 
278 	/* Example for zero radius. */
279 	rc = zsl_phy_rot_cent_accel(6.1, 0.0, &ca);
280 	zassert_true(rc == -EINVAL, NULL);
281 	/* IEEE standard states that x != x is true only for NAN values. */
282 	zassert_true(ca != ca, NULL);
283 
284 	/* Example for negative radius. */
285 	rc = zsl_phy_rot_cent_accel(6.1, -4.2, &ca);
286 	zassert_true(rc == -EINVAL, NULL);
287 	/* IEEE standard states that x != x is true only for NAN values. */
288 	zassert_true(ca != ca, NULL);
289 }
290 
ZTEST(zsl_tests,test_phy_rot_cent_accel2)291 ZTEST(zsl_tests, test_phy_rot_cent_accel2)
292 {
293 	int rc;
294 	zsl_real_t ca;
295 
296 	rc = zsl_phy_rot_cent_accel2(1.2, 4.2, &ca);
297 	zassert_true(rc == 0, NULL);
298 #ifdef CONFIG_ZSL_SINGLE_PRECISION
299  	zassert_true(val_is_equal(ca, 115.1453846794, 1E-5), NULL);
300 #else
301         zassert_true(val_is_equal(ca, 115.1453846794, 1E-8), NULL);
302 #endif
303 
304 	/* Example for zero radius. */
305 	rc = zsl_phy_rot_cent_accel2(1.2, 0.0, &ca);
306 	zassert_true(rc == 0, NULL);
307 	zassert_true(val_is_equal(ca, 0.0, 1E-6), NULL);
308 
309 	/* Example for negative radius. */
310 	rc = zsl_phy_rot_cent_accel2(1.2, -4.2, &ca);
311 	zassert_true(rc == -EINVAL, NULL);
312 	/* IEEE standard states that x != x is true only for NAN values. */
313 	zassert_true(ca != ca, NULL);
314 
315 	/* Example for zero period. */
316 	rc = zsl_phy_rot_cent_accel2(0.0, 4.2, &ca);
317 	zassert_true(rc == -EINVAL, NULL);
318 	/* IEEE standard states that x != x is true only for NAN values. */
319 	zassert_true(ca != ca, NULL);
320 
321 	/* Example for negative period. */
322 	rc = zsl_phy_rot_cent_accel2(-1.2, 4.2, &ca);
323 	zassert_true(rc == -EINVAL, NULL);
324 	/* IEEE standard states that x != x is true only for NAN values. */
325 	zassert_true(ca != ca, NULL);
326 }
327 
ZTEST(zsl_tests,test_phy_rot_total_accel)328 ZTEST(zsl_tests, test_phy_rot_total_accel)
329 {
330 	int rc;
331 	zsl_real_t at;
332 
333 	rc = zsl_phy_rot_total_accel(3.8, 5.5, &at);
334 	zassert_true(rc == 0, NULL);
335 	zassert_true(val_is_equal(at, 6.6850579653, 1E-6), NULL);
336 
337 	/* Example for negative accelerations. */
338 	rc = zsl_phy_rot_total_accel(-3.8, -5.5, &at);
339 	zassert_true(rc == 0, NULL);
340 	zassert_true(val_is_equal(at, 6.6850579653, 1E-6), NULL);
341 
342 	/* Example for zero tangencial acceleration. */
343 	rc = zsl_phy_rot_total_accel(0.0, 5.5, &at);
344 	zassert_true(rc == 0, NULL);
345 	zassert_true(val_is_equal(at, 5.5, 1E-6), NULL);
346 
347 	/* Example for zero centripetal acceleration. */
348 	rc = zsl_phy_rot_total_accel(3.8, 0.0, &at);
349 	zassert_true(rc == 0, NULL);
350 	zassert_true(val_is_equal(at, 3.8, 1E-6), NULL);
351 }
352 
ZTEST(zsl_tests,test_phy_rot_power)353 ZTEST(zsl_tests, test_phy_rot_power)
354 {
355 	int rc;
356 	zsl_real_t power;
357 
358 	rc = zsl_phy_rot_power(6.7, 5.1, &power);
359 	zassert_true(rc == 0, NULL);
360 	zassert_true(val_is_equal(power, 34.17, 1E-6), NULL);
361 }
362