-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_adding_puzzle.py
240 lines (195 loc) · 4.34 KB
/
test_adding_puzzle.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
from unittest import TestCase
from adding_puzzle import AddingBoardGraph
from domino_puzzle import MoveDescription
from queued_board import QueuedBoard
class AddingBoardGraphTest(TestCase):
def test_first_move(self):
board = QueuedBoard.create("""\
===
1 3 1
- - -
2 4 3
""",
border=1)
graph = AddingBoardGraph()
expected_state = """\
1|3
1 3
- -
2 4
"""
expected_moves = {MoveDescription('13h21', expected_state)}
moves = set(graph.generate_moves(board))
self.assertEqual(expected_moves, moves)
def test_move_to_complement(self):
board = QueuedBoard.create("""\
1|5
1 5
- -
2 4
""",
border=1)
graph = AddingBoardGraph()
expected_state1 = """\
1|5 x
x 1 5
- -
x 2 4
"""
expected_state2 = """\
x 1|5
1 5 x
- -
2 4 x
"""
expected_moves = {MoveDescription('15l', expected_state1, remaining=1-2/3),
MoveDescription('15r', expected_state2, remaining=1-2/3)}
moves = set(graph.generate_moves(board))
self.assertEqual(expected_moves, moves)
def test_walk(self):
board = QueuedBoard.create("""\
===
1 4 4
- - -
2 3 1
""")
graph = AddingBoardGraph()
expected_states = set("""\
===
1 4 4
- - -
2 3 1
###
1|4
1 4
- -
2 3
###
1|4
1 x
-
2 4
-
x 3
###
1 x
-
2 4
-
x 3
===
4
-
1
""".split('###\n'))
states = graph.walk(board)
self.assertEqual(expected_states, states)
def test_solution(self):
board = QueuedBoard.create("""\
===
1 4 4
- - -
2 3 1
""")
graph = AddingBoardGraph()
expected_solution = ['14h21']
graph.walk(board)
solution = graph.get_solution()
self.assertEqual(expected_solution, solution)
def test_no_moves(self):
start_state = """\
===
4 1 4
- - -
1 0 4
"""
board = QueuedBoard.create(start_state)
graph = AddingBoardGraph()
expected_states = {start_state}
expected_last = None
states = graph.walk(board)
self.assertEqual(expected_states, states)
self.assertEqual(expected_last, graph.last)
def test_4x3_solution(self):
board = QueuedBoard.create("""\
===
0 5 1 1 2 0
- - - - - -
1 5 5 1 5 2
""")
graph = AddingBoardGraph()
expected_solution = ['15h24', '01u', '15r', '11v24', '25v43', '02h32']
graph.walk(board)
solution = graph.get_solution()
self.assertEqual(expected_solution, solution)
def test_check_progress(self):
board = QueuedBoard.create("""\
0 5 x
- -
1 5 x
x 1|5
===
1 2 0
- - -
1 5 2
""")
expected_min_dominoes = 3
graph = AddingBoardGraph()
remaining = graph.check_progress(board)
self.assertEqual(remaining, expected_min_dominoes)
def test_check_progress_area(self):
""" After queue is empty, track progress toward rectangular shape.
Any gaps will make width*height more than the number of cells.
Progress is 1 - cell_count/(width*height). Progress is 0 for a
solution.
"""
board = QueuedBoard.create("""\
0 0|2 x
-
1 5 2 x
- -
1 5 5 x
-
1 x 1|5
""")
expected_min_dominoes = 0.25 # 1 - 12 / (4*4)
graph = AddingBoardGraph()
remaining = graph.check_progress(board)
self.assertEqual(remaining, expected_min_dominoes)
self.assertIsNone(graph.last)
def test_check_progress_solution(self):
""" After queue is empty, track progress toward rectangular shape.
Any gaps will make width*height more than the number of cells.
Progress is 1 - cell_count/(width*height). Progress is 0 for a
solution.
"""
state = """\
0 0|2
-
1 5 2
- -
1 5 5
-
1 1|5
"""
board = QueuedBoard.create(state)
expected_min_dominoes = 0 # 1 - 12 / (3*4)
graph = AddingBoardGraph()
remaining = graph.check_progress(board)
self.assertEqual(remaining, expected_min_dominoes)
def test_check_progress_ignores_border(self):
board = QueuedBoard.create("""\
x x x x x x
x 0 0|2 x x
-
x 1 5 2 x x
- -
x 1 5 5 x x
-
x 1 x 1|5 x
x x x x x x
""")
expected_min_dominoes = 0.25 # 1 - 12 / (4*4)
graph = AddingBoardGraph()
remaining = graph.check_progress(board)
self.assertEqual(remaining, expected_min_dominoes)