-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
371 lines (269 loc) · 9.27 KB
/
tests.cpp
File metadata and controls
371 lines (269 loc) · 9.27 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "breedSFC.h"
ofstream logfile;
bool test_tile_neighbours(Tile * tile) {
bool pass = true;
for (auto neighbour : tile->neighbours()) {
int i = 0;
for (auto next_neighbour : neighbour->neighbours()) {
if (next_neighbour == tile)
i++;
}
if (i != 1){
pass = false;
}
}
return pass;
}
bool test_neighbours(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
if (!test_tile_neighbours(tile)) {
pass = false;
logfile << "The tile " << tile->str() << " is not a neighbour of ";
logfile << "one of its neighbours." << endl;
}
}
return pass;
}
bool test_get_tiles(int N) {
auto Ntiles = SFCCube(N).getTiles().size();
bool pass = (Ntiles == N * N * 6);
if (!pass) {
logfile << "The cube has " << Ntiles << " tiles. Expected ";
logfile << N * N * 6 << "." << endl;
}
return pass;
}
bool test_centers(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
coordinate face_coordinate;
if (tile->face == LEFT) face_coordinate = -tile->getCenter().x();
else if (tile->face == FRONT) face_coordinate = -tile->getCenter().y();
else if (tile->face == RIGHT) face_coordinate = tile->getCenter().x();
else if (tile->face == BACK) face_coordinate = tile->getCenter().y();
else if (tile->face == TOP) face_coordinate = tile->getCenter().z();
else if (tile->face == BOTTOM) face_coordinate = -tile->getCenter().z();
if (!eq(face_coordinate, 0.5)) {
pass = false;
logfile << "test_centers: face_coordinate is " << face_coordinate;
logfile << ", expected " << 0.5 << ". The tile is " << tile->str() << ".";
logfile << endl;
}
}
return pass;
}
bool test_points(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
coordinate face_coordinate;
for (auto point : tile->getPoints()) {
if (tile->face == LEFT) face_coordinate = -point.x();
else if (tile->face == FRONT) face_coordinate = -point.y();
else if (tile->face == RIGHT) face_coordinate = point.x();
else if (tile->face == BACK) face_coordinate = point.y();
else if (tile->face == TOP) face_coordinate = point.z();
else if (tile->face == BOTTOM) face_coordinate = -point.z();
if (!eq(face_coordinate, 0.5)) {
pass = false;
logfile << "Points of tile " << tile->str() << " don't lie on the face. ";
logfile << "Coordinate is " << face_coordinate << " and expected 0.5";
logfile << endl;
}
}
}
return pass;
}
bool test_tile_edge(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
coordinate face_coordinate;
XYZVector previous;
bool has_previous = false;
for (auto point : tile->getPoints()) {
if (has_previous) {
XYZVector diff = XYZVector(point - previous);
auto diff_len = sqrt(diff.Dot(diff));
if (!eq(diff_len, coordinate(1) / c.N)) {
logfile << "Length of an edge is " << diff_len << ". ";
logfile << "Expected " << coordinate(1) / c.N << ". " << endl;
pass = false;
}
has_previous = true;
previous = point;
}
}
}
return pass;
}
bool test_getWires(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
for (auto wire : tile->getWires()) {
auto wire_length = sqrt((wire[1] - wire[0]).Mag2());
if (!eq(wire_length, coordinate(1) / c.N)) {
logfile << "Length of a wire is " << wire_length << ". ";
logfile << "Expected " << coordinate(1) / c.N << ". " << endl;
pass = false;
}
}
}
return pass;
}
bool test_wire_Z_rotation(int N) {
SFCCube c = SFCCube(N);
bool pass = true;
for (auto tile : c.getTiles()) {
for (auto wire : tile->getWires()) {
auto rotation = WireToZRotation(wire[0], wire[1]);
auto new_wire0 = rotation * wire[0];
auto new_wire1 = rotation * wire[1];
if (!eq(new_wire0.x(), new_wire1.x())) {
logfile << "The rotated wire is tilted around the x axis." << endl;
pass = false;
}
if (!eq(new_wire0.y(), new_wire1.y())) {
logfile << "The rotated wire is tilted around the y axis." << endl;
pass = false;
}
}
}
return pass;
}
bool test_WireAndPointTarnsform(int N) {
SFCCube c = SFCCube(N);
TRandom r;
bool pass = true;
for (auto tile : c.getTiles()) {
for (auto wire : tile->getWires()) {
// XYZPoint point = XYZPoint(r.Rndm(), r.Rndm(), r.Rndm());
XYZPoint point = XYZPoint(0, 0, 0);
auto transform = WireAndPointTarnsform(wire[0], wire[1], point);
auto new_point = transform * point;
auto new_wire0 = transform * wire[0];
auto new_wire1 = transform * wire[1];
if (!eq(new_point.y(), 0)) {
logfile << "The point after the WireAndPointTarnsform has y=" << new_point.y();
logfile << ". 0 expected." << endl;
pass = false;
}
for (auto new_wire : { new_wire0, new_wire1 }) {
if (!eq(new_wire.x(), 0)) {
logfile << "The wire point after the WireAndPointTarnsform has x=" << new_wire.x();
logfile << ". 0 expected." << endl;
pass = false;
}
if (!eq(new_wire.y(), 0)) {
logfile << "The wire point after the WireAndPointTarnsform has y=" << new_wire.y();
logfile << ". 0 expected." << endl;
pass = false;
}
}
}
}
return pass;
}
bool test_BfromWire_symmetry() {
TRandom r;
auto random_z = r.Rndm();
auto random_x = r.Rndm();
auto random_y = r.Rndm();
bool pass = true;
auto B = BfromWire({0, 0, -random_z}, {0, 0, random_z}, {random_x, random_y, 0});
if (!eq(B.z(), 0)) {
logfile << "BfromWire retured Bz = " << B.z() << " for symmetric wire. ";
logfile << "Expected 0." << endl;
pass = false;
}
return pass;
}
bool test_BfromWire_perpendicular() {
TRandom r;
auto random_z1 = r.Rndm();
auto random_z2 = r.Rndm();
auto random_z3 = r.Rndm();
auto random_x = r.Rndm();
auto random_y = r.Rndm();
bool pass = true;
auto B = BfromWire({0, 0, random_z1}, {0, 0, random_z2}, {random_x, random_y, random_z3});
if (!eq(B.x() * random_y, B.y() * random_x)) {
logfile << "B in the point is not perpendicular to the wire.";
pass = false;
}
return pass;
}
bool test_simple(bool (*test)(), string name) {
cout << " " << name << ": ";
bool pass = test();
if (pass)
cout << "\033[1;32m" << "✓ ";
else {
cout << "\033[1;31m" << "✘ ";
}
cout << '\r';
if (pass)
cout << "\033[1;32m" << "✔";
else
cout << "\033[1;31m" << "✗ ";
cout << "\033[0m" << endl;
return pass;
}
bool test_all_sizes(bool (*test)(int), string name, vector<int> sizes) {
bool all_pass = true;
cout << " " << name << ": ";
for (auto N : sizes) {
bool pass = test(N);
if (pass)
cout << "\033[1;32m" << "✓ ";
else {
all_pass = false;
cout << "\033[1;31m" << "✘ ";
}
}
cout << '\r';
if (all_pass)
cout << "\033[1;32m" << "✔";
else
cout << "\033[1;31m" << "✗ ";
cout << "\033[0m" << endl;
return all_pass;
}
int main(int argc, char **argv) {
logfile.open("tests.log");
// vector<int> Ns = {1, 2, 3, 4, 5, 10, 100};
vector<int> Ns = {1};
vector<bool> vpass;
vpass.push_back(test_all_sizes(test_get_tiles, "get tiles test ", Ns));
vpass.push_back(test_all_sizes(test_neighbours, "neighbours test ", Ns));
vpass.push_back(test_all_sizes(test_centers, "centers test ", Ns));
vpass.push_back(test_all_sizes(test_points, "points test ", Ns));
vpass.push_back(test_all_sizes(test_tile_edge, "edges test ", Ns));
vpass.push_back(test_all_sizes(test_getWires, "getWires test ", Ns));
vpass.push_back(test_all_sizes(test_wire_Z_rotation, "wire Z rotation test ", Ns));
vpass.push_back(test_all_sizes(test_WireAndPointTarnsform, "test_WireAndPointTarnsform test ", Ns));
vpass.push_back(test_simple(test_BfromWire_symmetry, "test_BfromWire symmetry test "));
vpass.push_back(test_simple(test_BfromWire_perpendicular, "test_BfromWire perp. test "));
bool pass = true;
for (auto p : vpass)
pass = pass && p;
// draw if passed all tests
if (pass)
cout << "\033[1;32m" << "✔";
else {
cout << "\033[1;31m" << "✗ ";
cout << endl;
cout << endl;
cout << "Test failed. Output written to tests.log." << endl;
}
cout << "\033[0m" << endl;
logfile.close();
}