-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbchean-TestDarwin.c++
499 lines (418 loc) · 14.9 KB
/
bchean-TestDarwin.c++
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
#include <iostream>
#include <sstream>
#include <string>
#include "gtest/gtest.h"
// expose functionality to make testing easier teehee
#define private public
#define protected public
#define class struct
#include "Darwin.h"
using namespace std;
// there are only 20 test methods
// this is because I chose only to test methods worth testing
// constructors, getters, setters, and the like don't have any nontrivial logic,
// so I didn't write any tests for them
// the tests I did write are thorough and cover (I hope) the necessary cases
// for what it's worth, there are 68 EXPECT statements
// -------
// SPECIES
// -------
TEST(SpeciesTest, AddGetInstruction) {
GoInstruction go(5);
LeftInstruction left;
InfectInstruction infect;
IfRandomInstruction ifrandom(2);
IfWallInstruction ifwall(10);
HopInstruction hop;
IfEmptyInstruction ifempty(1);
IfEnemyInstruction ifenemy(1);
RightInstruction right;
Species s;
s.add_instruction(go);
s.add_instruction(left);
s.add_instruction(infect);
s.add_instruction(ifrandom);
s.add_instruction(ifwall);
s.add_instruction(hop);
s.add_instruction(ifempty);
s.add_instruction(ifenemy);
s.add_instruction(right);
EXPECT_EQ(go, s.get_instruction(0));
EXPECT_EQ(left, s.get_instruction(1));
EXPECT_EQ(infect, s.get_instruction(2));
EXPECT_EQ(ifrandom, s.get_instruction(3));
EXPECT_EQ(ifwall, s.get_instruction(4));
EXPECT_EQ(hop, s.get_instruction(5));
EXPECT_EQ(ifempty, s.get_instruction(6));
EXPECT_EQ(ifenemy, s.get_instruction(7));
EXPECT_EQ(right, s.get_instruction(8));
}
// --------
// CREATURE
// --------
// test PC increment of action instructions
TEST(CreatureActTest, ActionInstructions) {
Species species;
species.add_instruction(RightInstruction());
species.add_instruction(InfectInstruction());
species.add_instruction(HopInstruction());
species.add_instruction(LeftInstruction());
species.add_instruction(HopInstruction());
Creature creature(species, Direction::SOUTH);
EXPECT_EQ(0, creature._program_counter);
CreatureActionType action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(1, creature._program_counter);
action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::INFECT, action);
EXPECT_EQ(2, creature._program_counter);
action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::HOP, action);
EXPECT_EQ(3, creature._program_counter);
action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(4, creature._program_counter);
}
// test PC increments of control instructions
TEST(CreatureActTest, IfEmpty) {
Species species;
species.add_instruction(IfEmptyInstruction(2));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
species.add_instruction(IfEmptyInstruction(5));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
Creature creature(species, Direction::SOUTH);
CreatureActionType action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(3, creature._program_counter);
action = creature.act(GridEntity::WALL);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(5, creature._program_counter);
}
TEST(CreatureActTest, IfWall) {
Species species;
species.add_instruction(IfWallInstruction(2));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
species.add_instruction(IfWallInstruction(5));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
Creature creature(species, Direction::SOUTH);
CreatureActionType action = creature.act(GridEntity::WALL);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(3, creature._program_counter);
action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(5, creature._program_counter);
}
TEST(CreatureActTest, IfRandom) {
srand(0);
Species species;
species.add_instruction(IfRandomInstruction(2));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
species.add_instruction(IfRandomInstruction(5));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
Creature creature(species, Direction::SOUTH);
CreatureActionType action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(3, creature._program_counter);
action = creature.act(GridEntity::WALL);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(5, creature._program_counter);
}
TEST(CreatureActTest, IfEnemy) {
Species species;
species.add_instruction(IfEnemyInstruction(3));
species.add_instruction(LeftInstruction());
species.add_instruction(GoInstruction(0));
species.add_instruction(RightInstruction());
species.add_instruction(LeftInstruction());
Creature creature(species, Direction::SOUTH);
string own_identity = species.get_identity();
CreatureActionType action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(2, creature._program_counter);
action = creature.act(GridEntity::WALL);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(2, creature._program_counter);
action = creature.act(own_identity);
EXPECT_EQ(CreatureActionType::TURN_LEFT, action);
EXPECT_EQ(2, creature._program_counter);
action = creature.act(string("enemy of ") + own_identity);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(4, creature._program_counter);
}
TEST(CreatureActTest, Go) {
Species species;
species.add_instruction(GoInstruction(2));
species.add_instruction(LeftInstruction());
species.add_instruction(RightInstruction());
species.add_instruction(HopInstruction());
Creature creature(species, Direction::SOUTH);
CreatureActionType action = creature.act(GridEntity::NOTHING);
EXPECT_EQ(CreatureActionType::TURN_RIGHT, action);
EXPECT_EQ(3, creature._program_counter);
}
// more complex branching
// imagine a hyphen between Multiple and Instruction
TEST(CreatureActTest, MultipleInstructionAction) {
// actions with logic
Species species;
species.add_instruction(IfEmptyInstruction(2));
species.add_instruction(HopInstruction());
species.add_instruction(HopInstruction());
species.add_instruction(IfWallInstruction(0));
Creature creature(species, Direction::SOUTH);
EXPECT_EQ(0, creature._program_counter);
// single branch
// if-empty succeeds, go to 2, which takes us to 3
creature.act(GridEntity::NOTHING);
EXPECT_EQ(3, creature._program_counter);
// double branch
creature.act(GridEntity::WALL);
// if-wall succeeds, go to 0, if-empty fails, go to 1, which takes us to 2
EXPECT_EQ(2, creature._program_counter);
}
// ----
// GRID
// ----
TEST(GridPrintTest, Square) {
Grid grid(4, 4);
ostringstream oss;
string expected(string() +
" 0123\n" +
"0 ....\n" +
"1 ....\n" +
"2 ....\n" +
"3 ....\n"
);
grid.print(oss);
EXPECT_STREQ(expected.c_str(), oss.str().c_str());
}
TEST(GridPrintTest, NotSquare) {
Grid grid(2, 3);
ostringstream oss;
string expected(string() +
" 012\n" +
"0 ...\n" +
"1 ...\n"
);
grid.print(oss);
EXPECT_STREQ(expected.c_str(), oss.str().c_str());
}
TEST(GridPrintTest, Large) {
Grid grid(11, 13);
ostringstream oss;
string expected(string() +
" 0123456789012\n" +
"0 .............\n" +
"1 .............\n" +
"2 .............\n" +
"3 .............\n" +
"4 .............\n" +
"5 .............\n" +
"6 .............\n" +
"7 .............\n" +
"8 .............\n" +
"9 .............\n" +
"0 .............\n"
);
grid.print(oss);
EXPECT_STREQ(expected.c_str(), oss.str().c_str());
}
TEST(GridPrintTest, NotEmpty) {
Grid grid(4, 4);
Creature c1(Food(), Direction::SOUTH);
Creature c2(Hopper(), Direction::SOUTH);
Creature c3(Rover(), Direction::SOUTH);
Creature c4(Trap(), Direction::SOUTH);
grid.place_creature(0, 0, c1);
grid.place_creature(1, 3, c2);
grid.place_creature(2, 0, c3);
grid.place_creature(3, 1, c4);
ostringstream oss;
string expected(string() +
" 0123\n" +
"0 f...\n" +
"1 ...h\n" +
"2 r...\n" +
"3 .t..\n"
);
grid.print(oss);
EXPECT_STREQ(expected.c_str(), oss.str().c_str());
}
TEST(GridPlaceCreatureTest, Success) {
Grid grid(4, 4);
Creature creature(Species(), Direction::SOUTH);
bool place_succeeded = grid.place_creature(0, 0, creature);
EXPECT_TRUE(place_succeeded);
}
TEST(GridPlaceCreatureTest, AttemptReplace) {
Grid grid(4, 4);
Creature creature(Species(), Direction::SOUTH);
grid.place_creature(0, 0, creature);
// can't place creature in occupied spot
bool replace_succeeded = grid.place_creature(0, 0, creature);
EXPECT_FALSE(replace_succeeded);
}
TEST(GridGoadSingleTest, Hop) {
Grid grid(4, 4);
// hoppers hop indefinitely
Creature hop1(Hopper(), Direction::EAST);
Creature hop2(Hopper(), Direction::EAST);
Creature hop3(Hopper(), Direction::EAST);
// food doesn't move
Creature food(Food(), Direction::EAST);
grid.place_creature(0, 0, hop1);
grid.place_creature(1, 3, hop2);
grid.place_creature(2, 1, hop3);
grid.place_creature(2, 2, food);
// this guy is facing nothing, so he will move
EXPECT_NE(nullptr, grid._creature_grid[0][0]);
grid.goad_single_creature(0, 0);
EXPECT_EQ(&hop1, grid._creature_grid[0][1]);
// must vacate the spot behind him
EXPECT_EQ(nullptr, grid._creature_grid[0][0]);
// this guy won't move, he's facing a wall
grid.goad_single_creature(1, 3);
EXPECT_EQ(&hop2, grid._creature_grid[1][3]);
// this guy won't move, he's facing someone else
grid.goad_single_creature(2, 1);
EXPECT_EQ(&hop3, grid._creature_grid[2][1]);
}
TEST(GridGoadSingleTest, Turn) {
Grid grid(2, 2);
// food only turns left
Creature food(Food(), Direction::WEST);
// anti-food only turns right
Species right_turner;
right_turner.add_instruction(RightInstruction());
right_turner.add_instruction(GoInstruction(0));
Creature anti_food(right_turner, Direction::EAST);
grid.place_creature(0, 0, food);
grid.place_creature(0, 1, anti_food);
Creature* const & food_cell = grid._creature_grid[0][0];
Creature* const & anti_food_cell = grid._creature_grid[0][1];
// both start facing walls, they will turn
grid.goad_single_creature(0, 0);
grid.goad_single_creature(0, 1);
EXPECT_NE(nullptr, food_cell);
EXPECT_EQ(Direction::SOUTH, food_cell->get_direction());
EXPECT_NE(nullptr, anti_food_cell);
EXPECT_EQ(Direction::SOUTH, anti_food_cell->get_direction());
// now facing nothing, they will turn
grid.goad_single_creature(0, 0);
grid.goad_single_creature(0, 1);
EXPECT_NE(nullptr, food_cell);
EXPECT_EQ(Direction::EAST, food_cell->get_direction());
EXPECT_NE(nullptr, anti_food_cell);
EXPECT_EQ(Direction::WEST, anti_food_cell->get_direction());
// now facing each other, they will turn
grid.goad_single_creature(0, 0);
grid.goad_single_creature(0, 1);
EXPECT_NE(nullptr, food_cell);
EXPECT_EQ(Direction::NORTH, food_cell->get_direction());
EXPECT_NE(nullptr, anti_food_cell);
EXPECT_EQ(Direction::NORTH, anti_food_cell->get_direction());
}
TEST(GridGoadSingleTest, Infect) {
Grid grid(3, 2);
// not an infector
Creature enemy(Food(), Direction::SOUTH);
enemy._program_counter = 1;
Species infector("infector");
infector.add_instruction(InfectInstruction());
infector.add_instruction(GoInstruction(0));
// an infector
Creature guy(infector, Direction::WEST);
// fellow infector
// can't spell friend correctly unfortunately
Creature freind(infector, Direction::NORTH);
grid.place_creature(0, 0, enemy);
grid.place_creature(1, 0, guy);
grid.place_creature(1, 1, freind);
Creature* const & enemy_cell = grid._creature_grid[0][0];
Creature* const & guy_cell = grid._creature_grid[1][0];
Creature* const & freind_cell = grid._creature_grid[1][1];
// starts facing wall, will do nothing
grid.goad_single_creature(1, 0);
EXPECT_STREQ("infector", freind_cell->get_identity().c_str());
EXPECT_STREQ("Food", enemy_cell->get_identity().c_str());
guy_cell->set_direction(Direction::SOUTH);
// now facing nothing, will do nothing
grid.goad_single_creature(1, 0);
EXPECT_STREQ("infector", freind_cell->get_identity().c_str());
EXPECT_STREQ("Food", enemy_cell->get_identity().c_str());
guy_cell->set_direction(Direction::EAST);
// now facing friend, will do nothing
grid.goad_single_creature(1, 0);
EXPECT_STREQ("infector", freind_cell->get_identity().c_str());
EXPECT_STREQ("Food", enemy_cell->get_identity().c_str());
EXPECT_EQ(1, enemy_cell->_program_counter);
EXPECT_EQ(Direction::SOUTH, enemy_cell->get_direction());
guy_cell->set_direction(Direction::NORTH);
// now facing enemy, will infect Food
grid.goad_single_creature(1, 0);
EXPECT_STREQ("infector", freind_cell->get_identity().c_str());
// identity changes
EXPECT_STREQ("infector", enemy_cell->get_identity().c_str());
// program counter reset
EXPECT_EQ(0, enemy_cell->_program_counter);
// direction stays the same
EXPECT_EQ(Direction::SOUTH, enemy_cell->get_direction());
}
TEST(GridGoadAllTest, DisparateSpecies) {
Grid grid(2, 4);
Creature food(Food(), Direction::NORTH);
Creature hopper(Hopper(), Direction::WEST);
Creature trap(Trap(), Direction::NORTH);
grid.place_creature(0, 0, food);
grid.place_creature(0, 2, hopper);
grid.place_creature(1, 3, trap);
grid.goad_all_creatures();
// after one cycle, food and trap will have turned, and hopper will have hopped
EXPECT_EQ(Direction::WEST, grid._creature_grid[0][0]->get_direction());
EXPECT_NE(nullptr, grid._creature_grid[0][1]);
EXPECT_EQ(nullptr, grid._creature_grid[0][2]);
EXPECT_EQ(Direction::WEST, grid._creature_grid[1][3]->get_direction());
grid.goad_all_creatures();
// food and trap will have turned again, hopper will have stopped
EXPECT_EQ(Direction::SOUTH, grid._creature_grid[0][0]->get_direction());
EXPECT_NE(nullptr, grid._creature_grid[0][1]);
EXPECT_EQ(Direction::SOUTH, grid._creature_grid[1][3]->get_direction());
}
TEST(GridGoadAllTest, SameSpecies) {
Grid grid(2, 3);
Creature hop1(Hopper(), Direction::EAST);
Creature hop2(Hopper(), Direction::WEST);
grid.place_creature(0, 0, hop1);
grid.place_creature(1, 2, hop2);
// two hoppers passing along each other
grid.goad_all_creatures();
EXPECT_EQ(nullptr, grid._creature_grid[0][0]);
EXPECT_NE(nullptr, grid._creature_grid[0][1]);
EXPECT_EQ(nullptr, grid._creature_grid[1][2]);
EXPECT_NE(nullptr, grid._creature_grid[1][1]);
grid.goad_all_creatures();
EXPECT_EQ(nullptr, grid._creature_grid[0][1]);
EXPECT_NE(nullptr, grid._creature_grid[0][2]);
EXPECT_EQ(nullptr, grid._creature_grid[1][1]);
EXPECT_NE(nullptr, grid._creature_grid[1][0]);
}
TEST(GridGoadAllTest, GoadedOnlyOnce) {
Grid grid(3, 1);
Creature hopper(Hopper(), Direction::SOUTH);
grid.place_creature(0, 0, hopper);
// hopper will move down
// will pass over a second time during goad scan
// but shouldn't goad again
grid.goad_all_creatures();
EXPECT_EQ(nullptr, grid._creature_grid[0][0]);
EXPECT_NE(nullptr, grid._creature_grid[1][0]);
grid.goad_all_creatures();
EXPECT_EQ(nullptr, grid._creature_grid[1][0]);
EXPECT_NE(nullptr, grid._creature_grid[2][0]);
}