Skip to content

go-counting: adapt tests to canonical data v1.0.0 #1360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions exercises/go-counting/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Board:
def __init__(self, board):
self.board = board.splitlines()
self.board = board
self.width = len(self.board[0])
self.height = len(self.board)

Expand All @@ -35,10 +35,10 @@ def walk(self, x, y,

return (visited_territory, visited_stones)

def territoryFor(self, coord):
assert len(coord) == 2
x, y = coord[0], coord[1]
if not self.valid(x, y) or self.board[y][x] in STONES:
def territory(self, x, y):
if not self.valid(x, y):
raise ValueError('invalid coordinate')
if self.board[y][x] in STONES:
return (NONE, set())

visited_territory, visited_stones = self.walk(x, y)
Expand All @@ -55,7 +55,7 @@ def territories(self):
for y in range(self.height):
for x in range(self.width):
if not (x, y) in visited:
owner, owned_territories = self.territoryFor((x, y))
owner, owned_territories = self.territory(x, y)
result[owner].update(owned_territories)
visited.update(owned_territories)

Expand Down
5 changes: 3 additions & 2 deletions exercises/go-counting/go_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class Board:
def __init__(self, board):
pass

def territoryFor(self, coord):
def territory(self, x, y):
"""Find the owner and the territories given a coordinate on
the board

Args:
coord ((int,int)): Coordinate on the board
x (int): Column on the board
y (int): Row on the board

Returns:
(str, set): A tuple, the first element being the owner
Expand Down
109 changes: 58 additions & 51 deletions exercises/go-counting/go_counting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,97 @@
import go_counting


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0

board5x5 = "\n".join([
board5x5 = [
" B ",
" B B ",
"B W B",
" W W ",
" W "
])

board9x9 = "\n".join([
" B B ",
"B B B",
"WBBBWBBBW",
"W W W W W",
" ",
" W W W W ",
"B B B B",
" W BBB W ",
" B B "
])
]


class GoCountingTest(unittest.TestCase):
def test_5x5_for_black(self):
def test_black_corner_territory_on_5x5_board(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((0, 1))
stone, territory = board.territory(x=0, y=1)
self.assertEqual(stone, go_counting.BLACK)
self.assertEqual(territory, set([(0, 0), (0, 1), (1, 0)]))
self.assertSetEqual(territory, {(0, 0), (0, 1), (1, 0)})

def test_5x5_for_white(self):
def test_white_center_territory_on_5x5_board(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((2, 3))
stone, territory = board.territory(x=2, y=3)
self.assertEqual(stone, go_counting.WHITE)
self.assertEqual(territory, set([(2, 3)]))
self.assertSetEqual(territory, {(2, 3)})

def test_5x5_for_open_territory(self):
def test_open_corner_territory_on_5x5_board(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((1, 4))
stone, territory = board.territory(x=1, y=4)
self.assertEqual(stone, go_counting.NONE)
self.assertEqual(territory, set([(0, 3), (0, 4), (1, 4)]))
self.assertSetEqual(territory, {(0, 3), (0, 4), (1, 4)})

def test_5x5_for_non_territory(self):
def test_a_stone_and_not_a_territory_on_5x5_board(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((1, 1))
stone, territory = board.territory(x=1, y=1)
self.assertEqual(stone, go_counting.NONE)
self.assertEqual(territory, set())
self.assertSetEqual(territory, set())

def test_5x5_for_valid_coordinate(self):
def test_invalid_because_x_is_too_low(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((-1, 1))
self.assertEqual(stone, go_counting.NONE)
self.assertEqual(territory, set())
with self.assertRaisesWithMessage(ValueError):
board.territory(x=-1, y=1)

def test_5x5_for_valid_coordinate2(self):
def test_invalid_because_x_is_too_high(self):
board = go_counting.Board(board5x5)
stone, territory = board.territoryFor((1, 5))
self.assertEqual(stone, go_counting.NONE)
self.assertEqual(territory, set())
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)

def test_one_territory_whole_board(self):
board = go_counting.Board(" ")
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([" "])
territories = board.territories()
self.assertEqual(territories[go_counting.BLACK], set())
self.assertEqual(territories[go_counting.WHITE], set())
self.assertEqual(territories[go_counting.NONE], set([(0, 0)]))
self.assertSetEqual(territories[go_counting.BLACK], set())
self.assertSetEqual(territories[go_counting.WHITE], set())
self.assertSetEqual(territories[go_counting.NONE], {(0, 0)})

def test_two_territories_rectangular_board(self):
input_board = "\n".join([
input_board = [
" BW ",
" BW "
])
]
board = go_counting.Board(input_board)
territories = board.territories()
self.assertEqual(territories[go_counting.BLACK], set([(0, 0), (0, 1)]))
self.assertEqual(territories[go_counting.WHITE], set([(3, 0), (3, 1)]))
self.assertEqual(territories[go_counting.NONE], set())
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())

def test_9x9_for_open_territory(self):
board = go_counting.Board(board9x9)
stone, territory = board.territoryFor((0, 8))
self.assertEqual(stone, go_counting.NONE)
self.assertEqual(territory,
set([(2, 7), (2, 8), (1, 8), (0, 8), (0, 7)]))
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 setUp(self):
try:
self.assertRaisesRegex
except AttributeError:
self.assertRaisesRegex = self.assertRaisesRegexp

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")


if __name__ == '__main__':
Expand Down