Add a PromotionManager.
[tagua/yd.git] / tests / variants / chessgamestatetest.cpp
blob13a07dc842f210c72cdb5ce8710f8e053db8a861
1 #include "chessgamestatetest.h"
2 #include <board.h>
3 #include <color.h>
4 #include <defaultstate.h>
5 #include <move.h>
6 #include <piece.h>
7 #include <statefactory.h>
8 #include <types/chess/pawn.h>
9 #include <types/chess/king.h>
10 #include <types/chess/queen.h>
11 #include <types/chess/rook.h>
12 #include <types/chess/knight.h>
13 #include <types/chess/bishop.h>
14 #include <variant.h>
16 #include "test_utils.h"
18 CPPUNIT_TEST_SUITE_REGISTRATION(ChessGameStateTest);
20 Variant* chess_variant_factory();
22 using namespace Chess;
24 void ChessGameStateTest::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_state->setup();
32 void ChessGameStateTest::tearDown() {
33 delete m_state;
34 delete m_chess;
37 void ChessGameStateTest::test_setup() {
38 for (int i = 0; i < 8; i++) {
39 CPPUNIT_ASSERT_EQUAL(
40 Piece(Black::self(), Pawn::self()),
41 m_state->board()->get(Point(i, 1)));
42 CPPUNIT_ASSERT_EQUAL(
43 Piece(White::self(), Pawn::self()),
44 m_state->board()->get(Point(i, 6)));
45 CPPUNIT_ASSERT_EQUAL(Piece(),
46 m_state->board()->get(Point(i, 4)));
49 CPPUNIT_ASSERT_EQUAL(
50 Piece(Black::self(), Rook::self()),
51 m_state->board()->get(Point(0, 0)));
52 CPPUNIT_ASSERT_EQUAL(
53 Piece(White::self(), King::self()),
54 m_state->board()->get(Point(4, 7)));
57 void ChessGameStateTest::test_simple_move() {
58 m_state->move(Move(Point(4, 6), Point(4, 5))); // e3
59 CPPUNIT_ASSERT_EQUAL(Piece(),
60 m_state->board()->get(Point(4, 6)));
61 CPPUNIT_ASSERT_EQUAL(
62 Piece(White::self(), Pawn::self()),
63 m_state->board()->get(Point(4, 5)));
66 void ChessGameStateTest::test_capture() {
67 m_state->move(Move(Point(4, 6), Point(4, 4))); // e4
68 m_state->move(Move(Point(3, 1), Point(3, 3))); // d5
69 m_state->move(Move(Point(4, 4), Point(3, 3))); // exd5
71 CPPUNIT_ASSERT_EQUAL(
72 Piece(White::self(), Pawn::self()),
73 m_state->board()->get(Point(3, 3)));
76 void ChessGameStateTest::test_en_passant() {
77 m_state->move(Move(Point(4, 6), Point(4, 4))); // e4
78 m_state->move(Move(Point(7, 1), Point(7, 2))); // h6
79 m_state->move(Move(Point(4, 4), Point(4, 3))); // e5
81 Move d5(Point(3, 1), Point(3, 3));
82 d5.setType("en_passant_trigger");
83 m_state->move(d5);
85 CPPUNIT_ASSERT_EQUAL(
86 Point(3, 2),
87 m_state->flags().get("en_passant_square").value<Point>());
89 Move exd6(Point(4, 3), Point(3, 2));
90 exd6.setType("en_passant_capture");
91 m_state->move(exd6);
93 CPPUNIT_ASSERT_EQUAL(Piece(),
94 m_state->board()->get(Point(3, 3)));
97 void ChessGameStateTest::test_kingside_castling() {
98 Move oo(Point(4, 7), Point(6, 7));
99 oo.setType("king_side_castling");
100 m_state->move(oo);
102 CPPUNIT_ASSERT_EQUAL(
103 Piece(White::self(), King::self()),
104 m_state->board()->get(Point(6, 7)));
105 CPPUNIT_ASSERT_EQUAL(
106 Piece(White::self(), Rook::self()),
107 m_state->board()->get(Point(5, 7)));
108 CPPUNIT_ASSERT_EQUAL(Piece(),
109 m_state->board()->get(Point(7, 7)));
112 void ChessGameStateTest::test_queenside_castling() {
113 Move oo(Point(4, 7), Point(2, 7));
114 oo.setType("queen_side_castling");
115 m_state->move(oo);
117 CPPUNIT_ASSERT_EQUAL(
118 Piece(White::self(), King::self()),
119 m_state->board()->get(Point(2, 7)));
120 CPPUNIT_ASSERT_EQUAL(
121 Piece(White::self(), Rook::self()),
122 m_state->board()->get(Point(3, 7)));
123 CPPUNIT_ASSERT_EQUAL(Piece(),
124 m_state->board()->get(Point(0, 7)));
127 void ChessGameStateTest::test_promotion() {
128 m_state->board()->set(Point(7, 1), Piece(White::self(), Pawn::self()));
129 Move h8B(Point(7, 1), Point(7, 0), Bishop::self());
130 m_state->move(h8B);
132 CPPUNIT_ASSERT_EQUAL(
133 Piece(White::self(), Bishop::self()),
134 m_state->board()->get(Point(7, 0)));