func TestDump(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()) } buf := bytes.NewBuffer([]byte{}) p.Dump(buf) if buf.String() != goodPuzzle { t.Errorf("failed to dump puzzle correctly") } }
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 TestParsePuzzle(t *testing.T) { if _, err := sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)); err != nil { t.Errorf("failed to parse a good puzzle, err=%s", err.Error()) } if _, err := sudoku.ParsePuzzle(strings.NewReader(NanPuzzle)); err == nil { t.Errorf("failed to error on a bad puzzle") } if _, err := sudoku.ParsePuzzle(strings.NewReader(BadSpacesPuzzle)); err == nil { t.Errorf("failed to error on a bad puzzle") } if _, err := sudoku.ParsePuzzle(strings.NewReader(InvalidRowsToFewPuzzle)); err == nil { t.Errorf("failed to error on a bad puzzle") } if _, err := sudoku.ParsePuzzle(strings.NewReader(InvalidRowsToManyPuzzle)); err == nil { t.Errorf("failed to error on a bad puzzle") } if _, err := sudoku.ParsePuzzle(strings.NewReader(InvalidLengthPuzzle)); err == nil { t.Errorf("failed to error on a bad puzzle") } }
func BenchmarkParsePuzzle(b *testing.B) { sudoku.SetRecursionDepth(-1) for i := 0; i < b.N; i++ { sudoku.ParsePuzzle(strings.NewReader(goodPuzzle)) } }