func TestSolvePuzzleRecursionDepth(t *testing.T) { p, err := sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)) if err != nil { t.Errorf("failed to parse a good puzzle, err=%s", err.Error()) } sudoku.SetRecursionDepth(10) err = p.BacktrackSolve() if err != sudoku.ErrSolveExceedRecursionDepth { t.Errorf("should have errored due to recursion depth") } }
func BenchmarkSolvePuzzle(b *testing.B) { sudoku.SetRecursionDepth(-1) p, err := sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)) if err != nil { b.Error(err.Error()) } for i := 0; i < b.N; i++ { var testPuzzle sudoku.Puzzle = p if err := testPuzzle.BacktrackSolve(); err != nil { b.Error(err.Error()) } } }
func TestSolvePuzzle(t *testing.T) { sudoku.SetRecursionDepth(-1) p, err := sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)) if err != nil { t.Errorf("failed to parse a good puzzle, err=%s", err.Error()) } p.BacktrackSolve() buf := bytes.NewBuffer([]byte{}) p.Dump(buf) if buf.String() != goodPuzzleSolve { t.Errorf("failed to solve puzzle correctly") } }
func main() { // set the recursion depth allowable for our backtrack solving algorithm sudoku.SetRecursionDepth(recursionDepth) // take stdin and make a Puzzle p, err := sudoku.ParsePuzzle(os.Stdin) if err != nil { // bad input log.Fatalf("Invalid Puzzle: %s", err.Error()) } // attempt to solve the puzzle if err := p.BacktrackSolve(); err != nil { // couldn't solve the puzzle log.Fatalf("Error solving Puzzle: %s", err.Error()) } // dump out the solution to stdout p.Dump(os.Stdout) }
func BenchmarkParsePuzzle(b *testing.B) { sudoku.SetRecursionDepth(-1) for i := 0; i < b.N; i++ { sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)) } }