Fix squares declaration bug
[purplehaze.git] / test / unit / test_board.cpp
blob66f31255f20208b3e3b3c4929767bb4a8c9511f5
1 #include "../../src/board.h"
2 #include "../../src/move.h"
3 #include "../../src/piece.h"
4 #include "gtest/gtest.h"
6 TEST(BoardTest, Constructor)
8 Board board;
10 // Test empty board
11 for (int i = 0; i < BOARD_SIZE; ++i) {
12 const Square s = static_cast<Square>(i);
13 EXPECT_EQ(Piece(), board[s]);
16 // Test attack and direction arrays
17 for (const Square &from : SQUARES) {
18 if (from == OUT) {
19 break;
21 for (const Square &to : SQUARES) {
22 if (to == OUT) {
23 break;
25 Direction dir = NO_DIR;
26 for (const PieceType& t : NOT_PAWN_TYPES) {
27 bool can_attack = false;
28 for (const Direction &d : PIECES_DIRS[t]) {
29 if (d == NO_DIR) {
30 break;
32 Square s = static_cast<Square>(from + d);
33 while (!board.is_out(s)) {
34 if (s == to) {
35 can_attack = true;
36 dir = d;
37 break;
39 switch (t) {
40 case KNIGHT:
41 case KING:
42 s = OUT;
43 break;
44 default:
45 s = static_cast<Square>(s + d);
46 break;
50 if (can_attack) {
51 EXPECT_TRUE(board.can_attack(t, from, to));
52 } else {
53 EXPECT_FALSE(board.can_attack(t, from, to));
56 EXPECT_EQ(dir, board.direction_to(from, to));
61 TEST(BoardTest, Square)
63 Square BOARD[BOARD_SIZE] = { OUT };
64 int i;
65 for (i = 0; i < 64; ++i) {
66 Square s = Board::square(i);
67 BOARD[i] = s;
69 i = 0;
70 for (const Square &s : SQUARES) {
71 if (s == OUT) {
72 continue;
74 EXPECT_EQ(s, BOARD[i]);
75 ++i;
77 EXPECT_EQ(i, 64);