-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha1_sample_test.py
More file actions
185 lines (149 loc) · 5.31 KB
/
a1_sample_test.py
File metadata and controls
185 lines (149 loc) · 5.31 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
"""CSC148 Assignment 1: Sample tests
=== CSC148 Winter 2022 ===
Department of Computer Science,
University of Toronto
=== Module description ===
This module contains sample tests for Assignment 0.
Warning: This is an extremely incomplete set of tests! Add your own tests
to be confident that your code is correct.
Note: this file is to only help you; you will not submit it when you hand in
the assignment.
This code is provided solely for the personal and private use of
students taking the CSC148 course at the University of Toronto.
Copying for purposes other than this use is expressly prohibited.
All forms of distribution of this code, whether as given or with
any changes, are expressly prohibited.
All of the files in this directory and all subdirectories are:
Copyright (c) University of Toronto
"""
from datetime import date
from io import StringIO
from a1 import *
# A string representing a simple 4 by 4 game board.
# We use this in one of the tests below. You can use it in your own testing, but
# you do not have to.
SIMPLE_BOARD_STRING = 'P-B-\n-BRB\n--BB\n-C--'
def simple_board_setup() -> GameBoard:
"""Set up a simple game board"""
b = GameBoard(4, 4)
b.setup_from_grid(SIMPLE_BOARD_STRING)
return b
def test_simple_place_character() -> None:
"""Test GameBoard.place_character by placing a single Raccoon."""
b = GameBoard(3, 2)
r = Raccoon(b, 1, 1)
assert b.at(1, 1)[0] == r # requires GameBoard.at be implemented to work
def test_simple_at() -> None:
"""Test GameBoard.at on docstring example"""
b = GameBoard(3, 2)
r = Raccoon(b, 1, 1)
assert b.at(1, 1)[0] == r
p = Player(b, 0, 1)
assert b.at(0, 1)[0] == p
def test_simple_str() -> None:
"""Test GameBoard.__str__ for the simple board in SIMPLE_BOARD_STRING."""
b = simple_board_setup()
assert str(b) == 'P-B-\n-BRB\n--BB\n-C--'
def test_simple_check_game_end() -> None:
"""Test GameBoard.check_game_end on the docstring example"""
b = GameBoard(3, 2)
Raccoon(b, 1, 0)
Player(b, 0, 0)
RecyclingBin(b, 1, 1)
assert b.check_game_end() is None
assert not b.ended
RecyclingBin(b, 2, 0)
score = b.check_game_end()
assert b.ended
assert score == 11 # will only pass this last one when done Task 5.
def test_simple_adjacent_bin_score() -> None:
"""Test GameBoard.adjacent_bin_score on the docstring example"""
b = GameBoard(3, 3)
RecyclingBin(b, 1, 1)
RecyclingBin(b, 0, 0)
RecyclingBin(b, 2, 2)
assert b.adjacent_bin_score() == 1
RecyclingBin(b, 2, 1)
assert b.adjacent_bin_score() == 3
RecyclingBin(b, 0, 1)
assert b.adjacent_bin_score() == 5
def test_simple_player_move() -> None:
"""Test Player.move on docstring example."""
b = GameBoard(4, 2)
p = Player(b, 0, 0)
assert not p.move(UP)
assert p.move(DOWN)
assert b.at(0, 1) == [p]
RecyclingBin(b, 1, 1)
assert p.move(RIGHT)
assert b.at(1, 1) == [p]
def test_simple_recyclingbin_move() -> None:
"""Test RecyclingBin.move on docstring example."""
b = GameBoard(4, 2)
rb = RecyclingBin(b, 0, 0)
assert not rb.move(UP)
assert rb.move(DOWN)
assert b.at(0, 1) == [rb]
def test_simple_raccoon_check_trapped() -> None:
"""Test Raccoon.check_trapped on docstring example."""
b = GameBoard(3, 3)
r = Raccoon(b, 2, 1)
Raccoon(b, 2, 2)
Player(b, 2, 0)
assert not r.check_trapped()
RecyclingBin(b, 1, 1)
assert r.check_trapped()
def test_simple_raccoon_move() -> None:
"""Test Raccoon.move on docstring example."""
b = GameBoard(4, 2)
r = Raccoon(b, 0, 0)
assert not r.move(UP)
assert r.move(DOWN)
assert b.at(0, 1) == [r]
g = GarbageCan(b, 1, 1, True)
assert r.move(RIGHT)
assert (r.x, r.y) == (0, 1) # Raccoon didn't change its position
assert not g.locked # Raccoon unlocked the garbage can!
assert r.move(RIGHT)
assert r.inside_can
assert len(b.at(1, 1)) == 2 # Raccoon and GarbageCan are both at (1, 1)!
def test_simple_raccoon_take_turn() -> None:
"""Test Raccoon.take_turn on docstring example."""
b = GameBoard(3, 4)
r1 = Raccoon(b, 0, 0)
r1.take_turn()
assert (r1.x, r1.y) in [(0, 1), (1, 0)]
r2 = Raccoon(b, 2, 1)
RecyclingBin(b, 2, 0)
RecyclingBin(b, 1, 1)
RecyclingBin(b, 2, 2)
r2.take_turn() # Raccoon is trapped
assert (r2.x, r2.y) == (2, 1)
def test_simple_smartraccoon_take_turn() -> None:
"""Test SmartRaccoon.take_turn on docstring example."""
b = GameBoard(8, 2)
s = SmartRaccoon(b, 4, 0)
GarbageCan(b, 3, 1, False)
GarbageCan(b, 0, 0, False)
GarbageCan(b, 7, 0, False)
s.take_turn()
assert s.x == 5
s.take_turn()
assert s.x == 6
def test_simple_give_turns() -> None:
"""Test GameBoard.give_turns on docstring example."""
b = GameBoard(4, 3)
p = Player(b, 0, 0)
r = Raccoon(b, 1, 1)
for _ in range(RACCOON_TURN_FREQUENCY - 1):
b.give_turns()
assert b.turns == RACCOON_TURN_FREQUENCY - 1
assert (r.x, r.y) == (1, 1) # Raccoon hasn't had a turn yet
assert (p.x, p.y) == (0, 0) # Player hasn't had any inputs
p.record_event(RIGHT)
b.give_turns()
assert (r.x, r.y) != (1, 1) # Raccoon has had a turn!
assert (p.x, p.y) == (1, 0) # Player moved right!
if __name__ == '__main__':
import pytest
pytest.main(['a1_sample_test.py'])