コード例 #1
0
def test_distance_map_complex() -> None:
    """Testing complex applications of DistanceMap"""
    m = DistanceMap()
    m.add_distance('A', 'B', 10)
    assert m.distance('A', 'C') == -1
    assert m.distance('A', 'A') == 0
    assert m.distance('A', 'B') == 10
    m.add_distance('A', 'B', 100)
    assert m.distance('A', 'B') == 100
コード例 #2
0
def test_distance_map_basic() -> None:
    """Test DistanceMap when a single distance is provided."""
    m = DistanceMap()
    assert m.distance('Montreal', 'Toronto') == -1
    m.add_distance('Montreal', 'Toronto', 4)
    assert m.distance('Montreal', 'Toronto') == 4
    assert m.distance('Toronto', 'Montreal') == 4
    m.add_distance('Toronto', 'Montreal', 5)
    assert m.distance('Toronto', 'Montreal') == 5
コード例 #3
0
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
コード例 #4
0
def read_distance_map(distance_map_file: str) -> DistanceMap:
    """Read distance data from <distance_map_file> and return a DistanceMap
    that records it.

    Precondition: <distance_map_file> is the path to a file containing distance
                  data in the form specified in Assignment 1.
    """
    dm = DistanceMap()
    with open(distance_map_file, 'r') as file:
        for line in file:
            tokens = line.strip().split(',')
            c1 = tokens[0].strip()
            c2 = tokens[1].strip()
            distance1 = int(tokens[2].strip())
            distance2 = int(tokens[3].strip()) if len(tokens) == 4 \
                else distance1
            dm.add_distance(c1, c2, distance1, distance2)
    return dm
コード例 #5
0
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)
コード例 #6
0
def test_add_distance_different_back_forth() -> None:
    m = DistanceMap()
    m.add_distance("Montreal", "Toronto", 115, 100)
    assert m.distance("Montreal", "Toronto") == 115
    assert m.distance("Toronto", "Montreal") == 100