Debian Codegraph @

old-sample/legacy-checkmate/test/dualCheckmateSearcher.t.cc

raw
1 /* dualCheckmateSearcher.t.cc
2 */
3 #include "osl/checkmate/dualCheckmateSearcher.h"
4 #include "osl/checkmate/analyzer/checkTableAnalyzer.h"
5 #include "osl/state/hashEffectState.h"
6 #include "osl/record/csaString.h"
7 #include "osl/apply_move/applyMove.h"
8
9 #include <cppunit/TestCase.h>
10 #include <cppunit/extensions/HelperMacros.h>
11
12 #include <iostream>
13
14 class DualCheckmateSearcherTest : public CppUnit::TestFixture
15 {
16 CPPUNIT_TEST_SUITE(DualCheckmateSearcherTest);
17 CPPUNIT_TEST(testCheck);
18 CPPUNIT_TEST(testDistance);
19 CPPUNIT_TEST(testInfty);
20 CPPUNIT_TEST(testInfty2);
21 CPPUNIT_TEST(testNoCheckmate);
22 CPPUNIT_TEST(testPawnCheckmate);
23 CPPUNIT_TEST(testPawnCheckmateDefense);
24 CPPUNIT_TEST(testCopy);
25 CPPUNIT_TEST_SUITE_END();
26 public:
27 void testDistance();
28 void testCheck();
29 void testInfty();
30 void testInfty2();
31 void testNoCheckmate();
32 void testPawnCheckmate();
33 void testPawnCheckmateDefense();
34 void testCopy();
35 };
36 CPPUNIT_TEST_SUITE_REGISTRATION(DualCheckmateSearcherTest);
37
38 using namespace osl;
39 using namespace osl::checkmate;
40 extern bool isShortTest;
41 const size_t limit = 400000;
42
43 void DualCheckmateSearcherTest::testNoCheckmate()
44 {
45 {
46 HashEffectState state(CsaString(
47 "P1 * * * * -OU * * * * \n"
48 "P2 * * * * * +GI * * * \n"
49 "P3 * * * * * * * * * \n"
50 "P4 * * * * * * * * * \n"
51 "P5 * * * * * * * * * \n"
52 "P6-HI-HI-KA-KA * * * * +KY\n"
53 "P7-FU-GI * +GI * * * * +KY\n"
54 "P8 * -FU * * * * * * +KY\n"
55 "P9+OU * +FU+FU+FU+FU+FU * +KY\n"
56 "P+00KI00KI00GI00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU00FU\n"
57 "P-00KI00KI00KE00KE00KE00KE\n"
58 "-\n").getInitialState());
59 // 98金の一手詰ではあるけど
60 DualCheckmateSearcher<> checker(limit);
61 Move check_move = Move::INVALID();
62 const PathEncoding path(WHITE, 3);
63 AttackOracleAges oracle_age;
64 const bool win = checker.isWinningState<WHITE>
65 (10, state, state.getHash(), path, check_move, oracle_age);
66 CPPUNIT_ASSERT_EQUAL(false, win);
67 }
68 }
69
70 void DualCheckmateSearcherTest::testDistance()
71 {
72 // bug 35
73 SimpleState state=CsaString(
74 "P1 * * * * * * * -OU * \n"
75 "P2 * * * * * * * * * \n"
76 "P3 * * * * * * * +FU * \n"
77 "P4 * * * * * * * * * \n"
78 "P5 * * * * * * * * * \n"
79 "P6 * * * * * * * * * \n"
80 "P7 * * * * * * * * * \n"
81 "P8 * * * * * * * * * \n"
82 "P9 * * +OU * * * * * * \n"
83 "P+00KI\n"
84 "P-00AL\n"
85 "+\n").getInitialState();
86 HashEffectState hState(state);
87 DualCheckmateSearcher<> checker(limit);
88 if (! isShortTest)
89 checker.setVerbose();
90 Move check_move = Move::INVALID();
91 const int distance = 3;
92 const PathEncoding path(BLACK, distance);
93 AttackOracleAges oracle_age;
94 const bool win = checker.isWinningState<BLACK>
95 (10, hState, hState.getHash(), path, check_move, oracle_age);
96 CPPUNIT_ASSERT_EQUAL(true, win);
97 CPPUNIT_ASSERT_EQUAL(Move(Square(2,2),GOLD,BLACK), check_move);
98
99 const CheckHashTable& table = checker.getTable(BLACK);
100 const CheckHashRecord *record = table.find(hState.getHash());
101 CPPUNIT_ASSERT_EQUAL(distance, static_cast<int>(record->distance));
102 }
103
104 void DualCheckmateSearcherTest::testPawnCheckmate()
105 {
106 // 逃げられないので打歩詰 (bug 33)
107 SimpleState stateNoEscape=CsaString(
108 "P1 * * * * * * * -KE-KY\n"
109 "P2 * * * * * * * -OU-KE\n"
110 "P3 * * * * * * * +FU-FU\n"
111 "P4 * * * * * * +RY * * \n"
112 "P5 * * * * * * * * * \n"
113 "P6 * * * * * * * * * \n"
114 "P7 * * * * * * * * * \n"
115 "P8 * * * * * * * * * \n"
116 "P9 * * +OU * * * * * * \n"
117 "P+00KI\n"
118 "P-00AL\n"
119 "-\n").getInitialState();
120 HashEffectState hStateNoEscape(stateNoEscape);
121 const Move last_move = Move(Square(2,3), PAWN, BLACK);
122 DualCheckmateSearcher<> checker(limit);
123 checker.setVerbose(!isShortTest);
124 const PathEncoding path(WHITE);
125 CPPUNIT_ASSERT_EQUAL(false,
126 checker.isLosingState<WHITE>(500, hStateNoEscape, hStateNoEscape.getHash(),
127 path, last_move));
128 }
129
130 void DualCheckmateSearcherTest::testPawnCheckmateDefense()
131 {
132 HashEffectState state(CsaString(
133 "P1+KA * * -KI * * * * * \n"
134 "P2 * * * -KI * * * * * \n"
135 "P3 * -FU+KY-OU-UM+RY-FU-FU * \n"
136 "P4-FU * * -FU-FU * * * * \n"
137 "P5 * +FU-FU * * -FU * +FU * \n"
138 "P6+FU+OU+FU+FU+FU * * * * \n"
139 "P7 * * * * +GI+FU+KE * * \n"
140 "P8 * * +GI+KI * * * * * \n"
141 "P9+KY * * +KI * * * * * \n"
142 "P+00GI00GI00KY00FU\n"
143 "P-00HI00KE00KE00KE00KY00FU00FU\n"
144 "-\n").getInitialState());
145 DualCheckmateSearcher<> checker(limit);
146 const PathEncoding path(WHITE);
147 Move check_move;
148 checker.isWinningState(500, state, state.getHash(),
149 path, check_move);
150 // 動かすだけ 途中で攻め方を打ち歩詰めにする王手回避が出現
151 }
152
153 /**
154 */
155 void DualCheckmateSearcherTest::testCheck()
156 {
157 SimpleState state=CsaString(
158 "P1+NY+TO * * * * -OU-KE-KY\n"
159 "P2 * * * * * -GI-KI * *\n"
160 "P3 * +RY * * +UM * -KI-FU-FU\n"
161 "P4 * * +FU-FU * * * * *\n"
162 "P5 * * -KE * +FU * * +FU *\n"
163 "P6-KE * * +FU+GI-FU * * +FU\n"
164 "P7 * * -UM * * * * * *\n"
165 "P8 * * * * * * * * * \n"
166 "P9 * +OU * -GI * * * * -NG\n"
167 "P+00HI00KI00KE00KY00FU00FU00FU00FU00FU00FU\n"
168 "P-00KI00KY00FU00FU\n"
169 "P-00AL\n"
170 "+\n").getInitialState();
171
172 ApplyMoveOfTurn::doMove(state, Move(Square(9,2),PAWN,BLACK));
173 HashEffectState hState(state);
174
175 size_t rootTreeSize = 0;
176 AttackOracleAges oracle_age;
177 {
178 DualCheckmateSearcher<> checker(limit);
179 checker.setVerbose(!isShortTest);
180 Move checkmate_move = Move::INVALID();
181 const PathEncoding path(WHITE);
182 // 3 手詰なのですぐに詰めてほしいところ
183 const bool win = checker.isWinningState<WHITE>
184 (70, hState, hState.getHash(), path, checkmate_move, oracle_age);
185 CPPUNIT_ASSERT(win);
186 CPPUNIT_ASSERT(! checkmate_move.isPass());
187 const CheckHashTable& table = checker.getTable(WHITE);
188 CheckTableAnalyzer analyzer(table.getTwinTable());
189 const HashKey& key = hState.getHash();
190 const CheckHashRecord *record = table.find(key);
191 rootTreeSize = analyzer.treeSize(record);
192 CPPUNIT_ASSERT_EQUAL(table.size(), rootTreeSize);
193 CPPUNIT_ASSERT(analyzer.proofTreeSize(record,key,path,true)
194 <= rootTreeSize);
195 if (! isShortTest) {
196 analyzer.showProofTree(record,key,path,true,std::cerr);
197 std::cerr << analyzer.proofTreeSize(record,key,path,true)
198 << " < " << rootTreeSize << "\n";
199 }
200 }
201
202 {
203 // 一手進めばすぐ詰む
204 ApplyMoveOfTurn::doMove(hState, Move(Square(8,8),GOLD,WHITE));
205 DualCheckmateSearcher<> checker(limit);
206 checker.setVerbose(!isShortTest);
207 const PathEncoding path(BLACK);
208 const bool lose = checker.isLosingState<BLACK>(10, hState, hState.getHash(), path);
209 CPPUNIT_ASSERT(lose);
210 const CheckHashTable& table = checker.getTable(WHITE);
211 CheckTableAnalyzer analyzer(table.getTwinTable());
212 const HashKey& key = hState.getHash();
213 const CheckHashRecord *record = table.find(key);
214 CPPUNIT_ASSERT(analyzer.treeSize(record) < rootTreeSize);
215 // analyzer.showTree(record, std::cerr, 10);
216 if (! isShortTest)
217 analyzer.showProofTree(record,key,path,false,std::cerr);
218 CPPUNIT_ASSERT(analyzer.proofTreeSize(record,key,path,false)
219 <= analyzer.treeSize(record));
220 }
221 }
222
223 void DualCheckmateSearcherTest::testInfty()
224 {
225 SimpleState state=CsaString(
226 "P1-KY * -KI-KE * * * * +UM\n"
227 "P2 * * * * -OU * -GI * * \n"
228 "P3-FU * -KE-FU-FU-FU * -KA-FU\n"
229 "P4 * -HI-FU * * * * * * \n"
230 "P5 * * +KI * * +FU * * * \n"
231 "P6 * * * +FU+KI * * * * \n"
232 "P7+FU * +OU * +FU * +KI+GI+FU\n"
233 "P8-TO * * * * * * * * \n"
234 "P9 * * * * * * * * +KY\n"
235 "P-00FU\n"
236 "P-00FU\n"
237 "P-00FU\n"
238 "P-00FU\n"
239 "P-00FU\n"
240 "P-00FU\n"
241 "P+00KE\n"
242 "P+00KE\n"
243 "P-00GI\n"
244 "P+00GI\n"
245 "P+00KY\n"
246 "P+00KY\n"
247 "P+00HI\n"
248 "-\n").getInitialState();
249 HashEffectState hState(state);
250 const PathEncoding path(WHITE);
251 DualCheckmateSearcher<> checker(limit);
252 checker.setVerbose(!isShortTest);
253 Move checkmate_move = Move::INVALID();
254 AttackOracleAges oracle_age;
255 const bool win = checker.isWinningState<WHITE>
256 (400, hState, hState.getHash(), path, checkmate_move, oracle_age);
257 CPPUNIT_ASSERT(win);
258 }
259
260 void DualCheckmateSearcherTest::testInfty2()
261 {
262 SimpleState state=CsaString(
263 "P1-KY-KE * * * -OU * -KE+KA\n"
264 "P2 * * +FU * -KI * * * * \n"
265 "P3-FU * * -FU-GI-KI * -FU-FU\n"
266 "P4 * * * * -FU * * * * \n"
267 "P5 * +GI * +FU * -FU * * * \n"
268 "P6 * * * * +FU * * * * \n"
269 "P7+FU+FU * +KI * +FU * +FU+FU\n"
270 "P8 * +OU-NY * * * * * -RY\n"
271 "P9+KY+KE * +KI * * +FU * * \n"
272 "P+00FU\n"
273 "P+00FU\n"
274 "P-00FU\n"
275 "P-00KE\n"
276 "P+00GI\n"
277 "P+00GI\n"
278 "P-00KY\n"
279 "P+00KA\n"
280 "P-00HI\n"
281 "+\n").getInitialState();
282
283 HashEffectState hState((NumEffectState(state)));
284 const PathEncoding path(BLACK);
285 DualCheckmateSearcher<> checker(limit);
286 checker.setVerbose(!isShortTest);
287 const bool lose = checker.isLosingState<BLACK>(100, hState, hState.getHash(), path);
288 CPPUNIT_ASSERT(! lose);
289 }
290
291 void DualCheckmateSearcherTest::testCopy()
292 {
293 // should have different table on copy
294 DualCheckmateSearcher<> searcher(limit);
295 DualCheckmateSearcher<> searcher2(searcher);
296 CPPUNIT_ASSERT(&searcher.getTable(BLACK) != &searcher2.getTable(BLACK));
297 #if 0
298 // currently not supported
299 DualCheckmateSearcher<> searcher3(limit);
300 searcher3 = searcher;
301 #endif
302 }
303
304
305 /* ------------------------------------------------------------------------- */
306 // ;;; Local Variables:
307 // ;;; mode:c++
308 // ;;; c-basic-offset:2
309 // ;;; End: