1 module doml.tests.vector_3i_test;
2 
3 import std.stdio;
4 
5 import Math = doml.math;
6 import doml.tests.dunit_tests;
7 import doml.rounding_mode;
8 
9 import doml.vector_3i;
10 import doml.vector_2d;
11 import doml.vector_3d;
12 
13 /*
14  * The MIT License
15  *
16  * Copyright (c) 2015-2021 JOML.
17  %$%#^ Translated by jordan4ibanez
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining a copy
20  * of this software and associated documentation files (the "Software"), to deal
21  * in the Software without restriction, including without limitation the rights
22  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23  * copies of the Software, and to permit persons to whom the Software is
24  * furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be included in
27  * all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35  * THE SOFTWARE.
36  */
37 
38 /**
39  * Test class for {@link Vector3i}.
40  */
41 unittest {
42 
43     writeln("\nBEGINNING VECTOR3I TESTING\n");
44 
45     writeln("TESTING VECTOR3I ROUNDING");
46 
47     {
48         Vector3i v1 = Vector3i(0.0, .6, .7, RoundingMode.FLOOR);
49         Vector3i v2 = Vector3i(9.5, 1.6, 5.0, RoundingMode.FLOOR);
50 
51         Vector3i v3 = Vector3i(Vector3d(0.0, .6, .7), RoundingMode.FLOOR);
52         Vector3i v4 = Vector3i(Vector3d(9.5, 1.6, 5.0), RoundingMode.FLOOR);
53 
54         Vector3i v5 = Vector3i(0.0, .6, .7, RoundingMode.CEILING);
55         Vector3i v6 = Vector3i(9.5, 1.6, 5.0, RoundingMode.CEILING);
56 
57         Vector3i v7 = Vector3i(Vector3d(0.0, .6, .7), RoundingMode.CEILING);
58         Vector3i v8 = Vector3i(Vector3d(9.5, 1.6, 5.0), RoundingMode.CEILING);
59 
60 
61         assertEquals(v1, Vector3i(0, 0, 0));
62         assertEquals(v2, Vector3i(9, 1, 5));
63 
64         assertEquals(v3, Vector3i(0, 0, 0));
65         assertEquals(v4, Vector3i(9, 1, 5));
66 
67         assertEquals(v5, Vector3i(0, 1, 1));
68         assertEquals(v6, Vector3i(10, 2, 5));
69 
70         assertEquals(v7, Vector3i(0, 1, 1));
71         assertEquals(v8, Vector3i(10, 2, 5));
72     }
73 
74     writeln("PASSED!");
75 
76     writeln("TESTING VECTOR3I ROUNDING VECTOR2D");
77 
78     {
79         Vector3i v1 = Vector3i(Vector2d(0.0, .6), .7, RoundingMode.FLOOR);
80         Vector3i v2 = Vector3i(Vector2d(9.5, 1.6), 5.0, RoundingMode.FLOOR);
81 
82         Vector3i v3 = Vector3i(Vector2d(0.0, .6), .7, RoundingMode.FLOOR);
83         Vector3i v4 = Vector3i(Vector2d(9.5, 1.6), 5.0, RoundingMode.FLOOR);
84 
85         Vector3i v5 = Vector3i(Vector2d(0.0, .6), .7, RoundingMode.CEILING);
86         Vector3i v6 = Vector3i(Vector2d(9.5, 1.6), 5.0, RoundingMode.CEILING);
87 
88 
89         assertEquals(v1, Vector3i(0, 0, 0));
90         assertEquals(v2, Vector3i(9, 1, 5));
91 
92         assertEquals(v3, Vector3i(0, 0, 0));
93         assertEquals(v4, Vector3i(9, 1, 5));
94 
95         assertEquals(v5, Vector3i(0, 1, 1));
96         assertEquals(v6, Vector3i(10, 2, 5));
97     }
98 
99     writeln("PASSED!");
100 
101     writeln("TESTING OF EMPTY CONSTRUCTOR");
102 
103     {
104 
105         Vector3i v = Vector3i();
106         assertEquals(0, v.x);
107         assertEquals(0, v.y);
108         assertEquals(0, v.z);
109     }
110 
111     writeln("PASSED!");
112 
113     writeln("TESTING TRIPLE CONSTRUCTOR");
114 
115     {
116         Vector3i v = Vector3i(1, 2, 3);
117         assertEquals(1, v.x);
118         assertEquals(2, v.y);
119         assertEquals(3, v.z);
120     }
121 
122     writeln("PASSED!");
123 
124     writeln("\nINITIALIZING SCOPED TEST VARIABLES\n");
125 
126     Vector3i v1 = Vector3i(1, 3, 7);
127     Vector3i v2 = Vector3i(2, 6, 14);
128     Vector3i v3 = Vector3i(3, 9, 21);
129 
130     writeln("TESTING COPY CONSTRUCTOR");
131 
132     {
133         Vector3i copy = Vector3i(v1);
134         assertEquals(v1.x, copy.x);
135         assertEquals(v1.y, copy.y);
136         assertEquals(v1.z, copy.z);
137     }
138 
139     writeln("PASSED!");
140 
141     writeln("TESTING EQUALS");
142 
143     {
144         assertFalse(v1.equals(v2));
145         assertTrue(v1.equals(Vector3i(v1.x, v1.y, v1.z)));
146         assertFalse(v1.equals(Vector3i()));
147     }
148 
149     writeln("PASSED!");
150 
151     writeln("TESTING ADD TRIPLE");
152 
153     {
154         Vector3i v = Vector3i(v1);
155         v.add(v2.x, v2.y, v2.z);
156         assertEquals(v3, v);
157 
158         v = Vector3i(v1);
159         v.add(v2);
160         assertEquals(v3, v);
161     }
162 
163     writeln("PASSED!");
164 
165     writeln("TESTING ADD TO SELF");
166 
167     {
168         Vector3i v = Vector3i(v1);
169         v.add(v);
170         assertEquals(Vector3i(2, 6, 14), v);
171     }
172 
173     writeln("PASSED!");
174 
175     writeln("TESTING MIN");
176 
177     {
178         Vector3i v = Vector3i(v1);
179         v.min(Vector3i(v1.z, v1.y, v1.x));
180         assertEquals(Math.min(v1.x, v1.z), v.x);
181         assertEquals(v1.y, v.y);
182         assertEquals(Math.min(v1.x, v1.z), v.z);
183     }
184 
185     writeln("PASSED!");
186 
187     writeln("TESTING MAX");
188     
189 
190     {
191         Vector3i v = Vector3i(v1);
192         v.max(Vector3i(v1.z, v1.y, v1.x));
193         assertEquals(Math.max(v1.x, v1.z), v.x);
194         assertEquals(v1.y, v.y);
195         assertEquals(Math.max(v1.x, v1.z), v.z);
196     }
197 
198     writeln("PASSED!");
199 
200     writeln("TESTING MANHATTAN DISTANCE");
201 
202     {
203         assertEquals(0, Vector3i().gridDistance(Vector3i()));
204         assertEquals(1, Vector3i().gridDistance(Vector3i(1, 0, 0)));
205         assertEquals(1, Vector3i().gridDistance(Vector3i(0, 0, 1)));
206         assertEquals(1, Vector3i().gridDistance(Vector3i(0, 1, 0)));
207         assertEquals(3, Vector3i().gridDistance(Vector3i(1, 1, 1)));
208         assertEquals(3, Vector3i().gridDistance(Vector3i(1, -1, 1)));
209     }
210 
211     writeln("PASSED!");
212 }