Using UnitTests to solve complex problems
by Maksim Kozyarchuk
|
| |
UnitTests | |
Including UnitTests for completeness.
class TestTraffic(unittest.TestCase):
def setUp(self):
r = Road()
self.t = Traffic(road = r)
def testNoCars(self):
self.assertEquals([],self.t.get_traffic())
def testOneCarHitTraffic(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.assertEquals([(1,2)],self.t.get_traffic())
def testOneCarHitAndClearsTraffic(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.assertEquals([(1,3)],self.t.get_traffic())
def testSecondCarEntersWithOverlappingSignalsOverrides(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 2)
self.assertEquals([(2,3)],self.t.get_traffic())
def testSecondCarEntersWithOverlappingSignalsOverrides(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 2)
self.t.clear_traffic(car_id = 2, mile = 4)
self.assertEquals([(2,4)],self.t.get_traffic())
def testSecondCarEntersWithNonOverlappingSignalsAdds(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 4)
self.assertEquals([(1,3), (4,10)],self.t.get_traffic())
def testSecondCarEntersWithNonOverlappingSignalsAdds(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 4)
self.t.clear_traffic(car_id = 2, mile = 6)
self.assertEquals([(1,3), (4,6)],self.t.get_traffic())
def testThirdCarEntersWithOverlappingSignals(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 4)
self.t.clear_traffic(car_id = 2, mile = 6)
self.t.enter_traffic(car_id = 3, mile = 2)
self.assertEquals([(2,3), (4,6)],self.t.get_traffic())
def testThirdCarClearsWithOverlappingSignals(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 2, mile = 4)
self.t.clear_traffic(car_id = 2, mile = 6)
self.t.enter_traffic(car_id = 3, mile = 2)
self.t.clear_traffic(car_id = 3, mile = 5)
self.assertEquals([(2,5)],self.t.get_traffic())
def testSameCarEntersAndClearsMultipletimes(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.t.enter_traffic(car_id = 1, mile = 4)
self.t.clear_traffic(car_id = 1, mile = 6)
self.assertEquals( [(1,3), (4,6)], self.t.get_traffic() )
def testStopAndGoTrafficAroundSameArea(self):
self.t.enter_traffic(car_id = 1, mile = 3)
self.t.clear_traffic(car_id = 1, mile = 5)
self.t.enter_traffic(car_id = 2, mile = 4)
self.t.enter_traffic(car_id = 3, mile = 2)
self.assertEquals( [(2,3), (4,5)], self.t.get_traffic() )
self.t.clear_traffic(car_id = 2, mile = 6)
self.t.clear_traffic(car_id = 3, mile = 5)
self.assertEquals( [(2,5)], self.t.get_traffic() )
def testTraficAgesOff(self):
self.t.MAX_AGE = 3
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 3)
self.assertEquals( [(1,3)], self.t.get_traffic() )
self.t.increment_age()
self.assertEquals( [(1,3)], self.t.get_traffic() )
self.t.increment_age()
self.assertEquals( [(1,3)], self.t.get_traffic() )
self.t.increment_age()
self.assertEquals( [], self.t.get_traffic() )
def testIncrementAgeRemovesPairedCars(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 6)
self.t.enter_traffic(car_id = 2, mile = 2)
self.t.clear_traffic(car_id = 2, mile = 7)
self.t.enter_traffic(car_id = 3, mile = 2)
self.t.clear_traffic(car_id = 3, mile = 7)
self.t.enter_traffic(car_id = 4, mile = 2)
self.t.enter_traffic(car_id = 5, mile = 2)
self.t.increment_age()
self.assertEquals( set([4,5]), self.t.readings[0].start_car_id )
self.assertEquals( set(), self.t.readings[0].end_car_id )
class TestTrafficWithExits(unittest.TestCase):
def setUp(self):
r = Road(exits = [10,20,30,40,60], entrances = [5,15,35,45,55])
self.t = Traffic(road = r)
def testEnterAtEntranceDoesNotOverrideStart(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 13)
self.t.enter_traffic(car_id = 2, mile = 5)
self.assertEquals( [(1,13)], self.t.get_traffic() )
def testEnterAtEntranceAndClearsTrafficFirst(self):
self.t.enter_traffic(car_id = 1, mile = 4)
self.t.enter_traffic(car_id = 2, mile = 5)
self.t.clear_traffic(car_id = 2, mile = 13)
self.assertEquals( [(4,13)], self.t.get_traffic() )
self.t.clear_traffic(car_id = 1, mile = 12)
self.assertEquals( [(4,12)], self.t.get_traffic() )
def testEnterAtEntranceAndClearsTrafficFirstWithDistance(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.enter_traffic(car_id = 2, mile = 5)
self.t.clear_traffic(car_id = 2, mile = 13)
self.assertEquals( [ (1,2) , (5,13)], self.t.get_traffic() )
self.t.clear_traffic(car_id = 1, mile = 12)
self.assertEquals( [ (1,12) ], self.t.get_traffic() )
def testClearAtExitIncreasesTraffic(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 10)
self.assertEquals( [(1,10)], self.t.get_traffic() )
def testClearAtExitDoesNotDecreeseTraffic(self):
self.t.enter_traffic(car_id = 1, mile = 1)
self.t.clear_traffic(car_id = 1, mile = 12)
self.t.enter_traffic(car_id = 2, mile = 1)
self.t.clear_traffic(car_id = 2, mile = 10)
self.assertEquals( [(1,12)], self.t.get_traffic() )
| |
No comments:
Post a Comment