-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_test.cpp
More file actions
197 lines (151 loc) · 6.82 KB
/
Copy pathvector_test.cpp
File metadata and controls
197 lines (151 loc) · 6.82 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "vector.hpp"
#include "catch_amalgamated.hpp"
// #include <catch2/matchers/catch_matchers_floating_point.hpp>
using geometry::Vector;
// for comparing floating point values
static const double EPSILON = 0.00001; // absolute difference
static const double MARGIN = 0.001; // 0.1% relative difference
static void require_almost_equal(const Vector& actual, const Vector& expected) {
REQUIRE_THAT(actual.x, Catch::Matchers::WithinRel(expected.x, MARGIN) ||
Catch::Matchers::WithinAbs(expected.x, EPSILON));
REQUIRE_THAT(actual.y, Catch::Matchers::WithinRel(expected.y, MARGIN) ||
Catch::Matchers::WithinAbs(expected.y, EPSILON));
REQUIRE_THAT(actual.z, Catch::Matchers::WithinRel(expected.z, MARGIN) ||
Catch::Matchers::WithinAbs(expected.z, EPSILON));
}
TEST_CASE("Vector constructors", "[constructors]") {
const Vector v1;
REQUIRE(v1.x == 0.0);
REQUIRE(v1.y == 0.0);
REQUIRE(v1.z == 0.0);
const Vector v2{1, -2.55, 3};
REQUIRE_THAT(v2.x, Catch::Matchers::WithinRel(1, MARGIN) ||
Catch::Matchers::WithinAbs(1, EPSILON));
REQUIRE_THAT(v2.y, Catch::Matchers::WithinRel(-2.55, MARGIN) ||
Catch::Matchers::WithinAbs(-2.55, EPSILON));
REQUIRE_THAT(v2.z, Catch::Matchers::WithinRel(3, MARGIN) ||
Catch::Matchers::WithinAbs(3, EPSILON));
}
TEST_CASE("Vector Addition", "[addition]") {
const Vector a{1, 2, 3};
const Vector b{4, 5, 6};
const Vector v1{a.plus(b)};
const Vector expected_v1{1+4, 2+5, 3+6};
require_almost_equal(v1, expected_v1);
const Vector v2{Vector{7.65, -3.4, 0.9}.plus(Vector{-7.65, 3.8, 1.7})};
const Vector expected_v2{0.0, 0.4, 2.6};
require_almost_equal(v2, expected_v2);
}
TEST_CASE("Vector Subtraction", "[subtraction]") {
Vector v1{Vector {1, 2, 3}.minus(Vector {4, 5, 6})};
Vector expected_v1{1-4, 2-5, 3-6};
require_almost_equal(v1, expected_v1);
Vector v2{Vector{7.65, -3.4, 0.9}.minus(Vector{-7.65, 3.8, 1.7})};
Vector expected_v2{15.3, -7.2, -0.8};
require_almost_equal(v2, expected_v2);
}
TEST_CASE("Dot Product", "[dot]") {
const Vector a{1, 2, 3};
const Vector b{4, 5, 6};
double ab_expected{32.0};
const Vector c{7.65, -3.4, 0.9};
const Vector d{-7.65, 3.8, 1.7};
double cd_expected {7.65 * (-7.65) + (-3.4) * 3.8 + 0.9 * 1.7};
SECTION("dot member") {
REQUIRE_THAT(a.dot(b), Catch::Matchers::WithinRel(ab_expected, MARGIN) ||
Catch::Matchers::WithinAbs(ab_expected, EPSILON));
REQUIRE_THAT(b.dot(a), Catch::Matchers::WithinRel(ab_expected, MARGIN) ||
Catch::Matchers::WithinAbs(ab_expected, EPSILON));
REQUIRE_THAT(c.dot(d), Catch::Matchers::WithinRel(cd_expected, MARGIN) ||
Catch::Matchers::WithinAbs(cd_expected, EPSILON));
REQUIRE_THAT(d.dot(c), Catch::Matchers::WithinRel(cd_expected, MARGIN) ||
Catch::Matchers::WithinAbs(cd_expected, EPSILON));
}
SECTION("dot operator (operator*)") {
REQUIRE_THAT(a.times(b), Catch::Matchers::WithinRel(ab_expected, MARGIN) ||
Catch::Matchers::WithinAbs(ab_expected, EPSILON));
REQUIRE_THAT(b.times(a), Catch::Matchers::WithinRel(ab_expected, MARGIN) ||
Catch::Matchers::WithinAbs(ab_expected, EPSILON));
REQUIRE_THAT(c.times(d), Catch::Matchers::WithinRel(cd_expected, MARGIN) ||
Catch::Matchers::WithinAbs(cd_expected, EPSILON));
REQUIRE_THAT(d.times(c), Catch::Matchers::WithinRel(cd_expected, MARGIN) ||
Catch::Matchers::WithinAbs(cd_expected, EPSILON));
}
}
// "Scalar * vector" would require overriding the * operator which is beyond
// the scope of this exercise; see cpp_overloading_vector_solution for this
TEST_CASE("Vector * scalar", "[times_scalar]") {
Vector v1{Vector {1, 2, 3}.times(2.5)};
Vector expected_v1{2.5, 5.0, 7.5};
require_almost_equal(v1, expected_v1);
Vector v2{Vector{7.65, -3.4, 0.9}.times(2.0)};
Vector expected_v2{15.3, -6.8, 1.8};
require_almost_equal(v2, expected_v2);
}
TEST_CASE("Cross Product", "[vector]") {
Vector v1{Vector {1, 0, 0}.cross(Vector {0, 1, 0})};
Vector expected_v1{0, 0, 1};
require_almost_equal(v1, expected_v1);
Vector v2{Vector{7.65, -3.4, 0.9}.cross(Vector {1, 1, 1})};
Vector expected_v2{-3.4 - 0.9, 0.9 -7.65, 7.65 + 3.4};
require_almost_equal(v2, expected_v2);
}
TEST_CASE("Length and Magnitude", "[length]") {
const Vector v1{3, 4, 0};
const Vector v2{1, 2, 2};
double expected_v1{5.0};
double expected_v2{3.0};
SECTION(".length") {
REQUIRE_THAT(v1.length(), Catch::Matchers::WithinRel(expected_v1, MARGIN) ||
Catch::Matchers::WithinAbs(expected_v1, EPSILON));
REQUIRE_THAT(v2.length(), Catch::Matchers::WithinRel(expected_v2, MARGIN) ||
Catch::Matchers::WithinAbs(expected_v2, EPSILON));
}
SECTION(".magnitude") {
REQUIRE_THAT(v1.magnitude(),
Catch::Matchers::WithinRel(expected_v1, MARGIN) ||
Catch::Matchers::WithinAbs(expected_v1, EPSILON));
REQUIRE_THAT(v2.magnitude(),
Catch::Matchers::WithinRel(expected_v2, MARGIN) ||
Catch::Matchers::WithinAbs(expected_v2, EPSILON));
}
}
TEST_CASE("Output Stream Operator", "[ostream]") {
const Vector v(1, 2.5, -3.14);
std::stringstream ss;
v.print(ss);
REQUIRE(ss.str() == "(1, 2.5, -3.14)");
}
TEST_CASE("Zero Vector Operations", "[zero]") {
const Vector zero;
const Vector a(1, 2, 3);
const Vector sum{zero.plus(a)};
require_almost_equal(sum, a);
const Vector diff{a.minus(zero)};
require_almost_equal(diff, a);
const double dot = zero.times(a);
REQUIRE_THAT(dot, Catch::Matchers::WithinRel(0, MARGIN) ||
Catch::Matchers::WithinAbs(0, EPSILON));
const Vector cross = zero.cross(a);
require_almost_equal(cross, zero);
REQUIRE_THAT(zero.magnitude(), Catch::Matchers::WithinRel(0, MARGIN) ||
Catch::Matchers::WithinAbs(0, EPSILON));
}
TEST_CASE("Parallel Vectors Cross Product", "[parallel]") {
const Vector a{1, 2, 3};
const Vector b{a.times(2.0)}; // b is parallel to a
const Vector cross{a.cross(b)};
REQUIRE_THAT(cross.magnitude(), Catch::Matchers::WithinRel(0, MARGIN) ||
Catch::Matchers::WithinAbs(0, EPSILON));
}
TEST_CASE("Large Vector Operations", "[large]") {
const Vector a{10000, 20000, 30000};
const Vector b{40000, 50000, 60000};
const Vector sum{a.plus(b)};
const Vector expected_sum{50000, 70000, 90000};
require_almost_equal(sum, expected_sum);
double dot{a.times(b)};
const double expected(3200000000);
REQUIRE_THAT(dot, Catch::Matchers::WithinRel(expected, MARGIN) ||
Catch::Matchers::WithinAbs(expected, EPSILON));
}