Esempio n. 1
0
def test_path_getters():
    p = Path([1, 4, 6, -1])
    p2 = PathWithMbox([1, 4, 6, -1], [4, 6])
    assert p.flow_fraction() == 0
    assert p.ingress() == 1
    assert p.egress() == -1
    assert p.iepair() == (1, -1)
    assert listeq(p.nodes(), [1, 4, 6, -1])
    assert listeq(list(p.links()), [(1, 4), (4, 6), (6, -1)])

    # test indexing/length
    assert p[1] == 4
    assert p[-1] == -1
    # Length should be number of hops, not number of nodes
    assert len(p) == 3
    assert len(p2) == 3

    # test the __contains__ method
    assert 6 in p
    assert 7 not in p
    assert 4 in p2 and -1 in p2
    assert not 4 not in p

    # test path setters
    with pytest.raises(TypeError):
        p[1] = 27
Esempio n. 2
0
def test_pathmbox_encoding():
    p = PathWithMbox([1, 2, 3], use_mboxes=[2])
    d = p.encode()
    assert u'nodes' in d
    assert d[u'type'] == u'PathWithMBox'
    assert u'flow_fraction' in d
    assert listeq(d[u'use_mboxes'], [2])
    l = p.decode(p.encode())
    assert p == l
Esempio n. 3
0
def test_list_eq():
    """ Check that list equality function is working as intended"""
    assert ph.listeq([1], [1])
    assert ph.listeq([], [])
    assert ph.listeq([2, 2, 2, 2], [2, 2, 2, 2])
    assert ph.listeq([1], [0x1])
    assert not ph.listeq([1], [1, 1])
    assert not ph.listeq([2, 3, 4], [0, 2, 3, 4])
    assert not ph.listeq([2, 3, 4], [1, 3, 4])
Esempio n. 4
0
def test_shortest(pptc, num):
    """Check shortest path selection"""
    k_shortest_paths(pptc, num)
    # ensure correct number of paths, for different ways of checking the size
    for tc in pptc.tcs():
        expected = min(num, pptc.num_paths(tc, all=True))
        assert len(pptc.paths(tc)) == expected
        assert pptc.paths(tc).size == expected
        assert pptc.num_paths(tc) == expected
        assert pptc.num_paths(tc, False) == expected
    # ensure that the paths are actually shortest for explicit examples
    if num == 4:
        for tc in pptc.tcs():
            assert listeq(list(map(len, pptc.paths(tc))), [1, 2, 2, 2])
Esempio n. 5
0
def test_pathgen_cutoffs(topo):
    # check that cutoffs work as desired, without any predicates
    # All paths for long (>100) length
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 100))
    # for a topology of size n, number of paths is
    # 1 + \sum_{k=2}^{n-2} k! (n-2 choose k)
    assert len(paths) == 1957
    # ensure that all paths have no flow (also sometimes catches wrong order constructor arguments)
    assert all([p.flow_fraction() == 0 for p in paths])
    # only one path with length 1
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 1))
    assert len(paths) == 1
    assert listeq(paths[0].nodes(), [1, 3])
    # 7 paths of length 2 or 1 (1 from previous, and 8-2 other midpoints)
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 2))
    assert len(paths) == 7
    # Test that length of each path is at most 2 since that was the cutoff
    assert max(map(len, paths)) == 2
    # check that max_paths works
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 100, max_paths=4))
    assert len(paths) == 4
Esempio n. 6
0
def test_pathgen_cutoffs(topo):
    # check that cutoffs work as desired, without any predicates
    # All paths for long (>100) length
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 100))
    # for a topology of size n, number of paths is
    # 1 + \sum_{k=2}^{n-2} k! (n-2 choose k)
    assert len(paths) == 1957
    # ensure that all paths have no flow (also sometimes catches wrong order constructor arguments)
    assert all([p.flow_fraction() == 0 for p in paths])
    # only one path with length 1
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 1))
    assert len(paths) == 1
    assert listeq(paths[0].nodes(), [1, 3])
    # 7 paths of length 2 or 1 (1 from previous, and 8-2 other midpoints)
    paths = list(generate_paths_ie(1, 3, topo, null_predicate, 2))
    assert len(paths) == 7
    # Test that length of each path is at most 2 since that was the cutoff
    assert max(map(len, paths)) == 2
    # check that max_paths works
    paths = list(
        generate_paths_ie(1, 3, topo, null_predicate, 100, max_paths=4))
    assert len(paths) == 4