From aadaa02e9ea8442b6cf2c816c17ab3605838b2a4 Mon Sep 17 00:00:00 2001 From: Eevee Date: Wed, 7 Jan 2009 09:54:16 -0500 Subject: [PATCH] Added a test for puzzles with diagonal constraints. Created a TestCase subclass to use for tests, too. --- pseudoku/__init__.py | 22 +--------------------- pseudoku/tests/__init__.py | 21 +++++++++++++++++++++ pseudoku/tests/constraints/__init__.py | 0 pseudoku/tests/constraints/diagonal.py | 18 ++++++++++++++++++ pseudoku/tests/integrity/simple.py | 17 ++++------------- 5 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 pseudoku/tests/constraints/__init__.py create mode 100644 pseudoku/tests/constraints/diagonal.py diff --git a/pseudoku/__init__.py b/pseudoku/__init__.py index c7a803b..33fe0b5 100644 --- a/pseudoku/__init__.py +++ b/pseudoku/__init__.py @@ -3,24 +3,4 @@ from grid.constraints import Diagonal import render.text def main(): - grid = Grid.from_string(""" - 3...2...7 - ...3.1... - ..6...4.. - 89.....54 - 4...5...8 - 61.....32 - ..8...2.. - ...5.9... - 1...6...9 - """) - - grid.add_constraint(Diagonal(grid, direction='down', offset=0)) - grid.add_constraint(Diagonal(grid, direction='up', offset=0)) - - r = render.text.AsciiArtGridRenderer() - print r.render_grid(grid) - - grid.solve() - - print r.render_grid(grid) + print "I don't do anything any more!" diff --git a/pseudoku/tests/__init__.py b/pseudoku/tests/__init__.py index e69de29..3821889 100644 --- a/pseudoku/tests/__init__.py +++ b/pseudoku/tests/__init__.py @@ -0,0 +1,21 @@ +from unittest import TestCase + +from pseudoku.grid import Grid + +class SudokuTestCase(TestCase): + """TestCase subclass that provides some convenience methods for testing + sudoku puzzles. + """ + + def assertSolvable(self, grid, msg=None): + """Asserts that the given puzzle, when run through Grid.solve(), + produces a filled grid. + + grid may be either a Grid object or a string. + """ + + if type(grid) == str: + grid = Grid.from_string(grid) + + grid.solve() + self.assertTrue(grid.filled, msg) diff --git a/pseudoku/tests/constraints/__init__.py b/pseudoku/tests/constraints/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pseudoku/tests/constraints/diagonal.py b/pseudoku/tests/constraints/diagonal.py new file mode 100644 index 0000000..c118339 --- /dev/null +++ b/pseudoku/tests/constraints/diagonal.py @@ -0,0 +1,18 @@ +from pseudoku.grid import Grid +from pseudoku.grid.constraints import Diagonal + +from pseudoku.tests import SudokuTestCase + +class DiagonalGridTestCase(SudokuTestCase): + """Tests that grids with extra non-standard constraints are solved.""" + + + def test_main_diagonals(self): + grid = Grid.from_string(""" + 3...2...7 ...3.1... ..6...4.. + 89.....54 4...5...8 61.....32 + ..8...2.. ...5.9... 1...6...9 + """) + grid.add_constraint(Diagonal(grid, direction='down', offset=0)) + grid.add_constraint(Diagonal(grid, direction='up', offset=0)) + self.assertSolvable(grid, "Main diagonals (Color Sudoku, page 8)") diff --git a/pseudoku/tests/integrity/simple.py b/pseudoku/tests/integrity/simple.py index 9558ba3..48cde80 100644 --- a/pseudoku/tests/integrity/simple.py +++ b/pseudoku/tests/integrity/simple.py @@ -1,23 +1,14 @@ -from unittest import TestCase - from pseudoku.grid import Grid -class SimpleGridTestCase(TestCase): +from pseudoku.tests import SudokuTestCase + +class SimpleGridTestCase(SudokuTestCase): """Tests the solving integrity of some puzzles that can be solved using only elimination and uniqueness. """ - def _test_single_puzzle(self, puzzle, msg=None): - """Solves a single puzzle and verifies that the result is, at least, - not incorrect. - """ - - grid = Grid.from_string(puzzle) - grid.solve() - self.assertTrue(grid.filled, msg) - def test_simple(self): - self._test_single_puzzle(""" + self.assertSolvable(""" ...69.... 9.5..876. ..4..1.2. 6...5...3 38.....49 7...3...2 .7.9..3.. .231..4.8 ....83... -- 2.7.4