2017-11-07 23:33:37 +08:00
|
|
|
import unittest
|
2017-11-13 16:53:09 -06:00
|
|
|
import go_counting
|
2017-11-07 23:33:37 +08:00
|
|
|
|
|
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
board5x5 = [
|
2017-11-07 23:33:37 +08:00
|
|
|
" B ",
|
|
|
|
|
" B B ",
|
|
|
|
|
"B W B",
|
|
|
|
|
" W W ",
|
|
|
|
|
" W "
|
2018-04-03 09:52:56 -04:00
|
|
|
]
|
2017-11-07 23:33:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GoCountingTest(unittest.TestCase):
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_black_corner_territory_on_5x5_board(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
stone, territory = board.territory(x=0, y=1)
|
2017-11-13 16:53:09 -06:00
|
|
|
self.assertEqual(stone, go_counting.BLACK)
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territory, {(0, 0), (0, 1), (1, 0)})
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_white_center_territory_on_5x5_board(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
stone, territory = board.territory(x=2, y=3)
|
2017-11-13 16:53:09 -06:00
|
|
|
self.assertEqual(stone, go_counting.WHITE)
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territory, {(2, 3)})
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_open_corner_territory_on_5x5_board(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
stone, territory = board.territory(x=1, y=4)
|
2017-11-13 16:53:09 -06:00
|
|
|
self.assertEqual(stone, go_counting.NONE)
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territory, {(0, 3), (0, 4), (1, 4)})
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_a_stone_and_not_a_territory_on_5x5_board(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
stone, territory = board.territory(x=1, y=1)
|
2017-11-13 16:53:09 -06:00
|
|
|
self.assertEqual(stone, go_counting.NONE)
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territory, set())
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_invalid_because_x_is_too_low(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
board.territory(x=-1, y=1)
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_invalid_because_x_is_too_high(self):
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(board5x5)
|
2018-04-03 09:52:56 -04:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
board.territory(x=5, y=1)
|
|
|
|
|
|
|
|
|
|
def test_invalid_because_y_is_too_low(self):
|
|
|
|
|
board = go_counting.Board(board5x5)
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
board.territory(x=1, y=-1)
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_invalid_because_y_is_too_high(self):
|
|
|
|
|
board = go_counting.Board(board5x5)
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
|
|
|
|
board.territory(x=1, y=5)
|
|
|
|
|
|
|
|
|
|
def test_one_territory_is_the_whole_board(self):
|
|
|
|
|
board = go_counting.Board([" "])
|
2017-11-07 23:33:37 +08:00
|
|
|
territories = board.territories()
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territories[go_counting.BLACK], set())
|
|
|
|
|
self.assertSetEqual(territories[go_counting.WHITE], set())
|
|
|
|
|
self.assertSetEqual(territories[go_counting.NONE], {(0, 0)})
|
2017-11-07 23:33:37 +08:00
|
|
|
|
|
|
|
|
def test_two_territories_rectangular_board(self):
|
2018-04-03 09:52:56 -04:00
|
|
|
input_board = [
|
2017-11-07 23:33:37 +08:00
|
|
|
" BW ",
|
|
|
|
|
" BW "
|
2018-04-03 09:52:56 -04:00
|
|
|
]
|
2017-11-13 16:53:09 -06:00
|
|
|
board = go_counting.Board(input_board)
|
2017-11-07 23:33:37 +08:00
|
|
|
territories = board.territories()
|
2018-04-03 09:52:56 -04:00
|
|
|
self.assertSetEqual(territories[go_counting.BLACK], {(0, 0), (0, 1)})
|
|
|
|
|
self.assertSetEqual(territories[go_counting.WHITE], {(3, 0), (3, 1)})
|
|
|
|
|
self.assertSetEqual(territories[go_counting.NONE], set())
|
2017-11-07 23:33:37 +08:00
|
|
|
|
2018-04-03 09:52:56 -04:00
|
|
|
def test_two_region_rectangular_board(self):
|
|
|
|
|
input_board = [" B "]
|
|
|
|
|
board = go_counting.Board(input_board)
|
|
|
|
|
territories = board.territories()
|
|
|
|
|
self.assertSetEqual(territories[go_counting.BLACK], {(0, 0), (2, 0)})
|
|
|
|
|
self.assertSetEqual(territories[go_counting.WHITE], set())
|
|
|
|
|
self.assertSetEqual(territories[go_counting.NONE], set())
|
|
|
|
|
|
|
|
|
|
# Utility functions
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
2017-11-07 23:33:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|