1 module tests.matrix_2d_test;
2 
3 import std.stdio;
4 import tests.dunit_tests;
5 import Math = math;
6 import matrix_2d;
7 import vector_2d;
8 
9 /*
10  * The MIT License
11  *
12  * Copyright (c) 2015-2021 Richard Greenlees
13  ^$%# Translated by jordan4ibanez
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this software and associated documentation files (the "Software"), to deal
17  * in the Software without restriction, including without limitation the rights
18  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19  * copies of the Software, and to permit persons to whom the Software is
20  * furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31  * THE SOFTWARE.
32  */
33 
34 unittest {
35 
36     // This is going to be lumped in to one thing, assert tells you what line anyways
37 
38     writeln("\nBEGINNING MATRIX2D TESTING\n");
39     
40     // test mul
41     {
42         assertTrue(
43             Matrix2d(87, 124, 129, 184).equals(Matrix2d(2, 3, 5, 7).mul(Matrix2d(11, 13, 17, 19)), 0.001)
44         );
45     }
46 
47     // test mul local
48     {
49         assertTrue(
50             Matrix2d(87, 124, 129, 184).equals(Matrix2d(11, 13, 17, 19).mulLocal(Matrix2d(2, 3, 5, 7)), 0.001)
51         );
52     }
53 
54     // test determinant
55     {
56         assertTrue(-1.0 == Matrix2d(2, 3, 5, 7).determinant());
57     }
58 
59     // test invert
60     {
61         assertTrue(
62             Matrix2d(-19.0/12, 13.0/12, 17.0/12, -11.0/12).equals(Matrix2d(11, 13, 17, 19).invert(), 0.001)
63         );
64     }
65 
66     // test rotation
67     {
68         immutable double angle = Math.PI / 4.0;
69         Matrix2d mat = Matrix2d().rotation(angle);
70         immutable double coord = 1 / Math.sqrt(2);
71         assertTrue(
72             Vector2d(coord, coord).equals(mat.transform(Vector2d(1, 0)), 0.001)
73         );
74     }
75 
76     // test normal
77     {
78         assertTrue(
79             Matrix2d(2, 3, 5, 7).invert().transpose().equals(Matrix2d(2, 3, 5, 7).normal(), 0.001)
80         );
81     }
82 
83     // test positive x
84     {
85         Matrix2d inv = Matrix2d(2, 3, 5, 7).invert();
86         Vector2d expected = inv.transform(Vector2d(1, 0)).normalize();
87         assertTrue(
88             expected.equals(Matrix2d(2, 3, 5, 7).positiveX(Vector2d()), 0.001)
89         );
90     }
91 
92     // test positive y
93     {
94         Matrix2d inv = Matrix2d(11, 13, 17, 19).invert();
95         Vector2d expected = inv.transform(Vector2d(0, 1)).normalize();
96         assertTrue(
97             expected.equals(Matrix2d(11, 13, 17, 19).positiveY(Vector2d()), 0.001)
98         );
99     }
100 
101     writeln("PASSED!");
102 }