-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.py
More file actions
289 lines (234 loc) · 15.4 KB
/
Copy pathTests.py
File metadata and controls
289 lines (234 loc) · 15.4 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import Parser
import Models
import Simulation
import unittest
from mock import Mock
from mock import patch
import random
class ParserTests(unittest.TestCase):
def setUp(self):
self.simulation = Simulation.Simulation()
def testBoard1(self):
''' Tests if the board parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('board experiment 0.5 0.6 0.7', self.simulation)
self.assertEqual(self.simulation.params['general']['board'], [0.5, 0.6, 0.7])
def testBoard2(self):
''' Tests if the board parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('board 0.5', self.simulation)
self.assertEqual(self.simulation.params['general']['board'], [0.5])
def testBoard3(self):
''' Tests if the board parameters are parsed correctly by the parser'''
self.assertRaises(Exception, Parser.Parser._parseLine, 'board ', self.simulation)
self.assertRaises(Exception, Parser.Parser._parseLine, 'board 0.5 0.6 0.7', self.simulation)
def testDisembarks1(self):
''' Tests if the disembarks parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('disembarks experiment 0.5 0.6 0.7', self.simulation)
self.assertEqual(self.simulation.params['general']['disembarks'], [0.5, 0.6, 0.7])
def testDisembarks2(self):
''' Tests if the disembarks parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('disembarks 0.5', self.simulation)
self.assertEqual(self.simulation.params['general']['disembarks'], [0.5])
def testDisembarks3(self):
''' Tests if the disembarks parameters are parsed correctly by the parser'''
self.assertRaises(Exception, Parser.Parser._parseLine, 'disembarks 0.5 0.6 0.7', self.simulation)
def testDeparts1(self):
''' Tests if the departs parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('departs experiment 0.5 0.6 0.7', self.simulation)
self.assertEqual(self.simulation.params['general']['departs'], [0.5, 0.6, 0.7])
def testDeparts2(self):
''' Tests if the departs parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('departs 0.5', self.simulation)
self.assertEqual(self.simulation.params['general']['departs'], [0.5])
def testDeparts3(self):
''' Tests if the departs parameters are parsed correctly by the parser'''
self.assertRaises(Exception, Parser.Parser._parseLine, 'departs 0.5 0.6 0.7', self.simulation)
def testNewPassengers1(self):
''' Tests if the new passengers parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('new passengers experiment 0.5 0.6 0.7', self.simulation)
self.assertEqual(self.simulation.params['general']['new passengers'], [0.5, 0.6, 0.7])
def testNewPassengers2(self):
''' Tests if the new passengers parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('new passengers 0.5', self.simulation)
self.assertEqual(self.simulation.params['general']['new passengers'], [0.5])
def testNewPassengers3(self):
''' Tests if the new passengers parameters are parsed correctly by the parser'''
self.assertRaises(Exception, Parser.Parser._parseLine, 'new passengers 0.5 0.6 0.7', self.simulation)
def testStopTime(self):
''' Tests if the stop time parameters are parsed correctly by the parser'''
Parser.Parser._parseLine('stop time 111.1', self.simulation)
self.assertEqual(self.simulation.params['control']['stopTime'], 111.1)
def testIgnoreWarnings(self):
''' Tests if the ignore warnings flag was set as expected'''
Parser.Parser._parseLine('ignore warnings', self.simulation)
self.assertEqual(self.simulation.params['control']['ignoreWarnings'], True)
def testOptimiseParameters(self):
''' Tests if the ignore warnings flag was set as expected'''
Parser.Parser._parseLine('optimise parameters', self.simulation)
self.assertEqual(self.simulation.params['control']['optimiseParameters'], True)
def testInvalidLine(self):
'''Tests if error is thrown for an invalid input line'''
self.assertRaises(Exception, Parser.Parser._parseLine, 'a wrong line', self.simulation)
def testCommentOrEmptyLine(self):
'''Tests if comments and empty lines are ignored'''
Parser.Parser._parseLine('#comment', self.simulation)
self.assertEqual(self.simulation, Simulation.Simulation())
self.assertEqual(self.simulation.Network, Models.Network())
Parser.Parser._parseLine('', self.simulation)
self.assertEqual(self.simulation, Simulation.Simulation())
self.assertEqual(self.simulation.Network, Models.Network())
def testRoad(self):
''' Tests if the road parameters are parsed correctly by the parser:
1. Checks if the addRoad method is called with correct parameters
2. Checks if an exception is raised with incorrectly structured input'''
with patch.object(self.simulation, 'addRoad') as mock:
Parser.Parser._parseLine('road 1 2 0.3', self.simulation)
mock.assert_called_with(1, 2, [0.3])
with patch.object(self.simulation, 'addRoad') as mock:
Parser.Parser._parseLine('road 1 2 experiment 0.3 0.5 0.8', self.simulation)
mock.assert_called_with(1, 2, [0.3, 0.5, 0.8])
self.assertRaises(Exception, Parser.Parser._parseLine, 'road 1 2 4', self.simulation)
def testRoute(self):
''' Tests if the route parameters are parsed correctly by the parser:
1. Checks if the addRoute method is called with correct parameters
2. Checks if an exception is raised with incorrectly structured input'''
with patch.object(self.simulation, 'addRoute') as mock:
Parser.Parser._parseLine('route 1 stops 1 2 3 buses 4 capacity 50', self.simulation)
mock.assert_called_with(1, [1, 2, 3], [4], [50])
with patch.object(self.simulation, 'addRoute') as mock:
Parser.Parser._parseLine('route 1 stops 1 2 3 buses experiment 4 5 6 capacity 50', self.simulation)
mock.assert_called_with(1, [1, 2, 3], [4, 5, 6], [50])
with patch.object(self.simulation, 'addRoute') as mock:
Parser.Parser._parseLine('route 1 stops 1 2 3 buses 4 capacity experiment 50 500', self.simulation)
mock.assert_called_with(1, [1, 2, 3], [4], [50, 500])
with patch.object(self.simulation, 'addRoute') as mock:
Parser.Parser._parseLine('route 1 stops 1 2 3 buses experiment 4 5 9 capacity experiment 50 55 100', self.simulation)
mock.assert_called_with(1, [1, 2, 3], [4, 5, 9], [50, 55, 100])
self.assertRaises(Exception, Parser.Parser._parseLine, 'route 1 stops 1 2 3 buses experiment capacity 50', self.simulation)
class SimulationTests(unittest.TestCase):
''' This test case is going to be checking 3 main things:
1. Is the initial network constructed correctly?
2. Are the possible events and their rates calculated correctly?
3. Are the events carried out properly - is the resulting network correct?
'''
def setUp(self):
''' Constructing the test simulation'''
self.simulation = Simulation.Simulation()
Parser.Parser._parseLine('route 1 stops 1 2 3 buses 4 capacity 50', self.simulation)
Parser.Parser._parseLine('route 2 stops 3 5 8 buses 2 capacity 10', self.simulation)
Parser.Parser._parseLine('road 1 2 0.3', self.simulation)
Parser.Parser._parseLine('road 2 3 0.7', self.simulation)
Parser.Parser._parseLine('road 3 1 0.2', self.simulation)
Parser.Parser._parseLine('road 3 5 0.3', self.simulation)
Parser.Parser._parseLine('road 5 3 0.1', self.simulation)
Parser.Parser._parseLine('road 5 8 0.6', self.simulation)
Parser.Parser._parseLine('road 8 3 0.8', self.simulation)
Parser.Parser._parseLine('stop time 111.1', self.simulation)
Parser.Parser._parseLine('new passengers 0.5', self.simulation)
Parser.Parser._parseLine('departs 0.5', self.simulation)
Parser.Parser._parseLine('disembarks 0.5', self.simulation)
Parser.Parser._parseLine('board 0.3', self.simulation)
''' Constructing an equivalent simulation manually'''
self.expectedSimulation = Simulation.Simulation()
self.expectedSimulation.params['general']['board'] = [0.3]
self.expectedSimulation.params['general']['disembarks'] = [0.5]
self.expectedSimulation.params['general']['departs'] = [0.5]
self.expectedSimulation.params['general']['new passengers'] = [0.5]
self.expectedSimulation.params['control']['stopTime'] = 111.1
self.expectedSimulation.Network.params['board'] = 0.3
self.expectedSimulation.Network.params['disembarks'] = 0.5
self.expectedSimulation.Network.params['departs'] = 0.5
self.expectedSimulation.Network.params['new passengers'] = 0.5
self.expectedSimulation.Network.routes = {
1 : Models.Route([1, 2, 3], 1, 50),
2 : Models.Route([3, 5, 8], 2, 10)
}
self.expectedSimulation.Network.routes[1].buses = [Models.Bus(1, 0, 50, 1),
Models.Bus(1, 1, 50, 2),
Models.Bus(1, 2, 50, 3),
Models.Bus(1, 3, 50, 1)]
self.expectedSimulation.Network.routes[2].buses = [Models.Bus(2, 0, 10, 3),
Models.Bus(2, 1, 10, 5)]
self.expectedSimulation.params['roads'] = {
(1, 2) : [0.3],
(2, 3) : [0.7],
(3, 1) : [0.2],
(3, 5) : [0.3],
(5, 3) : [0.1],
(5, 8) : [0.6],
(8, 3) : [0.8]
}
self.expectedSimulation.params['routes'] = {
1 : {
'routeID' : [1],
'buses' : [4],
'capacity' : [50]
},
2 : {
'routeID' : [2],
'buses' : [2],
'capacity' : [10]
}
}
self.expectedSimulation.Network.stops = {
1: Models.Stop(1),
2: Models.Stop(2),
3: Models.Stop(3),
5: Models.Stop(5),
8: Models.Stop(8)
}
self.expectedSimulation.Network.stops[1].numberOfBusesQueued = 2
self.expectedSimulation.Network.stops[2].numberOfBusesQueued = 1
self.expectedSimulation.Network.stops[3].numberOfBusesQueued = 2
self.expectedSimulation.Network.stops[5].numberOfBusesQueued = 1
self.expectedSimulation.Network.stops[8].numberOfBusesQueued = 0
self.expectedSimulation.Network.stops[1].reachableStops = [2, 3]
self.expectedSimulation.Network.stops[2].reachableStops = [1, 3]
self.expectedSimulation.Network.stops[3].reachableStops = [1, 2, 5, 8]
self.expectedSimulation.Network.stops[5].reachableStops = [3, 8]
self.expectedSimulation.Network.stops[8].reachableStops = [3, 5]
self.expectedSimulation.Network.stops[1].qOfBuses = [self.expectedSimulation.Network.routes[1].buses[0],
self.expectedSimulation.Network.routes[1].buses[3]]
self.expectedSimulation.Network.stops[2].qOfBuses = [self.expectedSimulation.Network.routes[1].buses[1]]
self.expectedSimulation.Network.stops[3].qOfBuses = [self.expectedSimulation.Network.routes[1].buses[2],
self.expectedSimulation.Network.routes[2].buses[0]]
self.expectedSimulation.Network.stops[5].qOfBuses = [self.expectedSimulation.Network.routes[2].buses[1]]
self.assertTrue(self.expectedSimulation == self.simulation)
def testInitialNetwork(self):
''' This method will check if the constructed network looks as expected'''
self.assertEqual(self.expectedSimulation, self.simulation)
def testAddPassenger(self):
''' This method will check if the passenger is added correctly to the network.
Although the simulation is non-deterministic, since we specify the seed, we know what to expect
(at least it works on my computer)
'''
random.seed(0)
self.simulation.Network.addPassenger(0, False)
self.simulation.Network.addPassenger(0, False)
self.expectedSimulation.Network.stops[5].passengers.append(Models.Passenger(8))
self.expectedSimulation.Network.stops[2].passengers.append(Models.Passenger(1))
self.assertEqual(self.expectedSimulation, self.simulation)
def testRates(self):
''' This method will check if the event rates are calculated correctly'''
random.seed(0)
self.simulation.Network.changeGeneralParams(self.simulation.generateGeneralParamSets()[0])
self.simulation.Network.changeRoadParams(self.simulation.generateRoadSets()[0])
self.simulation.Network.addPassenger(0, False)
self.simulation.Network.addPassenger(0, False)
self.assertEqual(self.simulation.getEventRates(), {'paxRTDRate': 0.0, 'paxRTBRate': 0.6, 'busesRTDRate': 2.0, 'busesRTARate': 0})
self.simulation.Network.boardPassenger(0, False)
self.assertEqual(self.simulation.getEventRates(), {'paxRTDRate': 0.0, 'paxRTBRate': 0.3, 'busesRTDRate': 2.5, 'busesRTARate': 0})
self.simulation.Network.boardPassenger(0, False)
self.assertEqual(self.simulation.getEventRates(), {'paxRTDRate': 0.0, 'paxRTBRate': 0.0, 'busesRTDRate': 3.0, 'busesRTARate': 0})
self.simulation.Network.departBus(0, False)
self.assertEqual(self.simulation.getEventRates(), {'paxRTDRate': 0.0, 'paxRTBRate': 0.0, 'busesRTDRate': 2.5, 'busesRTARate': 0.3})
self.simulation.Network.arriveBus(0, False)
self.assertEqual(self.simulation.getEventRates(), {'paxRTDRate': 0.0, 'paxRTBRate': 0.0, 'busesRTDRate': 3.0, 'busesRTARate': 0})
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ParserTests))
suite.addTest(unittest.makeSuite(SimulationTests))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
test_suite = suite()
runner.run(test_suite)