1 #include "chesslegalitytest.h"
5 #include <defaultstate.h>
8 #include <statefactory.h>
9 #include <types/chess/pawn.h>
10 #include <types/chess/rook.h>
11 #include <types/chess/king.h>
12 #include <types/chess/queen.h>
13 #include <validator.h>
16 #include "test_utils.h"
18 using namespace Chess
;
20 Variant
* chess_variant_factory();
22 CPPUNIT_TEST_SUITE_REGISTRATION(ChessLegalityTest
);
24 void ChessLegalityTest::setUp() {
25 m_chess
= chess_variant_factory();
26 IStateFactory
* fact
= requestComponent
<IStateFactory
>(
27 m_chess
, "state_factory");
28 m_state
= dynamic_cast<IDefaultState
*>(fact
->createState());
29 m_validator
= requestComponent
<IValidator
>(
30 m_chess
, "validator");
35 void ChessLegalityTest::tearDown() {
40 void ChessLegalityTest::test_movements() {
41 Move
e4(Point(4, 6), Point(4, 4));
42 CPPUNIT_ASSERT(m_validator
->legal(m_state
, e4
));
43 CPPUNIT_ASSERT(e4
.type() == "en_passant_trigger");
45 Move
crazy(Point(0, 0), Point(7, 4));
46 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, crazy
));
49 void ChessLegalityTest::test_pseudolegal() {
50 Move
e4(Point(4, 6), Point(4, 4));
51 CPPUNIT_ASSERT(m_validator
->pseudolegal(m_state
, e4
));
53 Move
e5(Point(4, 6), Point(4, 3));
54 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, e5
));
56 Move
Nf3(Point(6, 7), Point(5, 5));
57 CPPUNIT_ASSERT(m_validator
->pseudolegal(m_state
, Nf3
));
59 Move
Bc4(Point(5, 7), Point(2, 4));
60 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, Bc4
));
63 m_state
->advanceTurn();
65 Move
crazy(Point(0, 0), Point(4, 7));
66 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, crazy
));
68 Move
e5_(Point(4, 1), Point(4, 3));
69 CPPUNIT_ASSERT(m_validator
->pseudolegal(m_state
, e5_
));
72 void ChessLegalityTest::test_simple_move() {
73 Move
e4(Point(4, 6), Point(4, 4));
74 CPPUNIT_ASSERT(m_validator
->legal(m_state
, e4
));
76 Move
e5(Point(4, 6), Point(4, 3));
77 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, e5
));
79 Move
Nf3(Point(6, 7), Point(5, 5));
80 CPPUNIT_ASSERT(m_validator
->legal(m_state
, Nf3
));
82 Move
Bc4(Point(5, 7), Point(2, 4));
83 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, Bc4
));
86 Move
tmp(Point(5, 7), Point(2, 4));
87 m_state
->board()->set(Point(4, 6), Piece());
88 CPPUNIT_ASSERT(m_validator
->legal(m_state
, tmp
));
91 Move
e6(Point(4, 1), Point(4, 2));
92 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, e6
));
95 Move
tmp(Point(4, 1), Point(4, 2));
96 m_state
->advanceTurn();
97 CPPUNIT_ASSERT(m_validator
->legal(m_state
, tmp
));
101 void ChessLegalityTest::test_promotion() {
102 m_state
->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
103 Move
h8Q(Point(7, 1), Point(7, 0), Queen::self());
104 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, h8Q
));
106 Move
hxg8R(Point(7, 1), Point(6, 0), Rook::self());
107 CPPUNIT_ASSERT(m_validator
->pseudolegal(m_state
, hxg8R
));
108 CPPUNIT_ASSERT(m_validator
->legal(m_state
, hxg8R
));
109 CPPUNIT_ASSERT_EQUAL(
110 static_cast<const IType
*>(Rook::self()),
114 void ChessLegalityTest::test_en_passant() {
115 m_state
->move(Move(Point(4, 6), Point(4, 4)));
116 m_state
->move(Move(Point(7, 1), Point(7, 2)));
117 m_state
->move(Move(Point(4, 4), Point(4, 3)));
119 Move
d5(Point(3, 1), Point(3, 3));
120 CPPUNIT_ASSERT(m_validator
->legal(m_state
, d5
));
121 CPPUNIT_ASSERT(d5
.type() == "en_passant_trigger");
124 Move
exd6(Point(4, 3), Point(3, 2));
125 CPPUNIT_ASSERT(m_validator
->legal(m_state
, exd6
));
126 CPPUNIT_ASSERT(m_state
->captureSquare(exd6
) == Point(3, 3));
128 m_state
->move(Move(Point(7, 6), Point(7, 5)));
129 m_state
->move(Move(Point(7, 2), Point(7, 3)));
131 Move
tmp(Point(4, 3), Point(3, 2));
132 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, tmp
));
135 void ChessLegalityTest::test_castling() {
136 Move
oo(Point(4, 7), Point(6, 7));
138 m_state
->board()->set(Point(6, 7), Piece());
141 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, tmp
));
144 m_state
->board()->set(Point(5, 7), Piece());
147 CPPUNIT_ASSERT(m_validator
->legal(m_state
, tmp
));
150 m_state
->board()->set(Point(3, 6), Piece(Black::self(), Pawn::self()));
153 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, tmp
));
156 m_state
->board()->set(Point(3, 6), Piece());
157 m_state
->board()->set(Point(4, 6), Piece(Black::self(), Pawn::self()));
160 CPPUNIT_ASSERT(!m_validator
->pseudolegal(m_state
, tmp
));
164 void ChessLegalityTest::test_attack1() {
165 m_state
->board()->set(Point(5, 5), Piece(Black::self(), Rook::self()));
166 CPPUNIT_ASSERT(m_validator
->attacks(m_state
, White::self(), Point(5, 5)));
169 void ChessLegalityTest::test_attack2() {
170 m_state
->board()->set(Point(4, 4), Piece(Black::self(), Queen::self()));
171 CPPUNIT_ASSERT(!m_validator
->attacks(m_state
, White::self(), Point(4, 4)));
174 void ChessLegalityTest::test_attack3() {
175 m_state
->board()->set(Point(0, 5), Piece(Black::self(), King::self()));
176 m_state
->board()->set(Point(1, 6), Piece(White::self(), Rook::self())); // b2: pawn -> rook
177 CPPUNIT_ASSERT(m_validator
->attacks(m_state
, White::self(), Point(0, 5)));
179 m_state
->board()->set(Point(1, 7), Piece()); // remove knight on b1
180 CPPUNIT_ASSERT(!m_validator
->attacks(m_state
, White::self(), Point(0, 5)));
182 m_state
->board()->set(Point(0, 6), Piece()); // remove pawn on a2
183 CPPUNIT_ASSERT(m_validator
->attacks(m_state
, White::self(), Point(0, 5)));
185 m_state
->board()->set(Point(0, 7), Piece()); // remove rook on a1
186 CPPUNIT_ASSERT(!m_validator
->attacks(m_state
, White::self(), Point(0, 5)));
189 void ChessLegalityTest::test_attack4() {
190 CPPUNIT_ASSERT(!m_validator
->attacks(m_state
, Black::self(), Point(4, 7)));