Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

schach / tests / test_think.py · 1.6 KB · at 3c63b1d52560 · raw

Outline: 5 definitions
1
import shutil
2
import threading
3
import unittest
4
5
import chess
6
import chess.engine
7
8
from schach import annotate
9
10
MATE_IN_ONE = "rn2kb1r/p4ppp/2p5/Bp2p3/4n1b1/1B3N2/PPP2PPP/RN1QR1K1 w kq - 0 11"  # Dd8#
11
12
13
@unittest.skipUnless(shutil.which("stockfish"), "stockfish nicht installiert")
14
class ThinkJobTest(unittest.TestCase):
15
    def think(self, fen, until):
16
        """think_job laufen lassen, bis ``until(hints)`` gilt; gibt die Hinweise zurück."""
17
        engine = chess.engine.SimpleEngine.popen_uci("stockfish")
18
        thinking, hints, done = annotate.Thinking(), [], threading.Event()
19
20
        def on_hint(move, final):
21
            hints.append((move, final))
22
            if until(hints):
23
                thinking.stop()
24
                done.set()
25
        try:
26
            worker = threading.Thread(target=annotate.think_job(chess.Board(fen), thinking, lambda e: None,
27
                                                                on_hint), args=(engine,))
28
            worker.start()
29
            done.wait(30)
30
            thinking.stop()
31
            worker.join()
32
        finally:
33
            engine.quit()
34
        return hints
35
36
    def test_mate_is_final_at_once(self):
37
        hints = self.think(MATE_IN_ONE, lambda h: True)
38
        self.assertEqual(hints[0], (chess.Move.from_uci("d1d8"), True))
39
40
    def test_hint_is_refined_until_final(self):
41
        hints = self.think(chess.STARTING_FEN, lambda h: h[-1][1])
42
        self.assertFalse(hints[0][1])  # erst vorläufig …
43
        self.assertTrue(hints[-1][1])  # … dann endgültig
44
        self.assertEqual(sum(final for _m, final in hints), 1)
45
46
47
if __name__ == "__main__":
48
    unittest.main()