def test_valid_number_of_path_lengths(edges):
    lengths = impl.all_pairs_shortest_paths(edges)
    if not edges:
        assert len(lengths) == 0
    else:
        n = len(get_nodes(edges))
        assert len(lengths) == n * (n - 1) / 2
def test_shortest_path(edges):
    assume(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    nodes = get_nodes(edges)
    dist = {(n, n): 0 for n in nodes}
    dist.update({(e[0], e[1]): e[2] for e in lengths})
    dist.update({(e[1], e[0]): e[2] for e in lengths})
    for n1, n2, d in lengths:
        assert min(dist[(n1, n)] + dist[(n, n2)] for n in nodes) == d
def test_no_smaller_than_the_length_can_be(edges):
    assume(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    node_2_min_length = {n: 100 for n in get_nodes(edges)}
    for n1, n2, d in edges:
        node_2_min_length[n1] = min(node_2_min_length[n1], d)
        node_2_min_length[n2] = min(node_2_min_length[n2], d)
    edge_2_weight = {(e[0], e[1]): e[2] for e in edges}
    edge_2_weight.update({(e[1], e[0]): e[2] for e in edges})
    for n1, n2, d in lengths:
        if (n1, n2) in edge_2_weight:
            if d < edge_2_weight[(n1, n2)]:
                assert node_2_min_length[n1] < d, (n1, n2)
                assert node_2_min_length[n2] < d, (n1, n2)
            else:
                assert d == edge_2_weight[(n1, n2)], (n1, n2)
        else:
            assert node_2_min_length[n1] <= d, (n1, n2)
            assert node_2_min_length[n2] <= d, (n1, n2)
def test_invalid_weights():
    with pytest.raises(ValueError):
        impl.all_pairs_shortest_paths([(1, 2, 'a')])
def test_invalid_type():
    with pytest.raises(ValueError):
        impl.all_pairs_shortest_paths({1: 3})
def test_empty_graph():
    assert len(impl.all_pairs_shortest_paths([])) == 0
def test_loops(edges):
    assume(not has_multiple_edges_between_nodes(edges)
           and not is_disconnected(edges))
    with pytest.raises(ValueError):
        impl.all_pairs_shortest_paths(edges)
def test_non_positive_weights(edges):
    assume(not has_multiple_edges_between_nodes(edges)
           and not is_disconnected(edges) and not has_loops(edges)
           and any(e[2] < 1 for e in edges))
    with pytest.raises(ValueError):
        impl.all_pairs_shortest_paths(edges)
def test_no_larger_than_the_length_can_be(edges):
    assume(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    max_length = sum(e[2] for e in edges)
    assert all(l[2] <= max_length for l in lengths), lengths
示例#10
0
def test_valid_nodes(edges):
    assume(edges)
    nodes = get_nodes(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    assert nodes == get_nodes(lengths)
示例#11
0
def test_valid_length_type(edges):
    assume(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    assert all(type(l[2]) == int for l in lengths)
示例#12
0
def test_non_zero_lengths(edges):
    assume(edges)
    lengths = impl.all_pairs_shortest_paths(edges)
    assert all(l[2] > 0 for l in lengths)