-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
298 lines (252 loc) · 8.88 KB
/
tests.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import re
import unittest
import display
from hex_model import Color, Hex, HexBoard
import util
def _get_bump(line):
# "-" = 0
# " -" = 0
# " -" = 1
# " -" = 1
# " -" = 0
# "{3}" = 0
# " {3}" = 1
# " {3}" = 1
# " {3}" = 0
spaces, char = re.match(r"^( *)\S(\S?)", line).groups()
return bool((len(spaces) + bool(char)) % 4 // 2)
def _get_hex(text):
color = Color.black
if text == '-':
color = Color.yellow
elif text == 'x':
text = '-'
elif text == 'o':
text = '-'
color = Color.blue
elif 'o' in text:
text = text.strip('o')
color = Color.blue
return Hex(text, color)
def _board_from_string(string, **kwargs):
lines = string.split('\n')
leading_spaces = min(len(re.match(r'^ *', line).group(0)) for line in lines if line.strip())
lines = [line[leading_spaces:] for line in lines]
hex_lines = []
bump = next(_get_bump(line) + i % 2 for i, line in enumerate(lines) if line.rstrip())
for line in lines:
line = line.rstrip()
hex_line = []
while line:
spaces, text = re.match(r'^( *)(\S+)', line).groups()
line = line[len(spaces) + len(text):]
spaces = (len(spaces) + (len(text) > 1)) // 4
hex_line += [None] * spaces
hex_line.append(_get_hex(text))
hex_lines.append(hex_line)
board = HexBoard(**kwargs)
for y, line in enumerate(hex_lines):
for x, elem in enumerate(line):
if elem:
hx = x * 2 + ((y - bump) % 2)
hy = y // 2 - x + ((bump % 2) * (not (hx % 2)))
board[hx, hy] = elem
return board
def _split_on_arrow(board_strings):
lines = board_strings.split('\n')
arrow = next(line.index('=>') for line in lines if '=>' in line)
left_lines = '\n'.join(line[:arrow] for line in lines)
right_lines = '\n'.join(line[arrow + 2:] for line in lines)
return left_lines, right_lines
class HexMock:
def __init__(self, text, color):
self.text = text
self.color = color
class BoardReaderTest(unittest.TestCase):
@classmethod
def set_display_fn(cls, fn):
cls.display_fn = staticmethod(fn)
def test_structure1(self):
board = _board_from_string("""
1
2 3
4
5 6
-
""")
dx, dy, _ = next(coord for coord, hex_ in board._board.items() if hex_.text == '4')
disp = '\n{}'.format(self.display_fn(board))
mock = HexMock(None, None)
assert board.get((dx, dy - 1), mock).text == '1', disp
assert board.get((dx - 1, dy), mock).text == '2', disp
assert board.get((dx + 1, dy - 1), mock).text == '3', disp
assert board.get((dx, dy), mock).text == '4', disp
assert board.get((dx - 1, dy + 1), mock).text == '5', disp
assert board.get((dx + 1, dy), mock).text == '6', disp
assert board.get((dx, dy + 1), mock).color == Color.yellow, disp
def test_structure2(self):
board = _board_from_string("""
x
1
2 3
4
5 6
-
""")
dx, dy, _ = next(coord for coord, hex_ in board._board.items() if hex_.text == '4')
disp = '\n{}'.format(self.display_fn(board))
mock = HexMock(None, None)
assert board.get((dx - 1, dy - 1), mock).color == Color.black, disp
assert board.get((dx, dy - 1), mock).text == '1', disp
assert board.get((dx - 1, dy), mock).text == '2', disp
assert board.get((dx + 1, dy - 1), mock).text == '3', disp
assert board.get((dx, dy), mock).text == '4', disp
assert board.get((dx - 1, dy + 1), mock).text == '5', disp
assert board.get((dx + 1, dy), mock).text == '6', disp
assert board.get((dx, dy + 1), mock).color == Color.yellow, disp
def test_leader_structure1(self):
board = _board_from_string("""
3 1
2
""")
dx, dy, _ = next(coord for coord, hex_ in board._board.items() if hex_.text == '1')
disp = '\n{}'.format(self.display_fn(board))
mock = HexMock(None, None)
assert board.get((dx - 2, dy + 1), mock).text == '3', disp
assert board.get((dx, dy), mock).text == '1', disp
assert board.get((dx, dy + 1), mock).text == '2', disp
def test_leader_structure2(self):
board = _board_from_string("""
3 1
{2}
""")
dx, dy, _ = next(coord for coord, hex_ in board._board.items() if hex_.text == '1')
disp = '\n{}'.format(self.display_fn(board))
mock = HexMock(None, None)
assert board.get((dx - 2, dy + 1), mock).text == '3', disp
assert board.get((dx, dy), mock).text == '1', disp
assert board.get((dx, dy + 1), mock).text == '{2}', disp
class SolverUnitTest(unittest.TestCase):
@classmethod
def set_display_fn(cls, fn):
cls.display_fn = staticmethod(fn)
def assertColor(self, coord, expected_color):
color = self.solved_board[coord].color
assert color == expected_color, "Found {} at {}, expected {}.\n{}\n\n{}".format(
color,
coord,
expected_color,
self.original,
self.display_fn(self.solved_board),
)
def assertSolve(self, start_string, expected_string=None, **kwargs):
if expected_string is None:
start_string, expected_string = _split_on_arrow(start_string)
self.board = _board_from_string(start_string, **kwargs)
self.expected = _board_from_string(expected_string)
assert set(self.board._board) == set(self.expected._board), (
"Board shapes must match.\n{}\n\n{}".format(
self.display_fn(self.board),
self.display_fn(self.expected),
)
)
list(self.board.solve())
self.board.apply_clicked()
for coord, hex_ in self.expected._board.items():
assert self.board[coord].color == hex_.color, (
"Solved board does not match expected.\nExpected:\n{}\n\nActual:\n{}".format(
self.display_fn(self.expected),
self.display_fn(self.board),
)
)
def test_noncontiguous_spot(self):
self.assertSolve("""
x x
- - o -
-3- => -3-
x - x -
- -
""")
def test_noncontiguous_center(self):
self.assertSolve("""
- x
o o o o
-3- => -3-
- - - -
- -
""")
def test_noncontiguous(self):
self.assertSolve("""
o o
- - x x
-2- => -2-
- - - -
- -
""")
def test_noncontiguous_edges(self):
self.assertSolve("""
- o
x - x -
-3- => -3-
x - x -
- o
""")
def test_contiguous_cant_fit(self):
self.assertSolve("""
x x
- - x -
{2} => {2}
x - x o
- -
""")
def test_noncontiguous_no_info(self):
self.assertSolve("""
x x
- - - -
-2- => -2-
x - x -
- -
""")
def test_noncontiguous_one_option(self):
self.assertSolve("""
x x
- - o x
-2- => -2-
x o x o
- x
""")
def test_infinite_loop(self):
try:
with util.Timeout(seconds=2):
self.assertSolve("""
- - - -
1 2 => 1 2
- - - -
""")
except TimeoutError:
assert False, 'Timeout!\n{}'.format(self.display_fn(self.board))
def test_known_remaining(self):
self.assertSolve("""
- - x x
- - - x o x
2 2 => 2 2
- - - x o x
- - x x
""", remaining=2)
def test_no_remaining(self):
self.assertSolve("""
- - x x
- - - x x x
- - => x x
- - - x x x
- - x x
""", remaining=0)
def run_tests(display_fn):
loader = unittest.TestLoader()
SolverUnitTest.set_display_fn(display_fn)
suite = loader.loadTestsFromTestCase(SolverUnitTest)
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
SolverUnitTest.set_display_fn(display.display_full_board)
BoardReaderTest.set_display_fn(display.display_full_board)
unittest.main()