def test_update_in_at_end(): road = Road([ [ [None, None, None], [None, None, None], [None, None, None] ], [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, None], [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)] ] ]) road.update_in(0) assert road.in_lanes[0][2] is not None assert isinstance(road.in_lanes[0][2], Car) assert road.in_lanes[0][2].waiting_time == 1 road.update_in(1) assert road.in_lanes[1][2] is None road.update_in(2) assert road.in_lanes[2][2] is not None assert isinstance(road.in_lanes[2][2], Car) assert road.in_lanes[2][2].waiting_time == 1 for _ in range(4): road.update_in(0) road.update_in(2) assert road.in_lanes[0][2].waiting_time == 5 assert road.in_lanes[2][2].waiting_time == 5
def test_pushCars(self): lanes = 5 for x in range(lanes + 1): road = Road(lanes, 100, None) self.assertEqual(x, road.pushCars(x)) for x in range(lanes + 1, 100): road = Road(lanes, 100, None) self.assertEqual(lanes, road.pushCars(x))
def test_tunneling(self): # obstacle speedLimits = SpeedLimits( [SpeedLimit.createObstacle((10,0))], 5 ) r = Road(1, 20, speedLimits) car = Car(r, (0,0)) self.assertTrue(r.placeObject(car)) for x in range(100): r.update() self.assertEqual(0, car.velocity) self.assertEqual((9, 0), car.pos)
def test_tunneling(self): # obstacle speedLimits = SpeedLimits([SpeedLimit.createObstacle((10, 0))], 5) r = Road(1, 20, speedLimits) car = Car(r, (0, 0)) self.assertTrue(r.placeObject(car)) for x in range(100): r.update() self.assertEqual(0, car.velocity) self.assertEqual((9, 0), car.pos)
def setUp(self): speedLimit1 = SpeedLimit(range=((50, 0), (80, 0)), limit=1, ticks=0) obstacle = SpeedLimit.createObstacle((90, 0)) self.maxSpeed = 5 self.oneLaneRoad = Road(1, 100, SpeedLimits([speedLimit1], self.maxSpeed)) self.twoLaneRoad = Road( 2, 100, SpeedLimits([speedLimit1, obstacle], self.maxSpeed)) self.threeLaneRoad = Road( 3, 100, SpeedLimits([speedLimit1, obstacle], self.maxSpeed))
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_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_waiting_car(): road = Road([ [ [None, None, None], [None, None, None], ], [ [None, None, Car(Directions.TOP, TurnDirection.LEFT)], [None, None, None], ] ]) assert road.has_waiting_car(0) assert not road.has_waiting_car(1)
def test_pull_no_car(): road = Road([ [ [None, None, None], [None, None, None], ], [ [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], ] ]) assert road.pull_car(0) is None assert road.pull_car(1) is None
def test_update_in_two(): road = Road([ [ [None, None, None], [None, None, None], [None, None, None] ], [ [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None], [None, None, None], [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT), None] ] ]) road.update_in(0) assert road.in_lanes[0][1] is not None assert isinstance(road.in_lanes[0][1], Car) assert road.in_lanes[0][2] is not None assert isinstance(road.in_lanes[0][2], Car) road.update_in(1) assert road.in_lanes[1][1] is None assert road.in_lanes[1][2] is None road.update_in(2) assert road.in_lanes[2][1] is not None assert isinstance(road.in_lanes[2][1], Car) assert road.in_lanes[2][2] is not None assert isinstance(road.in_lanes[2][2], Car)
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 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 setUp(self): self.maxSpeed = 5 speedLimits = SpeedLimits([ SpeedLimit(range=((25, 0), (30, 0)), limit=1, ticks=0), SpeedLimit.createObstacle((80, 2)) ], self.maxSpeed) self.road = Road(3, 100, speedLimits)
def test_update_out_at_end(): road = Road([ [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, None], [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)] ], [ [None, None, None], [None, None, None], [None, None, None] ] ]) road.update_out() assert road.out_lanes[0][2] is None assert road.out_lanes[1][2] is None assert road.out_lanes[2][2] is None
def test_pull_car(): road = Road([ [ [None, None, None], [None, None, None], ], [ [None, None, Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, Car(Directions.TOP, TurnDirection.LEFT)], ] ]) car1 = road.pull_car(1) car0 = road.pull_car(0) assert car0 is not None assert isinstance(car0, Car) assert car0.source == Directions.BOTTOM assert car1 is not None assert isinstance(car1, Car) assert car1.source == Directions.TOP
def test_update_out_middle(): road = Road([ [ [None, Car(Directions.BOTTOM, TurnDirection.LEFT), None], [None, None, None], [None, Car(Directions.BOTTOM, TurnDirection.LEFT), None] ], [ [None, None, None], [None, None, None], [None, None, None] ] ]) road.update_out() assert road.out_lanes[0][2] is not None assert isinstance(road.out_lanes[0][2], Car) assert road.out_lanes[1][2] is None assert road.out_lanes[2][2] is not None assert isinstance(road.out_lanes[2][2], Car)
def test_init(self): r = Road(3, 20, None) self.assertEqual(3, r.getLanesCount()) self.assertEqual(20, r.getLength()) self.assertEqual(r.carCount(), 0) # road is empty, so we should be able to add a car self.assertTrue(r.addCar()) self.assertEqual(1, r.carCount())
def test_update_out_three(): road = Road([ [ [Car(Directions.BOTTOM, TurnDirection.LEFT), Car(Directions.TOP, TurnDirection.LEFT), Car(Directions.BOTTOM, TurnDirection.LEFT)], [None, None, None], [None, None, None] ], [ [None, None, None], [None, None, None], [None, None, None] ] ]) road.update_out() assert road.out_lanes[0][0] is None assert road.out_lanes[0][1] is not None assert isinstance(road.out_lanes[0][1], Car) assert road.out_lanes[0][1].source == Directions.BOTTOM assert road.out_lanes[0][2] is not None assert isinstance(road.out_lanes[0][2], Car) assert road.out_lanes[0][2].source == Directions.TOP
def setUp(self): random.seed(None) self.maxSpeed = 5 speedLimits = SpeedLimits( [SpeedLimit(range=((50, 0), (99, 0)), limit=2, ticks=0)], self.maxSpeed) road = self.road = Road(1, 100, speedLimits) self.car1 = Car(road, (0, 0)) self.carA = Car(road, (25, 0)) self.carB = Car(road, (26, 0)) self.carC = Car(road, (28, 0)) self.car2 = Car(road, (50, 0)) self.allCars = [self.car1, self.carA, self.carB, self.carC, self.car2] self.road.placeObjects(self.allCars)
def test_car_pull_push_in(): road = Road([[[None, None, None], [None, None, None], [None, None, None]], [[None, None, None], [None, None, None], [None, None, None]]]) road.push_car_in(0, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 1 assert arr[0][0] is not None and isinstance(arr[0][0], Car) for _ in range(10): road.push_car_in(0, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 1 assert arr[0][0] is not None and isinstance(arr[0][0], Car) road.push_car_in(1, Car(Directions.BOTTOM, TurnDirection.LEFT)) arr = np.array(road.in_lanes) assert len(arr[arr != np.array(None)]) == 2 assert arr[0][0] is not None and isinstance(arr[0][0], Car) and arr[1][0] is not None and isinstance(arr[1][0], Car)
def create_road(x_start=0, x_end=ROAD_LENGTH, nlanes=NUM_LANES): road = Road() nup = nlanes // 2 merge_lanes(road) # Reversable Lanes for i in range(2, NUM_LANES): # 2,3,4,5,6,7 y = Y_ROAD_START + ((i - 1) * LANE_WIDTH) road.network.add_lane( StraightLane([x_start, y], [x_end, y], (WEST_NODE, EAST_NODE, i), [CL if i == nlanes // 2 + 1 else SL, NL], forbidden=i > nup, is_source=True, is_sink=True)) road.network.add_lane( StraightLane([x_end, y], [x_start, y], (EAST_NODE, WEST_NODE, nlanes - i + 1), forbidden=i <= nup, is_source=True, is_sink=True)) # Entry Ramps for i, x in enumerate(ON_RAMP_STARTS): onramp(road=road, x=x, y=Y_UPSTREAM_RAMP_START, upstream=True, idx=i) onramp(road=road, x=ROAD_LENGTH - x, y=Y_DOWNSTREAM_RAMP_START, upstream=False, idx=i) # Exit Ramps for i, x in enumerate(OFF_RAMP_STARTS): offramp(road=road, x=x, y=Y_ROAD_START, upstream=True, idx=i) offramp(road=road, x=ROAD_LENGTH - x, y=Y_ROAD_START + ROAD_WIDTH, upstream=False, idx=i) return road
[None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None] ] ]), ] @pytest.mark.parametrize("vector,expected", empty_roads_data) def test_generate_empty_road(vector, expected): assert get_empty_road(vector).out_lanes == expected[0] assert get_empty_road(vector).in_lanes == expected[1] yield_first_data = [ (Road([[], []]), []), (Road([[[0]], [[0]]]), [(0, 0)]), (Road([[[0, 0]], [[0, 0]]]), [(0, 0), (0, 1)]), ] @pytest.mark.parametrize("vector,expected", yield_first_data) def test_yielding_first_indexes(vector, expected): output = [(x, y) for (x, y) in vector.out_indexes] assert len(output) == len(expected), "{0}".format(expected) assert output == expected yield_second_data = [ (Road([[], []]), []), (Road([[[0]], [[0]]]), [(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 ))
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_cant_push_car_on_stop(self): speedLimits = SpeedLimits( [SpeedLimit.createObstacle((0,1))], 5 ) r = Road(3, 40, speedLimits) self.assertFalse( r.placeObject( Car(r, (0,1)) ))
def test_cant_push_car_on_stop(self): speedLimits = SpeedLimits([SpeedLimit.createObstacle((0, 1))], 5) r = Road(3, 40, speedLimits) self.assertFalse(r.placeObject(Car(r, (0, 1))))
def test_raise_constructor_exception(road_data, str_contains): with pytest.raises(ValueError) as excinfo: Road(road_data) assert str_contains in str(excinfo.value).lower()
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))
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) ) )