1 /********************************************************************
3 * Copyright (C) 2008 Daniele Battaglia
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
22 #include "GameLoopTest.h"
23 #include "GameMatrix.h"
25 void GameLoopTest::initTestCase()
27 qRegisterMetaType
<Move
>();
28 qRegisterMetaType
< QList
<int> >();
31 void GameLoopTest::init()
33 GameMatrix::create(5, 1, 2);
34 _players
.append(new PlayerStub(0));
35 _players
.append(new PlayerStub(1));
36 _gameLoop
= new GameLoop(_players
);
37 _movedSpy
= new QSignalSpy(_gameLoop
, SIGNAL(moved(Move
)));
38 _turnSpy
= new QSignalSpy(_gameLoop
, SIGNAL(turn(int)));
39 _winSpy
= new QSignalSpy(_gameLoop
, SIGNAL(win(int)));
40 _drawSpy
= new QSignalSpy(_gameLoop
, SIGNAL(draw(QList
<int>)));
43 QVERIFY(_movedSpy
->isValid() == true);
44 QVERIFY(_turnSpy
->isValid() == true);
45 QVERIFY(_winSpy
->isValid() == true);
46 QVERIFY(_drawSpy
->isValid() == true);
47 QCOMPARE(_movedSpy
->count(), 0);
48 QCOMPARE(_turnSpy
->count(), 0);
49 QCOMPARE(_winSpy
->count(), 0);
50 QCOMPARE(_drawSpy
->count(), 0);
53 void GameLoopTest::cleanup()
55 GameMatrix::destroy();
56 _gameLoop
->deleteLater();
65 void GameLoopTest::gameLoop()
68 QVERIFY(_gameLoop
->_currentPlayer
== _players
[0]);
69 QCOMPARE(_gameLoop
->_winStatus
.size(), 2);
70 QVERIFY(_gameLoop
->_winStatus
[0] == false);
71 QVERIFY(_gameLoop
->_winStatus
[1] == false);
74 void GameLoopTest::setTurn()
77 _gameLoop
->setTurn(1);
79 QVERIFY(_gameLoop
->_currentPlayer
== _players
[1]);
82 void GameLoopTest::runWin()
87 list0
.append(Point(0, 0, 0));
88 list1
.append(Point(1, 1, 1));
89 list0
.append(Point(1, 0, 0));
90 list1
.append(Point(2, 2, 2));
91 list0
.append(Point(2, 0, 0));
92 list1
.append(Point(3, 3, 3));
93 list0
.append(Point(3, 0, 0));
94 list1
.append(Point(4, 4, 4));
95 list0
.append(Point(4, 0, 0));
96 list1
.append(Point(4, 3, 3));
97 dynamic_cast<PlayerStub
*>(_players
[0])->setPoints(list0
);
98 dynamic_cast<PlayerStub
*>(_players
[1])->setPoints(list1
);
99 _players
[0]->moveToThread(_gameLoop
);
100 _players
[1]->moveToThread(_gameLoop
);
104 QVERIFY(_gameLoop
->wait() == true);
105 QCOMPARE(_turnSpy
->count(), 10);
106 QCOMPARE(_movedSpy
->count(), 10);
107 QCOMPARE(_winSpy
->count(), 1);
108 QCOMPARE(_winSpy
->at(0).at(0).toInt(), 0);
109 QCOMPARE(_drawSpy
->count(), 0);
110 QCOMPARE(_turnSpy
->at(0).at(0).toInt(), 0);
111 QCOMPARE(_turnSpy
->at(1).at(0).toInt(), 1);
112 QCOMPARE(_turnSpy
->at(2).at(0).toInt(), 0);
113 QCOMPARE(_turnSpy
->at(3).at(0).toInt(), 1);
114 QCOMPARE(_turnSpy
->at(4).at(0).toInt(), 0);
115 QCOMPARE(_turnSpy
->at(5).at(0).toInt(), 1);
116 QCOMPARE(_turnSpy
->at(6).at(0).toInt(), 0);
117 QCOMPARE(_turnSpy
->at(7).at(0).toInt(), 1);
118 QCOMPARE(_turnSpy
->at(8).at(0).toInt(), 0);
119 QCOMPARE(_turnSpy
->at(9).at(0).toInt(), 1);
120 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(0).at(0)).point(), Point(0, 0, 0));
121 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(1).at(0)).point(), Point(1, 1, 1));
122 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(2).at(0)).point(), Point(1, 0, 0));
123 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(3).at(0)).point(), Point(2, 2, 2));
124 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(4).at(0)).point(), Point(2, 0, 0));
125 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(5).at(0)).point(), Point(3, 3, 3));
126 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(6).at(0)).point(), Point(3, 0, 0));
127 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(7).at(0)).point(), Point(4, 4, 4));
128 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(8).at(0)).point(), Point(4, 0, 0));
129 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(9).at(0)).point(), Point(4, 3, 3));
132 void GameLoopTest::runDraw()
137 list0
.append(Point(0, 0, 0));
138 list1
.append(Point(1, 0, 1));
139 list0
.append(Point(1, 0, 0));
140 list1
.append(Point(1, 1, 1));
141 list0
.append(Point(2, 0, 0));
142 list1
.append(Point(1, 2, 1));
143 list0
.append(Point(3, 0, 0));
144 list1
.append(Point(1, 3, 1));
145 list0
.append(Point(4, 0, 0));
146 list1
.append(Point(1, 4, 1));
147 dynamic_cast<PlayerStub
*>(_players
[0])->setPoints(list0
);
148 dynamic_cast<PlayerStub
*>(_players
[1])->setPoints(list1
);
149 _players
[0]->moveToThread(_gameLoop
);
150 _players
[1]->moveToThread(_gameLoop
);
154 QVERIFY(_gameLoop
->wait() == true);
155 QCOMPARE(_turnSpy
->count(), 10);
156 QCOMPARE(_movedSpy
->count(), 10);
157 QCOMPARE(_winSpy
->count(), 0);
158 QCOMPARE(_drawSpy
->count(), 1);
159 QCOMPARE(_turnSpy
->at(0).at(0).toInt(), 0);
160 QCOMPARE(_turnSpy
->at(1).at(0).toInt(), 1);
161 QCOMPARE(_turnSpy
->at(2).at(0).toInt(), 0);
162 QCOMPARE(_turnSpy
->at(3).at(0).toInt(), 1);
163 QCOMPARE(_turnSpy
->at(4).at(0).toInt(), 0);
164 QCOMPARE(_turnSpy
->at(5).at(0).toInt(), 1);
165 QCOMPARE(_turnSpy
->at(6).at(0).toInt(), 0);
166 QCOMPARE(_turnSpy
->at(7).at(0).toInt(), 1);
167 QCOMPARE(_turnSpy
->at(8).at(0).toInt(), 0);
168 QCOMPARE(_turnSpy
->at(9).at(0).toInt(), 1);
169 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(0).at(0)).point(), Point(0, 0, 0));
170 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(1).at(0)).point(), Point(1, 0, 1));
171 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(2).at(0)).point(), Point(1, 0, 0));
172 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(3).at(0)).point(), Point(1, 1, 1));
173 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(4).at(0)).point(), Point(2, 0, 0));
174 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(5).at(0)).point(), Point(1, 2, 1));
175 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(6).at(0)).point(), Point(3, 0, 0));
176 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(7).at(0)).point(), Point(1, 3, 1));
177 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(8).at(0)).point(), Point(4, 0, 0));
178 QCOMPARE(qvariant_cast
<Move
>(_movedSpy
->at(9).at(0)).point(), Point(1, 4, 1));
181 QTEST_MAIN(GameLoopTest
)