-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstTestingCode.py
executable file
·256 lines (233 loc) · 9.49 KB
/
pstTestingCode.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import unittest
import random
import __builtin__
import ps7
'''
class Problem_1(unittest.TestCase):
def test_1(self):
-Check if Position objects are created with floats
-Check if the random.seed() function isn't being called
def test_2(self):
-Check if the first tile is cleaned
-Check if the direction is being defined before position
-Check if the direction is being returned as an integer
def test_3(self):
-Check if random.randint() isn't called with end at 360
-Check if the round() function isn't being called
def test_4(self):
-Check if there are exactly two calls to a random
function on the getRandomPosition() method
class Problem_2(unittest.TestCase):
def test_1(self):
-Check if the direction is being changed a single time
in updatePositionAndClean() method
-Check if the setRobotDirection() method is being called
on the updatePositionAndClean() method
def test_2(self):
-Check if the robot is cleaning the new tile it's moving to,
rather than the old tile it's moving from
-Check if the updatePositionAndClean() method is changing the
robot's position
def test_3(self):
-Check if isPositionInRoom() isn't being called multiple times(loops)
class Problem_3(unittest.TestCase):
def test_1(self):
-Check if return value for runSimulation() is a float
-Check if the room is being reset after each trial
'''
class Problem_1(unittest.TestCase):
def test_1(self):
'''
-Check if Position objects are created with floats
-Check if the random.seed() function isn't being called
'''
temp_init = ps7.Position.__init__
def custom_init(*args):
test = isinstance(args[1], float) and isinstance(args[2], float)
msg = 'A Position object must be created using floats.'
self.assertTrue(test, msg)
return temp_init(*args)
ps7.Position.__init__ = custom_init
temp_seed = random.seed
def custom_seed(*args):
msg = 'The random.seed function must not be used.'
self.assertTrue(False, msg)
return temp_seed(*args)
random.seed = custom_seed
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
room.getRandomPosition()
reload(random)
reload(ps7)
def test_2(self):
'''
-Check if the first tile is cleaned
-Check if the direction is being defined before position
-Check if the direction is being returned as an integer
'''
temp_method = ps7.RectangularRoom.getRandomPosition
random_state = random.getstate()
def custom_method(*args):
test = random.getstate() != random_state
msg = ('In the Robot class you need to define ' +
'the direction before the position.')
self.assertTrue(test, msg)
return temp_method(*args)
ps7.RectangularRoom.getRandomPosition = custom_method
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
pos = robot.getRobotPosition()
x = int(pos.getX())
y = int(pos.getY())
test = room.isTileCleaned(x, y)
msg = ('The tile the robot is first put on ' +
'must be cleaned during creation.')
self.assertTrue(test, msg)
direction = robot.getRobotDirection()
test = isinstance(direction, int)
msg = ('The direction should be an integer.')
self.assertTrue(test, msg)
reload(ps7)
def test_3(self):
'''
-Check if random.randint() isn't called with end at 360
-Check if the round() function isn't being called
'''
temp_function = random.randint
def custom_function(*args):
test = args[1] != 360
msg = ('The random.randint() function includes the end in ' +
'the results. You only need directions from 0 to 359.')
self.assertTrue(test, msg)
return temp_function(*args)
random.randint = custom_function
temp_builtin = __builtin__.round
def custom_builtin(*args):
msg = ('You shouldn\'t be using the round() function. It can ' +
'distort the results enough to fail the checker.')
self.assertTrue(False, msg)
return temp_builtin(*args)
__builtin__.round = custom_builtin
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
reload(random)
reload(__builtin__)
def test_4(self):
'''
-Check if there are exactly two calls to a random
function on the getRandomPosition() method
'''
random.seed(0)
random.random()
random.random()
check = random.random()
room = ps7.RectangularRoom(8, 8)
random.seed(0)
room.getRandomPosition()
test = check == random.random()
msg = ('The grader expects the getRandomPosition() method to make ' +
'two calls to random functions, one for each coordinate.')
self.assertTrue(test, msg)
class Problem_2(unittest.TestCase):
def test_1(self):
'''
-Check if the direction is being changed a single time
in updatePositionAndClean() method
-Check if the setRobotDirection() method is being called
on the updatePositionAndClean() method
'''
temp_method = ps7.StandardRobot.setRobotDirection
self.count = 0
def custom_method(*args):
self.count += 1
return temp_method(*args)
ps7.StandardRobot.setRobotDirection = custom_method
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
for _ in range(room.getNumTiles()):
robot.updatePositionAndClean()
if self.count != 0:
break
else:
msg = ('The setRobotDirection() method isn\'t being ' +
'called on the updatePositionAndClean() method.')
self.assertTrue(False, msg)
test = self.count == 1
msg = ('In the updatePositionAndClean() method if the position is ' +
'invalid you\'re supposed to set a new robot direction and ' +
'not move during that step.')
self.assertTrue(test, msg)
reload(ps7)
def test_2(self):
'''
-Check if the robot is cleaning the new tile it's moving to,
rather than the old tile it's moving from
-Check if the updatePositionAndClean() method is changing the
robot's position
'''
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
pos = robot.getRobotPosition()
for _ in range(room.getNumTiles()):
robot.updatePositionAndClean()
if pos != robot.getRobotPosition():
break
else:
msg = ('The updatePositionAndClean() method isn\'t ' +
'changing the robot\'s position.')
self.assertTrue(False, msg)
pos = robot.getRobotPosition()
test = room.isTileCleaned(int(pos.getX()), int(pos.getY()))
msg = ('In the updatePositionAndClean() method you\'re cleaning ' +
'the old tile the robot is moving from, you need to clean ' +
'the new tile the robot is moving to.')
self.assertTrue(test, msg)
def test_3(self):
'''
-Check if isPositionInRoom() isn't being called multiple times(loops)
'''
temp_method = ps7.RectangularRoom.isPositionInRoom
self.count = 0
def custom_method(*args):
self.count += 1
return temp_method(*args)
ps7.RectangularRoom.isPositionInRoom = custom_method
room = ps7.RectangularRoom(8, 8)
robot = ps7.StandardRobot(room, 1.0)
robot.updatePositionAndClean()
test = self.count == 1
msg = ('You only need to call the isPositionInRoom() method once ' +
'in the updatePositionAndClean() method. You can\'t use loops.')
self.assertTrue(test, msg)
reload(ps7)
class Problem_3(unittest.TestCase):
def test_1(self):
'''
-Check if return value for runSimulation() is a float
'''
result = ps7.runSimulation(1, 1.0, 2, 2, 0.5, 2, ps7.StandardRobot)
test = isinstance(result, float)
msg = ('The runSimulation() function must return a float')
self.assertTrue(test, msg)
def test_2(self):
'''
-Check if the room is being reset after each trial
'''
temp_method = ps7.RectangularRoom.__init__
self.count = 0
def custom_method(*args):
self.count += 1
return temp_method(*args)
ps7.RectangularRoom.__init__ = custom_method
ps7.runSimulation(1, 1.0, 2, 2, 0.5, 2, ps7.StandardRobot)
test = self.count > 1
msg = ('The RectangularRoom object is only being created once, ' +
'you\'re probably not resetting the room after each trial.')
self.assertTrue(test, msg)
reload(ps7)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Problem_1))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Problem_2))
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Problem_3))
unittest.TextTestRunner(verbosity=2).run(suite)