forked from ostech1/ex4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
356 lines (318 loc) · 10.7 KB
/
Copy pathtest.cpp
File metadata and controls
356 lines (318 loc) · 10.7 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <functional>
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include "Players/Player.h"
#include "Cards/Card.h"
#include "Mtmchkin.h"
#include "Cards/Witch.h"
#include "Cards/Barfight.h"
#include "Cards/Dragon.h"
#include "Cards/Mana.h"
#include "Cards/Gremlin.h"
#include "Cards/Merchant.h"
#include "Cards/Well.h"
#include "Cards/Treasure.h"
#include "Players/Ninja.h"
#include "Players/Healer.h"
#include "Players/Warrior.h"
#include "Exception.h"
#include <algorithm>
#include <sstream>
#include <random>
#include <cstdio>
#include <fstream>
#include <cstring>
using std::unique_ptr;
using std::cout;
using std::cerr;
using std::string;
using std::fstream;
using std::istringstream;
using std::vector;
/* ---------------------------------------------------------------------------------------------- */
// -------------------------------- General Helper Functions ------------------------------
void createTextFile(const string &filename, const string &input)
{
std::ofstream file(filename);
if(file){
file << input;
}
}
void deleteTextFile(const string &filename)
{
std::remove(filename.c_str());
}
bool compareFiles(const string &filename1, const string &filename2)
{
string line1,line2;
fstream file1(filename1),file2(filename2);
if( !file2){
cerr<<"Error opening file 2"<<std::endl;
return false;
}
if(!file1 ){
cerr<<"Error opening file 1"<<std::endl;
return false;
}
while(!file1.eof()){ //read file until you reach the end
getline(file1,line1);
getline(file2,line2);
if(!(line1==line2))
{
return false;
}
}
if(!file2.eof()){
return false;
}
return true;
}
bool GeneralGameSimulationTest(const string &tempDeckFilename, string input, string deck, string expectedOutputFileName)
{
// init cin from file
createTextFile(tempDeckFilename+".txt",deck);
istringstream in(input);
std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
std::cin.rdbuf(in.rdbuf());
std::ofstream outfile(tempDeckFilename+"out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(outfile.rdbuf());
Mtmchkin game(tempDeckFilename+".txt");
while(!game.isGameOver() && game.getNumberOfRounds() < 100){
game.playRound();
game.printLeaderBoard();
}
bool res = compareFiles(tempDeckFilename+"out.txt", expectedOutputFileName);
outfile.close();
std::cin.rdbuf(cinbuf);
std::cout.rdbuf(coutbuf);
deleteTextFile(tempDeckFilename+".txt");
return res;
}
void run_test(std::function<bool()> test, std::string test_name)
{
if(!test()){
std::cout<< test_name <<" FAILED."<<std::endl;
std::cout << std::endl;
return;
}
std::cout<<test_name<<" SUCCEEDED."<<std::endl;
std::cout << std::endl;
}
/* ---------------------------------------------------------------------------------------------- */
// -------------------------------- Tests for Card class ------------------------------
bool cardsPrintsTest()
{
Barfight junta;
Dragon mushu;
Mana mana;
Gremlin gremlin;
Merchant pizzaHut;
Well well;
Treasure factor;
Witch witch;
cout << junta << std::endl << mushu << std::endl << mana
<< std::endl << gremlin << std::endl << pizzaHut
<< std::endl << well << std::endl << factor
<< std::endl << witch;
return true;
}
bool playersPrintsTest()
{
Ninja player1("Itay");
Warrior player2("Efrat");
Healer player3("Jimmy");
cout << player1 << std::endl << player2 << std::endl << player3
<< std::endl;
return true;
}
bool testCard()
{
vector<unique_ptr<Card>> cards;
cards.push_back(unique_ptr<Card>(new Gremlin()));
cards.push_back(unique_ptr<Card>(new Witch()));
cards.push_back(unique_ptr<Card>(new Dragon()));
cards.push_back(unique_ptr<Card>(new Treasure()));
cards.push_back(unique_ptr<Card>(new Merchant()));
cards.push_back(unique_ptr<Card>(new Mana()));
cards.push_back(unique_ptr<Card>(new Barfight()));
cards.push_back(unique_ptr<Card>(new Well()));
for(unique_ptr<Card>& card : cards){
cout << *card;
}
cards.erase(cards.begin(),cards.end());
return true;
}
/* ---------------------------------------------------------------------------------------------- */
// -------------------------------- Tests for Mtmchkin class ------------------------------
bool gameRunTest()
{
// init cin from file
std::ifstream in("in.txt");
if(!in.is_open()){
throw std::exception();
}
std::cin.rdbuf(in.rdbuf());
Mtmchkin game("gametest.txt");
while(!game.isGameOver()){
game.playRound();
}
return true;
}
bool dragonDenTest()
{
const string tmp_file("dragonDen_test");
string input("2\nJimmy Healer\nPikachu Warrior");
string deck("Dragon\nDragon\nDragon\nDragon\nDragon");
string expectedOutputFilename("tests/dragonDen_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool witchLairTest()
{
const string tmp_file("witchLair_test");
string input("2\nItay Warrior\nPikachu Ninja");
string deck("Witch\nWitch\nWitch\nWitch\nWitch");
string expectedOutputFilename("tests/witchLair_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool gremlinCaveTest()
{
const string tmp_file("gremlinCave_test");
string input("2\nItay Healer\nPikachu Ninja");
string deck("Gremlin\nGremlin\nGremlin\nGremlin\nGremlin");
string expectedOutputFilename("tests/gremlinCave_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool nonMostersTest()
{
const string tmp_file("noMonster_test");
string input("2\nItay Healer\nPikachu Ninja");
string deck("Mana\nBarfight\nWell\nTreasure\nMana");
string expectedOutputFilename("tests/noMonster_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool roundLimitTest()
{
const string tmp_file("roundLimit_test");
string input("2\nmatamDalf Healer\nrocky Warrior");
string deck("Mana\nMana\nMana\nMana\nMana");
string expectedOutputFilename("tests/roundLimit_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool allTenTest()
{
const string tmp_file("allTen_test");
string input("2\nmatamDalf Healer\nrocky Warrior");
string deck("Gremlin\nGremlin\nGremlin\nGremlin\nGremlin");
string expectedOutputFilename("tests/allTen_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool badPlayerInputTest()
{
const string tmp_file("badPlayerInput_test");
string input("2\nmatamDalf Healerd\nmatamDalf ninha\nmatamDalf Healer\nrocky Warrior");
string deck("Gremlin\nWitch\nGremlin\nGremlin\nDragon");
string expectedOutputFilename("tests/badPlayerInput_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
bool merchantInputTest()
{
const string tmp_file("merchantInput_test");
string input("2\nmatamDalf Healerd\nmatamDalf rogoe\nmatamDalf Healer\nrocky Warrior\n"
"1\n"
"1\n"
"0\n"
"2\n"
"0\n");
string deck("Gremlin\nGremlin\nMerchant\nMerchant\nDragon");
string expectedOutputFilename("tests/merchantInput_test_expected.txt");
return GeneralGameSimulationTest(tmp_file, input, deck, expectedOutputFilename);
}
/* ---------------------------------------------------------------------------------------------- */
// -------------------------------- Tests for Exceptions ------------------------------
bool badSizeTest()
{
const string tmp_file("badSize_test");
string input("4\nBarbieGirl Healer\nInABarbieWorld Ninja\nMadeOfPlastic Ninja\nITSFANTASTIC Healer");
string deck("Mana");
string expectedOutputFilename("notneeded.txt");
bool flag= false;
try{
Mtmchkin("inputs/empty.txt");
}
catch(const DeckFileInvalidSize& e){
flag = true;
}
return flag;
}
bool noFileTest()
{
const string tmp_file("noFile_test");
string input("2\nItay Healer\nPikachu Ninja");
string expectedOutputFilename("notneeded.txt");
string deck("This_is_not_the_file_your_looking_for");
bool flag = false;
try{
Mtmchkin("inputs/noFile.txt");
}
catch(const DeckFileNotFound& e){
flag=true;
}
return flag;
}
bool badFormatTest()
{
const string tmp_file("badFormat_test");
string input("2\nItay Healer\nPikachu Ninja");
string deck("SomeBody Once told me the world is gonna roll me\nWitch\n");
string expectedOutputFilename("notneeded.txt");
bool flag = false;
try {
Mtmchkin("inputs/badFormat_test.txt");
}
catch(const DeckFileFormatError& e){
if(strcmp(e.what(),"Deck File Error: File format error in line 2")==0) {
flag=true;
}
}
return flag;
}
bool badFormatStartTest()
{
const string tmp_file("badFormat_test");
string input("2\nItay Healer\nPikachu Ninja");
string deck("I aint the sharpest tool in the shed\nWitch");
string expectedOutputFilename("notneeded.txt");
bool flag = false;
try {
Mtmchkin("inputs/badFormat_test_start_of_file.txt");
}
catch(const DeckFileFormatError& e){
if(strcmp(e.what(),"Deck File Error: File format error in line 1")==0)
{
flag = true;
}
}
return flag;
}
/* ---------------------------------------------------------------------------------------------- */
// -------------------------------- Main function ------------------------------
int main(){
run_test(cardsPrintsTest,"cardsPrintsTest");
run_test(playersPrintsTest,"playersPrintsTest");
run_test(testCard,"Deck creation test");
run_test(dragonDenTest,"Dragon Den simulation test");
run_test(gremlinCaveTest,"Gremlin Cave simulation test");
run_test(witchLairTest,"Witch Lair simulation test");
run_test(nonMostersTest,"Non monsters cards simulation test");
run_test(badFormatStartTest,"Bad format at start of file exception test");
run_test(badFormatTest,"Bad format exception test");
run_test(noFileTest,"File Doesnt exist exception test");
run_test(badSizeTest,"Bad size exception test");
run_test(roundLimitTest,"Round upper limit test");
run_test(allTenTest,"All reach lvl 10 test");
run_test(badPlayerInputTest,"Bad player input test");
return 0;
}