Skip to content

minesweeper: update tests to version 1.1.0 #1062

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 3 commits into from
Nov 13, 2017
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
24 changes: 10 additions & 14 deletions exercises/minesweeper/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def board(inp):
if(inp == []):
return []
verify_board(inp)
rowlen = len(inp[0])
collen = len(inp)
Expand All @@ -7,33 +9,27 @@ def board(inp):
for i2 in range(rowlen):
if b[i1][i2] != ' ':
continue
cnt = inp[i1 - 1][i2 - 1:i2 + 2].count('*') + \
inp[i1][i2 - 1:i2 + 2].count('*') + \
inp[i1 + 1][i2 - 1:i2 + 2].count('*')
low = max(i2 - 1, 0)
high = min(i2 + 2, rowlen + 2)
cnt = inp[i1][low:high].count('*')
if(i1 > 0):
cnt += inp[i1 - 1][low:high].count('*')
if(i1 < collen - 1):
cnt += inp[i1 + 1][low:high].count('*')
if cnt == 0:
continue
b[i1][i2] = str(cnt)
return ["".join(r) for r in b]


def verify_board(inp):
# Null board or a null row
if not inp or not all(r for r in inp):
raise ValueError("Invalid board")
# Rows with different lengths
rowlen = len(inp[0])
collen = len(inp)
if not all(len(r) == rowlen for r in inp):
raise ValueError("Invalid board")
# Unknown character in board
cset = set()
for r in inp:
cset.update(r)
if cset - set('+- *|'):
raise ValueError("Invalid board")
# Borders not as expected
if any(inp[i1] != '+' + '-' * (rowlen - 2) + '+'
for i1 in [0, -1]) or any(inp[i1][i2] != '|'
for i1 in range(1, collen - 1)
for i2 in [0, -1]):
if cset - set(' *'):
raise ValueError("Invalid board")
227 changes: 108 additions & 119 deletions exercises/minesweeper/minesweeper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,150 +11,139 @@
from minesweeper import board


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

class MinesweeperTest(unittest.TestCase):
def test_board1(self):
inp = ["+------+",
"| * * |",
"| * |",
"| * |",
"| * *|",
"| * * |",
"| |",
"+------+"]
out = ["+------+",
"|1*22*1|",
"|12*322|",
"| 123*2|",
"|112*4*|",
"|1*22*2|",
"|111111|",
"+------+"]

def test_no_rows(self):
self.assertEqual(board([]), [])

def test_no_columns(self):
self.assertEqual(board([""]), [""])

def test_no_mines(self):
inp = [" ",
" ",
" "]
out = [" ",
" ",
" "]
self.assertEqual(board(inp), out)

def test_board_with_only_mines(self):
inp = ["***",
"***",
"***"]
out = ["***",
"***",
"***"]
self.assertEqual(board(inp), out)

def test_board2(self):
inp = ["+-----+",
"| * * |",
"| |",
"| * |",
"| * *|",
"| * * |",
"+-----+"]
out = ["+-----+",
"|1*2*1|",
"|11322|",
"| 12*2|",
"|12*4*|",
"|1*3*2|",
"+-----+"]
def test_mine_surrounded_by_spaces(self):
inp = [" ",
" * ",
" "]
out = ["111",
"1*1",
"111"]
self.assertEqual(board(inp), out)

def test_board3(self):
inp = ["+-----+",
"| * * |",
"+-----+"]
out = ["+-----+",
"|1*2*1|",
"+-----+"]
def test_space_surrounded_by_mines(self):
inp = ["***",
"* *",
"***"]
out = ["***",
"*8*",
"***"]
self.assertEqual(board(inp), out)

def test_board4(self):
inp = ["+-+",
"|*|",
"| |",
"|*|",
"| |",
"| |",
"+-+"]
out = ["+-+",
"|*|",
"|2|",
"|*|",
"|1|",
"| |",
"+-+"]
def test_horizontal_line(self):
inp = [" * * "]
out = ["1*2*1"]
self.assertEqual(board(inp), out)

def test_board5(self):
inp = ["+-+",
"|*|",
"+-+"]
out = ["+-+",
"|*|",
"+-+"]
def test_horizontal_line_mines_at_edges(self):
inp = ["* *"]
out = ["*1 1*"]
self.assertEqual(board(inp), out)

def test_board6(self):
inp = ["+--+",
"|**|",
"|**|",
"+--+"]
out = ["+--+",
"|**|",
"|**|",
"+--+"]
def test_vertical_line(self):
inp = [" ",
"*",
" ",
"*",
" "]
out = ["1",
"*",
"2",
"*",
"1"]
self.assertEqual(board(inp), out)

def test_board7(self):
inp = ["+--+",
"|**|",
"|**|",
"+--+"]
out = ["+--+",
"|**|",
"|**|",
"+--+"]
def test_vertical_line_mines_at_edges(self):
inp = ["*",
" ",
" ",
" ",
"*"]
out = ["*",
"1",
" ",
"1",
"*"]
self.assertEqual(board(inp), out)

def test_board8(self):
inp = ["+---+",
"|***|",
"|* *|",
"|***|",
"+---+"]
out = ["+---+",
"|***|",
"|*8*|",
"|***|",
"+---+"]
def test_cross(self):
inp = [" * ",
" * ",
"*****",
" * ",
" * "]
out = [" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "]
self.assertEqual(board(inp), out)

def test_large_board(self):
inp = [" * * ",
" * ",
" * ",
" * *",
" * * ",
" "]
out = ["1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"]
self.assertEqual(board(inp), out)

# Additional test for this track
def test_board9(self):
inp = ["+-----+",
"| |",
"| * |",
"| |",
"| |",
"| * |",
"+-----+"]
out = ["+-----+",
"| 111|",
"| 1*1|",
"| 111|",
"|111 |",
"|1*1 |",
"+-----+"]
inp = [" ",
" * ",
" ",
" ",
" * "]
out = [" 111",
" 1*1",
" 111",
"111 ",
"1*1 "]
self.assertEqual(board(inp), out)

def test_different_len(self):
inp = ["+-+",
"| |",
"|* |",
"| |",
"+-+"]
with self.assertRaises(ValueError):
board(inp)

def test_faulty_border(self):
inp = ["+-----+",
"* * |",
"+-- --+"]
inp = [" ",
"* ",
" "]
with self.assertRaises(ValueError):
board(inp)

def test_invalid_char(self):
inp = ["+-----+",
"|X * |",
"+-----+"]
inp = ["X * "]
with self.assertRaises(ValueError):
board(inp)

Expand Down