-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTests.cpp
More file actions
323 lines (261 loc) · 9.02 KB
/
UnitTests.cpp
File metadata and controls
323 lines (261 loc) · 9.02 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
/*
* Fichier contenant les tests unitaires sur le mod�le
* \file UnitTests.cpp
* \author Erreur-404 et Mo-LK
* \date 25 avril 2022
* Cr�� le 25 avril 2022
*/
#include "Tester.hpp"
#include "King.hpp"
#include "Bishop.hpp"
#include "Rook.hpp"
#include "StartupPositions1.hpp"
#include "StartupPositions2.hpp"
#include "StartupPositions3.hpp"
#include "StartupPositions4.hpp"
#if __has_include("gtest/gtest.h")
#include "gtest/gtest.h"
#endif
#ifdef TEST
namespace model
{
TEST(Chessboard, King) {
Chessboard board;
King* king = new King(5, 5, Color::black);
board.addPiece(king);
bool isValid = board.isValidMove(king, 6, 6);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(king, 5, 6);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(king, 0, 0);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(king, 5, 5);
EXPECT_FALSE(isValid);
King* enemyKing = new King(6, 6, Color::white);
board.addPiece(enemyKing);
isValid = board.isValidMove(king, 6, 6);
EXPECT_TRUE(isValid);
}
TEST(Chessboard, Bishop) {
Chessboard board;
Bishop* bishop = new Bishop(4, 4, Color::white);
board.addPiece(bishop);
bool isValid = board.isValidMove(bishop, 7, 1);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(bishop, 0, 0);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(bishop, 0, 4);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(bishop, 4, 0);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(bishop, 4, 4);
EXPECT_FALSE(isValid);
Bishop* enemyBishop = new Bishop(1, 1, Color::black);
board.addPiece(enemyBishop);
isValid = board.isValidMove(bishop, 0, 0);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(bishop, 1, 1);
EXPECT_TRUE(isValid);
}
TEST(Chessboard, Rook) {
Chessboard board;
Rook* rook = new Rook(4, 4, Color::black);
board.addPiece(rook);
bool isValid = board.isValidMove(rook, 0, 4);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(rook, 4, 0);
EXPECT_TRUE(isValid);
isValid = board.isValidMove(rook, 0, 0);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(rook, 7, 1);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(rook, 4, 4);
EXPECT_FALSE(isValid);
Rook* enemyRook = new Rook(2, 4, Color::white);
board.addPiece(enemyRook);
isValid = board.isValidMove(rook, 0, 4);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(rook, 2, 4);
EXPECT_TRUE(isValid);
}
TEST(Chessboard, Turn) {
Chessboard board;
King* whiteKing = new King(7, 7, Color::white);
King* blackKing = new King(0, 0, Color::black);
board.addPiece(whiteKing);
board.addPiece(blackKing);
/* V�rifie que seuls les blancs peuvent effectuer le premier d�placement */
bool isValid = board.movePiece(blackKing, 1, 1);
EXPECT_FALSE(isValid);
isValid = board.movePiece(whiteKing, 6, 6);
EXPECT_TRUE(isValid);
/* V�rifie que le tour des noirs vient apr�s celui des blancs */
isValid = board.movePiece(whiteKing, 5, 5);
EXPECT_FALSE(isValid);
isValid = board.movePiece(blackKing, 1, 1);
EXPECT_TRUE(isValid);
/* V�rifie que le tour des blancs vient apr�s celui des noirs */
isValid = board.movePiece(blackKing, 2, 2);
EXPECT_FALSE(isValid);
Color currentPlayer = board.getColor();
EXPECT_EQ(currentPlayer, Color::white);
}
TEST(Chessboard, Check) {
Chessboard board;
King* king = new King(0, 0, Color::white);
Bishop* enemyBishop = new Bishop(5, 5, Color::black);
board.addPiece(king);
board.addPiece(enemyBishop);
/* V�rifie que le roi doit se d�placer vers une position o� il n'est pas en �chec */
bool isValid = board.isValidMove(king, 1, 1);
EXPECT_FALSE(isValid);
isValid = board.isValidMove(king, 0, 1);
EXPECT_TRUE(isValid);
}
TEST(Chessboard, BlockedCheckMate) {
Chessboard board;
Bishop* attacker = new Bishop(3, 4, Color::white);
board.addPiece(new King(3, 0, Color::black));
board.addPiece(new Bishop(2, 0, Color::black));
board.addPiece(new Bishop(5, 2, Color::black));
board.addPiece(new Rook(5, 0, Color::black));
board.addPiece(new King(3, 7, Color::white));
board.addPiece(new Bishop(1, 3, Color::white));
board.addPiece(attacker);
board.addPiece(new Rook(2, 6, Color::white));
board.addPiece(new Rook(4, 6, Color::white));
board.movePiece(attacker, 5, 2);
Tester tester;
EXPECT_FALSE(tester.testIsCheckMate(board));
}
TEST(Chessboard, CheckMate) {
Chessboard board;
Bishop* attacker = new Bishop(3, 4, Color::white);
board.addPiece(new King(3, 0, Color::black));
board.addPiece(new Bishop(2, 0, Color::black));
board.addPiece(new Bishop(5, 2, Color::black));
board.addPiece(new King(3, 7, Color::white));
board.addPiece(new Bishop(1, 3, Color::white));
board.addPiece(attacker);
board.addPiece(new Rook(2, 6, Color::white));
board.addPiece(new Rook(4, 6, Color::white));
board.movePiece(attacker, 5, 2);
Tester tester;
EXPECT_TRUE(tester.testIsCheckMate(board));
}
TEST(StartupPositions, Strategy1){
Chessboard board;
StartupPositions1 strategy;
board.startGame(&strategy);
// �quipe noire
bool isRook = typeid(Rook) == typeid(*board.getPiece(0, 0));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(7, 0));
EXPECT_TRUE(isRook);
bool isBishop = typeid(Bishop) == typeid(*board.getPiece(2, 0));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(5, 0));
EXPECT_TRUE(isBishop);
bool isKing = typeid(King) == typeid(*board.getPiece(3, 0));
EXPECT_TRUE(isKing);
// �quipe blanche
isRook = typeid(Rook) == typeid(*board.getPiece(0, 7));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(7, 7));
EXPECT_TRUE(isRook);
isBishop = typeid(Bishop) == typeid(*board.getPiece(2, 7));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(5, 7));
EXPECT_TRUE(isBishop);
isKing = typeid(King) == typeid(*board.getPiece(3, 7));
EXPECT_TRUE(isKing);
}
TEST(StartupPositions, Strategy2) {
Chessboard board;
StartupPositions2 strategy;
board.startGame(&strategy);
// �quipe noire
bool isBishop = typeid(Bishop) == typeid(*board.getPiece(0, 0));
EXPECT_TRUE(isBishop);
bool isKing = typeid(King) == typeid(*board.getPiece(3, 0));
EXPECT_TRUE(isKing);
// �quipe blanche
bool isRook = typeid(Rook) == typeid(*board.getPiece(7, 7));
EXPECT_TRUE(isRook);
isKing = typeid(King) == typeid(*board.getPiece(3, 7));
EXPECT_TRUE(isKing);
}
TEST(StartupPositions, Strategy3) {
Chessboard board;
StartupPositions3 strategy;
board.startGame(&strategy);
// �quipe noire
bool isRook = typeid(Rook) == typeid(*board.getPiece(3, 4));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(5, 4));
EXPECT_TRUE(isRook);
bool isBishop = typeid(Bishop) == typeid(*board.getPiece(4, 5));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(4, 3));
EXPECT_TRUE(isBishop);
bool isKing = typeid(King) == typeid(*board.getPiece(4, 4));
EXPECT_TRUE(isKing);
// �quipe blanche
isRook = typeid(Rook) == typeid(*board.getPiece(0, 4));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(7, 4));
EXPECT_TRUE(isRook);
isBishop = typeid(Bishop) == typeid(*board.getPiece(4, 0));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(4, 7));
EXPECT_TRUE(isBishop);
isKing = typeid(King) == typeid(*board.getPiece(7, 7));
EXPECT_TRUE(isKing);
}
TEST(StartupPositions, Strategy4) {
Chessboard board;
StartupPositions4 strategy;
board.startGame(&strategy);
// �quipe noire
bool isRook = typeid(Rook) == typeid(*board.getPiece(1, 0));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(0, 1));
EXPECT_TRUE(isRook);
bool isBishop = typeid(Bishop) == typeid(*board.getPiece(1, 1));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(2, 2));
EXPECT_TRUE(isBishop);
bool isKing = typeid(King) == typeid(*board.getPiece(0, 0));
EXPECT_TRUE(isKing);
// �quipe blanche
isRook = typeid(Rook) == typeid(*board.getPiece(6, 7));
EXPECT_TRUE(isRook);
isRook = typeid(Rook) == typeid(*board.getPiece(7, 6));
EXPECT_TRUE(isRook);
isBishop = typeid(Bishop) == typeid(*board.getPiece(6, 6));
EXPECT_TRUE(isBishop);
isBishop = typeid(Bishop) == typeid(*board.getPiece(5, 5));
EXPECT_TRUE(isBishop);
isKing = typeid(King) == typeid(*board.getPiece(7, 7));
EXPECT_TRUE(isKing);
}
TEST(King, exception) {
Chessboard board;
King* firstKing = new King(0, 0, Color::white);
King* secondKing = new King(7, 7, Color::black);
// Cette ligne �met une erreur puisque j'y instancie une variable, mais
// je ne l'utilise pas. C'est toutefois tout � fait normale puisque
// c'est l'instantiation de la variable que je souhaite tester et non
// son utilisation
EXPECT_THROW(King* thirdKing = new King(4, 4, Color::white), TooManyKingsException);
delete firstKing;
delete secondKing;
}
TEST(Chessboard, OutsideBoardMove) {
Chessboard board;
Bishop* bishop = new Bishop(0, 0, Color::white);
board.addPiece(bishop);
EXPECT_FALSE(board.movePiece(bishop, 64, 64));
}
}
#endif