-
Notifications
You must be signed in to change notification settings - Fork 0
/
oasis_test.cpp
173 lines (135 loc) · 3.21 KB
/
oasis_test.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
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include "oasis.h"
#define RUN_TEST(b) if( (b)() ) std::cout << #b << ": [PASS]" << std::endl; \
else std::cout << #b << ": [FAIL]" << std::endl;
using hw1::Oasis;
bool addPlayerTest() {
Oasis oasis;
int num_players = 100;
int max_coins = 30;
for( int i=1; i < num_players; i++) {
try {
oasis.addPlayer(i, i%max_coins);
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::playerExist& exc) {
return false;
} catch ( ... ){
return false;
}
}
for( int i=1; i < num_players; i++) {
try {
oasis.addPlayer(i, i%max_coins);
return false;
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::playerExist& exc) {
} catch ( ... ){
return false;
}
}
return true;
}
bool addClanTest() {
Oasis oasis;
int num_clans = 100;
for( int i=1; i < num_clans; i++) {
try {
oasis.addClan(i);
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanExists & exc) {
return false;
} catch ( ... ){
return false;
}
}
for( int i=1; i < num_clans; i++) {
try {
oasis.addClan(i);
return false;
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanExists & exc) {
} catch ( ... ){
return false;
}
}
return true;
}
bool joinClanTest() {
Oasis oasis;
int num_players = 1200;
int num_clans = 30;
int max_coins = 30;
for( int i=1; i < num_players; i++) {
try {
oasis.addPlayer(i, i%max_coins);
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::playerExist& exc) {
return false;
} catch ( ... ){
return false;
}
}
for( int i=1; i <= num_clans; i++) {
try {
oasis.addClan(i);
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanExists & exc) {
return false;
} catch ( ... ){
return false;
}
}
for ( int i = 1; i< 2*num_players; i++ ) {
try {
oasis.joinClan(i, i%(num_clans + 1) +1);
if( i >= num_players ||
( (i % (num_clans +1)) +1) == (num_clans +1))
return false;
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanOrPlayerDoesntExist& exc) {
if( i < num_players &&
((i % (num_clans+1)) +1) != (num_clans +1))
return false;
}
}
// we add players to the same clan. It shouldn't fail!
for ( int i = 1; i< num_players; i++ ) {
try {
oasis.joinClan(i, i%(num_clans + 1) +1);
if( ((i % (num_clans+1)) +1) == (num_clans +1))
return false;
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanOrPlayerDoesntExist& exc) {
if( ((i % (num_clans+1)) +1) != (num_clans +1))
return false;
}
}
for ( int i = 1; i< num_clans; i++ ) {
try {
oasis.joinClan(i, i);
return false;
} catch (Oasis::memoryAllocFailure& exc) {
return false;
} catch (Oasis::clanOrPlayerDoesntExist& exc) {
return false;
} catch ( Oasis::playerAlreadyHasClan& exc) {
}
}
return true;
}
int main () {
RUN_TEST(addPlayerTest);
RUN_TEST(addClanTest);
RUN_TEST(joinClanTest);
return 0;
}