def test_make_random_board(): for _ in range(NTESTS_LIFE): nrows = random.randint(4, 7) ncols = nrows + random.randint(-2, 2) p = random.random() b = m.make_random_board(nrows, ncols, p) assert type(b) is list # check that b is a list assert len(b) == nrows for r in range(nrows): assert type(b[r]) is list assert len(b[r]) == ncols for r in range(nrows): for c in range(ncols): assert b[r][c] == 0 or b[r][c] == 1 # Make a new board. All values should be 1. b = m.make_random_board(nrows, ncols, 1.0) for r in range(nrows): for c in range(ncols): assert b[r][c] == 1 # Make a new board. All values should be 0. b = m.make_random_board(nrows, ncols, 0.0) for r in range(nrows): for c in range(ncols): assert b[r][c] == 0
def test_display_board_sums(): for _ in range(NTESTS_LIFE): nrows = random.randint(4, 7) ncols = nrows + random.randint(-2, 2) p = random.random() b = m.make_random_board(nrows, ncols, p) s = m.display_board_sums(b) assert type(s) is str for char in s: assert char in '012345678+-|\n' lines = s.split('\n') # break on newlines assert len(lines) == nrows + 3 assert lines[-1] == '' # last line was the extra newline at the end lines.pop() # get rid of the last line first = lines.pop(0) # first line; part of the surrounding box last = lines.pop() # last line; part of the surrounding box first = first.rstrip() # in case there are trailing blanks last = last.rstrip() # in case there are trailing blanks assert len(first) == ncols + 2 assert first[0] == '+' and first[-1] == '+' for i in range(1, ncols + 1): assert first[i] == '-' assert len(last) == ncols + 2 assert last[0] == '+' and last[-1] == '+' for i in range(1, ncols + 1): assert last[i] == '-' for line in lines: line = line.rstrip() assert len(line) == ncols + 2 assert line[0] == '|' assert line[-1] == '|' for char in line[1:-1]: assert char in '012345678'
def test_board_to_num(): for _ in range(NTESTS_LIFE): nrows = random.randint(4, 7) ncols = nrows + random.randint(-2, 2) p = random.random() b = m.make_random_board(nrows, ncols, p) n = m.board_to_num(b) assert type(n) is int assert n >= 0
def test_board_sums(): for _ in range(NTESTS_LIFE): nrows = random.randint(4, 7) ncols = nrows + random.randint(-2, 2) p = random.random() b = m.make_random_board(nrows, ncols, p) b2 = copy.deepcopy(b) sums = m.board_sums(b) assert type(sums) is list assert len(sums) == nrows for r in range(len(b)): assert type(sums[r]) is list assert len(sums[r]) == ncols for r in range(len(sums)): row = sums[r] for c in range(len(row)): val = row[c] assert type(val) is int assert val >= 0 and val <= 8 # Make sure the input board is unchanged. assert b == b2 # Make a new board. # Fill the board with 1s; sums should all be 8. b = m.make_random_board(nrows, ncols, 1.0) sums = m.board_sums(b) for r in range(nrows): for c in range(ncols): assert sums[r][c] == 8 # Make a new board. # Fill the board with 0s; sums should all be 0. b = m.make_random_board(nrows, ncols, 0.0) sums = m.board_sums(b) for r in range(nrows): for c in range(ncols): assert sums[r][c] == 0
def test_board_update(): for _ in range(NTESTS_LIFE): nrows = random.randint(4, 7) ncols = nrows + random.randint(-2, 2) p = random.random() b = m.make_random_board(nrows, ncols, p) copy_b = copy.deepcopy(b) b2 = m.board_update(b) assert type(b2) is list assert len(b2) == nrows for r in range(len(b)): assert type(b2[r]) is list assert len(b2[r]) == ncols for r in range(nrows): for c in range(ncols): assert b[r][c] == 0 or b[r][c] == 1 # Check that b wasn't modified when creating b2. assert b == copy_b