-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhst_single_test.go
365 lines (276 loc) · 9.49 KB
/
hst_single_test.go
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package sudoku
import (
"testing"
)
func TestObviousInCollectionRow(t *testing.T) {
options := solveTechniqueTestHelperOptions{
targetCells: []CellRef{{2, 3}},
targetSame: _GROUP_ROW,
targetGroup: 2,
targetNums: IntSlice([]int{7}),
description: "(2,3) is the only cell in row 2 that is unfilled, and it must be 7",
}
humanSolveTechniqueTestHelper(t, "obviousrow.sdk", "Obvious In Row", options)
techniqueVariantsTestHelper(t, "Obvious In Row")
}
func TestObviousInCollectionCol(t *testing.T) {
options := solveTechniqueTestHelperOptions{
transpose: true,
targetCells: []CellRef{{3, 2}},
targetSame: _GROUP_COL,
targetGroup: 2,
targetNums: IntSlice([]int{7}),
description: "(3,2) is the only cell in column 2 that is unfilled, and it must be 7",
}
humanSolveTechniqueTestHelper(t, "obviousrow.sdk", "Obvious In Col", options)
techniqueVariantsTestHelper(t, "Obvious In Col")
}
func TestObviousInCollectionBlock(t *testing.T) {
options := solveTechniqueTestHelperOptions{
targetCells: []CellRef{{4, 1}},
targetSame: _GROUP_BLOCK,
targetGroup: 3,
targetNums: IntSlice([]int{9}),
description: "(4,1) is the only cell in block 3 that is unfilled, and it must be 9",
}
humanSolveTechniqueTestHelper(t, "obviousblock.sdk", "Obvious In Block", options)
techniqueVariantsTestHelper(t, "Obvious In Block")
}
func TestSolveOnlyLegalNumber(t *testing.T) {
techniqueVariantsTestHelper(t, "Only Legal Number")
grid := MutableLoadSDK(SOLVED_TEST_GRID)
cell := grid.MutableCell(3, 3)
num := cell.Number()
cell.SetNumber(0)
//Now that cell should be filled by this technique.
techniqueName := "Only Legal Number"
solver := techniquesByName[techniqueName]
if solver == nil {
t.Fatal("Couldn't find technique object: ", techniqueName)
}
results := make(chan *SolveStep, DIM*DIM)
done := make(chan bool)
coordinator := &channelFindCoordinator{
results: results,
done: done,
}
//Find is meant to be run in a goroutine; it won't complete until it's searched everything.
solver.find(grid, coordinator)
//TODO: test that Find exits early when done is closed. (or maybe just doesn't send after done is closed)
close(done)
var step *SolveStep
//TODO: test cases where we expectmultipel results...
select {
case step = <-results:
default:
t.Fatal(techniqueName, " didn't find a cell it should have.")
}
description := solver.Description(step)
if description != "3 is the only remaining valid number for that cell" {
t.Error("Wrong description for ", techniqueName, ": ", description)
}
cellFromStep := step.TargetCells[0]
if cellFromStep.Col != 3 || cellFromStep.Row != 3 {
t.Log("The only legal number technique identified the wrong cell.")
t.Fail()
}
numFromStep := step.TargetNums[0]
if numFromStep != num {
t.Log("The only legal number technique identified the wrong number.")
t.Fail()
}
if grid.Solved() {
t.Log("The only legal number technique did actually mutate the grid.")
t.Fail()
}
}
//TODO: use the test solve helper func for these three tests.
func TestNecessaryInRow(t *testing.T) {
//Load up a solved grid
grid := MutableLoad(SOLVED_TEST_GRID)
//We're going to cheat an set up an unrealistic grid.
impossibles := make([]int, DIM)
for i := 0; i < DIM-1; i++ {
impossibles[i] = 0
}
impossibles[DIM-1] = 1
//SetNumber will affect the other cells in row, so do it first.
for _, cell := range grid.MutableRow(3) {
cellI := cell.(*mutableCellImpl)
cellI.number = 0
copy(cellI.impossibles[:], impossibles)
}
cell := grid.MutableCell(3, 3)
cellI := cell.(*mutableCellImpl)
//This is the only cell where DIM will be allowed.
cellI.impossibles[DIM-1] = 0
//Now that cell should be filled by this technique.
techniqueName := "Necessary In Row"
solver := techniquesByName[techniqueName]
techniqueVariantsTestHelper(t, "Necessary In Row")
if solver == nil {
t.Fatal("Couldn't find technique object: ", techniqueName)
}
results := make(chan *SolveStep, DIM*DIM)
done := make(chan bool)
coordinator := &channelFindCoordinator{
results: results,
done: done,
}
//Find is meant to be run in a goroutine; it won't complete until it's searched everything.
solver.find(grid, coordinator)
//TODO: test that Find exits early when done is closed. (or maybe just doesn't send after done is closed)
close(done)
var step *SolveStep
//TODO: test cases where we expectmultipel results...
select {
case step = <-results:
default:
t.Fatal(techniqueName, " didn't find a cell it should have.")
}
description := solver.Description(step)
if description != "9 is required in the 3 row, and 3 is the only column it fits" {
t.Error("Wrong description for ", techniqueName, ": ", description)
}
cellFromStep := step.TargetCells[0]
if cellFromStep.Col != 3 || cellFromStep.Row != 3 {
t.Log("The necessary in row technique identified the wrong cell.")
t.Fail()
}
numFromStep := step.TargetNums[0]
if numFromStep != DIM {
t.Log("The necessary in row technique identified the wrong number.")
t.Fail()
}
//Can't check if grid is solved because we un-set all the other cells in the row.
if cell.Number() != 0 {
t.Log("The necessary in row technique did actually mutate the grid.")
t.Fail()
}
}
func TestNecessaryInCol(t *testing.T) {
//Load up a solved grid
grid := MutableLoadSDK(SOLVED_TEST_GRID)
//We're going to cheat an set up an unrealistic grid.
impossibles := make([]int, DIM)
for i := 0; i < DIM-1; i++ {
impossibles[i] = 0
}
impossibles[DIM-1] = 1
//SetNumber will affect the other cells in row, so do it first.
for _, cell := range grid.MutableCol(3) {
cellI := cell.(*mutableCellImpl)
cellI.number = 0
copy(cellI.impossibles[:], impossibles)
}
cell := grid.MutableCell(3, 3)
cellI := cell.(*mutableCellImpl)
//This is the only cell where DIM will be allowed.
cellI.impossibles[DIM-1] = 0
//Now that cell should be filled by this technique.
techniqueName := "Necessary In Col"
solver := techniquesByName[techniqueName]
techniqueVariantsTestHelper(t, "Necessary In Col")
if solver == nil {
t.Fatal("Couldn't find technique object: ", techniqueName)
}
results := make(chan *SolveStep, DIM*DIM)
done := make(chan bool)
coordinator := &channelFindCoordinator{
results: results,
done: done,
}
//Find is meant to be run in a goroutine; it won't complete until it's searched everything.
solver.find(grid, coordinator)
//TODO: test that Find exits early when done is closed. (or maybe just doesn't send after done is closed)
close(done)
var step *SolveStep
//TODO: test cases where we expectmultipel results...
select {
case step = <-results:
default:
t.Fatal(techniqueName, " didn't find a cell it should have.")
}
description := solver.Description(step)
if description != "9 is required in the 3 column, and 3 is the only row it fits" {
t.Error("Wrong description for ", techniqueName, ": ", description)
}
cellFromStep := step.TargetCells[0]
if cellFromStep.Col != 3 || cellFromStep.Row != 3 {
t.Log("The necessary in col technique identified the wrong cell.")
t.Fail()
}
numFromStep := step.TargetNums[0]
if numFromStep != DIM {
t.Log("The necessary in col technique identified the wrong number.")
t.Fail()
}
//Can't check if grid is solved because we un-set all the other cells in the row.
if cell.Number() != 0 {
t.Log("The necessary in col technique did actually mutate the grid.")
t.Fail()
}
}
func TestNecessaryInBlock(t *testing.T) {
//Load up a solved grid
grid := MutableLoadSDK(SOLVED_TEST_GRID)
//We're going to cheat an set up an unrealistic grid.
impossibles := make([]int, DIM)
for i := 0; i < DIM-1; i++ {
impossibles[i] = 0
}
impossibles[DIM-1] = 1
//SetNumber will affect the other cells in row, so do it first.
for _, cell := range grid.MutableBlock(4) {
cellI := cell.(*mutableCellImpl)
cellI.number = 0
copy(cellI.impossibles[:], impossibles)
}
cell := grid.MutableCell(3, 3)
cellI := cell.(*mutableCellImpl)
//This is the only cell where DIM will be allowed.
cellI.impossibles[DIM-1] = 0
//Now that cell should be filled by this technique.
techniqueName := "Necessary In Block"
solver := techniquesByName[techniqueName]
techniqueVariantsTestHelper(t, "Necessary In Block")
if solver == nil {
t.Fatal("Couldn't find technique object: ", techniqueName)
}
results := make(chan *SolveStep, DIM*DIM)
done := make(chan bool)
coordinator := &channelFindCoordinator{
results: results,
done: done,
}
//Find is meant to be run in a goroutine; it won't complete until it's searched everything.
solver.find(grid, coordinator)
//TODO: test that Find exits early when done is closed. (or maybe just doesn't send after done is closed)
close(done)
var step *SolveStep
//TODO: test cases where we expectmultipel results...
select {
case step = <-results:
default:
t.Fatal(techniqueName, " didn't find a cell it should have.")
}
description := solver.Description(step)
if description != "9 is required in the 4 block, and (3,3) is the only cell it fits" {
t.Error("Wrong description for ", techniqueName, ": ", description)
}
cellFromStep := step.TargetCells[0]
if cellFromStep.Col != 3 || cellFromStep.Row != 3 {
t.Log("The necessary in block technique identified the wrong cell.")
t.Fail()
}
numFromStep := step.TargetNums[0]
if numFromStep != DIM {
t.Log("The necessary in block technique identified the wrong number.")
t.Fail()
}
//Can't check if grid is solved because we un-set all the other cells in the row.
if cell.Number() != 0 {
t.Log("The necessary in block technique did actually mutate the grid.")
t.Fail()
}
}