forked from gbtami/fairyfishnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·156 lines (121 loc) · 4.35 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of the lichess.org fishnet client.
# Copyright (C) 2016-2019 Niklas Fiekas <[email protected]>
# See LICENSE.txt for licensing information.
import fairyfishnet
import unittest
import sys
import multiprocessing
try:
import configparser
except ImportError:
import ConfigParser as configparser
STARTPOS = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
class WorkerTest(unittest.TestCase):
def setUp(self):
conf = configparser.ConfigParser()
conf.add_section("Fishnet")
conf.set("Fishnet", "Key", "testkey")
fairyfishnet.get_stockfish_command(conf, update=True)
self.worker = fairyfishnet.Worker(conf,
threads=multiprocessing.cpu_count(),
memory=32,
progress_reporter=None)
self.worker.start_stockfish()
def tearDown(self):
self.worker.stop()
def test_bestmove(self):
job = {
"work": {
"type": "move",
"id": "abcdefgh",
"level": 8,
},
"game_id": "hgfedcba",
"variant": "chess",
"position": STARTPOS,
"moves": "f2f3 e7e6 g2g4",
}
response = self.worker.bestmove(job)
self.assertEqual(response["move"]["bestmove"], "d8h4")
def test_zh_bestmove(self):
job = {
"work": {
"type": "move",
"id": "hihihihi",
"level": 1,
},
"game_id": "ihihihih",
"variant": "crazyhouse",
"position": "rnbqk1nr/ppp2ppp/3b4/3N4/4p1PP/5P2/PPPPP3/R1BQKBNR[P] b KQkq - 9 5",
"moves": "d6g3",
}
response = self.worker.bestmove(job)
self.assertEqual(response["move"]["bestmove"], "P@f2") # only move
def xxxtest_3check_bestmove(self):
job = {
"work": {
"type": "move",
"id": "3c3c3c3c",
"level": 8,
},
"game_id": "c3c3c3c3",
"variant": "3check",
"position": "r1b1kbnr/pppp1ppp/2n2q2/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 4 4 +2+0",
"moves": "f1c4 d7d6",
}
response = self.worker.bestmove(job)
self.assertEqual(response["move"]["bestmove"], "c4f7")
def test_analysis(self):
job = {
"work": {
"type": "analysis",
"id": "12345678",
},
"game_id": "87654321",
"variant": "chess",
"position": STARTPOS,
"moves": "f2f3 e7e6 g2g4 d8h4",
"skipPositions": [1],
}
response = self.worker.analysis(job)
result = response["analysis"]
self.assertTrue(0 <= result[0]["score"]["cp"] <= 90)
self.assertTrue(result[1]["skipped"])
self.assertEqual(result[3]["score"]["mate"], 1)
self.assertTrue(result[3]["pv"].startswith("d8h4"))
self.assertEqual(result[4]["score"]["mate"], 0)
def test_analysis_contempt(self):
fairyfishnet.setoption(self.worker.stockfish, "Threads", 1)
job = {
"work": {
"type": "analysis",
"id": "contempt 100",
},
"variant": "chess",
"position": STARTPOS,
"moves": "d2d4 d7d5",
"skipPositions": [0, 1],
"nodes": 1000,
}
fairyfishnet.setoption(self.worker.stockfish, "Contempt", 100)
response = self.worker.analysis(job)
cp_100 = response["analysis"][2]["score"]["cp"]
job["work"]["id"] = "contempt 0"
fairyfishnet.setoption(self.worker.stockfish, "Contempt", 0)
response = self.worker.analysis(job)
cp_0 = response["analysis"][2]["score"]["cp"]
self.assertEqual(cp_100, cp_0)
class UnitTests(unittest.TestCase):
def test_parse_bool(self):
self.assertEqual(fairyfishnet.parse_bool("yes"), True)
self.assertEqual(fairyfishnet.parse_bool("no"), False)
self.assertEqual(fairyfishnet.parse_bool(""), False)
self.assertEqual(fairyfishnet.parse_bool("", default=True), True)
if __name__ == "__main__":
if "-v" in sys.argv or "--verbose" in sys.argv:
fairyfishnet.setup_logging(3)
else:
fairyfishnet.setup_logging(0)
unittest.main()