-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2Test.cpp
More file actions
112 lines (93 loc) · 2.48 KB
/
Vector2Test.cpp
File metadata and controls
112 lines (93 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <cmath>
#include "Vector2.h"
bool doubleEquals(double a, double b, double epsilon = 1e-9)
{
return std::abs(a - b) < epsilon;
}
void testConstructors()
{
Vector2 v1;
Vector2 v2(3.0, 4.0);
std::cout << "Constructor Test: "
<< (v1.x == 0 && v1.y == 0 && v2.x == 3.0 && v2.y == 4.0 ? "PASSED" : "FAILED")
<< "\n";
}
void testOperators()
{
Vector2 v1(1.0, 2.0);
Vector2 v2(3.0, 4.0);
Vector2 vAdd = v1 + v2;
Vector2 vSub = v1 - v2;
Vector2 vMul = v1 * 2.0;
Vector2 vDiv = v2 / 2.0;
v1 += v2;
v2 -= Vector2(1.0, 1.0);
v1 *= 0.5;
v2 /= 2.0;
std::cout << "Operator Test: "
<< (vAdd.x == 4.0 && vAdd.y == 6.0 &&
vSub.x == -2.0 && vSub.y == -2.0 &&
vMul.x == 2.0 && vMul.y == 4.0 &&
vDiv.x == 1.5 && vDiv.y == 2.0 &&
v1.x == 2.0 && v1.y == 3.0 &&
v2.x == 1.0 && v2.y == 1.5)
<< "\n";
}
void testMagnitudeNormalize()
{
Vector2 v(3.0, 4.0);
double mag = v.magnitude();
Vector2 norm = v.normalize();
std::cout << "Magnitude/Normalize Test: "
<< (doubleEquals(mag, 5.0) &&
doubleEquals(norm.x, 0.6) &&
doubleEquals(norm.y, 0.8))
<< "\n";
}
void testDotDistance()
{
Vector2 v1(1.0, 0.0);
Vector2 v2(0.0, 1.0);
Vector2 v3(3.0, 4.0);
double dot = v1.dot(v2); // Should be 0
double distance = v3.distanceTo(Vector2(0.0, 0.0)); // Should be 5
std::cout << "Dot/Distance Test: "
<< (dot == 0.0 && doubleEquals(distance, 5.0))
<< "\n";
}
void testRotation()
{
Vector2 v(1.0, 0.0);
Vector2 rotated = v.rotate(M_PI / 2); // 90 degrees rotation
std::cout << "Rotate Test: "
<< (doubleEquals(rotated.x, 0.0) && doubleEquals(rotated.y, 1.0))
<< "\n";
}
void testEquality()
{
Vector2 v1(1.0, 2.0);
Vector2 v2(1.0, 2.0);
Vector2 v3(2.0, 3.0);
std::cout << "Equality Test: "
<< (v1 == v2 && !(v1 == v3))
<< "\n";
}
void testZero()
{
Vector2 zero = Vector2::zeroObj();
std::cout << "Zero Vector Test: "
<< (zero.x == 0.0 && zero.y == 0.0)
<< "\n";
}
int main()
{
testConstructors();
testOperators();
testMagnitudeNormalize();
testDotDistance();
testRotation();
testEquality();
testZero();
return 0;
}