python-ai / src / sudoku / sudoku_test.py

#!/usr/bin/env python

import unittest
import tempfile
import os
import sudoku

class TestEmptyBoard(unittest.TestCase):
    
    def test_board4(self):
        b = sudoku.Board(size=4)
        self.assertEqual(b.size, 4)
        self.assertEqual(b.width, 2)
        self.assertFalse(b.complete())
        self.assertFalse(b.filled())
        self.assertFalse(b.valid())
    
    def test_board25(self):
        b = sudoku.Board(size=25)
        self.assertEqual(b.size, 25)
        self.assertEqual(b.width, 5)
        self.assertFalse(b.complete())
        self.assertFalse(b.filled())
        self.assertFalse(b.valid())


class TestBoardFromFile(unittest.TestCase):
    
    def setUp(self):
        (fp, p) = tempfile.mkstemp(suffix='.sudoku')
        self.path = p
        os.close(fp)
    
    def tearDown(self):
        os.remove(self.path)
    
    def test_board4_unfilled(self):
        f = open(self.path, 'w')
        f.write("4\n")
        f.write("5\n")
        f.write("1 2 2\n")
        f.write("2 4 2\n")
        f.write("3 2 3\n")
        f.write("3 3 4\n")
        f.write("4 1 4\n")
        f.close()
        
        b = sudoku.Board(filename=self.path)
        self.assertEqual(b.size, 4)
        self.assertEqual(b.width, 2)
        self.assertFalse(b.complete())
        self.assertFalse(b.filled())
        self.assertFalse(b.valid())
    
    def test_board4_invalid(self):
        f = open(self.path, 'w')
        f.write("4\n")
        f.write("16\n")
        f.write("1 1 3\n")
        f.write("1 2 4\n")
        f.write("1 3 1\n")
        f.write("1 4 2\n")
        f.write("2 1 2\n")
        f.write("2 2 1\n")
        f.write("2 3 4\n")
        f.write("2 4 3\n")
        f.write("3 1 4\n")
        f.write("3 2 3\n")
        f.write("3 3 2\n")
        f.write("3 4 1\n")
        f.write("4 1 1\n")
        f.write("4 2 2\n")
        f.write("4 3 4\n") # invalid
        f.write("4 4 3\n") # invalid
        f.close()
        
        b = sudoku.Board(filename=self.path)
        self.assertEqual(b.size, 4)
        self.assertEqual(b.width, 2)
        self.assertFalse(b.complete())
        self.assertTrue(b.filled())
        self.assertFalse(b.valid())
    
    def test_board4_complete(self):
        f = open(self.path, 'w')
        f.write("4\n")
        f.write("16\n")
        f.write("1 1 3\n")
        f.write("1 2 4\n")
        f.write("1 3 1\n")
        f.write("1 4 2\n")
        f.write("2 1 2\n")
        f.write("2 2 1\n")
        f.write("2 3 4\n")
        f.write("2 4 3\n")
        f.write("3 1 4\n")
        f.write("3 2 3\n")
        f.write("3 3 2\n")
        f.write("3 4 1\n")
        f.write("4 1 1\n")
        f.write("4 2 2\n")
        f.write("4 3 3\n")
        f.write("4 4 4\n")
        f.close()
        
        b = sudoku.Board(filename=self.path)
        self.assertEqual(b.size, 4)
        self.assertEqual(b.width, 2)
        self.assertTrue(b.complete())
        self.assertTrue(b.filled())
        self.assertTrue(b.valid())
    
    def test_board9_unfilled(self):
        f = open(self.path, 'w')
        f.write("9\n")
        f.write("5\n")
        f.write("3 9 1\n")
        f.write("8 3 7\n")
        f.write("6 4 2\n")
        f.write("4 4 3\n")
        f.write("5 9 6\n")
        f.close()
        
        b = sudoku.Board(filename=self.path)
        self.assertEqual(b.size, 9)
        self.assertEqual(b.width, 3)
        self.assertFalse(b.complete())
        self.assertFalse(b.filled())
        self.assertFalse(b.valid())


if __name__ == "__main__":
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.