1 module doml.tests.dunit_tests;
2 
3 import std.stdio;
4 
5 import Math = doml.math;
6 
7 import doml.matrix_2d;
8 import doml.matrix_3d;
9 import doml.matrix_3x2d;
10 import doml.matrix_4d;
11 import doml.matrix_4x3d;
12 import doml.vector_2d;
13 import doml.vector_2i;
14 import doml.vector_3d;
15 import doml.vector_3i;
16 import doml.vector_4d;
17 import doml.vector_4i;
18 import doml.axis_angle_4d;
19 import doml.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 double MANY_OPS_AROUND_ZERO_PRECISION_double = 0.001f;
157 /**
158     * Precision for methods that do basic operations calculating with a magnitude around zero.
159     */
160 immutable double STANDARD_AROUND_ZERO_PRECISION_double = 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 doubleing 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 #doubleCompare(double, double, double)
178     */
179 bool doubleEqual(double a, double b, double precision) {
180     return Math.abs(a - b) <= precision;
181 }
182 
183 /**
184     * Compare two doubleing 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 #doubleEqual(double, double, double)
190     */
191 public static int doubleCompare(double a, double b, double 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 /**
202     * Return whether two quaternions are equal. They are considered equal when their difference is 
203     * less than or equal to the precision.
204     * @param a the first quaternion
205     * @param b the second quaternion
206     * @param precision if abs(a.[comp] - b.[comp]) <= precision for every component comp (x, y, z, w), a and b are considered equal
207     * @return whether a and b are equal
208     */
209 public static bool quatEqual(Quaterniond a, Quaterniond b, double precision) {
210     return doubleEqual(a.x, b.x, precision)
211         && doubleEqual(a.y, b.y, precision)
212         && doubleEqual(a.z, b.z, precision)
213         && doubleEqual(a.w, b.w, precision);
214 }
215 
216 
217 /**
218     * Assert that both quaternions are equal with respect to the given delta.
219     *
220     * @param expected
221     * @param actual
222     * @param delta
223     */
224 public static void assertQuaterniondEquals(Quaterniond expected, Quaterniond actual, double delta) {
225     assertEquals(expected.x, actual.x, delta);
226     assertEquals(expected.y, actual.y, delta);
227     assertEquals(expected.z, actual.z, delta);
228     assertEquals(expected.w, actual.w, delta);
229 }
230 
231 
232 /**
233     * Assert that both matrices are equal with respect to the given delta.
234     * 
235     * @param m1
236     * @param m2
237     * @param delta
238     */
239 public static void assertMatrix4dEquals(Matrix4d m1, Matrix4d m2, double delta) {
240     assertEquals(m1.m00, m2.m00, delta);
241     assertEquals(m1.m01, m2.m01, delta);
242     assertEquals(m1.m02, m2.m02, delta);
243     assertEquals(m1.m03, m2.m03, delta);
244     assertEquals(m1.m10, m2.m10, delta);
245     assertEquals(m1.m11, m2.m11, delta);
246     assertEquals(m1.m12, m2.m12, delta);
247     assertEquals(m1.m13, m2.m13, delta);
248     assertEquals(m1.m20, m2.m20, delta);
249     assertEquals(m1.m21, m2.m21, delta);
250     assertEquals(m1.m22, m2.m22, delta);
251     assertEquals(m1.m23, m2.m23, delta);
252     assertEquals(m1.m30, m2.m30, delta);
253     assertEquals(m1.m31, m2.m31, delta);
254     assertEquals(m1.m32, m2.m32, delta);
255     assertEquals(m1.m33, m2.m33, delta);
256 }
257 
258 void assertEquals(Vector2i a, Vector2i b) {
259     if (verbose) {
260         writeln("X1 = ", a.x, " | X2 = ", b.x);
261         writeln("Y1 = ", a.y, " | Y2 = ", b.y);
262         writeln("");
263     }
264     assert(a.equals(b));
265 }
266 
267 
268 /**
269     * Assert that both matrices are equal with respect to the given delta.
270     * 
271     * @param m1
272     * @param m2
273     * @param delta
274     */
275 public static void assertMatrix3dEquals(Matrix3d m1, Matrix3d m2, double delta) {
276     assertEquals(m1.m00, m2.m00, delta);
277     assertEquals(m1.m01, m2.m01, delta);
278     assertEquals(m1.m02, m2.m02, delta);
279     assertEquals(m1.m10, m2.m10, delta);
280     assertEquals(m1.m11, m2.m11, delta);
281     assertEquals(m1.m12, m2.m12, delta);
282     assertEquals(m1.m20, m2.m20, delta);
283     assertEquals(m1.m21, m2.m21, delta);
284     assertEquals(m1.m22, m2.m22, delta);
285 }
286 
287 /**
288     * Assert that both matrices are equal with respect to the given delta.
289     * 
290     * @param m1
291     * @param m2
292     * @param delta
293     */
294 public static void assertMatrix4x3dEquals(Matrix4x3d m1, Matrix4x3d m2, double delta) {
295     assertEquals(m1.m00, m2.m00, delta);
296     assertEquals(m1.m01, m2.m01, delta);
297     assertEquals(m1.m02, m2.m02, delta);
298     assertEquals(m1.m10, m2.m10, delta);
299     assertEquals(m1.m11, m2.m11, delta);
300     assertEquals(m1.m12, m2.m12, delta);
301     assertEquals(m1.m20, m2.m20, delta);
302     assertEquals(m1.m21, m2.m21, delta);
303     assertEquals(m1.m22, m2.m22, delta);
304     assertEquals(m1.m30, m2.m30, delta);
305     assertEquals(m1.m31, m2.m31, delta);
306     assertEquals(m1.m32, m2.m32, delta);
307 }
308 
309 
310 /**
311     * Assert that both vectors are equal with respect to the given delta.
312     * 
313     * @param expected
314     * @param actual
315     * @param delta
316     */
317 public static void assertVector4dEquals(Vector4d expected, Vector4d actual, double delta) {
318     assertEquals(expected.x, actual.x, delta);
319     assertEquals(expected.y, actual.y, delta);
320     assertEquals(expected.z, actual.z, delta);
321     assertEquals(expected.w, actual.w, delta);
322 }
323 
324 
325 /**
326     * Assert that both quaternions are equal with respect to the given delta.
327     * 
328     * @param expected
329     * @param actual
330     * @param delta
331     */
332 public static void assertVector3dEquals(Vector3d expected, Vector3d actual, double delta) {
333     assertEquals(expected.x, actual.x, delta);
334     assertEquals(expected.y, actual.y, delta);
335     assertEquals(expected.z, actual.z, delta);
336 }
337 
338 
339 /**
340     * Assert that both vectors are equal with respect to the given delta.
341     *
342     * @param expected
343     * @param actual
344     * @param delta
345     */
346 public static void assertVector2dEquals(Vector2d expected, Vector2d actual, double delta) {
347     assertEquals(expected.x, actual.x, delta);
348     assertEquals(expected.y, actual.y, delta);
349 }