Update NEWS
[purplehaze.git] / test / unit / test_moves.cpp
blob9f78bcaf97d37afc1cac03d808ef39913ba09510
1 #include "../../src/moves.h"
2 #include "../../src/board.h"
3 #include "../../src/position.h"
4 #include "gtest/gtest.h"
6 TEST(MoveListTest, Size)
8 int list_size = (MAX_PLY * MAX_MOVES) * sizeof(ExtendedMove);
9 int size = list_size + sizeof(unsigned int);
11 // MoveList's internal array's size should not be a power of two
12 //EXPECT_NE(0, list_size & (list_size - 1));
14 EXPECT_EQ(128 * 256 * 4, list_size);
15 EXPECT_EQ(list_size + 4, size);
16 EXPECT_EQ(size, sizeof(MoveList));
19 TEST(MoveListTest, Constructor)
21 MoveList moves;
22 for (int i = 0; i < MAX_PLY; ++i) {
23 for (int j = 0; j < MAX_MOVES; ++j) {
24 // Test default move
25 EXPECT_TRUE(moves[j].is_null());
27 // Test default score
28 EXPECT_EQ(0, moves[j].value());
30 moves.inc_ply();
34 TEST(MoveListTest, Assignement)
36 MoveList moves;
37 Move m;
38 for (int i = 0; i < MAX_PLY; ++i) {
39 for (int j = 0; j < MAX_MOVES; ++j) {
40 ExtendedMove em1(m, i + j);
41 moves[j] = em1;
42 EXPECT_EQ(em1, moves[j]);
43 EXPECT_EQ(em1.value(), moves[j].value());
45 moves.inc_ply();
47 for (int i = MAX_PLY - 1; i > 0; --i) {
48 moves.dec_ply();
49 for (int j = 0; j < MAX_MOVES; ++j) {
50 ExtendedMove em1(m, i + j);
51 EXPECT_EQ(em1, moves[j]);
52 EXPECT_EQ(em1.value(), moves[j].value());
55 moves.clear();
56 for (int i = 0; i < MAX_PLY; ++i) {
57 for (int j = 0; j < MAX_MOVES; ++j) {
58 ExtendedMove em1(m, i + j);
59 EXPECT_EQ(em1, moves[j]);
60 EXPECT_EQ(em1.value(), moves[j].value());
62 moves.inc_ply();
67 TEST(MovesTest, Size)
69 EXPECT_EQ(1, sizeof(MovesState));
70 EXPECT_EQ(5, MOVES_STATE_SIZE * sizeof(unsigned char));
71 int size =
72 sizeof(MoveList*) + // 4-8 bytes
73 sizeof(Position*) + // 4-8 bytes
74 sizeof(Board*) + // 4-8 bytes
75 sizeof(Pieces*) + // 4-8 bytes
76 sizeof(int) + // 4 bytes
77 sizeof(int) + // 4 bytes
78 MOVES_STATE_SIZE * sizeof(unsigned char) + // 5 bytes
79 1 + // 1 byte (padding)
80 sizeof(MovesState) + // 1 byte
81 sizeof(bool); // 1 byte
83 #ifdef __x86_64__
84 EXPECT_EQ(48, size);
85 #elif __i386__
86 EXPECT_EQ(32, size);
87 #endif
89 EXPECT_EQ(size, sizeof(Moves));
92 TEST(MovesTest, Constructor)
94 Board board;
95 Pieces pieces;
96 Position position;
97 MoveList list;
99 Move m;
100 ExtendedMove em(m, 50);
101 list[0] = em;
102 EXPECT_EQ(em.value(), list[0].value());
104 EXPECT_EQ(0, list.cur_ply());
106 Moves moves(board, pieces, position, list);
108 // Test if MoveList::inc_ply() has been implicitly called by Moves()
109 EXPECT_EQ(1, list.cur_ply());
111 EXPECT_EQ(BEST, moves.state());
113 EXPECT_EQ(0, moves.count(BEST));
114 EXPECT_EQ(0, moves.count(GOOD_CAPTURES));
115 EXPECT_EQ(0, moves.count(KILLERS));
116 EXPECT_EQ(0, moves.count(BAD_CAPTURES));
117 EXPECT_EQ(0, moves.count(QUIET_MOVES));
119 for (int i = 0; i < MAX_PLY; ++i) {
120 EXPECT_EQ(0, list[i].value());
122 list.dec_ply();
123 EXPECT_EQ(em.value(), list[0].value());
126 TEST(MovesTest, Score)
128 Board board;
129 Pieces pieces;
130 Position position;
131 MoveList list;
132 Moves moves(board, pieces, position, list);
134 Moves::init_mvv_lva_scores();
135 for (const PieceType& v : PIECE_TYPES) {
136 for (const PieceType& a : PIECE_TYPES) {
137 Square to = E5;
138 Square from;
139 switch (a) {
140 case KNIGHT:
141 from = D3;
142 break;
143 default:
144 from = D4;
145 break;
147 board[from] = Piece(WHITE, a);
148 board[to] = Piece(BLACK, v);
149 Move capture(from, to, CAPTURE);
150 Score score = moves.mvv_lva_score(capture);
152 EXPECT_GT(BEST_SCORE, score);
153 EXPECT_LT(KILLERS_SCORE, score);
155 for (const PieceType& v2 : PIECE_TYPES) {
156 for (const PieceType& a2 : PIECE_TYPES) {
157 switch (a2) {
158 case KNIGHT:
159 from = D3;
160 break;
161 default:
162 from = D4;
163 break;
165 board[from] = Piece(WHITE, a2);
166 board[to] = Piece(BLACK, v2);
167 Move capture2(from, to, CAPTURE);
168 Score score2 = moves.mvv_lva_score(capture2);
170 if (v > v2) {
171 EXPECT_GT(score, score2);
172 } else if (v < v2) {
173 EXPECT_LT(score, score2);
174 } else {
175 if (a > a2) {
176 EXPECT_LT(score, score2);
177 } else if (a < a2) {
178 EXPECT_GT(score, score2);
179 } else {
180 EXPECT_EQ(score, score2);
189 // TODO Add test for Moves destructor (implicit call of MoveList::dec_ply())