/
jack-TestLife.c++
275 lines (215 loc) · 5.77 KB
/
jack-TestLife.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
/*
To test the program:
% ls /usr/include/cppunit/
...
TestFixture.h
...
% locate libcppunit.a
/usr/lib/libcppunit.a
% g++ -lcppunit -ldl -pedantic -std=c++0x -Wall Collatz.c++ TestCollatz.c++ -o TestCollatz.c++.app
% valgrind TestCollatz.c++.app >& TestCollatz.c++.out
*/
// --------
// includes
// --------
#include <iostream> // cout, endl
#include <sstream> // istringtstream, ostringstream
#include <string> // ==
#include <vector>
#include "cppunit/extensions/HelperMacros.h" // CPPUNIT_TEST, CPPUNIT_TEST_SUITE, CPPUNIT_TEST_SUITE_END
#include "cppunit/TestFixture.h" // TestFixture
#include "cppunit/TextTestRunner.h" // TextTestRunner
#include "Life.h"
struct TestLife : CppUnit::TestFixture {
void test_constructor_1() {
Life<ConwayCell> game(5, 5);
CPPUNIT_ASSERT(true);
}
void test_constructor_2() {
Life<FredkinCell> game(5, 5);
CPPUNIT_ASSERT(true);
}
void test_constructor_3() {
Life<Cell> game(5, 5);
CPPUNIT_ASSERT(true);
}
void test_add_1() {
Life<ConwayCell> game(5, 5);
game.add(1,1, ConwayCell(true));
CPPUNIT_ASSERT(game.population() == 1);
CPPUNIT_ASSERT(true);
}
void test_add_2() {
Life<FredkinCell> game(5, 5);
game.add(1,1, FredkinCell(true));
CPPUNIT_ASSERT(game.population() == 1);
CPPUNIT_ASSERT(true);
}
void test_add_3() {
Life<Cell> game(5, 5);
game.add(1,1, ConwayCell(true).clone());
game.add(1,2, FredkinCell(true).clone());
CPPUNIT_ASSERT(game.population() == 2);
CPPUNIT_ASSERT(true);
}
void test_population_1() {
Life<ConwayCell> game(5, 5);
game.add(1,1, ConwayCell(true));
game.run(); // should kill the 1 conwaycell
CPPUNIT_ASSERT(game.population() == 0);
CPPUNIT_ASSERT(true);
}
void test_population_2() {
Life<FredkinCell> game(5, 5);
game.add(1,1, FredkinCell(true));
game.add(1,1, FredkinCell(true)); // shouldn't add to population
CPPUNIT_ASSERT(game.population() == 1);
CPPUNIT_ASSERT(true);
}
void test_population_3() {
Life<Cell> game(5, 5);
game.add(1,1, ConwayCell(true).clone());
game.add(1,2, FredkinCell(true).clone());
CPPUNIT_ASSERT(game.population() == 2);
CPPUNIT_ASSERT(true);
}
void test_run_1() {
Life<ConwayCell> game(5, 5);
game.add(1,1, ConwayCell(true));
game.add(1,2, ConwayCell(true));
game.add(1,3, ConwayCell(true));
for (int i = 0; i < 40; i++) {
CPPUNIT_ASSERT(game.population() == 3);
game.run();
}
CPPUNIT_ASSERT(game.population() == 3);
CPPUNIT_ASSERT(true);
}
void test_run_2() {
Life<FredkinCell> game(5, 5);
game.add(1,1, FredkinCell(true));
game.add(1,2, FredkinCell(true));
game.add(1,3, FredkinCell(true));
Life<FredkinCell> game2(5, 5);
game2.add(1,1, FredkinCell(true));
game2.add(1,2, FredkinCell(true));
game2.add(1,3, FredkinCell(true));
for (int i = 0; i < 40; i++) {
CPPUNIT_ASSERT(game.population() == game2.population());
game.run(i);
for(int j = 0; j < i; j++) game2.run();
}
CPPUNIT_ASSERT(game.population() == game2.population());
CPPUNIT_ASSERT(true);
}
void test_run_3() {
Life<Cell> game(5, 5);
game.add(1,1, ConwayCell(true).clone());
game.add(1,2, FredkinCell(true).clone());
game.add(1,3, ConwayCell(true).clone());
game.run(10000); // test lots of runs
CPPUNIT_ASSERT(game.population() <= 25);
CPPUNIT_ASSERT(true);
}
void test_print_1() {
Life<ConwayCell> game(5, 5);
game.add(1,1, ConwayCell(true));
game.add(1,2, ConwayCell(true));
game.add(2,1, ConwayCell(true));
game.add(2,2, ConwayCell(true));
std::ostringstream w;
for(int i = 0; i < 10; i++){
game.run(); // test lots of runs
w.str(""); w.clear();
game.print(w);
std::stringstream s;
s << "Generation = ";
s << (i+1);
s << ", Population = ";
s << game.population();
s << ".\n";
s << ".....\n";
s << ".**..\n";
s << ".**..\n";
s << ".....\n";
s << ".....\n\n";
CPPUNIT_ASSERT(w.str() == s.str());
}
CPPUNIT_ASSERT(game.population() == 4);
CPPUNIT_ASSERT(true);
}
void test_print_2() {
Life<FredkinCell> game(5, 5);
game.add(1,1, FredkinCell(true));
game.add(1,2, FredkinCell(true));
game.add(2,1, FredkinCell(true));
game.add(2,2, FredkinCell(true));
std::ostringstream w;
game.run(); // test lots of runs
game.print(w);
std::stringstream s;
s << "Generation = ";
s << 1;
s << ", Population = ";
s << 8;
s << ".\n";
s << "-00--\n";
s << "0--0-\n";
s << "0--0-\n";
s << "-00--\n";
s << "-----\n\n";
CPPUNIT_ASSERT(w.str() == s.str());
CPPUNIT_ASSERT(game.population() == 8);
CPPUNIT_ASSERT(true);
}
void test_print_3() {
Life<Cell> game(5, 5);
game.add(2,2, FredkinCell(true).clone());
std::ostringstream w;
game.run(); // test lots of runs
game.print(w);
std::stringstream s;
s << "Generation = ";
s << 1;
s << ", Population = ";
s << 4;
s << ".\n";
s << "-----\n";
s << "--0--\n";
s << "-0-0-\n";
s << "--0--\n";
s << "-----\n\n";
CPPUNIT_ASSERT(w.str() == s.str());
CPPUNIT_ASSERT(game.population() == 4);
CPPUNIT_ASSERT(true);
}
CPPUNIT_TEST_SUITE(TestLife);
CPPUNIT_TEST(test_constructor_1);
CPPUNIT_TEST(test_constructor_2);
CPPUNIT_TEST(test_constructor_3);
CPPUNIT_TEST(test_add_1);
CPPUNIT_TEST(test_add_2);
CPPUNIT_TEST(test_add_3);
CPPUNIT_TEST(test_population_1);
CPPUNIT_TEST(test_population_2);
CPPUNIT_TEST(test_population_3);
CPPUNIT_TEST(test_run_1);
CPPUNIT_TEST(test_run_2);
CPPUNIT_TEST(test_run_3);
CPPUNIT_TEST(test_print_1);
CPPUNIT_TEST(test_print_2);
CPPUNIT_TEST(test_print_3);
CPPUNIT_TEST_SUITE_END();};
// ----
// main
// ----
int main () {
using namespace std;
ios_base::sync_with_stdio(false); // turn off synchronization with C I/O
cout << "TestLife.c++" << endl;
CppUnit::TextTestRunner tr;
tr.addTest(TestLife::suite());
tr.run();
cout << "Done." << endl;
return 0;
}