mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-06 11:15:43 -05:00
add expect test for the sudoku solver
This commit is contained in:
parent
72d121fa64
commit
2d8fc78bc4
4 changed files with 431 additions and 9 deletions
|
|
@ -282,9 +282,23 @@ let solve_grid (g:Grid.t) : Grid.t option =
|
||||||
let s = Solver.create g in
|
let s = Solver.create g in
|
||||||
Solver.solve s
|
Solver.solve s
|
||||||
|
|
||||||
let solve_file file =
|
module type CHRONO = sig
|
||||||
|
val pp_elapsed : Fmt.formatter -> unit
|
||||||
|
end
|
||||||
|
|
||||||
|
let chrono ~pp_time : (module CHRONO) =
|
||||||
|
let module M = struct
|
||||||
|
let start = Sys.time()
|
||||||
|
let pp_elapsed out =
|
||||||
|
if pp_time then
|
||||||
|
Fmt.fprintf out " (in %.3fs)" (Sys.time() -. start)
|
||||||
|
end in
|
||||||
|
(module M)
|
||||||
|
|
||||||
|
let solve_file ~pp_time file =
|
||||||
|
let open (val chrono ~pp_time) in
|
||||||
Format.printf "solve grids in file %S@." file;
|
Format.printf "solve grids in file %S@." file;
|
||||||
let start = Sys.time() in
|
|
||||||
let grids =
|
let grids =
|
||||||
CCIO.with_in file CCIO.read_lines_l
|
CCIO.with_in file CCIO.read_lines_l
|
||||||
|> CCList.filter_map
|
|> CCList.filter_map
|
||||||
|
|
@ -296,13 +310,13 @@ let solve_file file =
|
||||||
| exception e ->
|
| exception e ->
|
||||||
errorf "cannot parse sudoku %S: %s@." s (Printexc.to_string e))
|
errorf "cannot parse sudoku %S: %s@." s (Printexc.to_string e))
|
||||||
in
|
in
|
||||||
Format.printf "parsed %d grids (in %.3fs)@." (List.length grids) (Sys.time()-.start);
|
Format.printf "parsed %d grids%t@." (List.length grids) pp_elapsed;
|
||||||
List.iter
|
List.iter
|
||||||
(fun g ->
|
(fun g ->
|
||||||
Format.printf "@[<v>@,#########################@,@[<2>solve grid:@ %a@]@]@." Grid.pp g;
|
Format.printf "@[<v>@,#########################@,@[<2>solve grid:@ %a@]@]@." Grid.pp g;
|
||||||
let start=Sys.time() in
|
let open (val chrono ~pp_time) in
|
||||||
match solve_grid g with
|
match solve_grid g with
|
||||||
| None -> Format.printf "no solution (in %.3fs)@." (Sys.time()-.start)
|
| None -> Format.printf "no solution%t@." pp_elapsed
|
||||||
| Some g' when not @@ Grid.is_full g' ->
|
| Some g' when not @@ Grid.is_full g' ->
|
||||||
errorf "grid %a@ is not full" Grid.pp g'
|
errorf "grid %a@ is not full" Grid.pp g'
|
||||||
| Some g' when not @@ Grid.is_valid g' ->
|
| Some g' when not @@ Grid.is_valid g' ->
|
||||||
|
|
@ -310,24 +324,26 @@ let solve_file file =
|
||||||
| Some g' when not @@ Grid.matches ~pat:g g' ->
|
| Some g' when not @@ Grid.matches ~pat:g g' ->
|
||||||
errorf "grid %a@ @[<2>does not match original@ %a@]" Grid.pp g' Grid.pp g
|
errorf "grid %a@ @[<2>does not match original@ %a@]" Grid.pp g' Grid.pp g
|
||||||
| Some g' ->
|
| Some g' ->
|
||||||
Format.printf "@[<v>@[<2>solution (in %.3fs):@ %a@]@,###################@]@."
|
Format.printf "@[<v>@[<2>solution%t:@ %a@]@,###################@]@."
|
||||||
(Sys.time()-.start) Grid.pp g')
|
pp_elapsed Grid.pp g')
|
||||||
grids;
|
grids;
|
||||||
Format.printf "@.solved %d grids (in %.3fs)@." (List.length grids) (Sys.time()-.start);
|
Format.printf "@.solved %d grids%t@." (List.length grids) pp_elapsed;
|
||||||
()
|
()
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Fmt.set_color_default true;
|
Fmt.set_color_default true;
|
||||||
let files = ref [] in
|
let files = ref [] in
|
||||||
let debug = ref 0 in
|
let debug = ref 0 in
|
||||||
|
let pp_time = ref true in
|
||||||
let opts = [
|
let opts = [
|
||||||
"--debug", Arg.Set_int debug, " debug";
|
"--debug", Arg.Set_int debug, " debug";
|
||||||
"-d", Arg.Set_int debug, " debug";
|
"-d", Arg.Set_int debug, " debug";
|
||||||
|
"--no-time", Arg.Clear pp_time, " do not print solve time";
|
||||||
] |> Arg.align in
|
] |> Arg.align in
|
||||||
Arg.parse opts (fun f -> files := f :: !files) "sudoku_solve [options] <file>";
|
Arg.parse opts (fun f -> files := f :: !files) "sudoku_solve [options] <file>";
|
||||||
Log.set_debug !debug;
|
Log.set_debug !debug;
|
||||||
try
|
try
|
||||||
List.iter (fun f -> solve_file f) !files;
|
List.iter (fun f -> solve_file ~pp_time:!pp_time f) !files;
|
||||||
with
|
with
|
||||||
| Failure msg | Invalid_argument msg ->
|
| Failure msg | Invalid_argument msg ->
|
||||||
Format.printf "@{<Red>Error@}:@.%s@." msg;
|
Format.printf "@{<Red>Error@}:@.%s@." msg;
|
||||||
|
|
|
||||||
12
examples/sudoku/tests/dune
Normal file
12
examples/sudoku/tests/dune
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
(rule
|
||||||
|
(targets head15_top1465.out)
|
||||||
|
(deps head15_top1465.txt)
|
||||||
|
(action
|
||||||
|
(with-stdout-to
|
||||||
|
%{targets}
|
||||||
|
(run ../sudoku_solve.exe --no-time %{deps}))))
|
||||||
|
|
||||||
|
(alias
|
||||||
|
(name runtest)
|
||||||
|
(action (diff head15_top1465.expected head15_top1465.out)))
|
||||||
379
examples/sudoku/tests/head15_top1465.expected
Normal file
379
examples/sudoku/tests/head15_top1465.expected
Normal file
|
|
@ -0,0 +1,379 @@
|
||||||
|
solve grids in file "head15_top1465.txt"
|
||||||
|
parsed 15 grids
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
4...3....
|
||||||
|
...6..8..
|
||||||
|
........1
|
||||||
|
....5..9.
|
||||||
|
.8....6..
|
||||||
|
.7.2.....
|
||||||
|
...1.27..
|
||||||
|
5.3....4.
|
||||||
|
9........
|
||||||
|
|
||||||
|
solution:
|
||||||
|
468931527
|
||||||
|
751624839
|
||||||
|
392578461
|
||||||
|
134756298
|
||||||
|
289413675
|
||||||
|
675289314
|
||||||
|
846192753
|
||||||
|
513867942
|
||||||
|
927345186
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
7.8...3..
|
||||||
|
...2.1...
|
||||||
|
5........
|
||||||
|
.4.....26
|
||||||
|
3...8....
|
||||||
|
...1...9.
|
||||||
|
.9.6....4
|
||||||
|
....7.5..
|
||||||
|
.........
|
||||||
|
|
||||||
|
solution:
|
||||||
|
728946315
|
||||||
|
934251678
|
||||||
|
516738249
|
||||||
|
147593826
|
||||||
|
369482157
|
||||||
|
852167493
|
||||||
|
293615784
|
||||||
|
481379562
|
||||||
|
675824931
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
7.8...3..
|
||||||
|
...6.1...
|
||||||
|
5........
|
||||||
|
.4.....26
|
||||||
|
3...8....
|
||||||
|
...1...9.
|
||||||
|
.9.2....4
|
||||||
|
....7.5..
|
||||||
|
.........
|
||||||
|
|
||||||
|
solution:
|
||||||
|
768942315
|
||||||
|
934651278
|
||||||
|
512738649
|
||||||
|
147593826
|
||||||
|
329486157
|
||||||
|
856127493
|
||||||
|
693215784
|
||||||
|
481379562
|
||||||
|
275864931
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
3.7.4....
|
||||||
|
.......91
|
||||||
|
8........
|
||||||
|
4.....7..
|
||||||
|
...16....
|
||||||
|
...25....
|
||||||
|
......38.
|
||||||
|
.9....5..
|
||||||
|
.2.6.....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
317849265
|
||||||
|
245736891
|
||||||
|
869512473
|
||||||
|
456398712
|
||||||
|
732164958
|
||||||
|
981257634
|
||||||
|
174925386
|
||||||
|
693481527
|
||||||
|
528673149
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
5..7..6..
|
||||||
|
..38.....
|
||||||
|
......2..
|
||||||
|
62.4.....
|
||||||
|
.......91
|
||||||
|
7........
|
||||||
|
....35.8.
|
||||||
|
4.....1..
|
||||||
|
....9....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
582743619
|
||||||
|
963821547
|
||||||
|
174956238
|
||||||
|
621479853
|
||||||
|
348562791
|
||||||
|
795318426
|
||||||
|
217635984
|
||||||
|
439287165
|
||||||
|
856194372
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
4..7..6..
|
||||||
|
..38.....
|
||||||
|
......2..
|
||||||
|
62.5.....
|
||||||
|
.......91
|
||||||
|
7........
|
||||||
|
....43.8.
|
||||||
|
5.....1..
|
||||||
|
....9....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
482731659
|
||||||
|
963852417
|
||||||
|
175964238
|
||||||
|
621579843
|
||||||
|
358426791
|
||||||
|
794318526
|
||||||
|
217643985
|
||||||
|
539287164
|
||||||
|
846195372
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
.4..1.2..
|
||||||
|
.....9.7.
|
||||||
|
.1.......
|
||||||
|
...43.6..
|
||||||
|
8......5.
|
||||||
|
...2.....
|
||||||
|
7.5..8...
|
||||||
|
...6..3..
|
||||||
|
9........
|
||||||
|
|
||||||
|
solution:
|
||||||
|
347516289
|
||||||
|
258349176
|
||||||
|
619872543
|
||||||
|
591437628
|
||||||
|
823961754
|
||||||
|
476285931
|
||||||
|
735198462
|
||||||
|
182654397
|
||||||
|
964723815
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
7.5.....2
|
||||||
|
...4.1...
|
||||||
|
3........
|
||||||
|
.1.6..4..
|
||||||
|
2...5....
|
||||||
|
.......9.
|
||||||
|
...37....
|
||||||
|
.8....6..
|
||||||
|
.9.....8.
|
||||||
|
|
||||||
|
solution:
|
||||||
|
745896132
|
||||||
|
928431756
|
||||||
|
361725849
|
||||||
|
519683427
|
||||||
|
274159368
|
||||||
|
836247591
|
||||||
|
652378914
|
||||||
|
483912675
|
||||||
|
197564283
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
......41.
|
||||||
|
9..3.....
|
||||||
|
3...5....
|
||||||
|
.48..7...
|
||||||
|
.......62
|
||||||
|
.1.......
|
||||||
|
6..2....5
|
||||||
|
.7....8..
|
||||||
|
....9....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
857629413
|
||||||
|
924318657
|
||||||
|
361754928
|
||||||
|
248167539
|
||||||
|
793485162
|
||||||
|
516932784
|
||||||
|
689271345
|
||||||
|
172543896
|
||||||
|
435896271
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
7.5.....2
|
||||||
|
...4.1...
|
||||||
|
3........
|
||||||
|
.1.6..4..
|
||||||
|
2...5....
|
||||||
|
.......9.
|
||||||
|
...37....
|
||||||
|
.9....8..
|
||||||
|
.8.....6.
|
||||||
|
|
||||||
|
solution:
|
||||||
|
745896312
|
||||||
|
928431756
|
||||||
|
361527984
|
||||||
|
519683427
|
||||||
|
274159638
|
||||||
|
836742591
|
||||||
|
652378149
|
||||||
|
193264875
|
||||||
|
487915263
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
.8..1....
|
||||||
|
..5....3.
|
||||||
|
......4..
|
||||||
|
...6.5.7.
|
||||||
|
89....2..
|
||||||
|
...3.....
|
||||||
|
2.....1.9
|
||||||
|
..67.....
|
||||||
|
...4.....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
382514697
|
||||||
|
145976832
|
||||||
|
769238415
|
||||||
|
421695378
|
||||||
|
893147256
|
||||||
|
657382941
|
||||||
|
274853169
|
||||||
|
936721584
|
||||||
|
518469723
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
8.9...3..
|
||||||
|
...7.1...
|
||||||
|
5........
|
||||||
|
.7.....26
|
||||||
|
3...9....
|
||||||
|
...1...4.
|
||||||
|
.6.2....4
|
||||||
|
....8.5..
|
||||||
|
.........
|
||||||
|
|
||||||
|
solution:
|
||||||
|
819462375
|
||||||
|
632751489
|
||||||
|
547839612
|
||||||
|
178543926
|
||||||
|
324697158
|
||||||
|
956128743
|
||||||
|
763215894
|
||||||
|
291384567
|
||||||
|
485976231
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
6.9.....8
|
||||||
|
...7.1...
|
||||||
|
4........
|
||||||
|
....6...4
|
||||||
|
.2.....3.
|
||||||
|
.3....5..
|
||||||
|
.1.5...7.
|
||||||
|
8...9....
|
||||||
|
......2..
|
||||||
|
|
||||||
|
solution:
|
||||||
|
679325148
|
||||||
|
283741965
|
||||||
|
451986327
|
||||||
|
598263714
|
||||||
|
124857639
|
||||||
|
736419582
|
||||||
|
912534876
|
||||||
|
867192453
|
||||||
|
345678291
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
......41.
|
||||||
|
9..3.....
|
||||||
|
3...2....
|
||||||
|
.48..7...
|
||||||
|
.......52
|
||||||
|
.1.......
|
||||||
|
5..2....6
|
||||||
|
.7....8..
|
||||||
|
....9....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
827569413
|
||||||
|
964318527
|
||||||
|
351724968
|
||||||
|
248157639
|
||||||
|
793486152
|
||||||
|
615932784
|
||||||
|
589271346
|
||||||
|
172643895
|
||||||
|
436895271
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
#########################
|
||||||
|
solve grid:
|
||||||
|
1...48...
|
||||||
|
.5....9..
|
||||||
|
..6...3..
|
||||||
|
...57.2..
|
||||||
|
8.3......
|
||||||
|
...9.....
|
||||||
|
.......41
|
||||||
|
67.......
|
||||||
|
...2.....
|
||||||
|
|
||||||
|
solution:
|
||||||
|
139648752
|
||||||
|
758132964
|
||||||
|
246795318
|
||||||
|
461573289
|
||||||
|
893426175
|
||||||
|
527981436
|
||||||
|
982357641
|
||||||
|
674819523
|
||||||
|
315264897
|
||||||
|
|
||||||
|
###################
|
||||||
|
|
||||||
|
solved 15 grids
|
||||||
15
examples/sudoku/tests/head15_top1465.txt
Normal file
15
examples/sudoku/tests/head15_top1465.txt
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
4...3.......6..8..........1....5..9..8....6...7.2........1.27..5.3....4.9........
|
||||||
|
7.8...3.....2.1...5.........4.....263...8.......1...9..9.6....4....7.5...........
|
||||||
|
7.8...3.....6.1...5.........4.....263...8.......1...9..9.2....4....7.5...........
|
||||||
|
3.7.4...........918........4.....7.....16.......25..........38..9....5...2.6.....
|
||||||
|
5..7..6....38...........2..62.4............917............35.8.4.....1......9....
|
||||||
|
4..7..6....38...........2..62.5............917............43.8.5.....1......9....
|
||||||
|
.4..1.2.......9.7..1..........43.6..8......5....2.....7.5..8......6..3..9........
|
||||||
|
7.5.....2...4.1...3.........1.6..4..2...5...........9....37.....8....6...9.....8.
|
||||||
|
......41.9..3.....3...5.....48..7..........62.1.......6..2....5.7....8......9....
|
||||||
|
7.5.....2...4.1...3.........1.6..4..2...5...........9....37.....9....8...8.....6.
|
||||||
|
.8..1......5....3.......4.....6.5.7.89....2.....3.....2.....1.9..67........4.....
|
||||||
|
8.9...3.....7.1...5.........7.....263...9.......1...4..6.2....4....8.5...........
|
||||||
|
6.9.....8...7.1...4............6...4.2.....3..3....5...1.5...7.8...9..........2..
|
||||||
|
......41.9..3.....3...2.....48..7..........52.1.......5..2....6.7....8......9....
|
||||||
|
1...48....5....9....6...3.....57.2..8.3.........9............4167..........2.....
|
||||||
Loading…
Add table
Reference in a new issue