Added test for grid computation

This commit is contained in:
flying-scorpio 2019-11-30 15:21:33 +01:00
parent 5080b0f815
commit fa967fd320
4 changed files with 99 additions and 15 deletions

View file

@ -30,14 +30,39 @@ class Board:
self.__assert_lines_and_columns_have_same_values()
def __str__(self):
return "\n".join(self.lines)
output = ""
for line in self.lines:
for item in line:
if isinstance(item, set):
if len(item) == 1:
output += str(item)[1:-1]
else:
output += '.'
elif isinstance(item, Square):
if len(item.content) == 1:
output += str(item.content)[1:-1]
else:
output += '.'
else:
output += item
output += ' '
output += '\n'
return output
def __assert_lines_and_columns_have_same_values(self):
differences = [
'X' for col, column in enumerate(self.columns)
for lin, line, in enumerate(self.lines)
if self.lines[lin][col] != self.columns[col][lin]
]
differences = []
for lin in range(len(self.lines)):
for col in range(len(self.columns)):
if isinstance(self.lines[lin][col], Square):
line_content = self.lines[lin][col].content
column_content = self.columns[col][lin].content
else:
line_content = self.lines[lin][col]
column_content = self.columns[col][lin]
if line_content != column_content:
differences.append('X')
assert len(differences) == 0
@ -58,7 +83,7 @@ class Board:
if str(number) not in line
}
else:
self.lines[line_index][column] = {line[column]}
self.lines[line_index][column] = {int(line[column])}
def __compute_possible_values_from_columns(self):
"""Compute possible values for each number in a column.
@ -77,7 +102,7 @@ class Board:
if str(number) not in column
}
else:
self.columns[column_index][line] = {column[line]}
self.columns[column_index][line] = {int(column[line])}
def compute_possible_values(self):
"""Compute possible values for each square in the board."""
@ -111,10 +136,4 @@ class Board:
self.lines[lin][col] = Square(lin, col, line[col])
self.columns[col][lin] = Square(lin, col, column[lin])
differences = [
'X' for col, column in enumerate(self.columns)
for lin, line, in enumerate(self.lines)
if self.lines[lin][col].content != self.columns[col][lin].content
]
assert len(differences) == 0
self.__assert_lines_and_columns_have_same_values()

View file

@ -26,6 +26,8 @@ def main():
board.intersect_possible_values()
board.create_squares()
print(board)
if __name__ == '__main__':
main()

View file

@ -24,3 +24,26 @@ class Square:
3 4 5
6 7 8
"""
grids = [
(0, 0), # grid 0
(0, 1), # grid 1
(0, 2), # grid 2
(1, 0), # grid 3
(1, 1), # grid 4
(1, 2), # grid 5
(2, 0), # grid 6
(2, 1), # grid 7
(2, 2), # grid 8
]
grid_coordinates = (self.line // 3, self.column // 3)
grid_number = 0
while grids[grid_number] != grid_coordinates:
grid_number += 1
if grid_number == 9:
return None
return grid_number

40
tests.py Normal file
View file

@ -0,0 +1,40 @@
"""All tests for the Sudoku Solver."""
import unittest
from square import Square
class TestSquare(unittest.TestCase):
"""TestCase for Square"""
def setUp(self):
content = '.'
self.board = [
Square(line, column, content)
for line in range(9)
for column in range(9)
]
self.assertEqual(len(self.board), 81)
def test_grid_is_correctly_computed(self):
"""Square.grid is correctly computed for all values."""
correct_grids = (
"000111222"
"000111222"
"000111222"
"333444555"
"333444555"
"333444555"
"666777888"
"666777888"
"666777888"
)
for i, square in enumerate(self.board):
self.assertEqual(square.grid, int(correct_grids[i]))
if __name__ == '__main__':
unittest.main()