-
Notifications
You must be signed in to change notification settings - Fork 1
/
SudokuSolveTest.py
265 lines (222 loc) · 5.83 KB
/
SudokuSolveTest.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
#!/usr/bin/python
import unittest
import SudokuSolve
from math import sqrt
from cStringIO import StringIO
import sys
########################################
# Test Cases
########################################
class TestStuff(unittest.TestCase):
def setUp(self):
self.mySolver = SudokuSolve.Solver()
def test_Solve(self):
grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
expected = [[
[1, 2, 3, 4],
[3, 4, 1, 2],
[4, 3, 2, 1],
[2, 1, 4, 3]
]]
self.mySolver.SetGrid(grid)
actual = self.mySolver.Solve()
self.assertEquals(actual, expected)
def test_SetGrid(self):
expected = [
[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]
]
self.mySolver.SetGrid(expected)
actual = self.mySolver.GetGrid()
self.assertEquals(actual, expected)
def test_GetGrid(self):
expected = [
[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]
]
self.mySolver.SetGrid(expected)
actual = self.mySolver.GetGrid()
self.assertEquals(actual, expected)
def test_ReadGridFromFile(self):
expected = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]]
actual = self.mySolver.ReadGridFromFile("TestCase1.txt")
self.assertEquals(expected, actual)
def test_FindPsbs(self):
grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
expected = [1, 2, 4]
actual = self.mySolver.FindPsbs(grid, 2, 2)
self.assertEquals(actual, expected)
def test_CheckArray(self):
#print "Testing array check..."
grid_1 = [4, 8, 9, 2, 1, 3] #should pass
grid_2 = [8, 8, 2, 1, 3, 9] #should fail
self.assertTrue( self.mySolver.CheckArray( grid_1 ) )
self.assertFalse( self.mySolver.CheckArray( grid_2 ) )
def test_ColToArray(self):
#print "Testing column to array..."
grid = [
[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]
]
checkArray = [1, 6, 11, 16, 21]
array = self.mySolver.ColToArray( grid, 0 )
self.assertEquals(array, checkArray)
def test_CheckGridDimensions(self):
#print "Testing grid validation..."
grid_1 = [
[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]
]
self.assertFalse( self.mySolver.CheckGridDimensions( grid_1 ) )
grid_2 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
self.assertTrue( self.mySolver.CheckGridDimensions( grid_2 ) )
def test_BlockToArray(self):
#print "Testing blcok to array..."
grid = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
expected = [3, 4, 7, 8]
actual = self.mySolver.BlockToArray( grid, 1 )
self.assertEquals(actual, expected)
def test_LocateNextEmpty(self):
grid = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 0, 12],
[13, 14, 15, 16]
]
val = self.mySolver.LocateNextEmpty( grid )
self.assertEquals( val, [2,2] )
def test_LocateBlock(self):
grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
val = self.mySolver.LocateBlock( [0,3], sqrt( len( grid ) ) )
self.assertEquals( val, 1 )
def test_PrintGrid(self):
grid = [
[1, 2],
[2, 1]
]
expected = "----------\n| 1 2 | \n| 2 1 | \n----------\n"
# re-route print() output to a string instead
stdout_saved, sys.stdout = sys.stdout, StringIO()
self.mySolver.PrintGrid( grid );
# get printed value, reset stdout
actual = sys.stdout.getvalue()
sys.stdout = stdout_saved
self.assertEqual(actual, expected)
def test_CleanRowPrint(self):
grid_row = [1, 2]
expected = "| 1 2 | \n"
# re-route print() output to a string instead
stdout_saved, sys.stdout = sys.stdout, StringIO()
self.mySolver.CleanRowPrint(0, grid_row)
# get printed value, reset stdout
actual = sys.stdout.getvalue()
sys.stdout = stdout_saved
self.assertEqual(actual, expected)
def test_ValidateGrid(self):
good_grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
bad_grid = [
[2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
actual = self.mySolver.ValidateGrid( good_grid, 1, 1 )
self.assertTrue(actual)
actual = self.mySolver.ValidateGrid( bad_grid, 0, 0 )
self.assertFalse(actual)
#TODO: write tests for other bad grid permutations
def test_CreatePsbGrid(self):
grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
actual = self.mySolver.CreatePsbGrid(grid)
expected = [[[], [], [3], []], [[], [4], [1], []], [[4], [3, 4], [1, 2, 4], [1]], [[], [], [4], []]]
self.assertEquals(actual, expected)
def test_UpdatePsb(self):
grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
self.mySolver.SetGrid(grid)
self.mySolver.UpdatePsb(1,1,4)
actual = self.mySolver.psbGrid
expected = [[[], [], [3], []], [[], [], [1], []], [[4], [3], [1, 2, 4], [1]], [[], [], [4], []]]
self.assertEquals(actual, expected)
def test_FillGrid(self):
# ensure a True value is returned when solving a grid
good_grid = [
[1, 2, 0, 4],
[3, 0, 0, 2],
[0, 0, 0, 0],
[2, 1, 0, 3]
]
self.mySolver.SetGrid(good_grid)
actual = self.mySolver.FillGrid(good_grid,0)
self.assertTrue(actual)
# ensure grid was filled correctly
expected = [[1, 2, 3, 4], [3, 4, 1, 2], [4, 3, 2, 1], [2, 1, 4, 3]]
actual = self.mySolver.GetGrid()
self.assertEquals(actual, expected)
# ensure a False value is returned when a grid is unsolvable
bad_grid = [
[1, 2, 2, 4],
[3, 0, 0, 2],
[1, 1, 0, 4],
[2, 1, 1, 3]
]
self.mySolver.SetGrid(bad_grid)
actual = self.mySolver.FillGrid(bad_grid,0)
self.assertFalse(actual)
if __name__ == '__main__':
unittest.main()