1 #include "chessserializationtest.h"
4 #include <defaultstate.h>
6 #include <moveserializer.h>
8 #include <statefactory.h>
9 #include <types/chess/bishop.h>
10 #include <types/chess/king.h>
11 #include <types/chess/pawn.h>
12 #include <types/chess/queen.h>
13 #include <types/chess/rook.h>
14 #include <validator.h>
17 #include "test_utils.h"
19 using namespace Chess
;
21 CPPUNIT_TEST_SUITE_REGISTRATION(ChessSerializationTest
);
23 Variant
* chess_variant_factory();
25 void ChessSerializationTest::setUp() {
26 m_chess
= chess_variant_factory();
27 IStateFactory
* fact
= requestComponent
<IStateFactory
>(
28 m_chess
, "state_factory");
29 m_state
= dynamic_cast<IDefaultState
*>(fact
->createState());
30 m_validator
= requestComponent
<IValidator
>(
31 m_chess
, "validator");
32 m_decorator
= requestComponent
<IMoveSerializer
>(
33 m_chess
, "move_serializer/decorated");
34 m_san
= requestComponent
<IMoveSerializer
>(
35 m_chess
, "move_serializer/san");
36 m_simple
= requestComponent
<IMoveSerializer
>(
37 m_chess
, "move_serializer/simple");
40 void ChessSerializationTest::tearDown() {
45 void ChessSerializationTest::test_pawn() {
48 Move
move(Point(4, 6), Point(4, 4));
49 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
51 CPPUNIT_ASSERT_EQUAL(QString("e4"), m_san
->serialize(move
, m_state
));
53 CPPUNIT_ASSERT_EQUAL(QString("e2e4"), m_simple
->serialize(move
, m_state
));
56 void ChessSerializationTest::test_check() {
57 m_state
->board()->set(Point(5, 5), Piece(Black::self(), King::self()));
58 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
59 m_state
->board()->set(Point(3, 5), Piece(White::self(), Bishop::self()));
61 Move
move(Point(3, 5), Point(4, 4));
62 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
64 CPPUNIT_ASSERT_EQUAL(QString("Be4+"), m_san
->serialize(move
, m_state
));
66 CPPUNIT_ASSERT_EQUAL(QString("d3e4"), m_simple
->serialize(move
, m_state
));
68 CPPUNIT_ASSERT_EQUAL(QString("{bishop}e4+"), m_decorator
->serialize(move
, m_state
));
71 void ChessSerializationTest::test_check_capture() {
72 m_state
->board()->set(Point(5, 5), Piece(Black::self(), King::self()));
73 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
74 m_state
->board()->set(Point(3, 5), Piece(White::self(), Bishop::self()));
75 m_state
->board()->set(Point(4, 4), Piece(Black::self(), Rook::self()));
77 Move
move(Point(3, 5), Point(4, 4));
78 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
80 CPPUNIT_ASSERT_EQUAL(QString("Bxe4+"), m_san
->serialize(move
, m_state
));
82 CPPUNIT_ASSERT_EQUAL(QString("d3e4"), m_simple
->serialize(move
, m_state
));
84 CPPUNIT_ASSERT_EQUAL(QString("{bishop}xe4+"), m_decorator
->serialize(move
, m_state
));
87 void ChessSerializationTest::test_promotion() {
88 m_state
->board()->set(Point(4, 7), Piece(Black::self(), King::self()));
89 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
90 m_state
->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
92 Move
move(Point(7, 1), Point(7, 0), Rook::self());
93 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
95 CPPUNIT_ASSERT_EQUAL(QString("h8=R"), m_san
->serialize(move
, m_state
));
97 CPPUNIT_ASSERT_EQUAL(QString("h7h8=R"), m_simple
->serialize(move
, m_state
));
99 CPPUNIT_ASSERT_EQUAL(QString("h8={rook}"), m_decorator
->serialize(move
, m_state
));
102 void ChessSerializationTest::test_promotion_capture() {
103 m_state
->board()->set(Point(4, 7), Piece(Black::self(), King::self()));
104 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
105 m_state
->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
106 m_state
->board()->set(Point(6, 0), Piece(Black::self(), Bishop::self()));
108 Move
move(Point(7, 1), Point(6, 0), Rook::self());
109 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
111 CPPUNIT_ASSERT_EQUAL(QString("hxg8=R"), m_san
->serialize(move
, m_state
));
113 CPPUNIT_ASSERT_EQUAL(QString("h7g8=R"), m_simple
->serialize(move
, m_state
));
115 CPPUNIT_ASSERT_EQUAL(QString("hxg8={rook}"), m_decorator
->serialize(move
, m_state
));
118 void ChessSerializationTest::test_promotion_check() {
119 m_state
->board()->set(Point(4, 0), Piece(Black::self(), King::self()));
120 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
121 m_state
->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
123 Move
move(Point(7, 1), Point(7, 0), Rook::self());
124 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
126 CPPUNIT_ASSERT_EQUAL(QString("h8=R+"), m_san
->serialize(move
, m_state
));
128 CPPUNIT_ASSERT_EQUAL(QString("h7h8=R"), m_simple
->serialize(move
, m_state
));
130 CPPUNIT_ASSERT_EQUAL(QString("h8={rook}+"), m_decorator
->serialize(move
, m_state
));
133 void ChessSerializationTest::test_promotion_capture_check() {
134 m_state
->board()->set(Point(4, 0), Piece(Black::self(), King::self()));
135 m_state
->board()->set(Point(0, 0), Piece(White::self(), King::self()));
136 m_state
->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
137 m_state
->board()->set(Point(6, 0), Piece(Black::self(), Bishop::self()));
139 Move
move(Point(7, 1), Point(6, 0), Rook::self());
140 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
142 CPPUNIT_ASSERT_EQUAL(QString("hxg8=R+"), m_san
->serialize(move
, m_state
));
144 CPPUNIT_ASSERT_EQUAL(QString("h7g8=R"), m_simple
->serialize(move
, m_state
));
146 CPPUNIT_ASSERT_EQUAL(QString("hxg8={rook}+"), m_decorator
->serialize(move
, m_state
));
149 void ChessSerializationTest::test_castling_k() {
152 m_state
->board()->set(Point(5, 7), Piece());
153 m_state
->board()->set(Point(6, 7), Piece());
155 Move
move(Point(4, 7), Point(6, 7));
156 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
158 CPPUNIT_ASSERT_EQUAL(QString("O-O"), m_san
->serialize(move
, m_state
));
160 CPPUNIT_ASSERT_EQUAL(QString("e1g1"), m_simple
->serialize(move
, m_state
));
162 CPPUNIT_ASSERT_EQUAL(QString("O-O"), m_decorator
->serialize(move
, m_state
));
165 void ChessSerializationTest::test_castling_q() {
168 m_state
->board()->set(Point(3, 7), Piece());
169 m_state
->board()->set(Point(2, 7), Piece());
170 m_state
->board()->set(Point(1, 7), Piece());
172 Move
move(Point(4, 7), Point(2, 7));
173 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
175 CPPUNIT_ASSERT_EQUAL(QString("O-O-O"), m_san
->serialize(move
, m_state
));
177 CPPUNIT_ASSERT_EQUAL(QString("e1c1"), m_simple
->serialize(move
, m_state
));
179 CPPUNIT_ASSERT_EQUAL(QString("O-O-O"), m_decorator
->serialize(move
, m_state
));
182 void ChessSerializationTest::regression_knight_king() {
185 Move
move(Point(6, 7), Point(5, 5));
186 CPPUNIT_ASSERT(m_validator
->legal(m_state
, move
));
188 CPPUNIT_ASSERT_EQUAL(QString("Nf3"), m_san
->serialize(move
, m_state
));
190 CPPUNIT_ASSERT_EQUAL(QString("g1f3"), m_simple
->serialize(move
, m_state
));
192 CPPUNIT_ASSERT_EQUAL(QString("{knight}f3"), m_decorator
->serialize(move
, m_state
));