-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid_test.go
1352 lines (1045 loc) · 33.7 KB
/
grid_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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sudoku
import (
"container/list"
"io/ioutil"
"math"
"runtime"
"strings"
"testing"
)
//This grid is #27 from Totally Pocket Sudoku. It's included for testing purposes only.
const PUZZLES_DIRECTORY = "puzzles"
const TEST_GRID = `6|1|2|.|.|.|4|.|3
.|3|.|4|9|.|.|7|2
.|.|7|.|.|.|.|6|5
.|.|.|.|6|1|.|8|.
1|.|3|.|4|.|2|.|6
.|6|.|5|2|.|.|.|.
.|9|.|.|.|.|5|.|.
7|2|.|.|8|5|.|3|.
5|.|1|.|.|.|9|4|7`
const TRANSPOSED_TEST_GRID = `6|.|.|.|1|.|.|7|5
1|3|.|.|.|6|9|2|.
2|.|7|.|3|.|.|.|1
.|4|.|.|.|5|.|.|.
.|9|.|6|4|2|.|8|.
.|.|.|1|.|.|.|5|.
4|.|.|.|2|.|5|.|9
.|7|6|8|.|.|.|3|4
3|2|5|.|6|.|.|.|7`
const SOLVED_TEST_GRID = `6|1|2|7|5|8|4|9|3
8|3|5|4|9|6|1|7|2
9|4|7|2|1|3|8|6|5
2|5|9|3|6|1|7|8|4
1|7|3|8|4|9|2|5|6
4|6|8|5|2|7|3|1|9
3|9|6|1|7|4|5|2|8
7|2|4|9|8|5|6|3|1
5|8|1|6|3|2|9|4|7`
const TEST_GRID_DIAGRAM = `XXX|XXX|XXX|| | | ||XXX| |XXX
X6X|X1X|X2X|| | 5 | ||X4X| |X3X
XXX|XXX|XXX||78 |7 |78 ||XXX| 9|XXX
---+---+---||---+---+---||---+---+---
|XXX| ||XXX|XXX| ||1 |XXX|XXX
|X3X| 5 ||X4X|X9X| 6|| |X7X|X2X
8 |XXX| 8 ||XXX|XXX| 8 || 8 |XXX|XXX
---+---+---||---+---+---||---+---+---
| |XXX||123|1 3| 23||1 |XXX|XXX
4 |4 |X7X|| | | || |X6X|X5X
89| 8 |XXX|| 8 | | 8 || 8 |XXX|XXX
-----------++-----------++-----------
-----------++-----------++-----------
2 | | || 3|XXX|XXX|| 3|XXX|
4 |45 |45 || |X6X|X1X|| |X8X|4
9|7 | 9||7 9|XXX|XXX||7 |XXX| 9
---+---+---||---+---+---||---+---+---
XXX| |XXX|| |XXX| ||XXX| |XXX
X1X| 5 |X3X|| |X4X| ||X2X| 5 |X6X
XXX|78 |XXX||789|XXX|789||XXX| 9|XXX
---+---+---||---+---+---||---+---+---
|XXX| ||XXX|XXX| 3||1 3|1 |1
4 |X6X|4 ||X5X|X2X| || | |4
89|XXX| 89||XXX|XXX|789||7 | 9| 9
-----------++-----------++-----------
-----------++-----------++-----------
3|XXX| ||123|1 3| 23||XXX|12 |1
4 |X9X|4 6|| 6| |4 6||X5X| |
8 |XXX| 8 ||7 |7 |7 ||XXX| | 8
---+---+---||---+---+---||---+---+---
XXX|XXX| ||1 |XXX|XXX||1 |XXX|1
X7X|X2X|4 6|| 6|X8X|X5X|| 6|X3X|
XXX|XXX| || 9|XXX|XXX|| |XXX|
---+---+---||---+---+---||---+---+---
XXX| |XXX|| 23| 3| 23||XXX|XXX|XXX
X5X| |X1X|| 6| | 6||X9X|X4X|X7X
XXX| 8 |XXX|| | | ||XXX|XXX|XXX`
const TEST_GRID_EXCLUDED_DIAGRAM = `XXX|XXX|XXX|| | | ||XXX| |XXX
X6X|X1X|X2X|| | 5 | ||X4X| |X3X
XXX|XXX|XXX||78 |7 |78 ||XXX| 9|XXX
---+---+---||---+---+---||---+---+---
|XXX| ||XXX|XXX| ||1 |XXX|XXX
|X3X| 5 ||X4X|X9X| 6|| |X7X|X2X
8 |XXX| 8 ||XXX|XXX| 8 || 8 |XXX|XXX
---+---+---||---+---+---||---+---+---
| |XXX||123|1 3| 23||1 |XXX|XXX
|4 |X7X|| | | || |X6X|X5X
89| 8 |XXX|| 8 | | 8 || 8 |XXX|XXX
-----------++-----------++-----------
-----------++-----------++-----------
2 | | || 3|XXX|XXX|| 3|XXX|
4 |45 |45 || |X6X|X1X|| |X8X|4
9|7 | 9||7 9|XXX|XXX||7 |XXX| 9
---+---+---||---+---+---||---+---+---
XXX| |XXX|| |XXX| ||XXX| |XXX
X1X| 5 |X3X|| |X4X| ||X2X| 5 |X6X
XXX|78 |XXX||789|XXX|789||XXX| 9|XXX
---+---+---||---+---+---||---+---+---
|XXX| ||XXX|XXX| 3||1 3|1 |1
4 |X6X|4 ||X5X|X2X| || | |4
89|XXX| 89||XXX|XXX|789||7 | 9| 9
-----------++-----------++-----------
-----------++-----------++-----------
3|XXX| ||123|1 3| 23||XXX|12 |1
4 |X9X|4 6|| 6| |4 6||X5X| |
8 |XXX| 8 ||7 |7 |7 ||XXX| | 8
---+---+---||---+---+---||---+---+---
XXX|XXX| ||1 |XXX|XXX||1 |XXX|1
X7X|X2X|4 6|| 6|X8X|X5X|| 6|X3X|
XXX|XXX| || 9|XXX|XXX|| |XXX|
---+---+---||---+---+---||---+---+---
XXX| |XXX|| 23| 3| 23||XXX|XXX|XXX
X5X| |X1X|| 6| | 6||X9X|X4X|X7X
XXX| 8 |XXX|| | | ||XXX|XXX|XXX`
//This grid is #102 from Totally Pocket Sudoku. It's included for testing purposes only.
//This is also in a costant because we want to just string compare it to the loaded file.
const ADVANCED_TEST_GRID = `.|5|.|.|.|.|7|4|.
7|2|.|.|3|.|.|.|.
.|.|1|5|.|.|.|.|8
.|8|3|.|.|2|.|.|.
1|.|.|.|5|.|.|.|2
.|.|.|9|.|.|1|7|.
9|.|.|.|.|1|4|.|.
.|.|.|.|8|.|.|9|1
.|1|6|.|.|.|.|3|.`
const SOLVED_ADVANCED_TEST_GRID = `3|5|8|2|1|6|7|4|9
7|2|9|8|3|4|5|1|6
4|6|1|5|9|7|3|2|8
6|8|3|1|7|2|9|5|4
1|9|7|4|5|3|8|6|2
5|4|2|9|6|8|1|7|3
9|3|5|6|2|1|4|8|7
2|7|4|3|8|5|6|9|1
8|1|6|7|4|9|2|3|5`
func init() {
runtime.GOMAXPROCS(_NUM_SOLVER_THREADS)
}
//returns true if the provided grid is a gridImpl
func isGridImpl(grid Grid) bool {
switch grid.(type) {
default:
return false
case *gridImpl:
return true
}
}
//returns true if the provided grid is a mutableGridImpl
func isMutableGridImpl(grid Grid) bool {
switch grid.(type) {
default:
return false
case *mutableGridImpl:
return true
}
}
func BenchmarkGridCopy(b *testing.B) {
grid := LoadSDK(ADVANCED_TEST_GRID)
for i := 0; i < b.N; i++ {
_ = grid.Copy()
}
}
func TestGridCopy(t *testing.T) {
grid := MutableLoadSDK(ADVANCED_TEST_GRID)
cell := grid.MutableCell(0, 0)
cell.SetMark(3, true)
cell.SetMark(4, true)
cell = grid.MutableCell(0, 2)
cell.SetExcluded(3, true)
cell.SetExcluded(4, true)
gridCopy := grid.Copy()
if !isGridImpl(gridCopy) {
t.Error("Expected grid.copy() to return a gridImpl but it didn't")
}
if grid.Diagram(true) != gridCopy.Diagram(true) {
t.Error("Grid and copy don't match in marks. Got", gridCopy.Diagram(true), "wanted", grid.Diagram(true))
}
if grid.Diagram(false) != gridCopy.Diagram(false) {
t.Error("Grid and copy don't match in terms of excludes. Got", gridCopy.Diagram(false), "wanted", grid.Diagram(false))
}
//Make sure that the grid that was returned from copy does not change when the grid it was derived from is modified.
grid.MutableCell(1, 2).SetNumber(5)
if grid.Diagram(true) == gridCopy.Diagram(true) {
t.Error("A read-only grid copy changed when the grid it was created from was mutated. Got", gridCopy.Diagram(true), "from both")
}
}
func TestGridCreation(t *testing.T) {
blockUpperLeftRow := make([]int, DIM)
blockUpperLeftCol := make([]int, DIM)
row := 0
col := -1 * BLOCK_DIM
for i := 0; i < DIM; i++ {
col += BLOCK_DIM
if col >= DIM {
col = 0
row += BLOCK_DIM
}
blockUpperLeftRow[i] = row
blockUpperLeftCol[i] = col
}
cellData := "1"
rowData := strings.Join(nCopies(cellData, DIM), COL_SEP)
data := strings.Join(nCopies(rowData, DIM), ROW_SEP)
grid := MutableLoadSDK(data)
if !isMutableGridImpl(grid) {
t.Error("Expected grid to be mutable under the covers but it wasn't")
}
//We want to run all of the same tests on a Grid and MutableGrid.
roGrid := grid.Copy()
if !isGridImpl(roGrid) {
t.Error("Expected ROgrid to be immutable under the covers but it wasn't")
}
grids := []struct {
grid Grid
description string
}{
{
grid: grid,
description: "Mutable grid",
},
{
grid: roGrid,
description: "Read Only Grid",
},
}
for _, config := range grids {
grid := config.grid
description := config.description
if len(grid.Cells()) != DIM*DIM {
t.Error(description, "Didn't generate enough cells")
}
if grid.DataString() != data {
t.Error(description, "The grid round-tripped with different result than data in")
}
if grid.Cells()[10].Number() != 1 {
t.Error(description, "A random spot check of a cell had the wrong number: %s", grid.Cells()[10])
}
if grid.numFilledCells() != DIM*DIM {
t.Error(description, "We didn't think all cells were filled, but they were!")
}
for count := 0; count < DIM; count++ {
col := grid.Col(count)
if num := len(col); num != DIM {
t.Error(description, "We got back a column but it had the wrong amount of items: ", num, "\n")
}
for i, cell := range col {
if cell.Col() != count {
t.Error(description, "One of the cells we got back when asking for column ", count, " was not in the right column.")
}
if cell.Row() != i {
t.Error(description, "One of the cells we got back when asking for column ", count, " was not in the right row.")
}
}
row := grid.Row(count)
if len(row) != DIM {
t.Error(description, "We got back a row but it had the wrong number of items.")
}
for i, cell := range row {
if cell.Row() != count {
t.Error(description, "One of the cells we got back when asking for row ", count, " was not in the right rows.")
}
if cell.Col() != i {
t.Error(description, "One of the cells we got back from row ", count, " was not in the right column.")
}
}
block := grid.Block(count)
if len(block) != DIM {
t.Error(description, "We got back a block but it had the wrong number of items.")
}
for _, cell := range block {
if cell.Block() != count {
t.Error(description, "We got a cell back in a block with the wrong block number")
}
}
if block[0].Row() != blockUpperLeftRow[count] || block[0].Col() != blockUpperLeftCol[count] {
t.Error(description, "We got back the wrong first cell from block ", count, ": ", block[0])
}
if block[DIM-1].Row() != blockUpperLeftRow[count]+BLOCK_DIM-1 || block[DIM-1].Col() != blockUpperLeftCol[count]+BLOCK_DIM-1 {
t.Error(description, "We got back the wrong last cell from block ", count, ": ", block[0])
}
}
cell := grid.Cell(2, 2)
if cell.Row() != 2 || cell.Col() != 2 {
t.Error(description, "We grabbed a cell but what we got back was the wrong row and col.")
}
neighbors := cell.Neighbors()
if len(neighbors) != _NUM_NEIGHBORS {
t.Error(description, "We got a different number of neighbors than what we were expecting: ", len(neighbors))
}
neighborsMap := make(map[Cell]bool)
for _, neighbor := range neighbors {
if neighbor == nil {
t.Error(description, "We found a nil neighbor")
}
if neighbor.Row() != cell.Row() && neighbor.Col() != cell.Col() && neighbor.Block() != cell.Block() {
t.Error(description, "We found a neighbor in ourselves that doesn't appear to be related: Neighbor: ", neighbor, " Cell: ", cell)
}
if _, ok := neighborsMap[neighbor]; ok {
t.Error(description, "We found a duplicate in the neighbors list")
} else {
neighborsMap[cell] = true
}
}
}
}
func TestGridCells(t *testing.T) {
grid := MutableLoadSDK(TEST_GRID)
//Test the mutableGridImpl
if !isMutableGridImpl(grid) {
t.Error("Expected to get a mutableGridimpl from LoadSDK")
}
cells := grid.MutableCells()
if len(cells) != DIM*DIM {
t.Fatal("Grid.cells gave back a cellslice with wrong number of cells", len(cells))
}
//Make sure it's the same cells
for i, cell := range cells {
if cell.Grid() != grid {
t.Error("cell #", i, "had wrong grid")
}
if cell != grid.MutableCell(cell.Row(), cell.Col()) {
t.Error("cell #", i, "was not the same as the right one in the grid")
}
}
//Now do the same test for a gridImpl
roGrid := grid.Copy()
if !isGridImpl(roGrid) {
t.Error("Expected to get a gridImpl from grid.Copy")
}
roCells := roGrid.Cells()
if len(roCells) != DIM*DIM {
t.Fatal("Grid.cells gave back a cellslice with wrong number of cells", len(roCells))
}
//Make sure it's the same cells
for i, cell := range roCells {
if cell.Grid() != roGrid {
t.Error("cell #", i, "had wrong grid")
}
if cell != roGrid.Cell(cell.Row(), cell.Col()) {
t.Error("cell #", i, "was not the same as the right one in the grid")
}
}
//make sure mutating a cell from the celllist mutates the grid.
cell := cells[3]
if cell.Number() != 0 {
t.Fatal("We expected cell #3 to be empty, but had", cell.Number())
}
cell.SetNumber(3)
if grid.Cell(0, 3).Number() != 3 {
t.Error("Mutating a cellin cells did not mutate the grid.")
}
}
func TestMutableGridLoad(t *testing.T) {
//Substantially recreated in TestGridLoad
grid := MutableLoadSDK(TEST_GRID)
if !isMutableGridImpl(grid) {
t.Fatal("Expected grid from LoadSDK to be mutable")
}
cell := grid.MutableCell(0, 0)
if cell.Number() != 6 {
t.Error("The loaded grid did not have a 6 in the upper left corner")
}
cell = grid.MutableCell(DIM-1, DIM-1)
if cell.Number() != 7 {
t.Error("The loaded grid did not have a 7 in the bottom right corner")
}
if grid.DataString() != TEST_GRID {
t.Error("The real test grid did not survive a round trip via DataString: \n", grid.DataString(), "\n\n", TEST_GRID)
}
if grid.Diagram(false) != TEST_GRID_DIAGRAM {
t.Error("The grid did not match the expected diagram: \n", grid.Diagram(false))
}
//Twiddle an exclude to make sure it copies over correctly.
grid.MutableCell(2, 0).SetExcluded(4, true)
if grid.Diagram(false) != TEST_GRID_EXCLUDED_DIAGRAM {
t.Error("Diagram did not reflect the manually excluded item: \n", grid.Diagram(false))
}
//Test copying.
copy := grid.MutableCopy()
if grid.DataString() != copy.DataString() {
t.Error("Copied grid does not have the same datastring!")
}
for c, cell := range grid.MutableCells() {
copyCell := cell.MutableInGrid(copy)
cellI := cell.(*mutableCellImpl)
copyCellI := copyCell.(*mutableCellImpl)
if !IntSlice(cellI.impossibles[:]).SameAs(IntSlice(copyCellI.impossibles[:])) {
t.Error("Cells at position", c, "had different impossibles:\n", cellI.impossibles, "\n", copyCellI.impossibles)
}
for i := 1; i <= DIM; i++ {
if cell.Possible(i) != copyCell.Possible(i) {
t.Error("The copy of the grid did not have the same possible at cell ", c, " i ", i)
}
}
}
copy.MutableCell(0, 0).SetNumber(5)
if copy.Cell(0, 0).Number() == grid.Cell(0, 0).Number() {
t.Error("When we modified the copy's cell, it also affected the original.")
}
if grid.Solved() {
t.Error("Grid reported it was solved when it was not.")
}
if grid.Invalid() {
t.Error("Grid thought it was invalid when it wasn't: \n", grid.Diagram(false))
}
previousRank := grid.rank()
grid = withSimpleCellsFilled(grid).MutableCopy()
cell = cell.MutableInGrid(grid)
if num := previousRank - grid.rank(); num != 45 {
t.Error("We filled simple cells on the test grid but didn't get as many as we were expecting: ", num, "/", 45)
}
if grid.Invalid() {
t.Error("fillSimpleCells filled in something that made the grid invalid: \n", grid.Diagram(false))
}
if !grid.Solved() {
t.Error("Grid didn't think it was solved when it was.")
}
if grid.DataString() != SOLVED_TEST_GRID {
t.Error("After filling simple cells, the grid was not actually solved correctly.")
}
cell.SetNumber(cell.Number() + 1)
if !grid.Invalid() {
t.Error("Grid didn't notice it was invalid when it actually was.", grid)
}
cell.SetNumber(cell.Number() - 1)
if grid.Invalid() {
t.Error("Grid didn't noticed when it flipped from being invalid to being valid again.")
}
for i := 1; i <= DIM; i++ {
cell.setImpossible(i)
}
if !grid.Invalid() {
t.Error("Grid didn't notice when it became invalid because one of its cells has no more possibilities")
}
}
func TestGridLoad(t *testing.T) {
//Substantially recreated in TestMutableGridLoad
mutableGrid := LoadSDK(TEST_GRID)
grid := mutableGrid.Copy()
if !isGridImpl(grid) {
t.Fatal("Expected grid from mutable copy to be immutable")
}
cell := grid.Cell(0, 0)
if cell.Number() != 6 {
t.Error("The loaded grid did not have a 6 in the upper left corner")
}
cell = grid.Cell(DIM-1, DIM-1)
if cell.Number() != 7 {
t.Error("The loaded grid did not have a 7 in the bottom right corner")
}
if grid.DataString() != TEST_GRID {
t.Error("The real test grid did not survive a round trip via DataString: \n", grid.DataString(), "\n\n", TEST_GRID)
}
if grid.Diagram(false) != TEST_GRID_DIAGRAM {
t.Error("The grid did not match the expected diagram: \n", grid.Diagram(false))
}
//Test copying.
copy := grid.Copy()
if grid.DataString() != copy.DataString() {
t.Error("Copied grid does not have the same datastring!")
}
for c, cell := range grid.Cells() {
copyCell := cell.InGrid(copy)
cellI := cell.(*cellImpl)
copyCellI := copyCell.(*cellImpl)
if !IntSlice(cellI.impossibles[:]).SameAs(IntSlice(copyCellI.impossibles[:])) {
t.Error("Cells at position", c, "had different impossibles:\n", cellI.impossibles, "\n", copyCellI.impossibles)
}
for i := 1; i <= DIM; i++ {
if cell.Possible(i) != copyCell.Possible(i) {
t.Error("The copy of the grid did not have the same possible at cell ", c, " i ", i)
}
}
}
if grid.Solved() {
t.Error("Grid reported it was solved when it was not.")
}
if grid.Invalid() {
t.Error("Grid thought it was invalid when it wasn't: \n", grid.Diagram(false))
}
previousRank := grid.rank()
grid = withSimpleCellsFilled(grid)
cell = cell.InGrid(grid)
if num := previousRank - grid.rank(); num != 45 {
t.Error("We filled simple cells on the test grid but didn't get as many as we were expecting: ", num, "/", 45)
}
if grid.Invalid() {
t.Error("fillSimpleCells filled in something that made the grid invalid: \n", grid.Diagram(false))
}
if !grid.Solved() {
t.Error("Grid didn't think it was solved when it was.")
}
if grid.DataString() != SOLVED_TEST_GRID {
t.Error("After filling simple cells, the grid was not actually solved correctly.")
}
grid = grid.CopyWithModifications(GridModification{
&CellModification{
Cell: cell.Reference(),
Number: cell.Number() + 1,
},
})
if !grid.Invalid() {
t.Error("Grid didn't notice it was invalid when it actually was.", grid)
}
grid = grid.CopyWithModifications(GridModification{
&CellModification{
Cell: cell.Reference(),
Number: cell.Number(),
},
})
if grid.Invalid() {
t.Error("Grid didn't noticed when it flipped from being invalid to being valid again.")
}
grid = grid.CopyWithModifications(GridModification{
&CellModification{
Cell: cell.Reference(),
Number: 0,
},
})
excludes := map[int]bool{}
for i := 1; i <= DIM; i++ {
excludes[i] = true
}
grid = grid.CopyWithModifications(GridModification{
&CellModification{
Cell: cell.Reference(),
ExcludesChanges: excludes,
},
})
if !grid.Invalid() {
t.Error("Grid didn't notice when it became invalid because one of its cells has no more possibilities", grid.Diagram(false))
}
}
func TestAdvancedSolve(t *testing.T) {
grid, _ := MutableLoadSDKFromFile(puzzlePath("advancedtestgrid.sdk"))
if !isMutableGridImpl(grid) {
t.Fatal("Expected load sdk from file to return mutable grid")
}
roGrid := grid.Copy()
if !isGridImpl(roGrid) {
t.Fatal("Expected copy to return roGrid")
}
//Run all the tests on mutable and non-mutable grids.
data := []struct {
grid Grid
mGridTest bool
description string
}{
{
grid,
true,
"mutable",
},
{
roGrid,
false,
"ro",
},
}
for _, rec := range data {
grid := rec.grid
description := rec.description
if grid.DataString() != ADVANCED_TEST_GRID {
t.Error(description, "Advanced grid didn't survive a roundtrip to DataString")
}
if grid.numFilledCells() != 27 {
t.Error(description, "The advanced grid's rank was wrong at load: ", grid.rank())
}
grid.HasMultipleSolutions()
if grid.DataString() != ADVANCED_TEST_GRID {
t.Error(description, "HasMultipleSolutions mutated the underlying grid.")
}
copy := withSimpleCellsFilled(grid)
if copy.Solved() {
t.Error(description, "Advanced grid was 'solved' with just fillSimpleCells")
}
solutions := grid.Solutions()
if grid.DataString() != ADVANCED_TEST_GRID {
t.Error(description, "Calling Solutions() modified the original grid.")
}
if len(solutions) != 1 {
t.Error(description, "We found the wrong number of solutions in Advanced grid:", len(solutions))
}
if solutions[0].DataString() != SOLVED_ADVANCED_TEST_GRID {
t.Error(description, "Solve found the wrong solution.")
}
if grid.NumSolutions() != 1 {
t.Error(description, "Grid didn't find any solutions but there is one.")
}
if !grid.HasSolution() {
t.Error(description, "Grid didn't find any solutions but there is one.")
}
if rec.mGridTest {
mGrid := grid.MutableCopy()
mGrid.Solve()
if !mGrid.Solved() {
t.Error("The grid itself didn't get mutated to a solved state.")
}
if mGrid.numFilledCells() != DIM*DIM {
t.Error("After solving, we didn't think all cells were filled.")
}
if mGrid.(*mutableGridImpl).cachedSolutions != nil {
t.Error("The cache of solutions was supposed to be expired when we copied in the solution, but it wasn't")
t.Fail()
}
if !mGrid.Solve() {
t.Error("Solve called on already solved grid did not return true")
}
}
//TODO: test that nOrFewerSolutions does stop at max (unless cached)
//TODO: test HasMultipleSolutions
}
}
func TestMultiSolutions(t *testing.T) {
//TODO: consider testing immutable grids here, too. Currently don't since
//this is such an expensive test anyway and we should have good coverage
//elsewhere.
if testing.Short() {
t.Skip("Skipping TestMultiSolutions in short test mode,")
}
var grid MutableGrid
files := map[string]int{
"multiple-solutions.sdk": 4,
"multiple-solutions2.sdk": 2,
}
for i := 0; i < 1000; i++ {
for file, numSolutions := range files {
grid, _ = MutableLoadSDKFromFile(puzzlePath(file))
//Test num solutions from the beginning.
if num := grid.NumSolutions(); num != numSolutions {
t.Fatal("On run", i, "Grid", file, " with", numSolutions, "solutions was found to only have", num)
}
//Get a new version of grid to reset all caches
grid, _ = MutableLoadSDKFromFile(puzzlePath(file))
if !grid.HasMultipleSolutions() {
t.Fatal("On run", i, "Grid", file, "with multiple solutions was reported as only having one.")
}
//Test num solutions after already having done other solution gathering.
//this is in here because at one point calling this after HasMultipleSolutions
//would find 2 vs 1 calling it fresh.
if num := grid.NumSolutions(); num != numSolutions {
t.Fatal("On run", i, "Grid", file, "with", numSolutions, "solutions was found to only have", num, "after calling HasMultipleSolutions first.")
}
}
}
}
func TestTranspose(t *testing.T) {
//TODO: this test doesn't verify that a grid with specific excludes set is transposed as well
//(although that does work)
grid := MutableLoadSDK(TEST_GRID)
transposedGrid := grid.(*mutableGridImpl).transpose()
if transposedGrid == nil {
t.Log("Transpose gave us back a nil grid")
t.FailNow()
}
if transposedGrid == grid {
t.Log("Transpose did not return a copy")
t.Fail()
}
if transposedGrid.DataString() != TRANSPOSED_TEST_GRID {
t.Log("Transpose did not operate correctly")
t.Fail()
}
}
func TestFill(t *testing.T) {
grid := NewGrid()
if !grid.Fill() {
t.Log("We were unable to find a fill for an empty grid.")
t.Fail()
}
if !grid.Solved() {
t.Log("The grid that came back from fill was not actually fully solved.")
t.Fail()
}
}
func BenchmarkFill(b *testing.B) {
for i := 0; i < b.N; i++ {
grid := NewGrid()
grid.Fill()
}
}
func BenchmarkAdvancedSolve(b *testing.B) {
for i := 0; i < b.N; i++ {
grid := MutableLoadSDK(ADVANCED_TEST_GRID)
grid.Solve()
}
}
func BenchmarkDifficulty(b *testing.B) {
//NOTE: this benchmark is exceptionally noisy--it's heavily dependent on
//how quickly the difficulty converges given the specific HumanSolutions
//generated.
for i := 0; i < b.N; i++ {
grid := LoadSDK(ADVANCED_TEST_GRID)
grid.Difficulty()
}
}
func TestGenerate(t *testing.T) {
grid := GenerateGrid(nil)
if grid == nil {
t.Log("We didn't get back a generated grid")
t.Fail()
}
if grid.Solved() {
t.Log("We got back a solved generated grid.")
t.Fail()
}
if grid.HasMultipleSolutions() {
t.Log("We got back a generated grid that has more than one solution.")
t.Fail()
}
lockedCellCount := 0
for _, cell := range grid.Cells() {
if cell.Locked() {
lockedCellCount++
}
}
if lockedCellCount == 0 || lockedCellCount == DIM*DIM {
t.Error("Found an unreasonable number of locked cells from generate:", lockedCellCount)
}
}
func TestGridEmpty(t *testing.T) {
grid := NewGrid()
if !isMutableGridImpl(grid) {
t.Fatal("Expected mutable grid impl from NewGrid")
}
if !grid.Empty() {
t.Error("Fresh grid wasn't empty")
}
roGrid := grid.Copy()
if !isGridImpl(roGrid) {
t.Error("Expected immutable grid impl from NewGrid.Copy()")
}
if !grid.Empty() {
t.Error("RO fresh grid wasn't empty")
}
grid.Load(TEST_GRID)
if grid.Empty() {
t.Error("A filled grid was reported as empty")
}
roGrid = grid.Copy()
if grid.Diagram(false) != roGrid.Diagram(false) {
t.Error("Grid and roGrid had different data strings")
}
if roGrid.Empty() {
t.Error("A filled rogrid was reported as empty")
}
//Reset the grid
for r := 0; r < DIM; r++ {
for c := 0; c < DIM; c++ {
grid.MutableCell(r, c).SetNumber(0)
}
}
if !grid.Empty() {
t.Error("A forcibly cleared grid did not report as empty.")
}
roGrid = grid.Copy()
if !roGrid.Empty() {
t.Error("A forcibly cleared ro grid did not report as empty")
}
}
func TestGenerationOptions(t *testing.T) {
options := DefaultGenerationOptions()
if options.Symmetry != SYMMETRY_VERTICAL {
t.Error("Wrong symmetry")
}
if options.SymmetryPercentage != 0.7 {
t.Error("Wrong Symmetry percentage")
}
if options.MinFilledCells != 0 {
t.Error("Wrong MinFilledCells")
}
}
//This is an extremely expensive test desgined to help ferret out #134.
//TODO: remove this test!
func TestGenerateMultipleSolutions(t *testing.T) {
if testing.Short() {
t.Skip("Skipping TestGenerateDiabolical in short test mode,")
}
var grid MutableGrid
for i := 0; i < 10; i++ {
grid = GenerateGrid(nil)
if grid.HasMultipleSolutions() {
t.Fatal("On run", i, "we got back a generated grid that has more than one solution: ", grid)