test_sudoku_solver.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import unittest
  2. from sandbox import sudoku_solver
  3. board_1 = [
  4. ".1.82....",
  5. "...3.6...",
  6. "54..7....",
  7. "..2...95.",
  8. "7.52..1.4",
  9. ".89...2..",
  10. "....8..79",
  11. "...5.3...",
  12. "...792.3.",
  13. ]
  14. solution_1 = [
  15. "317825496",
  16. "298346715",
  17. "546971328",
  18. "432618957",
  19. "765239184",
  20. "189457263",
  21. "623184579",
  22. "971563842",
  23. "854792631",
  24. ]
  25. board_2 = [
  26. "16...7.9.",
  27. ".281.967.",
  28. ".....82..",
  29. "...7....4",
  30. "....6....",
  31. "5....3...",
  32. "..75.....",
  33. ".869.173.",
  34. ".4.3...86",
  35. ]
  36. solution_2 = [
  37. "163427598",
  38. "428159673",
  39. "759638241",
  40. "632795814",
  41. "891264357",
  42. "574813962",
  43. "317586429",
  44. "286941735",
  45. "945372186",
  46. ]
  47. board_3 = [
  48. "..1..8763",
  49. "8..9.....",
  50. "....1..5.",
  51. ".8....4..",
  52. "1.54.3...",
  53. ".6....2..",
  54. "....6..2.",
  55. "4..8.....",
  56. "..6..1378",
  57. ]
  58. solution_3 = [
  59. "941258763",
  60. "857936142",
  61. "632714859",
  62. "789625431",
  63. "125483697",
  64. "364179285",
  65. "518367924",
  66. "473892516",
  67. "296541378",
  68. ]
  69. empty = ["." * 9 for i in range(9)]
  70. class test_sudoku_solver(unittest.TestCase):
  71. def test_simple(self):
  72. board = sudoku_solver.Board()
  73. board.set_initial(board_1)
  74. row = board.get_row(0)
  75. self.assertEqual(row, board_1[0])
  76. col = board.get_col(0)
  77. self.assertEqual(col, "..5.7....")
  78. block = board.get_block(0, 0)
  79. self.assertEqual(block, [".1.", "...", "54."])
  80. block = board.get_block(4, 6)
  81. self.assertEqual(block, ["95.", "1.4", "2.."])
  82. def test_simple_set(self):
  83. board = sudoku_solver.Board()
  84. board.set_initial(board_1)
  85. board.set_cell(5, 7, "6")
  86. row = board.get_row(5)
  87. self.assertEqual(row, ".89...26.")
  88. def test_possibilities(self):
  89. board = sudoku_solver.Board()
  90. board.set_initial(board_1)
  91. p = board.get_possibilities(1, 4)
  92. self.assertEqual(p, {"1", "4", "5"})
  93. def test_all_possibilities(self):
  94. board = sudoku_solver.Board()
  95. board.set_initial(board_1)
  96. p = board.get_all_possibilities()
  97. self.assertEqual(p[1][4], {"1", "4", "5"})
  98. def test_unique_cell(self):
  99. board = sudoku_solver.Board()
  100. board.set_initial(board_1)
  101. solving_steps = [
  102. (5, 7, "6"),
  103. (4, 7, "8"),
  104. (4, 5, "9"),
  105. (2, 5, "1"),
  106. (2, 3, "9"),
  107. (2, 7, "2"),
  108. (6, 5, "4"),
  109. (0, 5, "5"),
  110. (1, 4, "4"),
  111. ]
  112. for x, y, v in solving_steps:
  113. cell = board.get_unique_cell()
  114. self.assertEqual(cell, (x, y))
  115. board.set_cell(x, y, v)
  116. def test_solve(self):
  117. board = sudoku_solver.Board()
  118. board.set_initial(board_2)
  119. b = board.solve()
  120. self.assertEqual(b, solution_2)
  121. board = sudoku_solver.Board()
  122. board.set_initial(board_1)
  123. b = board.solve()
  124. self.assertEqual(b, solution_1)
  125. board = sudoku_solver.Board()
  126. board.set_initial(board_3)
  127. b = board.solve()
  128. self.assertEqual(b, solution_3)
  129. if __name__ == "__main__":
  130. unittest.main()