-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestingGridWorld.cpp
129 lines (106 loc) · 3.18 KB
/
TestingGridWorld.cpp
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
/***************************************************************
* Filename: TestingGridWorld.cpp
* Purpose: Test cases for the GridWorld class using Catch2 framework
* Author: Alexis Rodriguez
***************************************************************/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "GridWorld.h"
// Test cases for the GridWorld class
TEST_CASE("GridWorld class") {
// Test case for the Constructor
SECTION("Constructor") {
GridWorld grid(3, 3);
REQUIRE(grid.num_rows() == 3);
REQUIRE(grid.num_cols() == 3);
REQUIRE(grid.population() == 0);
}
// Test case for the Birth method
SECTION("Birth") {
GridWorld grid(3, 3);
int idNum;
REQUIRE(grid.birth(0, 0, idNum) == true);
REQUIRE(grid.population() == 1);
REQUIRE(idNum == 0);
REQUIRE(grid.birth(1, 1, idNum) == true);
REQUIRE(grid.population() == 2);
REQUIRE(idNum == 1);
// Invalid birth positions
REQUIRE(grid.birth(-1, 1, idNum) == false);
REQUIRE(grid.birth(4, 4, idNum) == false);
}
// Test case for the Death method
SECTION("Death") {
GridWorld grid(3, 3);
int idNum;
grid.birth(0, 0, idNum);
grid.birth(1, 1, idNum);
grid.birth(2, 2, idNum);
REQUIRE(grid.death(0) == true);
REQUIRE(grid.population() == 2);
// Trying to kill a dead person
REQUIRE(grid.death(0) == false);
// Trying to kill a person with an invalid ID
REQUIRE(grid.death(5) == false);
}
// Test case for the Whereis method
SECTION("Whereis") {
GridWorld grid(3, 3);
int idNum;
grid.birth(1, 2, idNum);
int row, col;
REQUIRE(grid.whereis(0, row, col) == false); // Invalid ID
REQUIRE(grid.whereis(1, row, col) == true);
REQUIRE(row == 1);
REQUIRE(col == 2);
}
// Test case for the Move method
SECTION("Move") {
GridWorld grid(3, 3);
int idNum;
grid.birth(0, 0, idNum);
REQUIRE(grid.move(0, 2, 2) == true);
int row, col;
REQUIRE(grid.whereis(0, row, col) == true);
REQUIRE(row == 2);
REQUIRE(col == 2);
// Moving a dead person
REQUIRE(grid.move(1, 1, 1) == false);
// Moving to an invalid position
REQUIRE(grid.move(0, -1, 1) == false);
REQUIRE(grid.move(0, 4, 4) == false);
}
// Test case for the Members method
SECTION("Members") {
GridWorld grid(3, 3);
int idNum;
grid.birth(0, 0, idNum);
std::vector<int> *members = grid.members(0, 0);
REQUIRE(members->size() == 1);
REQUIRE((*members)[0] == 0);
delete members;
members = grid.members(1, 1);
REQUIRE(members->empty());
delete members;
members = grid.members(2, 2);
REQUIRE(members->empty());
delete members;
members = grid.members(-1, 0);
REQUIRE(members->empty());
delete members;
}
// Test case for the Population method
SECTION("Population") {
GridWorld grid(3, 3);
int idNum;
grid.birth(0, 0, idNum);
grid.birth(1, 1, idNum);
grid.birth(2, 2, idNum);
REQUIRE(grid.population() == 3);
REQUIRE(grid.population(0, 0) == 1);
REQUIRE(grid.population(1, 1) == 1);
REQUIRE(grid.population(2, 2) == 1);
REQUIRE(grid.population(-1, 0) == 0);
REQUIRE(grid.population(3, 3) == 0);
}
}