MathLib
Loading...
Searching...
No Matches
TestVector2D.cpp
Go to the documentation of this file.
3#include <iostream>
4#include <format>
5#include <numbers>
6#include <cstdlib>
7
8
9namespace Vector2DTests
10{
11
12using namespace Math;
13
14void TestFunction(const Vector2Df &input)
15{
16 std::cout << std::format("{}\t{} {}", __func__, input.x, input.y) << std::endl;
17}
18
20{
21 std::cout << __func__ << std::endl;
22
23 Vector2Df default_initialized;
24
25 CHECK_IF_EQUAL( default_initialized.x, 0.0f );
26 CHECK_IF_EQUAL( default_initialized.y, 0.0f );
27}
28
30{
31 std::cout << __func__ << std::endl;
32
33 Vector2Df one{ 2.2f };
34
35 CHECK_IF_EQUAL( one.x, 2.2f );
36 CHECK_IF_EQUAL( one.y, 0.0f );
37}
38
40{
41 std::cout << __func__ << std::endl;
42
43 Vector2Df one{ 2.2f, 3.3f };
44
45 CHECK_IF_EQUAL( one.x, 2.2f );
46 CHECK_IF_EQUAL( one.y, 3.3f );
47}
48
50{
51 std::cout << __func__ << std::endl;
52
53 TestFunction( { } );
54 TestFunction( { 1.0f } );
55 TestFunction( { 66.0f, 33.3f } );
56
57 Vector2Df t{ 1.1f, 2.2f };
58
59 TestFunction( t.yx() );
60}
61
63{
64 Vector2Df one{ 1.0f, 2.0f };
65 Vector2Df two{ 1.0f, 2.0f };
66 Vector2Df three{ 9.16f, 32.7f };
67
68 CHECK_IF_EQUAL( one, two );
69 CHECK_IF_NOT_EQUAL( one, three );
70}
71
73{
74 std::cout << __func__ << std::endl;
75
76 Vector2Df one{ 1.0f, 2.0f };
77 Vector2Df two{ 2.1f, 2.3f };
78
79 CHECK_IF_NOT_EQUAL( one.x, two.x );
80 CHECK_IF_NOT_EQUAL( one.y, two.y );
81
82 one.xy() = two;
83
84 CHECK_IF_EQUAL( one.x, two.x );
85 CHECK_IF_EQUAL( one.y, two.y );
86
87 one.yx() = two;
88
89 CHECK_IF_EQUAL( one.x, two.y );
90 CHECK_IF_EQUAL( one.y, two.x );
91
92 two.yx() = { 5.5f, 6.6f };
93
94 CHECK_IF_EQUAL( two.x, 6.6f );
95 CHECK_IF_EQUAL( two.y, 5.5f );
96}
97
99{
100 std::cout << __func__ << std::endl;
101
102 Vector2Df one{ 1.0f, 2.0f };
103 Vector2Df two{ 2.1f, 2.3f };
104
105 CHECK_IF_NOT_EQUAL( one.x, two.x );
106 CHECK_IF_NOT_EQUAL( one.y, two.y );
107
108 one.xy() = two.xy();
109
110 CHECK_IF_EQUAL( one.x, two.x );
111 CHECK_IF_EQUAL( one.y, two.y );
112}
113
115{
116 std::cout << __func__ << std::endl;
117
118 Vector2Df one{ 1.0f, 2.0f };
119 Vector2Df two{ 2.1f, 2.3f };
120
121 CHECK_IF_NOT_EQUAL( one.x, two.y );
122 CHECK_IF_NOT_EQUAL( one.y, two.y );
123
124 one = two.yx();
125
126 CHECK_IF_EQUAL( one.x, two.y );
127 CHECK_IF_EQUAL( one.y, two.x );
128}
129
131{
132 std::cout << __func__ << std::endl;
133
134 Vector2Df one{ 1.0f, 2.0f };
135 Vector2Df two{ 2.1f, 2.3f };
136
137 CHECK_IF_NOT_EQUAL( one.x, two.x );
138 CHECK_IF_NOT_EQUAL( one.y, two.y );
139
140 one = two;
141
142 CHECK_IF_EQUAL( one.x, two.x );
143 CHECK_IF_EQUAL( one.y, two.y );
144}
145
147{
148 std::cout << __func__ << std::endl;
149
150 {
151 Vector2Df one{ 1.0f, 2.0f };
152 Vector2Df two{ one.yx() };
153
154 CHECK_IF_EQUAL( two.x, 2.0f );
155 CHECK_IF_EQUAL( two.y, 1.0f );
156 }
157}
158
160{
161 Vector2Df one{1.0f, 2.0f};
162 const Vector2Df two{ 3.0f, 4.0f };
163
164 CHECK_IF_EQUAL( one.yx(), Vector2Df{ 2.0f, 1.0f } ); // L-Value Ref Qualifier
165 CHECK_IF_EQUAL( Vector2Df{ 1.0f, 2.0f }.yx(), Vector2Df{ 2.0f, 1.0f } ); // R-Value Ref Qualifier
166
167 CHECK_IF_EQUAL( two.yx(), Vector2Df{ 4.0f, 3.0f } ); // L-Value Ref Qualifier on const
168}
169
171{
172 std::cout << __func__ << std::endl;
173
174 Vector2Df one{ 1.0f, 2.0f };
175 Vector2Df two{ 1.0f, 2.0f };
176
177 assert( one == two );
178 assert( one == two.xy() );
179 assert( one.xy() == two );
180 assert( one.xy() == two.xy() );
181}
182
183void Dot()
184{
185 std::cout << __func__ << std::endl;
186
187 float sqrt2_over_2 = std::numbers::sqrt2_v<float> / 2.0f;
188 // At first, let's use unit vectors...
189
190 // Perpendicular vectors are 0
191 CHECK_IF_ZERO( dot(Vector2Df::unit_x(), Vector2Df::unit_y()) );
192
193 // Parallel vectors are 1
194 CHECK_IF_EQUAL( dot(Vector2Df::unit_x(), Vector2Df::unit_x()), 1.0f);
195 CHECK_IF_EQUAL( dot(Vector2Df::unit_y(), Vector2Df::unit_y()), 1.0f);
196
197 // Misc Rotations
198 CHECK_IF_EQUAL( dot(Vector2Df{sqrt2_over_2, sqrt2_over_2}, Vector2Df{sqrt2_over_2, sqrt2_over_2}), 1.0f ); // 45 deg rotation
199}
200
202{
203 std::cout << __func__ << std::endl;
204
205 // Vector2Df
206 {
207 Vector2Df left{ 1.0f, 2.0f };
208 Vector2Df right{ 1.0f, 2.0f };
209 Vector2Df result = left + right;
210
211 CHECK_IF_EQUAL( left + right, Vector2Df( 2.0f, 4.0f ) );
212 CHECK_IF_EQUAL( result, Vector2Df( 2.0f, 4.0f ) );
213 }
214
215 // Vector2Df & Vector2Df::Ref
216 {
217 Vector2Df left{ 1.0f, 2.0f };
218 Vector2Df right{ 3.0f, 4.0f };
219 Vector2Df result = left + right.yx();
220
221 CHECK_IF_EQUAL( left + right.yx(), Vector2Df( 5.0f, 5.0f ) );
222 CHECK_IF_EQUAL( result, Vector2Df( 5.0f, 5.0f ) );
223 }
224
225 // Vector2Df::Ref & Vector2Df::Ref
226 {
227 Vector2Df left{ 1.0f, 2.0f };
228 Vector2Df right{ 3.0f, 4.0f };
229 Vector2Df result = left.xy() + right.xy();
230
231 CHECK_IF_EQUAL( left.xy() + right.xy(), Vector2Df( 4.0f, 6.0f ) );
232 CHECK_IF_EQUAL( result, Vector2Df( 4.0f, 6.0f ) );
233 }
234}
235
237{
238 std::cout << __func__ << std::endl;
239
240 // Vector2Df
241 {
242 Vector2Df left{ 1.0f, 2.0f };
243 Vector2Df right{ 1.0f, 2.0f };
244 Vector2Df result = left - right;
245
246 CHECK_IF_ZERO( left - right );
247 CHECK_IF_ZERO( result );
248 }
249
250 // Vector2Df & Vector2Df::Ref
251 {
252 Vector2Df left{ 1.0f, 2.0f };
253 Vector2Df right{ 1.0f, 2.0f };
254 Vector2Df result = left - right.yx();
255
256 CHECK_IF_EQUAL( left - right.yx(), Vector2Df{ -1.0f, 1.0f } );
257 CHECK_IF_EQUAL( result, Vector2Df{ -1.0f, 1.0f } );
258 }
259
260 // Vector2Df::Ref & Vector2Df::Ref
261 {
262 Vector2Df left{ 5.0f, 9.0f };
263 Vector2Df right{ 3.0f, 7.0f };
264 Vector2Df result = left - right.yx();
265
266 CHECK_IF_EQUAL( left - right.yx(), Vector2Df{ -2.0f, 6.0f } );
267 CHECK_IF_EQUAL( result, Vector2Df{ -2.0f, 6.0f } );
268 }
269}
270
272{
273 std::cout << __func__ << std::endl;
274
275 // Vector2Df
276 {
277 Vector2Df left{ 3.0f, 4.0f };
278 Vector2Df right{ 5.0f, 9.0f };
279 Vector2Df result = left * right;
280
281 CHECK_IF_EQUAL( left * right, Vector2Df{ 15.0f, 36.0f } );
282 CHECK_IF_EQUAL( right * left, Vector2Df{ 15.0f, 36.0f } );
283 CHECK_IF_EQUAL( result, Vector2Df{ 15.0f, 36.0f } );
284 }
285
286 // Vector2Df & scalar
287 {
288 Vector2Df left{ 3.0f, 4.0f };
289 float scalar = 7.0f;
290 Vector2Df result = left * scalar;
291 Vector2Df result2 = scalar * left;
292
293 CHECK_IF_EQUAL( left * scalar, Vector2Df{ 21.0f, 28.0f } );
294 CHECK_IF_EQUAL( scalar * left, Vector2Df{ 21.0f, 28.0f } );
295 CHECK_IF_EQUAL( result, Vector2Df{ 21.0f, 28.0f } );
296 CHECK_IF_EQUAL( result2, Vector2Df{ 21.0f, 28.0f } );
297 }
298
299 // Vector2Df & Vector2Df::Ref
300 {
301 Vector2Df left{ 3.0f, 4.0f };
302 Vector2Df right{ 5.0f, 9.0f };
303 Vector2Df result = left * right.xy();
304 Vector2Df result2 = right.xy() * left;
305
306 CHECK_IF_EQUAL( left * right.xy(), Vector2Df{ 15.0f, 36.0f } );
307 CHECK_IF_EQUAL( right * left.xy(), Vector2Df{ 15.0f, 36.0f } );
308 CHECK_IF_EQUAL( result, Vector2Df{ 15.0f, 36.0f } );
309 CHECK_IF_EQUAL( result2, Vector2Df{ 15.0f, 36.0f } );
310 }
311
312 // Vector2Df::Ref & Vector2Df::Ref
313 {
314 Vector2Df left{ 1.0f, 3.0f };
315 Vector2Df right{ 2.0f, 6.0f };
316 Vector2Df result = left.xy() * right.xy();
317 Vector2Df result2 = right.xy() * left.xy();
318
319 CHECK_IF_EQUAL( left.xy() * right.xy(), Vector2Df{ 2.0f, 18.0f} );
320 CHECK_IF_EQUAL( right.xy() * left.xy(), Vector2Df{ 2.0f, 18.0f} );
321 CHECK_IF_EQUAL( result, Vector2Df{ 2.0f, 18.0f} );
322 CHECK_IF_EQUAL( result2, Vector2Df{ 2.0f, 18.0f} );
323 }
324
325 // Vector2Df::Ref & scalar
326 {
327 Vector2Df left{ 3.0f, 4.0f };
328 float scalar = 7.0f;
329 Vector2Df result = left.xy() * scalar;
330 Vector2Df result2 = scalar * left.xy();
331
332 CHECK_IF_EQUAL( left.xy() * scalar, Vector2Df{ 21.0f, 28.0f } );
333 CHECK_IF_EQUAL( scalar * left.xy(), Vector2Df{ 21.0f, 28.0f } );
334 CHECK_IF_EQUAL( result, Vector2Df{ 21.0f, 28.0f } );
335 CHECK_IF_EQUAL( result2, Vector2Df{ 21.0f, 28.0f } );
336 }
337
338}
339
341{
342 Addition();
343 Subtraction();
345}
346
347void Run()
348{
349 std::cout << "Running Vector2D Tests..." << std::endl;
350
361 Swizzle();
363 Dot();
364
366
367 std::cout << "PASSED!" << std::endl;
368}
369}
370
371int main(void)
372{
374 return EXIT_SUCCESS;
375}
int main(void)
void CHECK_IF_NOT_EQUAL(float input, float near_to, float tolerance=0.0002f)
Definition Checks.hpp:158
void CHECK_IF_EQUAL(float input, float near_to, float tolerance=0.0002f)
Definition Checks.hpp:127
void CHECK_IF_ZERO(float input, float tolerance=0.0002f)
Definition Checks.hpp:189
Definition Angle.hpp:16
void AssignVector2DToVector2D()
void ArithmeticOperations()
void DefaultInitialied()
void TwoInitializers()
void AssignVector2DRefToVector2D()
void OneInitializer()
void AssignVector2DToVector2DRef()
void OperatorEqualsEquals()
void SwizzleWithRValueRefQualifier()
void OperatorEqualsAndNotEquals()
void TestFunction(const Vector2Df &input)
void PassedToFunction()
void AssignVector2DRefToVector2DRef()