| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
#include "GameWorld.h" |
|---|
| 7 |
|
|---|
| 8 |
#include <string> |
|---|
| 9 |
#include <sstream> |
|---|
| 10 |
#include <fstream> |
|---|
| 11 |
using namespace std; |
|---|
| 12 |
|
|---|
| 13 |
#include "TestTools.h" |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
void testEmptyRules() { |
|---|
| 24 |
cout << "testEmptyRules ..." << endl; |
|---|
| 25 |
|
|---|
| 26 |
string matchID = "123"; |
|---|
| 27 |
string role = "player1"; |
|---|
| 28 |
double startClock = 30; |
|---|
| 29 |
double playClock = 30; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
string request; |
|---|
| 33 |
ostringstream stream1; |
|---|
| 34 |
stream1 << "(START " << matchID << " " << role << " () " << startClock |
|---|
| 35 |
<< " " << playClock << ")"; |
|---|
| 36 |
request = stream1.str(); |
|---|
| 37 |
|
|---|
| 38 |
GameWorld world(request); |
|---|
| 39 |
|
|---|
| 40 |
assertEquals(world.getMatchID(), matchID, "matchIDs do not match!"); |
|---|
| 41 |
assertEquals(world.getRole(), role, "roles do not match!"); |
|---|
| 42 |
assertEquals(world.getStartClock(), startClock, "startClocks do not match!"); |
|---|
| 43 |
assertEquals(world.getPlayClock(), playClock, "playClocks do not match!"); |
|---|
| 44 |
|
|---|
| 45 |
cout << "Passed!" << endl; |
|---|
| 46 |
return; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
void testEmptyRulesWithWhiteSpace() { |
|---|
| 56 |
cout << "testEmptyRulesWithWhiteSpace ..." << endl; |
|---|
| 57 |
|
|---|
| 58 |
string matchID = "123"; |
|---|
| 59 |
string role = "player1"; |
|---|
| 60 |
double startClock = 30; |
|---|
| 61 |
double playClock = 30; |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
string request; |
|---|
| 65 |
ostringstream stream1; |
|---|
| 66 |
stream1 << "(START " << matchID << " " << role << " () " << startClock |
|---|
| 67 |
<< " " << playClock << ")"; |
|---|
| 68 |
request = stream1.str(); |
|---|
| 69 |
|
|---|
| 70 |
GameWorld world(request); |
|---|
| 71 |
|
|---|
| 72 |
assertEquals(world.getMatchID(), matchID, "matchIDs do not match!"); |
|---|
| 73 |
assertEquals(world.getRole(), role, "roles do not match!"); |
|---|
| 74 |
assertEquals(world.getStartClock(), startClock, "startClocks do not match!"); |
|---|
| 75 |
assertEquals(world.getPlayClock(), playClock, "playClocks do not match!"); |
|---|
| 76 |
|
|---|
| 77 |
cout << "Passed!" << endl; |
|---|
| 78 |
return; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
void testTicTacToe(){ |
|---|
| 87 |
cout << "Test tic tac toes rules ..." << endl; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
string matchID = "123"; |
|---|
| 91 |
string role = "xplayer"; |
|---|
| 92 |
double startClock = 30; |
|---|
| 93 |
double playClock = 30; |
|---|
| 94 |
string request; |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
string ruleFile = "TicTacToe.rul"; |
|---|
| 98 |
|
|---|
| 99 |
ifstream ruleStream(ruleFile.c_str()); |
|---|
| 100 |
if (!ruleStream.is_open()) { |
|---|
| 101 |
cerr << "Could not open file " << ruleFile << endl; |
|---|
| 102 |
exit(1); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
string rules = ""; |
|---|
| 107 |
string line; |
|---|
| 108 |
while (!getline(ruleStream, line).eof()) { |
|---|
| 109 |
|
|---|
| 110 |
rules = rules +" " + line; |
|---|
| 111 |
|
|---|
| 112 |
} |
|---|
| 113 |
rules = rules + " "+line; |
|---|
| 114 |
|
|---|
| 115 |
ruleStream.close(); |
|---|
| 116 |
|
|---|
| 117 |
ostringstream stream1; |
|---|
| 118 |
|
|---|
| 119 |
stream1 << "(START " << matchID << " " << role << " (" << rules |
|---|
| 120 |
<< ") " << startClock |
|---|
| 121 |
<< " " << playClock << ")"; |
|---|
| 122 |
|
|---|
| 123 |
request = stream1.str(); |
|---|
| 124 |
GameWorld world(request); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
cout << endl << "Updating" << endl; |
|---|
| 129 |
vector<string> roles = world.getRoles(); |
|---|
| 130 |
vector<Action*> actions(roles.size()); |
|---|
| 131 |
for (unsigned int roleIndex = 0; roleIndex < roles.size(); roleIndex++) { |
|---|
| 132 |
vector<Action*> legalMoves = world.getLegalMoves(roles[roleIndex]); |
|---|
| 133 |
cout << roles[roleIndex] << endl; |
|---|
| 134 |
assertTrue(legalMoves.size() != 0, "No legal moves!"); |
|---|
| 135 |
actions[roleIndex] = legalMoves[0]; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
cout << "done?" << endl; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
void testTokenizer() { |
|---|
| 146 |
cout << "Running tokenizer tests ... "; |
|---|
| 147 |
|
|---|
| 148 |
string line = " (cell (a (b x)) c def \n\t g"; |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
string matchID = "123"; |
|---|
| 152 |
string role = "player1"; |
|---|
| 153 |
double startClock = 30; |
|---|
| 154 |
double playClock = 30; |
|---|
| 155 |
string request; |
|---|
| 156 |
ostringstream stream1; |
|---|
| 157 |
stream1 << "(START " << matchID << " " << role << " () " << startClock |
|---|
| 158 |
<< " " << playClock << ")"; |
|---|
| 159 |
request = stream1.str(); |
|---|
| 160 |
|
|---|
| 161 |
GameWorld world(request); |
|---|
| 162 |
|
|---|
| 163 |
vector<string> tokens = world.tokenize(line); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
assertEquals(tokens.size(), 5, "Wrong number of tokens"); |
|---|
| 171 |
assertEquals(tokens[0], "cell", "First token is incorrect"); |
|---|
| 172 |
assertEquals(tokens[1], "(a (b x))", "First token is incorrect"); |
|---|
| 173 |
assertEquals(tokens[2], "c", "First token is incorrect"); |
|---|
| 174 |
assertEquals(tokens[3], "def", "First token is incorrect"); |
|---|
| 175 |
assertEquals(tokens[4], "g", "Fifth token is incorrect"); |
|---|
| 176 |
|
|---|
| 177 |
cout << "finished" << endl; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
int main() { |
|---|
| 189 |
cout << "***** Running Tests *****" << endl; |
|---|
| 190 |
|
|---|
| 191 |
testEmptyRules(); |
|---|
| 192 |
testEmptyRulesWithWhiteSpace(); |
|---|
| 193 |
testTokenizer(); |
|---|
| 194 |
testTicTacToe(); |
|---|
| 195 |
|
|---|
| 196 |
cout << "***** Tests Passed! *****" << endl; |
|---|
| 197 |
return 0; |
|---|
| 198 |
} |
|---|