Fixed DecoratedMove and serialization bug.
[tagua.git] / tests / hlvariants / chessserializationtest.cpp
blob045081b1136eb39c0a04fa06e40de755a5904cf6
1 #include "chessserializationtest.h"
2 #include "common.h"
4 CPPUNIT_TEST_SUITE_REGISTRATION(ChessSerializationTest);
6 void ChessSerializationTest::setUp() {
7 m_state = new ChessGameState;
8 m_check = new ChessCheck(*m_state);
11 void ChessSerializationTest::tearDown() {
12 delete m_check;
13 delete m_state;
16 void ChessSerializationTest::test_pawn() {
17 m_state->setup();
19 ChessMove move(Point(4, 6), Point(4, 4));
20 CPPUNIT_ASSERT(m_check->legal(move));
22 ChessSerializer san(ChessSerializer::COMPACT);
23 CPPUNIT_ASSERT_EQUAL(QString("e4"), san.serialize(move, *m_state));
25 ChessSerializer simple(ChessSerializer::SIMPLE);
26 CPPUNIT_ASSERT_EQUAL(QString("e2e4"), simple.serialize(move, *m_state));
29 void ChessSerializationTest::test_check() {
30 m_state->board().set(Point(5, 5), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
31 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
32 m_state->board().set(Point(3, 5), ChessPiece(ChessPiece::WHITE, ChessPiece::BISHOP));
34 ChessMove move(Point(3, 5), Point(4, 4));
35 CPPUNIT_ASSERT(m_check->legal(move));
37 ChessSerializer san(ChessSerializer::COMPACT);
38 CPPUNIT_ASSERT_EQUAL(QString("Be4+"), san.serialize(move, *m_state));
40 ChessSerializer simple(ChessSerializer::SIMPLE);
41 CPPUNIT_ASSERT_EQUAL(QString("d3e4"), simple.serialize(move, *m_state));
43 ChessSerializer dec(ChessSerializer::DECORATED);
44 CPPUNIT_ASSERT_EQUAL(QString("{bishop}e4+"), dec.serialize(move, *m_state));
47 void ChessSerializationTest::test_check_capture() {
48 m_state->board().set(Point(5, 5), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
49 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
50 m_state->board().set(Point(3, 5), ChessPiece(ChessPiece::WHITE, ChessPiece::BISHOP));
51 m_state->board().set(Point(4, 4), ChessPiece(ChessPiece::BLACK, ChessPiece::ROOK));
53 ChessMove move(Point(3, 5), Point(4, 4));
54 CPPUNIT_ASSERT(m_check->legal(move));
56 ChessSerializer san(ChessSerializer::COMPACT);
57 CPPUNIT_ASSERT_EQUAL(QString("Bxe4+"), san.serialize(move, *m_state));
59 ChessSerializer simple(ChessSerializer::SIMPLE);
60 CPPUNIT_ASSERT_EQUAL(QString("d3e4"), simple.serialize(move, *m_state));
62 ChessSerializer dec(ChessSerializer::DECORATED);
63 CPPUNIT_ASSERT_EQUAL(QString("{bishop}xe4+"), dec.serialize(move, *m_state));
66 void ChessSerializationTest::test_promotion() {
67 m_state->board().set(Point(4, 7), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
68 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
69 m_state->board().set(Point(7, 1), ChessPiece(ChessPiece::WHITE, ChessPiece::PAWN));
71 ChessMove move(Point(7, 1), Point(7, 0), ChessPiece::ROOK);
72 CPPUNIT_ASSERT(m_check->legal(move));
74 ChessSerializer san(ChessSerializer::COMPACT);
75 CPPUNIT_ASSERT_EQUAL(QString("h8=R"), san.serialize(move, *m_state));
77 ChessSerializer simple(ChessSerializer::SIMPLE);
78 CPPUNIT_ASSERT_EQUAL(QString("h7h8=R"), simple.serialize(move, *m_state));
80 ChessSerializer dec(ChessSerializer::DECORATED);
81 CPPUNIT_ASSERT_EQUAL(QString("h8={rook}"), dec.serialize(move, *m_state));
84 void ChessSerializationTest::test_promotion_capture() {
85 m_state->board().set(Point(4, 7), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
86 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
87 m_state->board().set(Point(7, 1), ChessPiece(ChessPiece::WHITE, ChessPiece::PAWN));
88 m_state->board().set(Point(6, 0), ChessPiece(ChessPiece::BLACK, ChessPiece::BISHOP));
90 ChessMove move(Point(7, 1), Point(6, 0), ChessPiece::ROOK);
91 CPPUNIT_ASSERT(m_check->legal(move));
93 ChessSerializer san(ChessSerializer::COMPACT);
94 CPPUNIT_ASSERT_EQUAL(QString("hxg8=R"), san.serialize(move, *m_state));
96 ChessSerializer simple(ChessSerializer::SIMPLE);
97 CPPUNIT_ASSERT_EQUAL(QString("h7g8=R"), simple.serialize(move, *m_state));
99 ChessSerializer dec(ChessSerializer::DECORATED);
100 CPPUNIT_ASSERT_EQUAL(QString("hxg8={rook}"), dec.serialize(move, *m_state));
103 void ChessSerializationTest::test_promotion_check() {
104 m_state->board().set(Point(4, 0), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
105 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
106 m_state->board().set(Point(7, 1), ChessPiece(ChessPiece::WHITE, ChessPiece::PAWN));
108 ChessMove move(Point(7, 1), Point(7, 0), ChessPiece::ROOK);
109 CPPUNIT_ASSERT(m_check->legal(move));
111 ChessSerializer san(ChessSerializer::COMPACT);
112 CPPUNIT_ASSERT_EQUAL(QString("h8=R+"), san.serialize(move, *m_state));
114 ChessSerializer simple(ChessSerializer::SIMPLE);
115 CPPUNIT_ASSERT_EQUAL(QString("h7h8=R"), simple.serialize(move, *m_state));
117 ChessSerializer dec(ChessSerializer::DECORATED);
118 CPPUNIT_ASSERT_EQUAL(QString("h8={rook}+"), dec.serialize(move, *m_state));
121 void ChessSerializationTest::test_promotion_capture_check() {
122 m_state->board().set(Point(4, 0), ChessPiece(ChessPiece::BLACK, ChessPiece::KING));
123 m_state->board().set(Point(0, 0), ChessPiece(ChessPiece::WHITE, ChessPiece::KING));
124 m_state->board().set(Point(7, 1), ChessPiece(ChessPiece::WHITE, ChessPiece::PAWN));
125 m_state->board().set(Point(6, 0), ChessPiece(ChessPiece::BLACK, ChessPiece::BISHOP));
127 ChessMove move(Point(7, 1), Point(6, 0), ChessPiece::ROOK);
128 CPPUNIT_ASSERT(m_check->legal(move));
130 ChessSerializer san(ChessSerializer::COMPACT);
131 CPPUNIT_ASSERT_EQUAL(QString("hxg8=R+"), san.serialize(move, *m_state));
133 ChessSerializer simple(ChessSerializer::SIMPLE);
134 CPPUNIT_ASSERT_EQUAL(QString("h7g8=R"), simple.serialize(move, *m_state));
136 ChessSerializer dec(ChessSerializer::DECORATED);
137 CPPUNIT_ASSERT_EQUAL(QString("hxg8={rook}+"), dec.serialize(move, *m_state));
140 void ChessSerializationTest::test_castling_k() {
141 m_state->setup();
143 m_state->board().set(Point(5, 7), ChessPiece());
144 m_state->board().set(Point(6, 7), ChessPiece());
146 ChessMove move(Point(4, 7), Point(6, 7));
147 CPPUNIT_ASSERT(m_check->legal(move));
149 ChessSerializer san(ChessSerializer::COMPACT);
150 CPPUNIT_ASSERT_EQUAL(QString("O-O"), san.serialize(move, *m_state));
152 ChessSerializer simple(ChessSerializer::SIMPLE);
153 CPPUNIT_ASSERT_EQUAL(QString("e1g1"), simple.serialize(move, *m_state));
155 ChessSerializer dec(ChessSerializer::DECORATED);
156 CPPUNIT_ASSERT_EQUAL(QString("O-O"), dec.serialize(move, *m_state));
159 void ChessSerializationTest::test_castling_q() {
160 m_state->setup();
162 m_state->board().set(Point(3, 7), ChessPiece());
163 m_state->board().set(Point(2, 7), ChessPiece());
164 m_state->board().set(Point(1, 7), ChessPiece());
166 ChessMove move(Point(4, 7), Point(2, 7));
167 CPPUNIT_ASSERT(m_check->legal(move));
169 ChessSerializer san(ChessSerializer::COMPACT);
170 CPPUNIT_ASSERT_EQUAL(QString("O-O-O"), san.serialize(move, *m_state));
172 ChessSerializer simple(ChessSerializer::SIMPLE);
173 CPPUNIT_ASSERT_EQUAL(QString("e1c1"), simple.serialize(move, *m_state));
175 ChessSerializer dec(ChessSerializer::DECORATED);
176 CPPUNIT_ASSERT_EQUAL(QString("O-O-O"), dec.serialize(move, *m_state));
179 void ChessSerializationTest::regression_knight_king() {
180 m_state->setup();
182 ChessMove move(Point(6, 7), Point(5, 5));
183 CPPUNIT_ASSERT(m_check->legal(move));
185 ChessSerializer san(ChessSerializer::COMPACT);
186 CPPUNIT_ASSERT_EQUAL(QString("Nf3"), san.serialize(move, *m_state));
188 ChessSerializer simple(ChessSerializer::SIMPLE);
189 CPPUNIT_ASSERT_EQUAL(QString("g1f3"), simple.serialize(move, *m_state));
191 ChessSerializer dec(ChessSerializer::DECORATED);
192 CPPUNIT_ASSERT_EQUAL(QString("{knight}f3"), dec.serialize(move, *m_state));