-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
218 lines (196 loc) · 6.26 KB
/
main.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
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
#include <set>
#include <unordered_set>
#include <utility>
#include <iostream>
#include <cassert>
#include <fstream>
#include <string>
#include <cstdlib>
#include "game.h"
#include "gameBoard.h"
#include "utils.h"
using namespace std;
void testBoundingBox() {
BoundingBox box(10, 20, 20, 20);
cout << "Testing bounding box Contains..." << endl;
// Top left
assert(!box.Contains(10, 20));
// Bottom right
assert(!box.Contains(10, 40));
// Bottom right
assert(!box.Contains(30, 40));
// Top right
assert(box.Contains(30, 20));
// Top border
assert(box.Contains(13, 20));
// Right border
assert(box.Contains(30, 30));
// Negative
assert(!box.Contains(0,0));
// Positive
assert(box.Contains(20, 30));
cout << "Contains tests passed..." << endl;
cout << "Testing bounding box Intersects..." << endl;
// Completely overlap, smaller
assert(box.Intersects(BoundingBox(15, 25, 10, 10)));
// Completely overlap, bigger
assert(box.Intersects(BoundingBox(5, 10, 100, 100)));
// Right side X
assert(box.Intersects(BoundingBox(5, 20, 20, 20)));
// Right side top
assert(box.Intersects(BoundingBox(5, 15, 20, 20)));
// Right side bottom
assert(box.Intersects(BoundingBox(5, 35, 20, 20)));
// Left side X
assert(box.Intersects(BoundingBox(25, 20, 20, 20)));
// Left side top
assert(box.Intersects(BoundingBox(25, 15, 20, 20)));
// Left side bottom
assert(box.Intersects(BoundingBox(25, 35, 20, 20)));
// Top
assert(box.Intersects(BoundingBox(10, 5, 20, 20)));
// Bottom
assert(box.Intersects(BoundingBox(10, 35, 20, 20)));
// Negative
assert(!box.Intersects(BoundingBox(9, 11, 2, 2)));
// Adjacent - all should succeed
assert(box.Intersects(BoundingBox(0, 20, 10, 10)));
assert(box.Intersects(BoundingBox(10, 0, 20, 20)));
assert(box.Intersects(BoundingBox(20, 40, 20, 20)));
assert(box.Intersects(BoundingBox(30, 20, 20, 20)));
// Random tests
BoundingBox box1(0, 1, 1, 2);
assert(box1.Intersects(BoundingBox(0, 0, 2, 2)));
cout << "Intersects tests passed" << endl;
}
void testQuadTree() {
QuadTree tree(BoundingBox(0, 0, ULONG_MAX, ULONG_MAX));
cout << "Testing quad tree..." << endl;
tree.Insert(Cell(1,1));
tree.Insert(Cell(2,2));
tree.Insert(Cell(1,0));
tree.Insert(Cell(1,2));
CellSet results;
tree.FindPoints(BoundingBox(0, 0, 10, 10), results);
assert(results.size() == 4);
results.clear();
tree.FindPoints(BoundingBox(0, 0, 1, 1), results);
assert(results.size() == 2);
results.clear();
tree.FindPoints(BoundingBox(0, 0, 2, 2), results);
assert(results.size() == 4);
results.clear();
tree.FindPoints(BoundingBox(0, 0, 2, 2), results);
assert(results.size() == 4);
results.clear();
tree.FindPoints(BoundingBox(1, 1, 2, 2), results);
assert(results.size() == 3);
results.clear();
tree.FindPoints(BoundingBox(0, 0, 2, 1), results);
assert(results.size() == 2);
results.clear();
cout << "Quad tree tests passed" << endl;
}
void testCellComps() {
cout << "Cell comparison tests..." << endl;
CellSet starterSet;
assert(starterSet.insert(Cell(1,1)).second);
assert(starterSet.insert(Cell(1,2)).second);
assert(!starterSet.insert(Cell(1,1)).second);
assert(starterSet.insert(Cell(2,1)).second);
assert(!starterSet.insert(Cell(2,1)).second);
assert(!starterSet.insert(Cell(2,1, false)).second);
assert(starterSet.insert(Cell(1,0)).second);
assert(!starterSet.insert(Cell(1,0)).second);
assert(starterSet.insert(Cell(0,0)).second);
assert(!starterSet.insert(Cell(1,2)).second);
starterSet.clear();
starterSet.insert(Cell(2,3));
starterSet.insert(Cell(1,3));
starterSet.insert(Cell(0,3));
starterSet.insert(Cell(3,2));
starterSet.insert(Cell(2,2));
starterSet.insert(Cell(1,2));
starterSet.insert(Cell(0,2));
starterSet.insert(Cell(3,1));
starterSet.insert(Cell(3,0));
starterSet.insert(Cell(2,1));
starterSet.insert(Cell(2,0));
starterSet.insert(Cell(1,1));
starterSet.insert(Cell(0,1));
starterSet.insert(Cell(0,0));
starterSet.insert(Cell(1,0));
assert(!starterSet.insert(Cell(1,2, false)).second);
assert(!starterSet.insert(Cell(1,0, false)).second);
cout << "Cell comparisons passed" << endl;
}
void testCellQueue() {
cout << "Cell queue tests.." <<endl;
CellQueue cQueue;
assert(cQueue.Push(Cell(1,2)));
assert(cQueue.Front() == Cell(1,2));
assert(!cQueue.Push(Cell(1,2)));
assert(cQueue.Push(Cell(2, 1)));
cQueue.Pop();
// Can't put a cell that we already had in again
assert(!cQueue.Push(Cell(1,2)));
assert(!cQueue.Push(Cell(1,2,false)));
assert(cQueue.Front() == Cell(2, 1));
CellSet startSet;
startSet.insert(Cell(1,2));
startSet.insert(Cell(2,1));
CellQueue cQueue2(startSet);
cQueue2.Pop();
cQueue2.Pop();
assert(!cQueue2.Push(Cell(1,2)));
assert(!cQueue2.Push(Cell(2,1)));
cout << "Cell queue passed" << endl;
}
int main(int argc, char ** argv) {
testBoundingBox();
testQuadTree();
testCellComps();
CellSet starterSet;
// Read input from file, or from default
const char * fileName = "config.cfg";
if (argc == 2) {
fileName = argv[1];
} else if (argc > 2) {
cerr << "Usage: game-of-life [config file]" << endl;
return 1;
}
ifstream configFile(fileName);
string line;
if (configFile.is_open()) {
while (getline(configFile, line)) {
size_t pos = line.find(" ");
if (pos == string::npos) {
cerr << "Invalid config line. Expecting space-separated longs" << endl;
return 1;
} else {
string xStr = line.substr(0, pos);
string yStr = line.substr(pos+1);
long x = atol(xStr.c_str());
// TODO: (not important) what if string is 0000
if (x == 0 && xStr != "0") {
cerr << "Could not convert " << xStr << " to long" << endl;
return 1;
}
long y = atol(yStr.c_str());
if (y == 0 && yStr != "0") {
cerr << "Could not convert " << yStr << " to long" << endl;
return 1;
}
// Input format in signed long, but want to deal with unsigned
// long internally so convert here.
unsigned long adjustedX = x + LONG_MAX + 1;
unsigned long adjustedY = y + LONG_MAX + 1;
starterSet.insert(Cell(adjustedX, adjustedY));
}
}
}
Game game(starterSet, "patterns.cfg");
game.Start();
cout << "Exiting..." << endl;
return 0;
}