-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test.cpp
263 lines (204 loc) · 7.36 KB
/
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
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
/**
* A test program for league.
*
* @author Yoav Gross and Elad Nevi
* @since 2019-06
*/
#include <iostream>
#include<set>
#include <map>
#include "league.hpp"
#include "schedule.hpp"
#include "game.hpp"
#include "Team.hpp"
#include "badkan.hpp"
#define COMMA ,
using namespace std;
using namespace ariel;
//return true if each team not played twice or more
bool is_valid_round(std::vector<game> &round) {
//for compiling
std::set<Team> teams_in_round;
for (game _game : round){
teams_in_round.insert(_game.home.first);
teams_in_round.insert(_game.away.first);
}
return (round.size() == teams_in_round.size());
}
//return true if played exactly (n-1)*2 games in schedule
//where n is the number of teams
bool played_exact_games(schedule &sch) {
return ( (sch.all_teams.size()-1)*2 == sch.all_games.size());
}
bool is_all_played_against_all(schedule &sch) {
//map every team to number
std::map<Team, int> myMap;
int count=0;
for (Team t : sch.all_teams){
myMap.emplace(t, count);
count++;
}
int number_of_teams = sch.all_teams.size();
//matrix of home team / away team.
//played[i][j] is true if team i play against team j: i - home, j - away
bool played[number_of_teams][number_of_teams];
//intilze false
for (int i=0; i<number_of_teams; i++)
for (int j=0; j<number_of_teams; j++)
played[i][j]=false;
//set true if i played against j
for (std::vector<game> round : sch.all_games)
for (game _game : round)
played[myMap.find(_game.home.first)->second][myMap.find(_game.away.first)->second]=true;
for (int i=0; i<number_of_teams; i++)
for (int j=0; j<number_of_teams; j++)
if (i!=j && played[i][j]==false)
return false;
return true;
}
//basic test for top and bottom functions
void test_top_and_bottom(badkan::TestCase &testcase, ariel::league &test_league, int size){
testcase.setname("Test top and bottom"); //you should change the name of testcase after calling to this function
auto up_half = test_league.top(size/2);
auto down_half = test_league.bottom(size/2);
for (Team up_team : up_half)
for (Team down_team : down_half){
testcase.CHECK_EQUAL(up_team.name != down_team.name, true)
.CHECK_EQUAL(up_team < down_team, false);
}
}
int main() {
badkan::TestCase testcase;
int grade=0;
int signal = setjmp(badkan::longjmp_buffer);
if (signal == 0) {
//testing Team!
testcase.setname("Init teams");
//Create teams with different names
std::vector<Team> team_v;
for (int i=0; i<20; i++){
Team new_team = Team();
for (Team t : team_v)
testcase.CHECK_EQUAL(t.name != new_team.name, true);
team_v.push_back(new_team);
}
/*Create teams by pramameters
skills should be in [0,1)*/
testcase.CHECK_THROWS(Team(-0.01, "name1"))
.CHECK_THROWS(Team(1, "name2"))
.CHECK_OK(Team(0, "name3"))
.CHECK_OK(Team(0.99, "name4"));
//testing game!
testcase.setname("Play games");
Team a_team(0, "a");
Team b_team(0.7, "b");
std::pair<Team&, int> a{a_team, 0};
std::pair<Team&, int> b{b_team, 0};
game my_game{a, b};
testcase.CHECK_OK(my_game.play())
//home team: [55,105) + 0*10
.CHECK_EQUAL(my_game.home.second < 105, true)
.CHECK_EQUAL(my_game.home.second >= 55, true)
//away team: [50,100) + 0.7*10
.CHECK_EQUAL(my_game.away.second < 107, true)
.CHECK_EQUAL(my_game.away.second >= 57, true);
//testing schedule!
testcase.setname("Set schedule");
Team c_team(0.2, "c");
Team d_team(0.5, "d");
std::vector<Team> team_v2;
team_v2.push_back(a_team);
team_v2.push_back(b_team);
team_v2.push_back(c_team);
team_v2.push_back(d_team);
schedule test_schedule(team_v2.begin(), team_v2.end());
for (std::vector<game> round : test_schedule.all_games)
testcase.CHECK_EQUAL(is_valid_round(round), true);
testcase.CHECK_EQUAL(played_exact_games(test_schedule), true)
.CHECK_EQUAL(is_all_played_against_all(test_schedule), true);
//testing league!
testcase.setname("Set league");
const int size = 4; //league with 4 teams
league test_league(size);
test_league.create();
//test schedule as above
for (std::vector<game> round : test_league.all_games)
testcase.CHECK_EQUAL(is_valid_round(round), true);
testcase.CHECK_EQUAL(played_exact_games(test_league), true)
.CHECK_EQUAL(is_all_played_against_all(test_league), true);
//test the total number of wins and loses in the league
testcase.CHECK_OK(test_league.play());
int total_win=0, total_lose=0;
int total_games = test_league.size()*(test_league.size()-1);
for (Team team : test_league._table()){
total_win += team.win;
total_lose += team.lose;
}
testcase.CHECK_EQUAL(total_win, total_games)
.CHECK_EQUAL(total_lose, total_games);
//basic test for top and bottom functions
auto up_half = test_league.top(size/2);
auto down_half = test_league.bottom(size/2);
for (Team up_team : up_half)
for (Team down_team : down_half){
testcase.CHECK_EQUAL(up_team.name != down_team.name, true)
.CHECK_EQUAL(up_team < down_team, false);
}
testcase.setname("Test my mini league");
league test_league2(4);
test_league2.all_teams.push_back(a_team);
test_league2.all_teams.push_back(b_team);
test_league2.all_teams.push_back(c_team);
test_league2.all_teams.push_back(d_team);
//set all results
game game1_1{pair<Team&,int>(a_team,70), pair<Team&,int>(b_team,80)};
game game1_2{pair<Team&,int>(c_team,70), pair<Team&,int>(d_team,65)};
std::vector<game> round1;
round1.push_back(game1_1);
round1.push_back(game1_2);
test_league2.all_games.push_back(round1);
game game2_1{pair<Team&,int>(c_team,70), pair<Team&,int>(a_team,80)};
game game2_2{pair<Team&,int>(d_team,70), pair<Team&,int>(b_team,65)};
std::vector<game> round2;
round2.push_back(game2_1);
round2.push_back(game2_2);
test_league2.all_games.push_back(round2);
game game3_1{pair<Team&,int>(a_team,70), pair<Team&,int>(d_team,80)};
game game3_2{pair<Team&,int>(b_team,70), pair<Team&,int>(c_team,65)};
std::vector<game> round3;
round3.push_back(game3_1);
round3.push_back(game3_2);
test_league2.all_games.push_back(round3);
game game4_1{pair<Team&,int>(b_team,70), pair<Team&,int>(a_team,80)};
game game4_2{pair<Team&,int>(d_team,70), pair<Team&,int>(c_team,65)};
std::vector<game> round4;
round4.push_back(game4_1);
round4.push_back(game4_2);
test_league2.all_games.push_back(round4);
game game5_1{pair<Team&,int>(a_team,70), pair<Team&,int>(c_team,80)};
game game5_2{pair<Team&,int>(b_team,70), pair<Team&,int>(d_team,65)};
std::vector<game> round5;
round5.push_back(game5_1);
round5.push_back(game5_2);
test_league2.all_games.push_back(round5);
game game6_1{pair<Team&,int>(d_team,90), pair<Team&,int>(a_team,75)};
game game6_2{pair<Team&,int>(c_team,85), pair<Team&,int>(b_team,75)};
std::vector<game> round6;
round6.push_back(game6_1);
round6.push_back(game6_2);
test_league2.all_games.push_back(round6);
testcase.CHECK_EQUAL(test_league2.longest_winnigs(), 3) //Team d in rounds 2-4
.CHECK_EQUAL(test_league2.longest_losses(), 3) //Team c in rounds 2-4
.CHECK_EQUAL(test_league2.count_positive(), 2); //Teams c,d
test_top_and_bottom(testcase, test_league, size);
//The tests above will run correctly after you fill some functions in league and schedule files
testcase.CHECK_EQUAL((*test_league2.top(1).begin()).name, d_team.name) // Team d in the first place
.CHECK_EQUAL((*test_league2.bottom(1).begin()).name, a_team.name); // Team d in the first place
grade = testcase.grade();
} else {
testcase.print_signal(signal);
grade = 0;
}
cout << "Your grade is: " << grade << endl;
return 0;
}