4 #include "../../src/move.h"
5 #include "../../src/board.h"
6 #include "gtest/gtest.h"
8 // Helper template returning the size of a one dimension array at compile time
9 template<typename T
, size_t N
>
10 constexpr size_t array_size(const T (&)[N
])
15 static const MoveType MOVES
[] = {
27 KNIGHT_PROMOTION_CAPTURE
,
28 BISHOP_PROMOTION_CAPTURE
,
29 ROOK_PROMOTION_CAPTURE
,
30 QUEEN_PROMOTION_CAPTURE
33 static const MoveType PROMOTIONS
[] = {
38 KNIGHT_PROMOTION_CAPTURE
,
39 BISHOP_PROMOTION_CAPTURE
,
40 ROOK_PROMOTION_CAPTURE
,
41 QUEEN_PROMOTION_CAPTURE
43 static const int NB_PROMOTIONS
= array_size(PROMOTIONS
);
45 static const MoveType CAPTURES
[] = {
48 KNIGHT_PROMOTION_CAPTURE
,
49 BISHOP_PROMOTION_CAPTURE
,
50 ROOK_PROMOTION_CAPTURE
,
51 QUEEN_PROMOTION_CAPTURE
53 static const int NB_CAPTURES
= array_size(CAPTURES
);
57 EXPECT_EQ(2, sizeof(uint16_t));
58 EXPECT_EQ(sizeof(uint16_t), sizeof(Move
));
61 TEST(MoveTest
, Constructor
)
64 EXPECT_TRUE(null_move
.is_null());
65 EXPECT_TRUE(Move().is_null());
67 for (const Square
&o
: SQUARES
) {
68 if (o
== OUT
) continue;
69 for (const Square
&d
: SQUARES
) {
70 if (d
== OUT
|| d
== o
) continue;
71 for (const MoveType
&t
: MOVES
) {
73 EXPECT_EQ(o
, m
.orig());
74 EXPECT_EQ(d
, m
.dest());
75 EXPECT_EQ(t
, m
.type());
77 EXPECT_EQ(Board::file(o
), m
.orig_file());
78 EXPECT_EQ(Board::file(d
), m
.dest_file());
79 EXPECT_EQ(Board::rank(o
), m
.orig_rank());
80 EXPECT_EQ(Board::rank(d
), m
.dest_rank());
83 // FIXME: Takes too long, too much moves
84 for (const Square &o2 : SQUARES) {
85 if (o2 == OUT) continue;
86 for (const Square &d2 : SQUARES) {
87 if (d2 == OUT || d2 == o2) continue;
88 for (const MoveType &t2 : MOVES) {
90 if (o == o2 && d == d2 && t == t2) {
106 for (const MoveType
&t
: MOVES
) {
108 bool is_null
= t
== NULL_MOVE
;
109 bool is_en_passant
= t
== EN_PASSANT
;
110 bool is_double_pawn_push
= t
== DOUBLE_PAWN_PUSH
;
111 bool is_castle
= t
== KING_CASTLE
|| t
== QUEEN_CASTLE
;
113 std::binary_search(CAPTURES
, CAPTURES
+ NB_CAPTURES
, t
);
115 std::binary_search(PROMOTIONS
, PROMOTIONS
+ NB_PROMOTIONS
, t
);
117 for (const Square
&o
: SQUARES
) {
118 if (o
== OUT
) continue;
119 for (const Square
&d
: SQUARES
) {
120 if (d
== OUT
|| d
== o
) continue;
122 EXPECT_EQ(is_null
, m
.is_null());
123 EXPECT_EQ(is_en_passant
, m
.is_en_passant());
124 EXPECT_EQ(is_double_pawn_push
, m
.is_double_pawn_push());
125 EXPECT_EQ(is_castle
, m
.is_castle());
126 EXPECT_EQ(is_capture
, m
.is_capture());
127 EXPECT_EQ(is_promotion
, m
.is_promotion());
131 EXPECT_EQ(KING
, m
.castle_side());
134 EXPECT_EQ(QUEEN
, m
.castle_side());
136 case KNIGHT_PROMOTION
:
137 case KNIGHT_PROMOTION_CAPTURE
:
138 EXPECT_EQ(KNIGHT
, m
.promotion_type());
140 case BISHOP_PROMOTION
:
141 case BISHOP_PROMOTION_CAPTURE
:
142 EXPECT_EQ(BISHOP
, m
.promotion_type());
145 case ROOK_PROMOTION_CAPTURE
:
146 EXPECT_EQ(ROOK
, m
.promotion_type());
148 case QUEEN_PROMOTION
:
149 case QUEEN_PROMOTION_CAPTURE
:
150 EXPECT_EQ(QUEEN
, m
.promotion_type());
161 TEST(ExtendedMoveTest
, Size
)
163 int size
= sizeof(uint16_t) + sizeof(char) + 1; // 1 byte of padding
165 EXPECT_EQ(size
, sizeof(ExtendedMove
));
168 TEST(ExtendedMoveTest
, Constructor
)
170 EXPECT_EQ(Move(), ExtendedMove());
171 EXPECT_EQ(0, ExtendedMove().value());
173 for (const Square
&o
: SQUARES
) {
174 if (o
== OUT
) continue;
175 for (const Square
&d
: SQUARES
) {
176 if (d
== OUT
|| d
== o
) continue;
177 for (const MoveType
&t
: MOVES
) {
180 ExtendedMove
em2(m
, 0);
184 // '==' and '!=' operators are inherited from Move class
185 ExtendedMove
em3(m
, 50);
192 TEST(ExtendedMoveTest
, Score
)
195 for (int i
= SCHAR_MIN
; i
<= SCHAR_MAX
; ++i
) {
196 ExtendedMove
em1(m
, i
);
197 EXPECT_EQ(i
, em1
.value());
199 // Test getter and setter
200 ExtendedMove
em2(m
, 0);
201 EXPECT_EQ(0, em2
.value());
205 TEST(ExtendedMoveTest
, Comp
)
208 ExtendedMove
em1(m
, 0);
209 ExtendedMove
em2(m
, SCHAR_MAX
);
210 for (int i
= SCHAR_MIN
; i
<= SCHAR_MAX
; ++i
) {
211 ExtendedMove
em3(m
, i
);
212 EXPECT_EQ(i
< 0, em1
< em3
);