Ejemplo n.º 1
0
        public void TrySolve_WithInvalidPuzzle_ReturnsFalse(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();

            Assert.False(solver.TrySolve(puzzle));
        }
Ejemplo n.º 2
0
        public void HasUniqueSolution_WithUniqueSolution_ReturnsTrue(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();

            Assert.True(solver.HasUniqueSolution(puzzle));
        }
Ejemplo n.º 3
0
        public void Solve_WithInvalidPuzzle_Throws(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();

            Assert.Throws <ArgumentException>(() => solver.Solve(puzzle));
        }
Ejemplo n.º 4
0
        public bool SudokuSpice(PuzzleSample puzzle)
        {
            var p      = new PuzzleWithPossibleValues(puzzle.NullableJaggedMatrix);
            var solver = StandardPuzzles.CreateSolver();
            var result = solver.Solve(p, randomizeGuesses: true);

            return(result.NumEmptySquares == 0);
        }
Ejemplo n.º 5
0
        public void Generate_WithShortTimeout_ThrowsTimeoutException()
        {
            var generator = new PuzzleGenerator <PuzzleWithPossibleValues>(
                size => new PuzzleWithPossibleValues(size), StandardPuzzles.CreateSolver());

            Assert.Throws <TimeoutException>(
                () => generator.Generate(16, 150, TimeSpan.FromMilliseconds(1)));
        }
Ejemplo n.º 6
0
        public void CreateSolver_CanSolveStandardPuzzles(int?[][] matrix)
        {
            var solver = StandardPuzzles.CreateSolver();

            var solved = solver.Solve(new Puzzle(matrix));

            PuzzleTestUtils.AssertStandardPuzzleSolved(solved);
        }
Ejemplo n.º 7
0
        public void TrySolve_ValidPuzzle_SolvesPuzzleInPlace(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();

            Assert.True(solver.TrySolve(puzzle));
            PuzzleTestUtils.AssertStandardPuzzleSolved(puzzle);
        }
Ejemplo n.º 8
0
        public void HasUniqueSolution_WithCanceledToken_Throws()
        {
            var solver             = StandardPuzzles.CreateSolver();
            var cancellationSource = new CancellationTokenSource();

            cancellationSource.Cancel();
            Assert.Throws <OperationCanceledException>(() => solver.HasUniqueSolution(new PuzzleWithPossibleValues(9), cancellationSource.Token));
        }
Ejemplo n.º 9
0
        public void Solve_ValidPuzzle_SolvesPuzzleCopy(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();
            var solved = solver.Solve(puzzle);

            PuzzleTestUtils.AssertStandardPuzzleSolved(solved);
        }
Ejemplo n.º 10
0
        public void SolveRandomly_ValidPuzzle_SolvesPuzzleInPlace(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();
            var solved = solver.Solve(puzzle, randomizeGuesses: true);

            PuzzleTestUtils.AssertStandardPuzzleSolved(solved);
        }
Ejemplo n.º 11
0
        public void ComputeStatsForAllSolutions_WithInvalidPuzzle_ReturnsNoSolutions(int?[][] matrix)
        {
            var puzzle = new PuzzleWithPossibleValues(matrix);
            var solver = StandardPuzzles.CreateSolver();

            var stats = solver.ComputeStatsForAllSolutions(puzzle);

            Assert.Equal(0, stats.NumSolutionsFound);
        }
        public void Generate_CreatesPuzzleWithUniqueSolution(int size, int numToSet)
        {
            var generator = new StandardPuzzleGenerator();

            PuzzleWithPossibleValues puzzle = generator.Generate(size, numToSet, TimeSpan.FromSeconds(60));

            Assert.Equal(size * size - numToSet, puzzle.NumEmptySquares);
            var        solver = StandardPuzzles.CreateSolver();
            SolveStats stats  = solver.ComputeStatsForAllSolutions(puzzle);

            Assert.Equal(1, stats.NumSolutionsFound);
        }
Ejemplo n.º 13
0
        public void Solve_LeavesPuzzleUnchanged()
        {
            var puzzle = new PuzzleWithPossibleValues(9);
            var solver = StandardPuzzles.CreateSolver();
            var solved = solver.Solve(puzzle);

            for (int row = 0; row < puzzle.Size; ++row)
            {
                for (int col = 0; col < puzzle.Size; ++col)
                {
                    Assert.False(puzzle[row, col].HasValue);
                    Assert.True(solved[row, col].HasValue);
                }
            }
        }
Ejemplo n.º 14
0
        public void HasUniqueSolution_LeavesPuzzleUnchanged(int?[][] matrix)
        {
            var puzzle     = new PuzzleWithPossibleValues(matrix);
            var puzzleCopy = new PuzzleWithPossibleValues(puzzle);
            var solver     = StandardPuzzles.CreateSolver();

            solver.HasUniqueSolution(puzzle);

            for (int row = 0; row < puzzleCopy.Size; ++row)
            {
                for (int col = 0; col < puzzleCopy.Size; ++col)
                {
                    Assert.Equal(puzzleCopy[row, col], puzzle[row, col]);
                }
            }
        }
Ejemplo n.º 15
0
 public void Constructor_WithValidArgs_Works()
 {
     var generator = new PuzzleGenerator <PuzzleWithPossibleValues>(
         size => new PuzzleWithPossibleValues(size), StandardPuzzles.CreateSolver());
 }