示例#1
0
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")
	}
}
示例#2
0
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")
	}
}
示例#3
0
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())
		}
	}
}
示例#4
0
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")
	}
}
示例#5
0
文件: main.go 项目: husobee/sudoku
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)
}
示例#6
0
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")
	}
}
示例#7
0
func BenchmarkParsePuzzle(b *testing.B) {
	sudoku.SetRecursionDepth(-1)
	for i := 0; i < b.N; i++ {
		sudoku.ParsePuzzle(strings.NewReader(goodPuzzle))
	}
}