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(short));
58 EXPECT_EQ(sizeof(short), sizeof(Move
));
61 TEST(MoveTest
, Constructor
)
63 EXPECT_TRUE(Move().is_null());
64 for (const Square
&o
: SQUARES
) {
65 if (o
== OUT
) continue;
66 for (const Square
&d
: SQUARES
) {
67 if (d
== OUT
|| d
== o
) continue;
68 for (const MoveType
&t
: MOVES
) {
70 EXPECT_EQ(o
, m
.get_orig());
71 EXPECT_EQ(d
, m
.get_dest());
72 EXPECT_EQ(t
, m
.get_type());
74 EXPECT_EQ(Board::get_file(o
), m
.get_orig_file());
75 EXPECT_EQ(Board::get_file(d
), m
.get_dest_file());
76 EXPECT_EQ(Board::get_rank(o
), m
.get_orig_rank());
77 EXPECT_EQ(Board::get_rank(d
), m
.get_dest_rank());
80 // FIXME: Takes too long, too much moves
81 for (const Square &o2 : SQUARES) {
82 if (o2 == OUT) continue;
83 for (const Square &d2 : SQUARES) {
84 if (d2 == OUT || d2 == o2) continue;
85 for (const MoveType &t2 : MOVES) {
87 if (o == o2 && d == d2 && t == t2) {
103 for (const MoveType
&t
: MOVES
) {
105 bool is_null
= t
== NULL_MOVE
;
106 bool is_en_passant
= t
== EN_PASSANT
;
107 bool is_double_pawn_push
= t
== DOUBLE_PAWN_PUSH
;
108 bool is_castle
= t
== KING_CASTLE
|| t
== QUEEN_CASTLE
;
110 std::binary_search(CAPTURES
, CAPTURES
+ NB_CAPTURES
, t
);
112 std::binary_search(PROMOTIONS
, PROMOTIONS
+ NB_PROMOTIONS
, t
);
114 for (const Square
&o
: SQUARES
) {
115 if (o
== OUT
) continue;
116 for (const Square
&d
: SQUARES
) {
117 if (d
== OUT
|| d
== o
) continue;
119 EXPECT_EQ(is_null
, m
.is_null());
120 EXPECT_EQ(is_en_passant
, m
.is_en_passant());
121 EXPECT_EQ(is_double_pawn_push
, m
.is_double_pawn_push());
122 EXPECT_EQ(is_castle
, m
.is_castle());
123 EXPECT_EQ(is_capture
, m
.is_capture());
124 EXPECT_EQ(is_promotion
, m
.is_promotion());
128 EXPECT_EQ(KING
, m
.get_castle_side());
131 EXPECT_EQ(QUEEN
, m
.get_castle_side());
133 case KNIGHT_PROMOTION
:
134 case KNIGHT_PROMOTION_CAPTURE
:
135 EXPECT_EQ(KNIGHT
, m
.get_promotion_type());
137 case BISHOP_PROMOTION
:
138 case BISHOP_PROMOTION_CAPTURE
:
139 EXPECT_EQ(BISHOP
, m
.get_promotion_type());
142 case ROOK_PROMOTION_CAPTURE
:
143 EXPECT_EQ(ROOK
, m
.get_promotion_type());
145 case QUEEN_PROMOTION
:
146 case QUEEN_PROMOTION_CAPTURE
:
147 EXPECT_EQ(QUEEN
, m
.get_promotion_type());
158 TEST(ExtendedMoveTest
, Size
)
160 int size
= sizeof(short) + sizeof(char) + 1; // 1 byte of padding
162 EXPECT_EQ(size
, sizeof(ExtendedMove
));
165 TEST(ExtendedMoveTest
, Constructor
)
167 EXPECT_EQ(Move(), ExtendedMove());
168 EXPECT_EQ(0, ExtendedMove().get_score());
170 for (const Square
&o
: SQUARES
) {
171 if (o
== OUT
) continue;
172 for (const Square
&d
: SQUARES
) {
173 if (d
== OUT
|| d
== o
) continue;
174 for (const MoveType
&t
: MOVES
) {
177 ExtendedMove
em2(m
, 0);
181 // '==' and '!=' operators are inherited from Move class
182 ExtendedMove
em3(m
, 50);
189 TEST(ExtendedMoveTest
, Score
)
192 for (int i
= SCHAR_MIN
; i
<= SCHAR_MAX
; ++i
) {
193 ExtendedMove
em1(m
, i
);
194 EXPECT_EQ(i
, em1
.get_score());
196 // Test getter and setter
197 ExtendedMove
em2(m
, 0);
198 EXPECT_EQ(0, em2
.get_score());
200 EXPECT_EQ(i
, em2
.get_score());
202 EXPECT_EQ(0, em2
.get_score());
206 TEST(ExtendedMoveTest
, Comp
)
209 ExtendedMove
em1(m
, 0);
210 ExtendedMove
em2(m
, SCHAR_MAX
);
211 for (int i
= SCHAR_MIN
; i
<= SCHAR_MAX
; ++i
) {
212 ExtendedMove
em3(m
, i
);
213 EXPECT_EQ(i
< 0, em1
< em3
);