-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
173 lines (141 loc) · 5.66 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from analyzer import tile, Position, black, red
from analyzer import player_piece, Move, loc
import unittest
# Goals for my unit tests:
# * Write examples of how the code should work
# * Sanity check that the code runs and returns sane answers in simple cases
#
# I agree with the motivation of TDD, if not the strict version of it
# TODO: make the tests clearer and easier to read!
class TestPosition(unittest.TestCase):
def setUp(self):
pieces = [[tile(i,j) for i in range(4)] for j in range(4)]
self.pieces = pieces
# check that the data is recalled correctly
def test_data_storage(self):
pieces = self.pieces
board = Position(pieces,red(),None)
self.assertEqual(red(),board.whose_move())
for i in range(4):
for j in range(4):
self.assertEqual(pieces[i][j],board.get_piece(i,j))
p = pieces[2][3]
for i in range(4):
for j in range(4):
pieces[i][j] = None
self.assertEqual(p,board.get_piece(2,3))
def test_non_victory(self):
pieces = self.pieces
board = Position(pieces,red(),None)
self.assertEqual(board.check_victory(),0)
def test_configuration_victory(self):
pieces = self.pieces
# check a 2x2 block in the middle
for i in range(1,3):
for j in range(1,3):
pieces[i][j] = player_piece(red())
board = Position(pieces,black(),None)
self.assertEqual(board.check_victory(),red())
# now change the board so that black wins and red doesn't
for i in range(4):
pieces[i][i] = player_piece(black())
# check that data is stored correctly
self.assertEqual(board.check_victory(),red())
# now check for real
board = Position(pieces,black(),None)
self.assertEqual(board.check_victory(),black())
def test_victory_values_distinct(self):
self.assertNotEqual(red(),black())
self.assertNotEqual(0,red())
self.assertNotEqual(0,black())
def test_victory_via_no_moves(self):
# make a board full of the same tile
pieces = [[tile(0,0) for i in range(4)] for j in range(4)]
# set the Previous Tile to something incompatible
board = Position(pieces,black(),
tile(1,1))
# black to move, so red wins
self.assertEqual(board.check_victory(),red())
# now reverse the roles and check again
board = Position(pieces,red(),
tile(1,1))
self.assertEqual(board.check_victory(),black())
def test_perimeter_moves_at_start(self):
pieces = self.pieces
board = Position(pieces,black(),None)
moves = board.get_moves()
self.assertEqual(len(moves),4*4-2*2)
for m in moves:
(i,j) = m.get_location()
self.assertTrue(i == 0 or i == 3 or j == 0 or j == 3)
def test_moves_with_previous(self):
pieces = self.pieces
board = Position(pieces,black(),tile(0,0))
# all the pieces close to tile(0,0)
analogous = [tile(i,0) for i in range(4)] + \
[tile(0,i) for i in range(4)]
moves = board.get_moves()
self.assertGreater(len(moves),0)
for move in moves:
(i,j) = move.get_location()
self.assertIn(pieces[i][j],analogous)
def test_doing_and_undoing(self):
pieces = self.pieces
board = Position(pieces,black(),None)
move = Move(board,loc(0,0))
move.do()
self.assertEqual(board.get_piece(0,0),
player_piece(black()))
self.assertNotEqual(board.get_piece(0,0),pieces[0][0])
self.assertEqual(board.whose_move(),red())
self.assertEqual(board.previous_piece,pieces[0][0])
moves = board.get_moves()
self.assertEqual(len(moves),3+3)
move.undo()
self.assertEqual(board.get_piece(0,0),pieces[0][0])
self.assertEqual(board.previous_piece,None)
self.assertEqual(board.whose_move(),black())
moves = board.get_moves()
self.assertEqual(len(moves),4*4-2*2)
def test_tile_count_via_moves(self):
board = Position(self.pieces,black(),None)
self.assertEqual(board.tile_count,4*4)
move1 = board.get_moves().pop()
move1.do()
move2 = board.get_moves().pop()
move2.do()
self.assertEqual(board.tile_count,4*4-2)
move2.undo()
self.assertEqual(board.tile_count,4*4-1)
move1.undo()
self.assertEqual(board.tile_count,4*4)
def test_tile_count_midgame(self):
board = Position(self.pieces,black(),None)
nMoves = 3
# do 3 random moves
for i in range(nMoves):
board.get_moves().pop().do()
pieces = [[None for i in range(4)] for j in range(4)]
for i in range(4):
for j in range(4):
pieces[i][j] = board.get_piece(i,j)
board = Position(pieces,board.whose_move(),
board.previous_piece)
self.assertEqual(board.tile_count,4*4-nMoves)
# Test get_key
def test_hashable_key(self):
nMoves = 4
board = Position(self.pieces,black(),None)
table = {}
moves = []
for i in range(nMoves):
table[board.get_key()] = i
move = board.get_moves().pop()
moves.append(move)
move.do()
for i in range(nMoves):
move = moves.pop()
j = len(moves)
move.undo()
self.assertEqual(table[board.get_key()],j)
self.assertEqual(i+j,nMoves-1)