forked from cs371pc-fall-2016/darwin-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcw33374-TestDarwin.c++
424 lines (345 loc) · 11.1 KB
/
cw33374-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
// ------------------------------
// projects/darwin/TestDarwin.c++
// Copyright (C) 2016
// Glenn P. Downing
// ------------------------------
// https://code.google.com/p/googletest/wiki/V1_7_Primer#Basic_Assertions
// --------
// includes
// --------
#include "gtest/gtest.h"
#include "Darwin.h"
#include <sstream>
#include <string>
using namespace std;
// ----
// test
// ----
// Critter should be able to hop.
TEST(CritterFixture, test_action_hop) {
vector<Instruction> inst({{Opcode::hop, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::hop, action.op);
}
// Critter should be able to turn left.
TEST(CritterFixture, test_action_left) {
vector<Instruction> inst({{Opcode::left, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should be able to turn right.
TEST(CritterFixture, test_action_right) {
vector<Instruction> inst({{Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should be able to infect.
TEST(CritterFixture, test_action_infect) {
vector<Instruction> inst({{Opcode::infect, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::infect, action.op);
}
// Critter should take the branch on if_empty if the space in front of it is
// empty.
TEST(CritterFixture, test_if_empty_y) {
vector<Instruction> inst(
{{Opcode::if_empty, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should not take the branch on if_empty if the space in front of it is
// not empty.
TEST(CritterFixture, test_if_empty_n) {
vector<Instruction> inst(
{{Opcode::if_empty, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
front.type = EntityType::wall;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should take the branch on if_wall if the space in front of it is
// a wall.
TEST(CritterFixture, test_if_wall_y) {
vector<Instruction> inst(
{{Opcode::if_wall, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
front.type = EntityType::wall;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should not take the branch on if_wall if the space in front of it is
// not a wall.
TEST(CritterFixture, test_if_wall_n) {
vector<Instruction> inst(
{{Opcode::if_wall, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should take the branch on if_random if the generated random integer
// is odd.
TEST(CritterFixture, test_rand_y) {
vector<Instruction> inst(
{{Opcode::if_random, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
srand(0);
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should not take the branch on if_random if the space in front of it
// is even.
TEST(CritterFixture, test_rand_n) {
vector<Instruction> inst(
{{Opcode::if_random, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
srand(2);
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should take the branch on if_enemy if the space in front of it is
// occupied and the thing that occupies it is a critter.
TEST(CritterFixture, test_if_enemy_y) {
vector<Instruction> inst(
{{Opcode::if_enemy, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Species s2({'s', inst});
Critter c2(&s2);
Entity front;
front.type = EntityType::critter;
front.critter = c2;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should not take the branch on if_empty if the space in front of it is
// occupied by a critter of the same species.
TEST(CritterFixture, test_if_enemy_n_same_species) {
vector<Instruction> inst(
{{Opcode::if_enemy, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Critter c2(&s);
Entity front;
front.type = EntityType::critter;
front.critter = c2;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should not take the branch on if_empty if the space in front of it is
// empty.
TEST(CritterFixture, test_if_enemy_n_empty) {
vector<Instruction> inst(
{{Opcode::if_enemy, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should be able to go.
TEST(CritterFixture, test_go) {
vector<Instruction> inst(
{{Opcode::go, 2}, {Opcode::left, 0}, {Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should execute an arbitrary number of control flow instructions
// (correctly) before returning an instruction.
TEST(CritterFixture, test_combo) {
vector<Instruction> inst({{Opcode::go, 2},
{Opcode::left, 0},
{Opcode::if_random, 4},
{Opcode::left, 0},
{Opcode::if_enemy, 7},
{Opcode::if_wall, 7},
{Opcode::if_empty, 8},
{Opcode::left, 0},
{Opcode::right, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
srand(0);
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::right, action.op);
}
// Critter should be able to execute instructions in a loop.
TEST(CritterFixture, test_loop) {
vector<Instruction> inst(
{{Opcode::left, 0}, {Opcode::right, 0}, {Opcode::go, 0}});
Species s({'s', inst});
Critter c(&s);
Entity front;
srand(0);
const auto &action = c.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
ASSERT_EQ(Opcode::right, c.next_action(front).op);
ASSERT_EQ(Opcode::left, c.next_action(front).op);
ASSERT_EQ(Opcode::right, c.next_action(front).op);
}
// When a critter infects an enemy critter, changing its species and resetting
// its program counter.
TEST(CritterFixture, test_infect_enemy) {
vector<Instruction> inst1({{Opcode::infect, 0}});
vector<Instruction> inst2({{Opcode::left, 0}, {Opcode::right, 0}});
Species s1({'s', inst1});
Critter c1(&s1);
Species s2({'2', inst2});
Critter c2(&s2);
Entity front;
c2.next_action(front);
c1.infect(c2);
// Test species changed.
ostringstream o2;
o2 << c2;
ASSERT_EQ("s", o2.str());
// Test program counter reset.
const auto &action = c2.next_action(front);
ASSERT_EQ(Opcode::infect, action.op);
}
// When a critter infects a critter of its own species, it should not change
// the species or the program counter of the infected critter.
TEST(CritterFixture, test_infect_friend) {
vector<Instruction> inst({{Opcode::infect, 0}, {Opcode::left, 0}});
Species s({'s', inst});
Critter c1(&s);
Critter c2(&s);
Entity front;
c2.next_action(front);
c1.infect(c2);
const auto &action = c2.next_action(front);
ASSERT_EQ(Opcode::left, action.op);
}
// Critter should print its name.
TEST(CritterFixture, test_print) {
vector<Instruction> inst({{Opcode::left, 0}});
Species s({'s', inst});
Critter c(&s);
ostringstream out;
out << c;
ASSERT_EQ("s", out.str());
}
// Board state should reflect adding one critter.
TEST(DarwinFixture, test_add_1) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::hop, 0}});
Species s({'s', inst});
game.add_critter(0, 1, Direction::south, &s);
ostringstream o;
o << game;
ASSERT_EQ("Turn = 0.\n 01\n0 ..\n1 s.\n", o.str());
}
// Board state should reflect a critter hopping forward.
TEST(DarwinFixture, test_hop_turn) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::hop, 0}});
Species s({'s', inst});
game.add_critter(0, 0, Direction::south, &s);
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 1.\n 01\n0 ..\n1 s.\n", o.str());
}
// Board state should reflect a critter taking a left turn and then hopping
// forward.
TEST(DarwinFixture, test_left_turn) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::left, 0}, {Opcode::hop, 0}});
Species s({'s', inst});
game.add_critter(0, 0, Direction::south, &s);
game.next_turn();
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 2.\n 01\n0 .s\n1 ..\n", o.str());
}
// Board state should reflect a critter taking a right turn and then hopping
// forward.
TEST(DarwinFixture, test_right_turn) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::right, 0}, {Opcode::hop, 0}});
Species s({'s', inst});
game.add_critter(0, 0, Direction::east, &s);
game.next_turn();
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 2.\n 01\n0 ..\n1 s.\n", o.str());
}
// Board state should reflect a critter infecting the critter in front of it.
TEST(DarwinFixture, test_infect_enemy_turn) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::infect, 0}});
Species sa({'a', inst});
Species sb({'b', inst});
game.add_critter(0, 0, Direction::east, &sa);
game.add_critter(1, 0, Direction::west, &sb);
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 1.\n 01\n0 aa\n1 ..\n", o.str());
}
// Board state should reflect a critter trying to infect a wall.
TEST(DarwinFixture, test_infect_wall_turn) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::infect, 0}});
Species s({'s', inst});
game.add_critter(0, 0, Direction::west, &s);
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 1.\n 01\n0 s.\n1 ..\n", o.str());
}
// Critters should get turns in the right order (row-major, starting at top
// left).
TEST(DarwinFixture, test_turn_order) {
Darwin game(2, 2);
vector<Instruction> inst({{Opcode::hop, 0}});
Species sa({'a', inst});
Species sb({'b', inst});
game.add_critter(0, 0, Direction::south, &sa);
game.add_critter(1, 1, Direction::west, &sb);
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 1.\n 01\n0 ..\n1 ab\n", o.str());
}
// Darwin should not crash or do anything weird when the board is not square.
TEST(DarwinFixture, test_rectangular_board) {
Darwin game(3, 2);
vector<Instruction> inst({{Opcode::hop, 0}, {Opcode::hop, 0}});
Species s({'s', inst});
game.add_critter(0, 0, Direction::east, &s);
game.next_turn();
game.next_turn();
ostringstream o;
o << game;
ASSERT_EQ("Turn = 2.\n 012\n0 ..s\n1 ...\n", o.str());
}