def test_parking_all_paths_from_example(self): parking_lot = ParkingLot([1, 2, 0, 3]) result = parking_lot.get_all_paths([3, 1, 2, 0]) self.assertIn([(2, 2), (1, 1), (3, 0)], result) self.assertIn([(3, 2), (1, 3), (2, 0), (1, 1), (2, 3), (3, 0), (2, 2)], result) self.assertEqual(len(result), len({tuple(path) for path in result}))
def test_validate_feasibility_ok(self): constraints = {0: {4}, 1: {1, 2, 3}} parking_lot = ParkingLot([1, 2, 3, 4], 1, constraints) try: parking_lot.get_moves([4, 1, 2, 3]) except ValueError: self.fail("Raised ValueError unexpectedly.")
def test_consistent_states(self): state1 = ParkingLot([1, 2, 3], 1) state2 = ParkingState([3, 2, 1], 1) try: state1._validate_two_states(state2) except ValueError: self.fail("Raised ValueError unexpectedly.")
def test_parking_fewer_moves(self): cars = [1, 2, 0, 3] parking_lot = ParkingLot(cars) final = [2, 3, 0, 1] self.assertIn(parking_lot.get_moves(final, retain_state=True), [[(1, 2), (2, 0), (3, 1), (1, 3)], [(2, 2), (3, 1), (1, 3), (2, 0)]])
def setUp(self): self.pk = ParkingLot(4) _, m1 = Car.create_and_park(self.pk, 'ABC', 'White') _, m2 = Car.create_and_park(self.pk, 'MNO', 'Gray') _, m3 = Car.create_and_park(self.pk, 'PQR', 'Black') _, m4 = Car.create_and_park(self.pk, 'XYZ', 'White') self.messages = [m1, m2, m3, m4] self.str_status = self.prepare_status()
def test_parking_all_paths_simple2(self): initial = [1, 2, 0, 3] final = [2, 1, 0, 3] parking_lot = ParkingLot(initial) result = parking_lot.get_all_paths(final) self.assertIn([(1, 2), (2, 0), (1, 1)], result) self.assertIn([(3, 2), (1, 3), (2, 0), (1, 1), (3, 3)], result) # checking uniqueness of paths self.assertEqual(len(result), len({tuple(path) for path in result}))
def test_parking_move_correctly_positioned_car(self): # Edge case: move correctly positioned car, due to constraints initial = [1, 2, 0, 3] final = [2, 1, 0, 3] constraints = {2: {3}} parking_lot = ParkingLot(initial, 0, constraints) self.assertIn(parking_lot.get_moves(final), [[(3, 2), (1, 3), (2, 0), (1, 1), (3, 3)], [(3, 2), (2, 3), (1, 1), (2, 0), (3, 3)]])
def test_parking_active_constraint(self): cars = [1, 2, 0, 3] parking_lot = ParkingLot(cars, 0) final = [2, 3, 0, 1] # due to the following constraints, there can be 1 set of moves possible constraints = {2: {3}} parking_lot.update_constraints(constraints) self.assertListEqual(parking_lot.get_moves(final), [(3, 2), (1, 3), (2, 0), (3, 1)])
def test_companyParked(self): parkingLotObj = ParkingLot(6, 30) # res1 = parkingLotObj.parkVehicle(Car(20, "Google")) # res2 = parkingLotObj.companyParked("Google") self.assertTrue(parkingLotObj.parkVehicle(Car(20, "Google"))) self.assertEqual(parkingLotObj.companyParked("Google"), [Car(20, "Google")]) #self.assertEqual(parkingLotObj.companyParked("Google"), Car(10, "Google")) print(parkingLotObj.companyParked("Google"))
def test_park(self): parkingLotObj = ParkingLot(6, 30) res2 = parkingLotObj.parkVehicle(Car(10, "Amazon")) res3 = parkingLotObj.parkVehicle(Bike(20, "Amazon")) res4 = parkingLotObj.parkVehicle(Bus(30, "Microsoft")) self.assertEqual(res2, True) self.assertEqual(res3, True) self.assertEqual(res4, True)
def test_pk_3_get_first_empty_overflow(self): pk = ParkingLot(3) self.assertEqual([None] * 3, pk.vehicles) car1, m1 = Car.create_and_park(pk, 'ABC', 'White') car2, m2 = Car.create_and_park(pk, 'MNO', 'Gray') car3, m3 = Car.create_and_park(pk, 'PQR', 'Black') self.assertEqual(3, pk.get_first_empty()) self.assertRaises(ParkingLotFull, Car.create_and_park, pk, 'XYZ', 'White')
def test_pk_car_complete_valid(self): pk = ParkingLot(3) car, _ = Car.create_and_park(pk, 'ABC', 'White') self.assertEqual(car, pk.vehicles[car.slot]) self.assertEqual(1, pk.get_first_empty()) message = pk.leave(0) self.assertEqual(None, pk.vehicles[0]) self.assertEqual('Slot number 1 is free', message)
def test_parking_constraints_with_no_solution(self): # normally this configuration would be backtracked initial = [0, 1, 3, 4, 5, 2] final = [2, 1, 4, 5, 3, 0] constraints = {0: {2}, 1: {1, 2, 3}, 2: {3, 4, 5}, 3: {3, 4, 5}, 4: {3, 4, 5}, 5: {2}} parking_lot = ParkingLot(initial, 0, constraints) self.assertIsNone(parking_lot.get_moves(final))
def test_get_first_empty_barebones(self): pk = ParkingLot(3) self.assertEqual(0, pk.get_first_empty()) pk.vehicles[2] = -1 self.assertEqual(0, pk.get_first_empty()) pk.vehicles[0] = -1 self.assertEqual(1, pk.get_first_empty()) pk.vehicles[1] = -1 self.assertEqual(3, pk.get_first_empty())
def test_parking_move_correctly_positioned_car_deeper(self): initial = [1, 2, 3, 4, 5, 0] final = [2, 1, 4, 5, 3, 0] constraints = {0: {1, 2}, 1: {1, 2, 3}, 2: {3, 4, 5}, 3: {3, 4, 5}, 4: {3, 4, 5}, 5: {2}} parking_lot = ParkingLot(initial, 0, constraints) self.assertListEqual(parking_lot.get_moves(final), [(2, 5), (3, 1), (4, 2), (5, 3), (3, 4), (1, 1), (2, 0)])
def main(): parser = argparse.ArgumentParser( description="Vehichle Ticket booking System") parser.add_argument('input_file', type=str, help="Absolute path of the input file") args = parser.parse_args() fpath = args.input_file parking_lot = ParkingLot() with open(fpath, 'r') as fd: commands = fd.readlines() for command in commands: parking_lot.execute_command(command.strip())
def test_car_invalid_inputs(self): pk = ParkingLot(3) self.assertRaises(CarInvalidInputs, Car.create_and_park, pk, None, None) self.assertRaises(CarInvalidInputs, Car.create_and_park, pk, 'ABC', None) self.assertRaises(CarInvalidInputs, Car.create_and_park, pk, None, 'White')
def testInit(self): lots = 6 my_parking = ParkingLot(lots) function_name = "ParkingLot.__init__" print("Running unittest cases for {}\n".format(function_name)) self.assertEqual(len(my_parking.lots.keys()), lots, "unittest for {} failed ".format(function_name)) self.assertEqual(len(my_parking.available_lots), lots, "unittest for {} failed ".format(function_name))
def create_parking_lot(command: str, args: list) -> ParkingLot: if command == CREATE_PARKING_LOT and len(args) == 1: slots = int(args[0]) print(f'Creating Parking lot of {slots} slots.') return ParkingLot(slots) else: print( 'Could not create parking lot initially, you might miss some customers :(' ) exit(1)
class TestParkingLotCar(unittest.TestCase): @staticmethod def prepare_status(): return [ str_status_header, str_status_raw.format('1', 'ABC', 'White'), str_status_raw.format('2', 'MNO', 'Gray'), str_status_raw.format('3', 'PQR', 'Black'), str_status_raw.format('4', 'XYZ', 'White') ] def setUp(self): self.pk = ParkingLot(4) _, m1 = Car.create_and_park(self.pk, 'ABC', 'White') _, m2 = Car.create_and_park(self.pk, 'MNO', 'Gray') _, m3 = Car.create_and_park(self.pk, 'PQR', 'Black') _, m4 = Car.create_and_park(self.pk, 'XYZ', 'White') self.messages = [m1, m2, m3, m4] self.str_status = self.prepare_status() def test_allocated_slot_number_message(self): self.assertEquals(str_allocated_slot_number.format(1), self.messages[0]) self.assertEquals(str_allocated_slot_number.format(2), self.messages[1]) self.assertEquals(str_allocated_slot_number.format(3), self.messages[2]) self.assertEquals(str_allocated_slot_number.format(4), self.messages[3]) def test_status_message(self): messages = self.pk.status() self.assertEquals(self.str_status, messages) def test_registration_numbers_for_cars_with_colour(self): white_cars = self.pk.registration_numbers_for_cars_with_colour('White') self.assertEqual(['ABC', 'XYZ'], white_cars) self.assertNotEqual(['ABC', 'MNO'], white_cars) def test_slot_numbers_for_cars_with_colour(self): slot_numbers = self.pk.slot_numbers_for_cars_with_colour('White') self.assertEqual(['1', '4'], slot_numbers) self.assertNotEqual(['1', '3'], slot_numbers) def test_slot_numbers_for_registration_number(self): slot_numbers = self.pk.slot_number_for_registration_number('ABC') self.assertEqual('1', slot_numbers) def test_slot_numbers_for_registration_number_not_found(self): slot_numbers = self.pk.slot_number_for_registration_number('XXX') self.assertEqual('Not found', slot_numbers) def test_leave(self): message = self.pk.leave(1) self.assertEquals(str_leave.format(2), message) def test_already_left(self): message = self.pk.leave(1) self.assertRaises(ParkingSlotEmpty, self.pk.leave, 1)
def testEmptyLot(self): lots = 6 function_name = "ParkingLot.empty_lot" print("Running unittest cases for {}\n".format(function_name)) my_parking = ParkingLot(lots) lot_id1 = my_parking.get_min_distance_lot() my_parking.park_in_lot(lot_id1, Car('KA-01-BB-0001', 'White')) lot_id2 = my_parking.get_min_distance_lot() my_parking.park_in_lot(lot_id2, Car('KA-01-BB-0002', 'Black')) emptied_car = my_parking.empty_lot(lot_id1) self.assertEqual(emptied_car.reg_no, 'KA-01-BB-0001', "unittest for {} failed ".format(function_name)) self.assertEqual(emptied_car.color, 'White', "testEmptyLot Failed") self.assertEqual(my_parking.lots[lot_id1].available, True, "unittest for {} failed ".format(function_name)) self.assertEqual(my_parking.lots[lot_id1].parked_car, None, "unittest for {} failed ".format(function_name)) lot_id3 = my_parking.get_min_distance_lot() self.assertEqual(lot_id1, lot_id3, "unittest for {} failed ".format(function_name))
class ParkingLotApp: def __init__(self, slots): self.parking_lot = ParkingLot(slots) def park_car(self, reg_no, color): if reg_no in self.parking_lot.cars_parked: return Results.Car_present_error if self.parking_lot.is_slot_available(): slot_id = self.parking_lot.get_min_distance_lot() self.parking_lot.park_in_lot(slot_id, Car(reg_no, color)) return Results.Slot_allocate_success.format(slot_id) else: return Results.Parking_lot_full def leave(self, slot_id): if not slot_id in self.parking_lot.lots: return Results.Invalid_slot slot = self.parking_lot.get_slot(slot_id) if slot.parked_car: car = self.parking_lot.empty_lot(slot_id) return Results.Free_slot_success.format(slot_id) else: return Results.Free_slot_failure.format(slot_id) def status(self): return_string = "Slot No. Registration No Colour\n" for slot in self.parking_lot.lots: if not self.parking_lot.lots[slot].available: slot_obj = self.parking_lot.get_slot(slot) return_string += ( "{}".format(slot) + " ".join(["" for i in range(13 - len(str(slot)))])) return_string += "{} ".format(slot_obj.parked_car.reg_no) return_string += "{}\n".format(slot_obj.parked_car.color) return return_string def filter_cars_with_colour(self, color, return_property="reg_no"): info_list = [] for slot_id in self.parking_lot.lots: if self.parking_lot.lots[slot_id].parked_car: car = self.parking_lot.lots[slot_id].parked_car if car.color.lower() == color.lower(): if return_property == "slot_id": info_list.append(str(slot_id)) else: info_list.append(car.reg_no) return ", ".join(info_list) def slot_number_for_registration_number(self, reg_no): if reg_no in self.parking_lot.cars_parked: return str(self.parking_lot.cars_parked[reg_no]) else: return Results.Not_found
def main(filename=None): """ This function is used to initialize parking lot and executing all instructions depending on the mode. Two modes are as follows: 1. File Mode: requires filename to be passed 2. Interactive mode: Command prompt based shell """ if filename: # Used for File Mode with open(filename) as f: content = f.readlines() content = [x.strip() for x in content] first_line = content[0].split() output = [] if first_line[0] == "exit": sys.exit() elif first_line[0] == "create_parking_lot": parking_lot_new = ParkingLot(int(first_line[1])) output.append( "Created a parking lot with {} slots".format( first_line[1])) if not content[1:]: return "No Instructions Found" for line in content[1:]: if line == "exit": break output.append(parking_lot_new.process(line.split())) for l in output: print(l) sys.exit() else: # Used for Interactive Mode inp = input("$ ").strip().split() if inp[0] == "exit": sys.exit() elif inp[0] == "create_parking_lot": parking_lot_new = ParkingLot(int(inp[1])) print("Created a parking lot with {} slots".format(inp[1])) else: print("No Parking Lot Found") inp = input("$ ").strip().split() while inp: if inp == "exit": sys.exit() print(parking_lot_new.process(inp)) inp = input("$ ").strip().split()
def create_parking_lot(self): capacity_dict = { "two_wheeler": 30, "hatchback": 20, "suv": 5 } rate_dict = { "two_wheeler": {"<2": 20, "2-4": 30, "4-8": 50, ">8": 100}, "hatchback": {"<2": 20, "2-4": 30, "4-8": 50, ">8": 100}, "suv": {"<2": 20, "2-4": 30, "4-8": 50, ">8": 100}, } id = len(self.parking_lot_dict) + 1 parking_lot = ParkingLot(id, capacity_dict, rate_dict) self.register_parking_lot(id, parking_lot)
def test_all(self): parkingLotObj = ParkingLot(3, 10) # Atleast 1 parking spot for car. # First park a car, it should return True. self.assertTrue(parkingLotObj.parkVehicle(Car(10, "Google"))) # Get the list of cars, it should give one car we parked. self.assertEqual(parkingLotObj.companyParked("Google"), [Car(10, "Google")]) # Remove that car successfully. self.assertTrue(parkingLotObj.leaveOperation(Car(10, "Google"))) # Now the list of cars should be empty. self.assertEqual(parkingLotObj.companyParked("Google"), [])
def test_park_in_lot(self): lots = 6 my_parking = ParkingLot(lots) lot_id = my_parking.get_min_distance_lot() function_name = "ParkingLot.park_in_lot" print("Running unittest cases for {}\n".format(function_name)) self.assertEqual(lot_id, 1, "unittest for {} failed ".format(function_name)) my_parking.park_in_lot(lot_id, Car('KA-01-BB-0001', 'White')) self.assertEqual(my_parking.lots[lot_id].available, False, "unittest for {} failed ".format(function_name)) self.assertEqual(my_parking.lots[lot_id].parked_car.reg_no, 'KA-01-BB-0001', "unittest for {} failed ".format(function_name)) with self.assertRaises(Exception) as context: my_parking.park_in_lot(lot_id, Car('KA-01-BB-0002', 'Black')) self.assertTrue('Lot already occupied !' in str(context.exception), "unittest for {} failed ".format(function_name))
def execute_command(parking_lot: ParkingLot, command: str, args: list) -> bool: if command == PARK and len(args) == 3: registration_number = args[0] driver_age = int(args[2]) print( f'Park car with Vehicle Registration Number: {registration_number}, ' f'and the car is driven by driver of Age : {driver_age}') parking_lot.park(Vehicle(registration_number, driver_age)) return True elif command == LEAVE and len(args) == 1: slot = int(args[0]) print(f'Vacating slot : {slot}') parking_lot.leave_from_slot(slot) return True elif command == SLOT_FOR_CAR_WITH_NUMBER and len(args) == 1: registration_number = args[0] slot = parking_lot.slot_number_by_registration_number( registration_number) print(f'The car {registration_number} is parked at {slot}') return True elif command == SLOTS_FOR_DRIVER_OF_AGE and len(args) == 1: driver_age = int(args[0]) slot_list = parking_lot.slot_numbers_by_driver_age(driver_age) print( f'Slot Number List for Drivers of Age {driver_age} is {slot_list}') return True elif command == VEHICLE_REGISTRATION_NUMBERS_FOR_DRIVER_OF_AGE and len( args) == 1: driver_age = int(args[0]) slot_list = parking_lot.vehicle_registration_numbers_by_driver_age( driver_age) print( f'Vehicle Number List for Drivers of Age {driver_age} is {slot_list}' ) return True print( f'Following command : {command} or arguments: {args} are Incorrect. \n Moving to Next command' ) return False
def execute_command(self): cmd = Command() receiver = ParkingLot() with open(self.file_path, encoding='utf-8') as input_file: for line in input_file: try: splitted = line.split(' ') command = splitted[0].strip().lower() if command in COMMANDS: param = COMMANDS[command][1] params = list() for i in range(1, param + 1): tmp = splitted[i].replace('\n', '') if tmp.isdigit(): tmp = int(tmp) params.append(tmp) cmd.dynamic_invoke(receiver, command, params) else: raise ValueError( 'Invalid command: {}' .format(command)) except Exception as ex: LOGGER.exception(str(ex), exc_info=True)
def test_leave_operation(self): parkingLotObj = ParkingLot(6, 30) self.assertTrue(parkingLotObj.parkVehicle(Car(20, "Google"))) #self.assertTrue(parkingLotObj.leaveOperation(Car(10, "Google"))) self.assertTrue(parkingLotObj.leaveOperation(Car(20, "Google"))) self.assertEqual(parkingLotObj.leaveOperation(Car(20, "Google")), None)
def __init_menu(self): cmd = Command() receiver = ParkingLot() while True: choice = input() self.__exec_choice(choice, cmd, receiver)