Use C++11 std::move for swap in selection sort algorithm
[purplehaze.git] / test / unit / test_move.cpp
blobb447ae35dcc6deedb8d99270c538819c07a97e72
1 #include <algorithm>
2 #include <climits>
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])
12 return N;
15 static const MoveType MOVES[] = {
16 QUIET_MOVE,
17 DOUBLE_PAWN_PUSH,
18 KING_CASTLE,
19 QUEEN_CASTLE,
20 CAPTURE,
21 EN_PASSANT,
22 NULL_MOVE,
23 KNIGHT_PROMOTION,
24 BISHOP_PROMOTION,
25 ROOK_PROMOTION,
26 QUEEN_PROMOTION,
27 KNIGHT_PROMOTION_CAPTURE,
28 BISHOP_PROMOTION_CAPTURE,
29 ROOK_PROMOTION_CAPTURE,
30 QUEEN_PROMOTION_CAPTURE
33 static const MoveType PROMOTIONS[] = {
34 KNIGHT_PROMOTION,
35 BISHOP_PROMOTION,
36 ROOK_PROMOTION,
37 QUEEN_PROMOTION,
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[] = {
46 CAPTURE,
47 EN_PASSANT,
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);
55 TEST(MoveTest, Size)
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) {
69 Move m(o, d, t);
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) {
86 Move m2(o2, d2, t2);
87 if (o == o2 && d == d2 && t == t2) {
88 EXPECT_EQ(m2, m);
89 } else {
90 EXPECT_NE(m2, m);
101 TEST(MoveTest, Type)
103 for (const MoveType &t : MOVES) {
104 // Define move types
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;
109 bool is_capture =
110 std::binary_search(CAPTURES, CAPTURES + NB_CAPTURES, t);
111 bool is_promotion =
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;
118 Move m(o, d, t);
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());
126 switch (t) {
127 case KING_CASTLE:
128 EXPECT_EQ(KING, m.get_castle_side());
129 break;
130 case QUEEN_CASTLE:
131 EXPECT_EQ(QUEEN, m.get_castle_side());
132 break;
133 case KNIGHT_PROMOTION:
134 case KNIGHT_PROMOTION_CAPTURE:
135 EXPECT_EQ(KNIGHT, m.get_promotion_type());
136 break;
137 case BISHOP_PROMOTION:
138 case BISHOP_PROMOTION_CAPTURE:
139 EXPECT_EQ(BISHOP, m.get_promotion_type());
140 break;
141 case ROOK_PROMOTION:
142 case ROOK_PROMOTION_CAPTURE:
143 EXPECT_EQ(ROOK, m.get_promotion_type());
144 break;
145 case QUEEN_PROMOTION:
146 case QUEEN_PROMOTION_CAPTURE:
147 EXPECT_EQ(QUEEN, m.get_promotion_type());
148 break;
149 default:
150 break;
158 TEST(ExtendedMoveTest, Size)
160 int size = sizeof(short) + sizeof(char) + 1; // 1 byte of padding
161 EXPECT_EQ(4, size);
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) {
175 Move m(o, d, t);
176 ExtendedMove em1(m);
177 ExtendedMove em2(m, 0);
178 EXPECT_EQ(m, em1);
179 EXPECT_EQ(em1, em2);
181 // '==' and '!=' operators are inherited from Move class
182 ExtendedMove em3(m, 50);
183 EXPECT_EQ(em2, em3);
189 TEST(ExtendedMoveTest, Score)
191 Move m;
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());
199 em2.set_score(i);
200 EXPECT_EQ(i, em2.get_score());
201 em2.set_score(0);
202 EXPECT_EQ(0, em2.get_score());
206 TEST(ExtendedMoveTest, Comp)
208 Move m;
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);
214 if (i < 0) {
215 EXPECT_LT(em1, em3);
217 if (i < SCHAR_MAX) {
218 EXPECT_LT(em2, em3);