class TestDistanceFunctions(unittest.TestCase): def setUp(self): speedLimits = SpeedLimits([ SpeedLimit(range=((25, 0), (30, 0)), limit=1, ticks=0), SpeedLimit.createObstacle((80, 2)) ], 5) self.road = Road(3, 100, speedLimits) self.freeCar = Car(self.road, (50, 0)) self.carFacingObstacle = Car(self.road, (79, 2)) self.car1 = Car(self.road, (75, 2)) self.road.placeObjects( [self.freeCar, self.carFacingObstacle, self.car1]) def test_findPrevCar(self): length = self.road.getLength() for x in range(length): self.assertEqual(None, self.road.findPrevCar((x, 1))) for x in ((80, length)): self.assertEqual(None, self.road.findPrevCar((x, 2))) for x in range(50, length): self.assertEqual(self.freeCar, self.road.findPrevCar((x, 0))) self.assertEqual(self.car1, self.road.findPrevCar((78, 2))) def test_distanceToNextThing(self): for y in range(self.road.getLanesCount()): self.assertTrue( self.road.distanceToNextThing((99, y)) >= self.road.getLength()) self.assertEqual( 0, self.road.distanceToNextThing(self.carFacingObstacle.pos)) self.assertEqual(3, self.road.distanceToNextThing(self.car1.pos)) self.assertEqual(49, self.road.distanceToNextThing((0, 0)))
class TestDistanceFunctions(unittest.TestCase): def setUp(self): speedLimits = SpeedLimits( [ SpeedLimit(range=((25, 0), (30, 0)), limit=1, ticks=0), SpeedLimit.createObstacle((80, 2)) ] , 5) self.road = Road(3, 100, speedLimits) self.freeCar = Car(self.road, (50, 0)) self.carFacingObstacle = Car(self.road, (79, 2)) self.car1 = Car(self.road, (75, 2)) self.road.placeObjects([self.freeCar, self.carFacingObstacle, self.car1]) def test_findPrevCar(self): length = self.road.getLength() for x in range(length): self.assertEqual(None, self.road.findPrevCar( (x, 1) )) for x in ((80, length)): self.assertEqual(None, self.road.findPrevCar( (x, 2) )) for x in range(50, length): self.assertEqual(self.freeCar, self.road.findPrevCar( (x, 0) )) self.assertEqual(self.car1, self.road.findPrevCar( (78, 2) )) def test_distanceToNextThing(self): for y in range(self.road.getLanesCount()): self.assertTrue( self.road.distanceToNextThing( (99, y)) >= self.road.getLength()) self.assertEqual(0, self.road.distanceToNextThing( self.carFacingObstacle.pos )) self.assertEqual(3, self.road.distanceToNextThing( self.car1.pos )) self.assertEqual(49, self.road.distanceToNextThing( (0, 0) ) )
def test_avoid_collisions(self): road = Road(5, 10, None) cars1 = [Car(road, (0, 0)), Car(road, (0, 2)), Car(road, (0, 4))] cars2 = [Car(road, (1, 0)), Car(road, (1, 3))] cars3 = [Car(road, (2, 3)), Car(road, (2, 4))] road.placeObjects(cars1 + cars2 + cars3) for car in cars1: self.assertFalse(road.possibleLaneChangeUp(car.pos)) self.assertFalse(road.possibleLaneChangeDown(car.pos)) self.assertTrue(road.possibleLaneChangeDown(cars2[0].pos)) self.assertFalse(road.possibleLaneChangeUp(cars2[0].pos)) self.assertTrue(road.possibleLaneChangeDown(cars2[1].pos)) self.assertTrue(road.possibleLaneChangeUp(cars2[1].pos)) self.assertTrue(road.possibleLaneChangeUp(cars3[0].pos)) self.assertFalse(road.possibleLaneChangeDown(cars3[0].pos)) self.assertFalse(road.possibleLaneChangeUp(cars3[1].pos)) self.assertFalse(road.possibleLaneChangeDown(cars3[1].pos))
def test_avoid_collisions(self): road = Road(5, 10, None) cars1 = [Car(road, (0, 0)), Car(road, (0, 2)), Car(road, (0, 4))] cars2 = [Car(road, (1, 0)), Car(road, (1, 3))] cars3 = [Car(road, (2, 3)), Car(road, (2, 4))] road.placeObjects( cars1 + cars2 + cars3 ) for car in cars1: self.assertFalse( road.possibleLaneChangeUp(car.pos) ) self.assertFalse( road.possibleLaneChangeDown(car.pos) ) self.assertTrue( road.possibleLaneChangeDown(cars2[0].pos) ) self.assertFalse( road.possibleLaneChangeUp(cars2[0].pos) ) self.assertTrue( road.possibleLaneChangeDown(cars2[1].pos) ) self.assertTrue( road.possibleLaneChangeUp(cars2[1].pos) ) self.assertTrue( road.possibleLaneChangeUp(cars3[0].pos )) self.assertFalse( road.possibleLaneChangeDown(cars3[0].pos )) self.assertFalse( road.possibleLaneChangeUp(cars3[1].pos )) self.assertFalse( road.possibleLaneChangeDown(cars3[1].pos ))
def test_placeObject(self): r = Road(3, 40, None) car1, car2 = Car(r, (20, 0)), Car(r, (30, 0)) self.assertTrue(r.placeObjects([car1, car2])) self.assertEqual(2, r.carCount()) self.assertEqual(9, r.distanceToNextThing(car1.pos)) self.assertTrue(r.distanceToNextThing(car2.pos) >= r.getLength()) car3 = Car(r, (21, 0)) self.assertTrue(r.placeObject(car3)) self.assertEqual(3, r.carCount()) self.assertEqual(0, r.distanceToNextThing(car1.pos)) self.assertEqual(8, r.distanceToNextThing(car3.pos))
def test_placeObject(self): r = Road(3, 40, None) car1, car2 = Car(r, (20, 0)), Car(r, (30, 0)) self.assertTrue(r.placeObjects([car1, car2])) self.assertEqual(2, r.carCount()) self.assertEqual(9, r.distanceToNextThing(car1.pos)) self.assertTrue(r.distanceToNextThing(car2.pos) >= r.getLength()) car3 = Car(r, (21, 0)) self.assertTrue( r.placeObject(car3) ) self.assertEqual(3, r.carCount()) self.assertEqual(0, r.distanceToNextThing(car1.pos)) self.assertEqual(8, r.distanceToNextThing(car3.pos))