1 module tests.dunit_tests;
2 
3 import std.stdio;
4 
5 import Math = math;
6 
7 import matrix_2d;
8 import matrix_3d;
9 import matrix_3x2d;
10 import matrix_4d;
11 import matrix_4x3d;
12 import vector_2d;
13 import vector_2i;
14 import vector_3d;
15 import vector_3i;
16 import vector_4d;
17 import vector_4i;
18 import axis_angle_4d;
19 import quaternion_d;
20 
21 
22 // This was written SPECIFICALLY to get the precision capabilities of JUNIT
23 
24 // I'll call it, DUNIT-mini
25 
26 private bool verbose = false;
27 
28 void setTestVerbose(bool newValue) {
29     verbose = newValue;
30 }
31 
32 void assertEquals(AxisAngle4d a, AxisAngle4d b) {
33     assert(a.equals(b));
34 }
35 void assertEquals(Quaterniond a, Quaterniond b) {
36     assert(a.equals(b));
37 }
38 
39 /*
40 void assertEquals(AxisAngle4d a, AxisAngle4d b) {
41     writeln("M1: ", a, " | M2: ", b)
42     assert(a.equals(b));
43 }*/
44 
45 void assertEquals(Vector3d a, Vector3d b) {
46     if (verbose) {
47         writeln("X1 = ", a.x, " | X2 = ", b.x);
48         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
49         writeln("Z1 = ", a.z, " | Z2 = ", b.z);
50         writeln("");
51     }
52     assert(a.equals(b));
53 }
54 
55 void assertEquals(Vector3i a, Vector3i b) {
56     if (verbose) {
57         writeln("X1 = ", a.x, " | X2 = ", b.x);
58         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
59         writeln("Z1 = ", a.z, " | Z2 = ", b.z);
60         writeln("");
61     }
62     assert(a.equals(b));
63 }
64 
65 void assertEquals(Vector4d a, Vector4d b) {
66     if (verbose) {
67         writeln("X1 = ", a.x, " | X2 = ", b.x);
68         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
69         writeln("Z1 = ", a.z, " | Z2 = ", b.z);
70         writeln("W1 = ", a.z, " | W2 = ", b.z);
71         writeln("");
72     }
73     assert(a.equals(b));
74 }
75 
76 void assertEquals(Vector4i a, Vector4i b) {
77     if (verbose) {
78         writeln("X1 = ", a.x, " | X2 = ", b.x);
79         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
80         writeln("Z1 = ", a.z, " | Z2 = ", b.z);
81         writeln("W1 = ", a.z, " | W2 = ", b.z);
82         writeln("");
83     }
84     assert(a.equals(b));
85 }
86 
87 /*
88 void assertEquals(double a, double b, long precision) {
89     long c = cast(long)(a * precision);
90     long d = cast(long)(b * precision);
91     if (verbose) {
92         writeln("Value1: ", a, " | Value2: ", b);
93     }
94     assert(c == d);
95 }
96 */
97 
98 void assertEquals(double a, double b, double precision) {
99     if (verbose) {
100         // double debugger = 1_000_000_000.0;
101         // writeln(cast(long)(a * debugger), " ", cast(long)(b * debugger));
102         writeln("Value1: ", a, " | Value2: ", b);
103     }
104     assert(Math.abs(a - b) < precision);
105 }
106 
107 void assertEquals(double a, double b) {
108     if (verbose) {
109         writeln("Value1: ", a, " | Value2: ", b);
110     }
111     assert(a == b);
112 }
113 
114 void assertFalse(bool value) {
115     assert(value == false);
116 }
117 
118 void assertTrue(bool value) {
119     assert(value == true);
120 }
121 
122 
123 /*
124  * The MIT License
125  *
126  * Copyright (c) 2015-2021 JOML.
127  $#^&^ Translated by jordan4ibanez
128  *
129  * Permission is hereby granted, free of charge, to any person obtaining a copy
130  * of this software and associated documentation files (the "Software"), to deal
131  * in the Software without restriction, including without limitation the rights
132  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
133  * copies of the Software, and to permit persons to whom the Software is
134  * furnished to do so, subject to the following conditions:
135  *
136  * The above copyright notice and this permission notice shall be included in
137  * all copies or substantial portions of the Software.
138  *
139  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
142  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
143  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
144  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
145  * THE SOFTWARE.
146  */
147 
148 /**
149  * Utilities for testing.
150  * @author Sebastian Fellner
151  */
152 
153 /**
154     * Precision for methods that do many operations calculating with a magnitude around zero, giving less accuracy.
155     */
156 immutable float MANY_OPS_AROUND_ZERO_PRECISION_FLOAT = 0.001f;
157 /**
158     * Precision for methods that do basic operations calculating with a magnitude around zero.
159     */
160 immutable float STANDARD_AROUND_ZERO_PRECISION_FLOAT = 0.000000000000000001f;
161 
162 /**
163     * Precision for methods that do many operations calculating with values with a magnitude around zero, giving less accuracy.
164     */
165 immutable double MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE = 0.00001;
166 /**
167     * Precision for methods that do basic operations calculating with a magnitude around zero.
168     */
169 immutable double STANDARD_AROUND_ZERO_PRECISION_DOUBLE = 0.000000000000000000001;
170 
171 /**
172     * Return whether two floating point numbers are equal. They are considered equal when their difference is less than or equal to the precision.
173     * @param a the first number
174     * @param b the second number
175     * @param precision if abs(a - b) <= precision, a and b are considered equal
176     * @return whether a and b are equal
177     * @see #floatCompare(float, float, float)
178     */
179 bool floatEqual(float a, float b, float precision) {
180     return Math.abs(a - b) <= precision;
181 }
182 
183 /**
184     * Compare two floating point numbers. They are considered equal when their difference is less than or equal to the precision.
185     * @param a the first number
186     * @param b the second number
187     * @param precision if abs(a - b) <= precision, a and b are considered equal
188     * @return 0 if a == b, 1 if a > b, -1 if a < b
189     * @see #floatEqual(float, float, float)
190     */
191 public static int floatCompare(float a, float b, float precision) {
192     if (Math.abs(a - b) <= precision)
193         return 0;
194     else if (a > b)
195         return 1;
196     else
197         return -1;
198 }
199 
200 /**
201     * Return whether two double-precision floating point numbers are equal. They are considered equal when their difference is 
202     * less than or equal to the precision.
203     * @param a the first number
204     * @param b the second number
205     * @param precision if abs(a - b) <= precision, a and b are considered equal
206     * @return whether a and b are equal
207     * @see #floatCompare(double, double, double)
208     */
209 public static bool doubleEqual(double a, double b, double precision) {
210     return Math.abs(a - b) <= precision;
211 }
212 
213 /**
214     * Compare two double-precision floating point numbers. They are considered equal when their difference is 
215     * less than or equal to the precision.
216     * @param a the first number
217     * @param b the second number
218     * @param precision if abs(a - b) <= precision, a and b are considered equal
219     * @return 0 if a == b, 1 if a > b, -1 if a < b
220     * @see #floatEqual(double, double, double)
221     */
222 public static int doubleCompare(double a, double b, double precision) {
223     if (Math.abs(a - b) <= precision)
224         return 0;
225     else if (a > b)
226         return 1;
227     else
228         return -1;
229 }
230 
231 
232 /**
233     * Return whether two quaternions are equal. They are considered equal when their difference is 
234     * less than or equal to the precision.
235     * @param a the first quaternion
236     * @param b the second quaternion
237     * @param precision if abs(a.[comp] - b.[comp]) <= precision for every component comp (x, y, z, w), a and b are considered equal
238     * @return whether a and b are equal
239     */
240 public static bool quatEqual(Quaterniond a, Quaterniond b, double precision) {
241     return doubleEqual(a.x, b.x, precision)
242         && doubleEqual(a.y, b.y, precision)
243         && doubleEqual(a.z, b.z, precision)
244         && doubleEqual(a.w, b.w, precision);
245 }
246 
247 
248 /**
249     * Assert that both quaternions are equal with respect to the given delta.
250     *
251     * @param expected
252     * @param actual
253     * @param delta
254     */
255 public static void assertQuaterniondEquals(Quaterniond expected, Quaterniond actual, double delta) {
256     assertEquals(expected.x, actual.x, delta);
257     assertEquals(expected.y, actual.y, delta);
258     assertEquals(expected.z, actual.z, delta);
259     assertEquals(expected.w, actual.w, delta);
260 }
261 
262 
263 /**
264     * Assert that both matrices are equal with respect to the given delta.
265     * 
266     * @param m1
267     * @param m2
268     * @param delta
269     */
270 public static void assertMatrix4dEquals(Matrix4d m1, Matrix4d m2, double delta) {
271     assertEquals(m1.m00, m2.m00, delta);
272     assertEquals(m1.m01, m2.m01, delta);
273     assertEquals(m1.m02, m2.m02, delta);
274     assertEquals(m1.m03, m2.m03, delta);
275     assertEquals(m1.m10, m2.m10, delta);
276     assertEquals(m1.m11, m2.m11, delta);
277     assertEquals(m1.m12, m2.m12, delta);
278     assertEquals(m1.m13, m2.m13, delta);
279     assertEquals(m1.m20, m2.m20, delta);
280     assertEquals(m1.m21, m2.m21, delta);
281     assertEquals(m1.m22, m2.m22, delta);
282     assertEquals(m1.m23, m2.m23, delta);
283     assertEquals(m1.m30, m2.m30, delta);
284     assertEquals(m1.m31, m2.m31, delta);
285     assertEquals(m1.m32, m2.m32, delta);
286     assertEquals(m1.m33, m2.m33, delta);
287 }
288 
289 void assertEquals(Vector2i a, Vector2i b) {
290     if (verbose) {
291         writeln("X1 = ", a.x, " | X2 = ", b.x);
292         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
293         writeln("");
294     }
295     assert(a.equals(b));
296 }
297 
298 
299 /**
300     * Assert that both matrices are equal with respect to the given delta.
301     * 
302     * @param m1
303     * @param m2
304     * @param delta
305     */
306 public static void assertMatrix3dEquals(Matrix3d m1, Matrix3d m2, double delta) {
307     assertEquals(m1.m00, m2.m00, delta);
308     assertEquals(m1.m01, m2.m01, delta);
309     assertEquals(m1.m02, m2.m02, delta);
310     assertEquals(m1.m10, m2.m10, delta);
311     assertEquals(m1.m11, m2.m11, delta);
312     assertEquals(m1.m12, m2.m12, delta);
313     assertEquals(m1.m20, m2.m20, delta);
314     assertEquals(m1.m21, m2.m21, delta);
315     assertEquals(m1.m22, m2.m22, delta);
316 }
317 
318 /**
319     * Assert that both matrices are equal with respect to the given delta.
320     * 
321     * @param m1
322     * @param m2
323     * @param delta
324     */
325 public static void assertMatrix4x3dEquals(Matrix4x3d m1, Matrix4x3d m2, double delta) {
326     assertEquals(m1.m00, m2.m00, delta);
327     assertEquals(m1.m01, m2.m01, delta);
328     assertEquals(m1.m02, m2.m02, delta);
329     assertEquals(m1.m10, m2.m10, delta);
330     assertEquals(m1.m11, m2.m11, delta);
331     assertEquals(m1.m12, m2.m12, delta);
332     assertEquals(m1.m20, m2.m20, delta);
333     assertEquals(m1.m21, m2.m21, delta);
334     assertEquals(m1.m22, m2.m22, delta);
335     assertEquals(m1.m30, m2.m30, delta);
336     assertEquals(m1.m31, m2.m31, delta);
337     assertEquals(m1.m32, m2.m32, delta);
338 }
339 
340 
341 /**
342     * Assert that both vectors are equal with respect to the given delta.
343     * 
344     * @param expected
345     * @param actual
346     * @param delta
347     */
348 public static void assertVector4dEquals(Vector4d expected, Vector4d actual, double delta) {
349     assertEquals(expected.x, actual.x, delta);
350     assertEquals(expected.y, actual.y, delta);
351     assertEquals(expected.z, actual.z, delta);
352     assertEquals(expected.w, actual.w, delta);
353 }
354 
355 
356 /**
357     * Assert that both quaternions are equal with respect to the given delta.
358     * 
359     * @param expected
360     * @param actual
361     * @param delta
362     */
363 public static void assertVector3dEquals(Vector3d expected, Vector3d actual, double delta) {
364     assertEquals(expected.x, actual.x, delta);
365     assertEquals(expected.y, actual.y, delta);
366     assertEquals(expected.z, actual.z, delta);
367 }
368 
369 
370 /**
371     * Assert that both vectors are equal with respect to the given delta.
372     *
373     * @param expected
374     * @param actual
375     * @param delta
376     */
377 public static void assertVector2dEquals(Vector2d expected, Vector2d actual, double delta) {
378     assertEquals(expected.x, actual.x, delta);
379     assertEquals(expected.y, actual.y, delta);
380 }