root/ggpa/TestGameWorld.cpp

Revision 1, 5.2 kB (checked in by pantley2, 4 years ago)

GGPA code from the good old days of SIGART

Line 
1 /*
2  * TestGameWorld.cpp
3  * A main function to test the GameWorld class
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  * testEmptyRules
18  * No parameters or return type
19  * Tests the GameWorld with a START request with no rules.  This is to check
20  *    that the basic parsing of the other four parameters (id, role,
21  *    startTime, and playTime) are parsed correctly
22  */
23 void testEmptyRules() {
24     cout << "testEmptyRules ..." << endl;
25     // parameters to test
26     string matchID = "123";
27     string role = "player1";
28     double startClock = 30;
29     double playClock = 30;
30
31     // build the request
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  * testEmptyRulesWithWhiteSpace
51  * No parameters or return type
52  * Tests the GameWorld with a START request with no rules, and with \n and \t
53  *    between the input values.
54  */
55 void testEmptyRulesWithWhiteSpace() {
56     cout << "testEmptyRulesWithWhiteSpace ..." << endl;
57     // parameters to test
58     string matchID = "123";
59     string role = "player1";
60     double startClock = 30;
61     double playClock = 30;
62
63     // build the request
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  *TestTicTacToe
83  *Parameter: no parameter
84  *Tests tic tac toe rules
85  */
86 void testTicTacToe(){
87     cout << "Test tic tac toes rules ..." << endl;   
88    
89     //parameters to test
90     string matchID = "123";
91     string role = "xplayer";
92     double startClock = 30;
93     double playClock = 30;
94     string request;
95
96     // read in the rules
97     string ruleFile = "TicTacToe.rul";
98     // open the file
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     // read in the rules, line by line
106     string rules = "";
107     string line;
108     while (!getline(ruleStream, line).eof()) {
109         //cout << "line: " << line << endl;
110         rules = rules +" " + line;
111
112     }
113     rules = rules + " "+line;
114     // close the file
115     ruleStream.close();
116    
117     ostringstream stream1;
118
119     stream1 << "(START " << matchID << " " << role << " (" <<  rules 
120             << ") " << startClock
121             << " " << playClock << ")";
122     //cout<< stream1 << endl;
123     request = stream1.str();
124     GameWorld world(request);
125     //world.output();
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     //world.update(actions);
139     
140     //world.output();
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     // build the request
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     for (int i = 0; i < tokens.size(); i++) {
166         cout << "token " << i << " = " << tokens[i] << endl;
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  * main function
184  * No parameters
185  * Returns an int with the error code
186  * Run the tests for the GameWorld class
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 }
Note: See TracBrowser for help on using the browser.