-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhs_techniques_test.go
505 lines (431 loc) · 14.1 KB
/
hs_techniques_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package sudoku
import (
"fmt"
"github.com/kr/pretty"
"log"
"reflect"
"testing"
)
func TestTechniquesSorted(t *testing.T) {
lastLikelihood := 0.0
for i, technique := range AllTechniques {
if technique.humanLikelihood(nil) < lastLikelihood {
t.Fatal("Technique named", technique.Name(), "with index", i, "has a likelihood lower than one of the earlier ones: ", technique.humanLikelihood(nil), lastLikelihood)
}
lastLikelihood = technique.humanLikelihood(nil)
}
}
func TestAllVariantNames(t *testing.T) {
expected := []string{
"Obvious In Block",
"Obvious In Row",
"Obvious In Col",
"Necessary In Block",
"Necessary In Row",
"Necessary In Col",
"Only Legal Number",
"Naked Pair Block",
"Naked Pair Row",
"Naked Pair Col",
"Naked Triple Block",
"Naked Triple Row",
"Naked Triple Col",
"Naked Quad Block",
"Naked Quad Row",
"Naked Quad Col",
"Pointing Pair Row",
"XWing Row",
"Pointing Pair Col",
"XWing Col",
"Block Block Interactions",
"XYWing",
"XYWing (Same Block)",
"Hidden Pair Block",
"Hidden Pair Row",
"Hidden Pair Col",
"Swordfish Row",
"Swordfish Col",
"Hidden Triple Block",
"Hidden Triple Row",
"Hidden Triple Col",
"Forcing Chain (1 steps)",
"Forcing Chain (2 steps)",
"Forcing Chain (3 steps)",
"Forcing Chain (4 steps)",
"Forcing Chain (5 steps)",
"Forcing Chain (6 steps)",
"Hidden Quad Block",
"Hidden Quad Row",
"Hidden Quad Col",
"Guess",
}
if !reflect.DeepEqual(expected, AllTechniqueVariants) {
t.Error("Got wrong technique variants. Expected", expected, "got", AllTechniqueVariants, "\nDifferences:\n", pretty.Diff(expected, AllTechniqueVariants))
}
}
func TestSubsetIndexes(t *testing.T) {
result := subsetIndexes(3, 1)
expectedResult := [][]int{{0}, {1}, {2}}
subsetIndexHelper(t, result, expectedResult)
result = subsetIndexes(3, 2)
expectedResult = [][]int{{0, 1}, {0, 2}, {1, 2}}
subsetIndexHelper(t, result, expectedResult)
result = subsetIndexes(5, 3)
expectedResult = [][]int{{0, 1, 2}, {0, 1, 3}, {0, 1, 4}, {0, 2, 3}, {0, 2, 4}, {0, 3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {2, 3, 4}}
subsetIndexHelper(t, result, expectedResult)
if subsetIndexes(1, 2) != nil {
t.Log("Subset indexes returned a subset where the length is greater than the len")
t.Fail()
}
}
func subsetIndexHelper(t *testing.T, result [][]int, expectedResult [][]int) {
if len(result) != len(expectedResult) {
t.Log("subset indexes returned wrong number of results for: ", result, " :", expectedResult)
t.FailNow()
}
for i, item := range result {
if len(item) != len(expectedResult[0]) {
t.Log("subset indexes returned a result with wrong numbrer of items ", i, " : ", result, " : ", expectedResult)
t.FailNow()
}
for j, value := range item {
if value != expectedResult[i][j] {
t.Log("Subset indexes had wrong number at ", i, ",", j, " : ", result, " : ", expectedResult)
t.Fail()
}
}
}
}
type multipleValidStepLoopOptions struct {
targetCells CellRefSlice
targetNums IntSlice
pointerCells CellRefSlice
pointerNums IntSlice
description string
extra interface{}
variantName string
}
//TODO: rename this to fit in with the other test helpers
func multipleValidStepsTestHelper(t *testing.T, puzzleName string, techniqueName string, tests []multipleValidStepLoopOptions) {
options := solveTechniqueTestHelperOptions{
checkAllSteps: true,
}
grid, solver, steps := humanSolveTechniqueTestHelperStepGenerator(t,
puzzleName, techniqueName, options)
options.stepsToCheck.grid = grid
options.stepsToCheck.solver = solver
options.stepsToCheck.steps = steps
//OK, now we'll walk through all of the options in a loop and make sure they all show
//up in the solve steps.
for _, test := range tests {
options.targetCells = test.targetCells
options.targetNums = test.targetNums
options.pointerCells = test.pointerCells
options.pointerNums = test.pointerNums
options.description = test.description
options.extra = test.extra
options.variantName = test.variantName
humanSolveTechniqueTestHelper(t, puzzleName, techniqueName, options)
}
if len(tests) != len(steps) {
t.Error("We didn't have enough tests for all of the steps that ", techniqueName, " returned. Got", len(tests), "expected", len(steps))
}
}
func techniqueVariantsTestHelper(t *testing.T, techniqueName string, variantNames ...string) {
technique, ok := techniquesByName[techniqueName]
if !ok {
t.Fatal("Couldn't find technqiue named", techniqueName)
}
if len(variantNames) == 0 {
variantNames = []string{technique.Name()}
}
names := technique.Variants()
if len(names) != len(variantNames) {
t.Fatal("Didn't receive the right number of variants for", technique.Name(), "Got", len(names), "expected", len(variantNames))
}
for i, name := range names {
goldenName := variantNames[i]
if name != goldenName {
t.Error(i, "th variant name for", technique.Name(), "wrong. Got", name, "expected", goldenName)
}
}
}
//multiTestWrapper wraps a testing.T and makes it possible to run loops
//where at least one run through the loop must not Error for the whole test
//to pass. Call t.Reset(), and at any time call Passed() to see if t.Error()
//has been called since last reset.
//Or, if looping is false, it's just a passthrough to t.Error.
type loopTest struct {
t *testing.T
looping bool
lastMessage string
}
func (l *loopTest) Reset() {
l.lastMessage = ""
}
func (l *loopTest) Passed() bool {
return l.lastMessage == ""
}
func (l *loopTest) Error(args ...interface{}) {
if l.looping == false {
l.t.Error(args...)
} else {
l.lastMessage = fmt.Sprint(args...)
}
}
type solveTechniqueMatchMode int
const (
solveTechniqueMatchModeAll solveTechniqueMatchMode = iota
solveTechniqueMatchModeAny
)
type solveTechniqueTestHelperOptions struct {
transpose bool
//Whether the descriptions of cells are a list of legal possible individual values, or must all match.
matchMode solveTechniqueMatchMode
targetCells CellRefSlice
pointerCells CellRefSlice
targetNums IntSlice
pointerNums IntSlice
targetSame cellGroupType
targetGroup int
variantName string
extra interface{}
//If true, will loop over all steps from the technique and see if ANY of them match.
checkAllSteps bool
//A way to skip the step generator by provding your own list of steps.
//Useful if you're going to be do repeated calls to the test helper with the
//same list of steps.
stepsToCheck struct {
grid MutableGrid
solver SolveTechnique
steps []*SolveStep
}
//If description provided, the description MUST match.
description string
//If descriptions provided, ONE of the descriptions must match.
//generally used in conjunction with solveTechniqueMatchModeAny.
descriptions []string
debugPrint bool
}
//TODO: 97473c18633203a6eaa075d968ba77d85ba28390 introduced an error here where we don't return all techniques,
//at least for forcing chains technique.
func getStepsForTechnique(technique SolveTechnique, grid Grid, fetchAll bool) []*SolveStep {
maxResults := 0
if !fetchAll {
maxResults = 1
}
return technique.Candidates(grid, maxResults)
}
func humanSolveTechniqueTestHelperStepGenerator(t *testing.T, puzzleName string, techniqueName string, options solveTechniqueTestHelperOptions) (MutableGrid, SolveTechnique, []*SolveStep) {
var grid MutableGrid
if options.stepsToCheck.grid != nil {
grid = options.stepsToCheck.grid
} else {
tempGrid, err := MutableLoadSDKFromFile(puzzlePath(puzzleName))
if err != nil {
t.Fatal("Couldn't load puzzle ", puzzleName)
}
grid = tempGrid
}
if options.transpose {
newGrid := grid.(*mutableGridImpl).transpose()
grid = newGrid
}
solver := techniquesByName[techniqueName]
if solver == nil {
t.Fatal("Couldn't find technique object: ", techniqueName)
}
steps := getStepsForTechnique(solver, grid, options.checkAllSteps)
return grid, solver, steps
}
func humanSolveTechniqueTestHelper(t *testing.T, puzzleName string, techniqueName string, options solveTechniqueTestHelperOptions) {
//TODO: it's weird that you have to pass in puzzleName a second time if you're also passing in options.
//TODO: test for col and block as well
var grid Grid
var solver SolveTechnique
var steps []*SolveStep
if options.stepsToCheck.grid != nil {
grid = options.stepsToCheck.grid
solver = options.stepsToCheck.solver
steps = options.stepsToCheck.steps
} else {
grid, solver, steps = humanSolveTechniqueTestHelperStepGenerator(t, puzzleName, techniqueName, options)
}
//This is so weird... if I don't have this no-op here the compiler warns
//that grid is declared and not used... despite the fact that it OBVIOUSLY
//is.
grid.Cell(0, 0)
//Check if solveStep is nil here
if len(steps) == 0 {
t.Fatal(techniqueName, " didn't find a cell it should have.")
}
//Instead of calling error on t, we'll call it on l. If we're not in checkAllSteps mode,
//l.Error() will be pass through; otherwise we can interrogate it at any point in the loop.
l := &loopTest{t: t, looping: options.checkAllSteps}
for _, step := range steps {
l.Reset()
if options.debugPrint {
log.Println(step)
}
variantName := options.variantName
if options.variantName == "" {
variantName = techniqueName
}
if step.TechniqueVariant() != variantName {
l.Error("TechniqueVariant name was not what was expected. Got", step.TechniqueVariant(), "expected", variantName)
continue
}
foundVariantNameMatch := false
for _, variant := range AllTechniqueVariants {
if variant == step.TechniqueVariant() {
foundVariantNameMatch = true
break
}
}
if !foundVariantNameMatch {
//This is a t.error, because every step should be valid in this way.
t.Error("Found a variant name that's not in the set: ", step.TechniqueVariant())
}
if !reflect.DeepEqual(step.extra, options.extra) {
l.Error("Extra did not match. Got", step.extra, "expected", options.extra)
continue
}
if options.matchMode == solveTechniqueMatchModeAll {
//All must match
if options.targetCells != nil {
if !step.TargetCells.sameAs(options.targetCells) {
l.Error(techniqueName, " had the wrong target cells: ", step.TargetCells)
continue
}
}
if options.pointerCells != nil {
if !step.PointerCells.sameAs(options.pointerCells) {
l.Error(techniqueName, " had the wrong pointer cells: ", step.PointerCells)
continue
}
}
switch options.targetSame {
case _GROUP_ROW:
if !step.TargetCells.SameRow() || step.TargetCells.Row() != options.targetGroup {
l.Error("The target cells in the ", techniqueName, " were wrong row :", step.TargetCells.Row())
continue
}
case _GROUP_BLOCK:
if !step.TargetCells.SameBlock() || step.TargetCells.Block() != options.targetGroup {
l.Error("The target cells in the ", techniqueName, " were wrong block :", step.TargetCells.Block())
continue
}
case _GROUP_COL:
if !step.TargetCells.SameCol() || step.TargetCells.Col() != options.targetGroup {
l.Error("The target cells in the ", techniqueName, " were wrong col :", step.TargetCells.Col())
continue
}
case _GROUP_NONE:
//Do nothing
default:
l.Error("human solve technique helper error: unsupported group type: ", options.targetSame)
continue
}
if options.targetNums != nil {
if !step.TargetNums.SameContentAs(options.targetNums) {
l.Error(techniqueName, " found the wrong numbers: ", step.TargetNums)
continue
}
}
if options.pointerNums != nil {
if !step.PointerNums.SameContentAs(options.pointerNums) {
l.Error(techniqueName, "found the wrong numbers:", step.PointerNums)
continue
}
}
} else if options.matchMode == solveTechniqueMatchModeAny {
foundMatch := false
if !reflect.DeepEqual(step.extra, options.extra) {
l.Error("Extra did not match. Got", step.extra, "expected", options.extra)
continue
}
if options.targetCells != nil {
foundMatch = false
for _, ref := range options.targetCells {
for _, cell := range step.TargetCells {
if ref == cell {
//TODO: break out early
foundMatch = true
}
}
}
if !foundMatch {
l.Error(techniqueName, " had the wrong target cells: ", step.TargetCells)
continue
}
}
if options.pointerCells != nil {
l.Error("Pointer cells in match mode any not yet supported.")
continue
}
if options.targetSame != _GROUP_NONE {
l.Error("Target Same in match mode any not yet supported.")
continue
}
if options.targetNums != nil {
foundMatch = false
for _, targetNum := range options.targetNums {
for _, num := range step.TargetNums {
if targetNum == num {
foundMatch = true
//TODO: break early here.
}
}
}
if !foundMatch {
l.Error(techniqueName, " had the wrong target nums: ", step.TargetNums)
continue
}
}
if options.pointerNums != nil {
foundMatch = false
for _, pointerNum := range options.pointerNums {
for _, num := range step.PointerNums {
if pointerNum == num {
foundMatch = true
//TODO: break early here
}
}
}
if !foundMatch {
l.Error(techniqueName, " had the wrong pointer nums: ", step.PointerNums)
continue
}
}
}
if options.description != "" {
//Normalize the step so that the description will be stable for the test.
step.normalize()
description := solver.Description(step)
if description != options.description {
l.Error("Wrong description for ", techniqueName, ". Got:*", description, "* expected: *", options.description, "*")
continue
}
} else if options.descriptions != nil {
foundMatch := false
step.normalize()
description := solver.Description(step)
for _, targetDescription := range options.descriptions {
if description == targetDescription {
foundMatch = true
}
}
if !foundMatch {
l.Error("No descriptions matched for ", techniqueName, ". Got:*", description)
continue
}
}
if options.checkAllSteps && l.Passed() {
break
}
}
if !l.Passed() {
t.Error("No cells matched any of the options: ", options)
}
//TODO: we should do exhaustive testing of SolveStep application. We used to test it here, but as long as targetCells and targetNums are correct it should be fine.
}