Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / testing / utils.py @ 5cef0f13

History | View | Annotate | Download (1.69 KB)

1
from nose.tools import assert_equal, assert_in
2

    
3
__all__ = ['assert_nodes_equal', 'assert_edges_equal', 'assert_graphs_equal']
4

    
5

    
6
def assert_nodes_equal(nodes1, nodes2):
7
    # Assumes iterables of nodes, or (node,datadict) tuples
8
    nlist1 = list(nodes1)
9
    nlist2 = list(nodes2)
10
    try:
11
        d1 = dict(nlist1)
12
        d2 = dict(nlist2)
13
    except (ValueError, TypeError):
14
        d1 = dict.fromkeys(nlist1)
15
        d2 = dict.fromkeys(nlist2)
16
    assert_equal(d1, d2)
17

    
18

    
19
def assert_edges_equal(edges1, edges2):
20
    # Assumes iterables with u,v nodes as
21
    # edge tuples (u,v), or
22
    # edge tuples with data dicts (u,v,d), or
23
    # edge tuples with keys and data dicts (u,v,k, d)
24
    from collections import defaultdict
25
    d1 = defaultdict(dict)
26
    d2 = defaultdict(dict)
27
    c1 = 0
28
    for c1, e in enumerate(edges1):
29
        u, v = e[0], e[1]
30
        data = [e[2:]]
31
        if v in d1[u]:
32
            data = d1[u][v] + data
33
        d1[u][v] = data
34
        d1[v][u] = data
35
    c2 = 0
36
    for c2, e in enumerate(edges2):
37
        u, v = e[0], e[1]
38
        data = [e[2:]]
39
        if v in d2[u]:
40
            data = d2[u][v] + data
41
        d2[u][v] = data
42
        d2[v][u] = data
43
    assert_equal(c1, c2)
44
    # can check one direction because lengths are the same.
45
    for n, nbrdict in d1.items():
46
        for nbr, datalist in nbrdict.items():
47
            assert_in(n, d2)
48
            assert_in(nbr, d2[n])
49
            d2datalist = d2[n][nbr]
50
            for data in datalist:
51
                assert_equal(datalist.count(data), d2datalist.count(data))
52

    
53

    
54
def assert_graphs_equal(graph1, graph2):
55
    assert_equal(graph1.adj, graph2.adj)
56
    assert_equal(graph1.nodes, graph2.nodes)
57
    assert_equal(graph1.graph, graph2.graph)