-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.h
More file actions
194 lines (153 loc) · 6.05 KB
/
test.h
File metadata and controls
194 lines (153 loc) · 6.05 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
#pragma once
#define TESTING false
#if TESTING
#include "move.h"
#include "board.h"
#define assertEQ(got, expect) {if((got)!=(expect)){std::cout<<"ERR: expected "<<(expect)<<", got "<<(got)<<" (line "<<__LINE__<<')';return false;}}
Board getEmptyBoard() {
Board board;
board.whitePawns = 0;
board.whiteKnights = 0;
board.whiteRooks = 0;
board.whiteBishops = 0;
board.whiteCar = 0;
board.blackPawns = 0;
board.blackKnights = 0;
board.blackRooks = 0;
board.blackBishops = 0;
board.blackCar = 0;
board.updatePieceAggregates();
return board;
}
bool testPawn() {
Board board = getEmptyBoard();
board.whitePawns.flipBit(3, 3);
board.blackPawns.flipBit(2, 1);
board.blackCar.flipBit(4, 1);
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 1);
board.performWhiteMove(board.getValidMoves(PieceRange::White, false)[0]);
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 2);
board = getEmptyBoard();
board.blackPawns.flipBit(3, 3);
board.whitePawns.flipBit(2, 5);
board.whiteCar.flipBit(4, 5);
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::Black, false).size(), 1);
board.performBlackMove(board.getValidMoves(PieceRange::Black, false)[0]);
assertEQ(board.getValidMoves(PieceRange::Black, false).size(), 2);
return true;
}
bool testKnight() {
Board board = getEmptyBoard();
board.whiteKnights.flipBit(3, 3); // +4 moves
board.blackCar.flipBit(4, 1); // -1 move
board.blackPawns.flipBit(2, 1); // no moves
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 3);
board = getEmptyBoard();
board.blackKnights.flipBit(3, 3); // +4 moves
board.whitePawns.flipBit(4, 1); // +1 move
board.whitePawns.flipBit(2, 1); // +1 move
board.whitePawns.flipBit(5, 2); // +1 move
board.whitePawns.flipBit(1, 2); // +1 move
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::Black, false).size(), 8);
return true;
}
bool testRook() {
Board board = getEmptyBoard();
board.whiteRooks.flipBit(2, 4);
board.blackRooks.flipBit(2, 1); // +3 moves
board.blackBishops.flipBit(0, 4); // +1 move
board.blackPawns.flipBit(4, 4); // +1 move
board.blackPawns.flipBit(2, 7); // +1 move
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 6);
board = getEmptyBoard();
board.whiteRooks.flipBit(2, 6);
board.whitePawns.flipBit(2, 4); // +1 move
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 2);
board = getEmptyBoard();
board.blackRooks.flipBit(1, 0); // +7 moves
board.whiteCar.flipBit(1, 6); // -2 moves
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::Black, false).size(), 5);
return true;
}
bool testBishop() {
Board board = getEmptyBoard();
board.whiteBishops.flipBit(3, 2); // +4 moves
board.blackPawns.flipBit(4, 1); // -1 moves
board.blackRooks.flipBit(1, 0); // no moves
board.blackKnights.flipBit(5, 4); // +1 move
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::White, false).size(), 4);
board = getEmptyBoard();
board.blackBishops.flipBit(3, 5); // +4 moves
board.whitePawns.flipBit(4, 6); // -1 move
board.whiteRooks.flipBit(1, 7); // no moves
board.whiteKnights.flipBit(5, 3); // +1 move
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::Black, false).size(), 4);
return true;
}
bool testCar() {
// Assert car can run over enemies if there's no other choice
Board board = getEmptyBoard();
board.blackCar.flipBit(0, 0);
board.blackRooks.flipBit(2, 5);
board.whitePawns.flipBit(1, 1);
board.updatePieceAggregates();
assertEQ(board.getValidMoves(PieceRange::Black).size(), 2);
board.performBlackMove(board.getValidMoves(PieceRange::Black)[0]);
assertEQ(board.getValidMoves(PieceRange::Black).size(), 1);
board.performBlackMove(board.getValidMoves(PieceRange::Black)[0]);
assertEQ(board.blackCar.isBitSet(0, 0), false);
assertEQ(board.blackCar.isBitSet(1, 1), true);
return true;
}
bool testRaycasting() {
assertEQ(rayLookupTable[North][20], C64(0b1000000010000000100000001000000010000000000000000000000000000));
assertEQ(rayLookupTable[NorthWest][20], C64(0b1000000100000010000001000000000000000000000000000));
assertEQ(rayLookupTable[NorthEast][20], C64(0b100000000100000000000000000000000000000));
assertEQ(rayLookupTable[East][20], C64(0b11000000000000000000000));
assertEQ(rayLookupTable[South][44], C64(0b1000000010000000100000001000000010000));
assertEQ(rayLookupTable[SouthWest][44], C64(0b100000000100000000100000000100000000));
assertEQ(rayLookupTable[SouthEast][44], C64(0b10000001000000000000000000000000000000));
assertEQ(rayLookupTable[West][44], C64(0b11110000000000000000000000000000000000000000));
return true;
}
bool testLookup() {
assertEQ(pieceLookupTable[4], C64(0b0000000000000000000000000000000000000000000000000000000000010000));
assertEQ(maskPieceLookupTable[4], C64(0b1111111111111111111111111111111111111111111111111111111111101111));
return true;
}
static int testCount = 0;
void test(const std::string& name, bool (*f)()) {
testCount++;
if (!f()) {
std::cout <<" (in test: " << name << ")" << std::endl;
}
}
void testingMain() {
initAll();
test("pawn", testPawn);
test("knight", testKnight);
test("rook", testRook);
test("bishop", testBishop);
test("car", testCar);
test("raycasting", testRaycasting);
test("lookup tables", testLookup);
std::cout << "All " << testCount << " tests complete." << std::endl;
}
#endif
/*
cout << board << endl;
auto moves = board.getValidMoves(PieceRange::White);
for (auto move : moves) {
cout << move << ' ';
}
cout << endl;
*/