def test_average_fullness_doctest() -> None: """Test the doctest provided for Fleet.average_fullness""" f = Fleet() t = Truck(1423, 10, 'Toronto') p = Parcel(1, 5, 'Buffalo', 'Hamilton') assert t.pack(p) is True f.add_truck(t) assert f.average_fullness() == 50.0
def test_parcel_allocations_doctest() -> None: """Test the doctest provided for Fleet.parcel_allocations""" f = Fleet() t1 = Truck(1423, 10, 'Toronto') p1 = Parcel(27, 5, 'Toronto', 'Hamilton') p2 = Parcel(12, 5, 'Toronto', 'Hamilton') assert t1.pack(p1) is True assert t1.pack(p2) is True t2 = Truck(1333, 10, 'Toronto') p3 = Parcel(28, 5, 'Toronto', 'Hamilton') assert t2.pack(p3) is True f.add_truck(t1) f.add_truck(t2) assert f.parcel_allocations() == {1423: [27, 12], 1333: [28]}
def test_average_distance_travelled_doctest() -> None: """Test the doctest provided for Fleet.average_distance_travelled""" f = Fleet() t1 = Truck(1423, 10, 'Toronto') p1 = Parcel(1, 5, 'Toronto', 'Hamilton') assert t1.pack(p1) is True t2 = Truck(1333, 10, 'Toronto') p2 = Parcel(2, 5, 'Toronto', 'Hamilton') assert t2.pack(p2) is True m = DistanceMap() m.add_distance('Toronto', 'Hamilton', 9) f.add_truck(t1) f.add_truck(t2) assert f.average_distance_travelled(m) == 18.0
def read_trucks(truck_file: str, depot_location: str) -> Fleet: """Read truck data from <truck_file> and return a Fleet containing these trucks, with each truck starting at the <depot_location>. Precondition: <truck_file> is a path to a file containing truck data in the form specified in Assignment 1. """ f = Fleet() with open(truck_file, 'r') as file: for line in file: tokens = line.strip().split(',') tid = int(tokens[0]) capacity = int(tokens[1]) t = Truck(tid, capacity, depot_location) f.add_truck(t) return f
def test_total_unused_space_doctest() -> None: """Test the doctest provided for Fleet.total_unused_space""" f = Fleet() assert f.total_unused_space() == 0 t = Truck(1423, 1000, 'Toronto') p = Parcel(1, 5, 'Buffalo', 'Hamilton') assert t.pack(p) is True f.add_truck(t) assert f.total_unused_space() == 995
def test_num_nonempty_trucks_doctest() -> None: """Test the doctest provided for Fleet.num_nonempty_trucks""" f = Fleet() t1 = Truck(1423, 10, 'Toronto') f.add_truck(t1) p1 = Parcel(1, 5, 'Buffalo', 'Hamilton') assert t1.pack(p1) is True p2 = Parcel(2, 4, 'Toronto', 'Montreal') assert t1.pack(p2) is True assert t1.fullness() == 90.0 t2 = Truck(5912, 20, 'Toronto') f.add_truck(t2) p3 = Parcel(3, 2, 'New York', 'Windsor') assert t2.pack(p3) is True assert t2.fullness() == 10.0 t3 = Truck(1111, 50, 'Toronto') f.add_truck(t3) assert f.num_nonempty_trucks() == 2
def test_greedy_scheduler_example() -> None: """Test GreedyScheduler on the example provided.""" p17 = Parcel(17, 25, 'York', 'Toronto') p21 = Parcel(21, 10, 'York', 'London') p13 = Parcel(13, 8, 'York', 'London') p42 = Parcel(42, 20, 'York', 'Toronto') p25 = Parcel(25, 15, 'York', 'Toronto') p61 = Parcel(61, 15, 'York', 'Hamilton') p76 = Parcel(76, 20, 'York', 'London') t1 = Truck(1, 40, 'York') t2 = Truck(2, 40, 'York') t3 = Truck(3, 25, 'York') f = Fleet() f.add_truck(t1) f.add_truck(t2) f.add_truck(t3) # We've left parcel_file, truck_file, and map_file empty in the config # dictionary below because you should *not* use these in your # GreedyScheduler. It is not responsible for reading data from these files. config = { 'depot_location': 'York', 'parcel_file': '', 'truck_file': '', 'map_file': '', 'algorithm': 'greedy', 'parcel_priority': 'destination', 'parcel_order': 'non-increasing', 'truck_order': 'non-increasing', 'verbose': 'false' } scheduler = GreedyScheduler(config) unscheduled = scheduler.schedule([p17, p21, p13, p42, p25, p61, p76], [t1, t2, t3]) assert unscheduled == [p76] truck_parcels = f.parcel_allocations() assert truck_parcels[1] == [17, 61] assert truck_parcels[2] == [42, 25] assert truck_parcels[3] == [21, 13]
def test_average_distance_travelled() -> None: # (...) p17 = Parcel(17, 25, 'York', 'Toronto') p21 = Parcel(21, 10, 'York', 'London') p13 = Parcel(13, 8, 'York', 'London') p42 = Parcel(42, 20, 'York', 'Toronto') p25 = Parcel(25, 15, 'York', 'Toronto') p61 = Parcel(61, 15, 'York', 'Hamilton') p76 = Parcel(76, 20, 'York', 'London') t1 = Truck(1, 40, 'York') t2 = Truck(2, 40, 'York') t3 = Truck(3, 25, 'York') f = Fleet() f.add_truck(t1) f.add_truck(t2) f.add_truck(t3) dmap = DistanceMap() dmap.add_distance('York', 'Toronto', 1) dmap.add_distance('York', 'London', 2) dmap.add_distance('York', 'Hamilton', 3)
def test_num_trucks_doctest() -> None: """Test the doctest provided for Fleet.num_trucks""" f = Fleet() t1 = Truck(1423, 10, 'Toronto') f.add_truck(t1) assert f.num_trucks() == 1
def test_empty_fleet_allocations() -> None: """Test that an empty dictionary is returned, for an empty fleet of parcel allocations.""" f = Fleet() assert f.parcel_allocations() == {}