-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-maze.cc
525 lines (400 loc) · 14.4 KB
/
test-maze.cc
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include "testbase.hh"
#include "maze.hh"
#include <cassert>
#include <cstdlib>
#include <iostream>
using namespace std;
/*
* Test code for two-argument constructor
*/
void test_constructor(TestContext &ctx) {
ctx.DESC("Two-argument constructor");
// Put these in separate blocks to force constructor/destructor calls.
{
Maze m(50, 30);
ctx.CHECK(m.getNumRows() == 50);
ctx.CHECK(m.getNumCols() == 30);
// Write and then read each cell just so we make sure we can do that.
// We will do more exhaustive tests later.
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
m.setCell(r, c, MazeCell::EMPTY);
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
}
}
}
{
Maze m(30, 50);
ctx.CHECK(m.getNumRows() == 30);
ctx.CHECK(m.getNumCols() == 50);
// Write and then read each cell just so we make sure we can do that.
// We will do more exhaustive tests later.
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
m.setCell(r, c, MazeCell::EMPTY);
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
}
}
}
ctx.result();
}
/*
* Test code for get/set start/end value
*/
void test_start_end(TestContext &ctx) {
ctx.DESC("Get/set start/end");
// Put these in separate blocks to force constructor/destructor calls.
Maze m(10, 20);
m.setStart(3, 5);
m.setEnd(2, 1);
ctx.CHECK(m.getStart() == Location(3, 5));
ctx.CHECK(m.getEnd() == Location(2, 1));
m.setStart(8, 18);
m.setEnd(0, 6);
ctx.CHECK(m.getStart() == Location(8, 18));
ctx.CHECK(m.getEnd() == Location(0, 6));
ctx.result();
}
/*
* Test code for get-neighbor-cell operation
*/
void test_neighbor(TestContext &ctx) {
ctx.DESC("Get neighbor");
Maze m(6, 6);
ctx.CHECK(m.getNeighborCell(3, 2, Direction::NORTH) == Location(2, 2));
ctx.CHECK(m.getNeighborCell(3, 2, Direction::SOUTH) == Location(4, 2));
ctx.CHECK(m.getNeighborCell(3, 2, Direction::EAST) == Location(3, 3));
ctx.CHECK(m.getNeighborCell(3, 2, Direction::WEST) == Location(3, 1));
ctx.result();
}
/*
* Test code for get/set cell operations
*/
bool check_only_cell_visited(const Maze &m, int r_check, int c_check) {
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
MazeCell expected = MazeCell::EMPTY;
if (r == r_check && c == c_check)
expected = MazeCell::VISITED;
if (m.getCell(r, c) != expected)
return false;
}
}
return true;
}
void test_get_set_cell(TestContext &ctx) {
ctx.DESC("Get/set cell");
Maze m(8, 16);
m.clear();
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
m.setCell(r, c, MazeCell::VISITED);
ctx.CHECK(check_only_cell_visited(m, r, c));
m.setCell(r, c, MazeCell::EMPTY);
ctx.CHECK(check_only_cell_visited(m, -1, -1));
}
}
ctx.result();
}
/*
* Test code for has-wall/set-wall/clear-wall operations
*/
Direction opposite_direction(Direction dir) {
switch (dir) {
case Direction::NORTH:
return Direction::SOUTH;
case Direction::SOUTH:
return Direction::NORTH;
case Direction::EAST:
return Direction::WEST;
case Direction::WEST:
return Direction::EAST;
}
assert(false);
}
bool cell_has_one_wall(const Maze &m, int r, int c, Direction dir) {
return m.hasWall(r, c, dir) &&
(!m.hasWall(r, c, Direction::NORTH) || dir == Direction::NORTH) &&
(!m.hasWall(r, c, Direction::EAST ) || dir == Direction::EAST ) &&
(!m.hasWall(r, c, Direction::SOUTH) || dir == Direction::SOUTH) &&
(!m.hasWall(r, c, Direction::WEST ) || dir == Direction::WEST );
}
bool check_only_wall_set(const Maze &m,
int r_check, int c_check, Direction dir_check) {
// The neighbor cell will also see the wall
bool hasNeighbor =
(r_check > 0 && dir_check == Direction::NORTH) ||
(r_check < m.getNumRows() - 1 && dir_check == Direction::SOUTH) ||
(c_check > 0 && dir_check == Direction::WEST) ||
(c_check < m.getNumCols() - 1 && dir_check == Direction::EAST);
Location neighbor(-1, -1);
Direction opposite_dir = opposite_direction(dir_check);
if (hasNeighbor)
neighbor = m.getNeighborCell(r_check, c_check, dir_check);
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
if (r == r_check && c == c_check) {
// We expect that one of the adjacent walls should be set.
if (!cell_has_one_wall(m, r, c, dir_check)) {
return false;
}
}
else if (hasNeighbor && r == neighbor.row && c == neighbor.col) {
// This cell borders the wall that was created.
// We expect that one of the adjacent walls should be set.
if (!cell_has_one_wall(m, r, c, opposite_dir)) {
return false;
}
}
else if (m.hasWall(r, c, Direction::NORTH) ||
m.hasWall(r, c, Direction::EAST) ||
m.hasWall(r, c, Direction::SOUTH) ||
m.hasWall(r, c, Direction::WEST)) {
return false;
}
}
}
return true;
}
bool check_no_wall_set(const Maze &m) {
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
if (m.hasWall(r, c, Direction::NORTH) ||
m.hasWall(r, c, Direction::EAST) ||
m.hasWall(r, c, Direction::SOUTH) ||
m.hasWall(r, c, Direction::WEST)) {
return false;
}
}
}
return true;
}
void test_walls(TestContext &ctx) {
ctx.DESC("Get/set wall");
Maze m(8, 16);
m.clear();
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
m.setWall(r, c, Direction::NORTH);
ctx.CHECK(check_only_wall_set(m, r, c, Direction::NORTH));
m.clearWall(r, c, Direction::NORTH);
ctx.CHECK(check_no_wall_set(m));
m.setWall(r, c, Direction::EAST);
ctx.CHECK(check_only_wall_set(m, r, c, Direction::EAST));
m.clearWall(r, c, Direction::EAST);
ctx.CHECK(check_no_wall_set(m));
m.setWall(r, c, Direction::SOUTH);
ctx.CHECK(check_only_wall_set(m, r, c, Direction::SOUTH));
m.clearWall(r, c, Direction::SOUTH);
ctx.CHECK(check_no_wall_set(m));
m.setWall(r, c, Direction::WEST);
ctx.CHECK(check_only_wall_set(m, r, c, Direction::WEST));
m.clearWall(r, c, Direction::WEST);
ctx.CHECK(check_no_wall_set(m));
}
}
ctx.result();
}
/*
* Test code for clear() operation
*/
void test_clear(TestContext &ctx) {
ctx.DESC("clear() operation");
// Put these in separate blocks to force constructor/destructor calls.
{
Maze m(50, 30);
m.clear();
// Verify that everything is cleared
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
// Yes, this will check many walls multiple times, but it's
// the easiest most brain-dead way to test.
ctx.CHECK(!m.hasWall(r, c, Direction::NORTH));
ctx.CHECK(!m.hasWall(r, c, Direction::WEST));
ctx.CHECK(!m.hasWall(r, c, Direction::SOUTH));
ctx.CHECK(!m.hasWall(r, c, Direction::EAST));
}
}
}
{
Maze m(30, 50);
m.clear();
// Verify that everything is cleared
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
ctx.CHECK(!m.hasWall(r, c, Direction::NORTH));
ctx.CHECK(!m.hasWall(r, c, Direction::WEST));
ctx.CHECK(!m.hasWall(r, c, Direction::SOUTH));
ctx.CHECK(!m.hasWall(r, c, Direction::EAST));
}
}
}
ctx.result();
}
/*
* Test code for set-all-walls operation
*/
void test_set_all_walls(TestContext &ctx) {
ctx.DESC("setAllWalls() operation");
// Put these in separate blocks to force constructor/destructor calls.
{
Maze m(50, 30);
m.clear();
m.setAllWalls();
// Verify that all walls are set
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
// Yes, this will check many walls multiple times, but it's
// the easiest most brain-dead way to test.
ctx.CHECK(m.hasWall(r, c, Direction::NORTH));
ctx.CHECK(m.hasWall(r, c, Direction::WEST));
ctx.CHECK(m.hasWall(r, c, Direction::SOUTH));
ctx.CHECK(m.hasWall(r, c, Direction::EAST));
}
}
}
{
Maze m(30, 50);
m.clear();
m.setAllWalls();
// Verify that all walls are set
for (int r = 0; r < m.getNumRows(); r++) {
for (int c = 0; c < m.getNumCols(); c++) {
ctx.CHECK(m.getCell(r, c) == MazeCell::EMPTY);
ctx.CHECK(m.hasWall(r, c, Direction::NORTH));
ctx.CHECK(m.hasWall(r, c, Direction::WEST));
ctx.CHECK(m.hasWall(r, c, Direction::SOUTH));
ctx.CHECK(m.hasWall(r, c, Direction::EAST));
}
}
}
ctx.result();
}
/*
* Test code for copy constructor
*
* We have this so far down the sequence because we want to be sure everything
* else works before we try out copying things.
*/
void test_copy_ctor(TestContext &ctx) {
ctx.DESC("Copy constructor");
Maze m1(4, 6);
m1.clear();
m1.setCell(2, 2, MazeCell::VISITED);
m1.setWall(1, 3, Direction::EAST);
m1.setCell(3, 5, MazeCell::VISITED);
m1.setWall(3, 4, Direction::SOUTH);
ctx.CHECK(m1.getCell(2, 2) == MazeCell::VISITED);
ctx.CHECK(m1.getCell(3, 5) == MazeCell::VISITED);
ctx.CHECK(m1.hasWall(1, 3, Direction::EAST));
ctx.CHECK(m1.hasWall(3, 4, Direction::SOUTH));
Maze m2(m1);
ctx.CHECK(m2.getCell(2, 2) == MazeCell::VISITED);
ctx.CHECK(m2.getCell(3, 5) == MazeCell::VISITED);
ctx.CHECK(m2.hasWall(1, 3, Direction::EAST));
ctx.CHECK(m2.hasWall(3, 4, Direction::SOUTH));
m1.setCell(2, 2, MazeCell::EMPTY);
ctx.CHECK(m1.getCell(2, 2) == MazeCell::EMPTY);
ctx.CHECK(m2.getCell(2, 2) == MazeCell::VISITED);
m2.clearWall(3, 4, Direction::SOUTH);
ctx.CHECK(m1.hasWall(3, 4, Direction::SOUTH));
ctx.CHECK(!m2.hasWall(3, 4, Direction::SOUTH));
ctx.result();
}
/*
* Test code for assignment operator
*
* We have this so far down the sequence because we want to be sure everything
* else works before we try out copying things.
*/
void test_assignment(TestContext &ctx) {
ctx.DESC("Assignment operator");
Maze m1(12, 8);
m1.clear();
m1.setStart(2, 3);
m1.setEnd(9, 7);
m1.setCell(2, 2, MazeCell::VISITED);
m1.setWall(1, 3, Direction::EAST);
m1.setCell(3, 5, MazeCell::VISITED);
m1.setWall(3, 4, Direction::SOUTH);
ctx.CHECK(m1.getCell(2, 2) == MazeCell::VISITED);
ctx.CHECK(m1.getCell(3, 5) == MazeCell::VISITED);
ctx.CHECK(m1.hasWall(1, 3, Direction::EAST));
ctx.CHECK(m1.hasWall(3, 4, Direction::SOUTH));
Maze m2(4, 4);
m2.clear();
m2.setStart(0, 0);
m2.setEnd(1, 1);
m2 = m1;
ctx.CHECK(m2.getNumRows() == 12);
ctx.CHECK(m2.getNumCols() == 8);
ctx.CHECK(m2.getStart() == m1.getStart());
ctx.CHECK(m2.getEnd() == m1.getEnd());
ctx.CHECK(m2.getCell(2, 2) == MazeCell::VISITED);
ctx.CHECK(m2.getCell(3, 5) == MazeCell::VISITED);
ctx.CHECK(m2.hasWall(1, 3, Direction::EAST));
ctx.CHECK(m2.hasWall(3, 4, Direction::SOUTH));
m1.setCell(2, 2, MazeCell::EMPTY);
ctx.CHECK(m1.getCell(2, 2) == MazeCell::EMPTY);
ctx.CHECK(m2.getCell(2, 2) == MazeCell::VISITED);
m2.clearWall(3, 4, Direction::SOUTH);
ctx.CHECK(m1.hasWall(3, 4, Direction::SOUTH));
ctx.CHECK(!m2.hasWall(3, 4, Direction::SOUTH));
ctx.result();
ctx.DESC("Self-assignment");
// Why not?
m1 = m1 = m1;
ctx.CHECK(m1.getCell(2, 2) == MazeCell::EMPTY);
ctx.CHECK(m1.getCell(3, 5) == MazeCell::VISITED);
ctx.CHECK(m1.hasWall(1, 3, Direction::EAST));
ctx.CHECK(m1.hasWall(3, 4, Direction::SOUTH));
ctx.result();
}
/*
* Test code for displaying maze
*
*/
void test_print() {
Maze m1(3, 4);
m1.clear();
m1.setStart(0, 0);
m1.setEnd(2, 3);
m1.setAllWalls();
m1.clearWall(0, 0, Direction::EAST);
m1.clearWall(0, 1, Direction::SOUTH);
m1.clearWall(1, 1, Direction::SOUTH);
m1.clearWall(2, 1, Direction::EAST);
m1.clearWall(2, 1, Direction::WEST);
m1.clearWall(2, 0, Direction::NORTH);
m1.clearWall(2, 2, Direction::NORTH);
m1.clearWall(1, 2, Direction::NORTH);
m1.clearWall(0, 2, Direction::EAST);
m1.clearWall(0, 3, Direction::SOUTH);
m1.clearWall(1, 3, Direction::SOUTH);
m1.print(cout);
}
/*
* Main program to run tests!
*/
/*! This program is a simple test-suite for the Maze class. */
int main() {
cout << "Testing the Maze class." << endl << endl;
// srand(654321L);
TestContext ctx(cout);
test_constructor(ctx);
test_start_end(ctx);
test_neighbor(ctx);
test_get_set_cell(ctx);
test_walls(ctx);
test_clear(ctx);
test_set_all_walls(ctx);
test_copy_ctor(ctx);
test_assignment(ctx);
cout << endl << "Maze display" << endl << endl;
test_print();
// Return 0 if everything passed, nonzero if something failed.
return !ctx.ok();
}