-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_SudokuSolver.py
258 lines (221 loc) · 9.52 KB
/
test_SudokuSolver.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
hexadoku_expert = [[0,11,0,0,12,0,0,15,0,9,14,0,0,0,3,4],
[0,0,0,0,7,0,0,0,0,1,0,0,11,0,0,0],
[3,8,5,0,0,0,0,0,0,12,0,0,0,0,0,0],
[15,0,12,9,0,0,16,4,0,0,3,0,0,6,0,0],
[1,6,16,0,0,0,9,3,0,0,8,0,0,0,0,0],
[0,3,15,0,0,7,12,0,0,13,0,0,5,2,0,0],
[0,0,0,0,8,0,13,0,0,10,4,1,0,0,0,0],
[2,0,0,0,5,0,0,0,0,0,15,0,0,10,0,0],
[0,5,0,0,0,4,0,9,0,7,10,3,0,1,0,0],
[11,0,9,0,0,0,0,0,8,2,0,0,0,0,0,13],
[0,0,0,0,0,2,0,5,0,0,0,16,0,0,11,0],
[13,12,0,0,6,0,1,8,9,0,0,0,16,0,14,0],
[0,0,0,2,0,0,0,6,0,0,9,10,0,13,16,0],
[0,9,0,8,0,0,5,0,0,0,1,7,15,0,4,0],
[6,0,0,0,0,8,0,1,0,15,0,14,7,5,0,0],
[0,0,4,13,3,0,0,16,0,0,5,0,6,12,0,0]]# This came from the Udacity course on testing and I wanted to use
# graph-traversal to solve this problem efficiently!
import unittest
import random
from SudokuSolver import *
ill_formed = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9], # <---
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
valid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
zero_zero_to_five_oneMove = [[0,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
invalid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,8,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
easy = [[2,9,0,0,0,0,0,7,0],
[3,0,6,0,0,8,4,0,0],
[8,0,0,0,4,0,0,0,2],
[0,2,0,0,3,1,0,0,7],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,0,0,0,0,0,0],
[15,0,12,9,0,0,16,4,0,0,3,0,0,6,0,0],
[1,6,16,0,0,0,9,3,0,0,8,0,0,0,0,0],
[0,3,15,0,0,7,12,0,0,13,0,0,5,2,0,0],
[0,0,0,0,8,0,13,0,0,10,4,1,0,0,0,0],
[2,0,0,0,5,0,0,0,0,0,15,0,0,10,0,0],
[0,5,0,0,0,4,0,9,0,7,10,3,0,1,0,0],
[11,0,9,0,0,0,0,0,8,2,0,0,0,0,0,13],
[0,0,0,0,0,2,0,5,0,0,0,16,0,0,11,0],
[13,12,0,0,6,0,1,8,9,0,0,0,16,0,14,0],
[0,0,0,2,0,0,0,6,0,0,9,10,0,13,16,0],
[0,9,0,8,0,0,5,0,0,0,1,7,15,0,4,0],
[6,0,0,0,0,8,0,1,0,15,0,14,7,5,0,0],
[0,0,4,13,3,0,0,16,0,0,5,0,6,12,0,0]]
class test_SudokuGrid(unittest.TestCase):
def test_initializer(self):
try:
SudokuGrid(initial_grid = ill_formed)
except IllFormedGridException:
self.assertTrue(True, msg="construction did not fail when given malformed grid")
# should pass without trouble
testgrid1 = SudokuGrid(initial_grid = valid)
try:
SudokuGrid(initial_grid = invalid)
except InvalidGridException:
self.assertTrue(True, msg="did not catch invalid grid")
grid_easy = SudokuGrid(initial_grid = easy)
grid_hard = SudokuGrid(initial_grid = hard)
#make several big, empty grids
for i in range(10):
big_grid = SudokuGrid(grid_size = i * 30 + 1)
def test_check_invalid_entry_number(self):
grid1 = SudokuGrid(initial_grid = None)
self.assertTrue(grid1.check_invalid_entry_number(-10))
self.assertTrue(grid1.check_invalid_entry_number(-1))
for i in range(10):
self.assertFalse(grid1.check_invalid_entry_number(i))
self.assertTrue(grid1.check_invalid_entry_number(10))
self.assertTrue(grid1.check_invalid_entry_number(100))
def test_toString(self):
grid1 = SudokuGrid(initial_grid = valid)
a = str(grid1)
def test_equals(self):
grid1 = SudokuGrid(initial_grid = valid)
grid2 = SudokuGrid(initial_grid = valid)
grid_easy = SudokuGrid(initial_grid = easy)
self.assertTrue(grid1==grid2, msg="equals failed when it should be true")
self.assertFalse(grid1 == grid_easy, msg="equals failed when it should be false")
def test_possible_moves(self):
grid_easy = SudokuGrid(initial_grid = easy)
easy_possible_moves = grid_easy.find_moves()
def test_hashing(self):
grid1 = SudokuGrid(initial_grid = valid)
grid_easy = SudokuGrid(initial_grid = easy)
grid_hard = SudokuGrid(initial_grid = hard)
grid_set = {grid1, grid_easy}
self.assertTrue(grid1 in grid_set)
self.assertTrue(grid_hard not in grid_set)
def test_finds_one_possible_move(self):
grid1 = SudokuGrid(initial_grid = zero_zero_to_five_oneMove)
grid1_possible_moves = grid1.find_moves()
self.assertTrue(len(grid1_possible_moves) == 1, "there is only supposed to be one possible move in this grid")
def test_is_solved(self):
grid1 = SudokuGrid(initial_grid = valid)
self.assertTrue(grid1.is_solved(), "Grid should be identified as solved")
grid2 = SudokuGrid(initial_grid = hard)
self.assertFalse(grid2.is_solved(), "Grid should not be identified as solved")
class test_SudokuGridSolver(unittest.TestCase):
def test_initializer(self):
grid1 = SudokuGrid(initial_grid = zero_zero_to_five_oneMove)
grid1_solver = SudokuGridSolver(grid1)
def test_solved_basic(self):
grid1 = SudokuGrid(initial_grid = zero_zero_to_five_oneMove)
grid1_solver = SudokuGridSolver(grid1)
grid1_solved = grid1_solver.solve()
grid2 = SudokuGrid(initial_grid = valid)
self.assertTrue(grid2 == grid1_solved)
def test_solve_easy(self):
grid_easy = SudokuGrid(initial_grid = easy)
# print("easy before: \n")
# print(str(grid_easy))
grid_easy_solver = SudokuGridSolver(grid_easy)
grid_easy_solved = grid_easy_solver.solve()
# print("easy after: \n")
# print(str(grid_easy_solved))
def test_solve_easy_1(self):
grid_easy = SudokuGrid(initial_grid = test1_easy)
grid_easy_solution = SudokuGrid(initial_grid = test1_easy_solution)
self.assertTrue(grid_easy_solution.is_solved())
# print("test1 before: \n")
# print(str(grid_easy))
grid_easy_solver = SudokuGridSolver(grid_easy)
grid_easy_possibly_solved = grid_easy_solver.solve()
# print("test1 after: \n")
# print(str(grid_easy_possibly_solved))
self.assertTrue(grid_easy_possibly_solved == grid_easy_solution)
self.assertTrue(grid_easy_possibly_solved.is_solved())
def test_solve_hard(self):
grid_hard = SudokuGrid(initial_grid = hard)
grid1_solver = SudokuGridSolver(grid_hard)
grid_hard_solved = grid1_solver.solve()
self.assertTrue(grid_hard_solved.is_solved())
def test_solve_really_hard(self):
grid_hard = SudokuGrid(initial_grid = really_hard)
grid1_solver = SudokuGridSolver(grid_hard)
grid_hard_solved = grid1_solver.solve()
self.assertTrue(grid_hard_solved.is_solved())
def test_hexadoku(self):
print("Do you want to test solving a hexadoku? It will take ~1.5 hours. Type 'yes' if so:")
answer = raw_input()
if answer == 'yes':
hexagrid = SudokuGrid(initial_grid = hexadoku_expert, grid_size = 16)
hex_grid_solver = SudokuGridSolver(hexagrid)
hex_grid_solution = hex_grid_solver.solve()
self.assertTrue(hex_grid_solution.is_solved())
if __name__ == '__main__':
unittest.main()