Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / classes / tests / test_digraph_historical.py @ 5cef0f13

History | View | Annotate | Download (3.78 KB)

1
#!/usr/bin/env python
2
"""Original NetworkX graph tests"""
3
from nose.tools import *
4
import networkx
5
import networkx as nx
6
from networkx.testing.utils import *
7

    
8
from historical_tests import HistoricalTests
9

    
10

    
11
class TestDiGraphHistorical(HistoricalTests):
12

    
13
    def setUp(self):
14
        HistoricalTests.setUp(self)
15
        self.G = nx.DiGraph
16

    
17
    def test_in_degree(self):
18
        G = self.G()
19
        G.add_nodes_from('GJK')
20
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
21
                          ('B', 'C'), ('C', 'D')])
22

    
23
        assert_equal(sorted(d for n, d in G.in_degree()), [0, 0, 0, 0, 1, 2, 2])
24
        assert_equal(dict(G.in_degree()),
25
                     {'A': 0, 'C': 2, 'B': 1, 'D': 2, 'G': 0, 'K': 0, 'J': 0})
26

    
27
    def test_out_degree(self):
28
        G = self.G()
29
        G.add_nodes_from('GJK')
30
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
31
                          ('B', 'C'), ('C', 'D')])
32
        assert_equal(sorted([v for k, v in G.in_degree()]),
33
                     [0, 0, 0, 0, 1, 2, 2])
34
        assert_equal(dict(G.out_degree()),
35
                     {'A': 2, 'C': 1, 'B': 2, 'D': 0, 'G': 0, 'K': 0, 'J': 0})
36

    
37
    def test_degree_digraph(self):
38
        H = nx.DiGraph()
39
        H.add_edges_from([(1, 24), (1, 2)])
40
        assert_equal(sorted(d for n, d in H.in_degree([1, 24])), [0, 1])
41
        assert_equal(sorted(d for n, d in H.out_degree([1, 24])), [0, 2])
42
        assert_equal(sorted(d for n, d in H.degree([1, 24])), [1, 2])
43

    
44
    def test_neighbors(self):
45
        G = self.G()
46
        G.add_nodes_from('GJK')
47
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
48
                          ('B', 'C'), ('C', 'D')])
49

    
50
        assert_equal(sorted(G.neighbors('C')), ['D'])
51
        assert_equal(sorted(G['C']), ['D'])
52
        assert_equal(sorted(G.neighbors('A')), ['B', 'C'])
53
        assert_raises(nx.NetworkXError, G.neighbors, 'j')
54
        assert_raises(nx.NetworkXError, G.neighbors, 'j')
55

    
56
    def test_successors(self):
57
        G = self.G()
58
        G.add_nodes_from('GJK')
59
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
60
                          ('B', 'C'), ('C', 'D')])
61
        assert_equal(sorted(G.successors('A')), ['B', 'C'])
62
        assert_equal(sorted(G.successors('A')), ['B', 'C'])
63
        assert_equal(sorted(G.successors('G')), [])
64
        assert_equal(sorted(G.successors('D')), [])
65
        assert_equal(sorted(G.successors('G')), [])
66
        assert_raises(nx.NetworkXError, G.successors, 'j')
67
        assert_raises(nx.NetworkXError, G.successors, 'j')
68

    
69
    def test_predecessors(self):
70
        G = self.G()
71
        G.add_nodes_from('GJK')
72
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
73
                          ('B', 'C'), ('C', 'D')])
74
        assert_equal(sorted(G.predecessors('C')), ['A', 'B'])
75
        assert_equal(sorted(G.predecessors('C')), ['A', 'B'])
76
        assert_equal(sorted(G.predecessors('G')), [])
77
        assert_equal(sorted(G.predecessors('A')), [])
78
        assert_equal(sorted(G.predecessors('G')), [])
79
        assert_equal(sorted(G.predecessors('A')), [])
80
        assert_equal(sorted(G.successors('D')), [])
81

    
82
        assert_raises(nx.NetworkXError, G.predecessors, 'j')
83
        assert_raises(nx.NetworkXError, G.predecessors, 'j')
84

    
85
    def test_reverse(self):
86
        G = nx.complete_graph(10)
87
        H = G.to_directed()
88
        HR = H.reverse()
89
        assert_true(nx.is_isomorphic(H, HR))
90
        assert_equal(sorted(H.edges()), sorted(HR.edges()))
91

    
92
    def test_reverse2(self):
93
        H = nx.DiGraph()
94
        foo = [H.add_edge(u, u + 1) for u in range(0, 5)]
95
        HR = H.reverse()
96
        for u in range(0, 5):
97
            assert_true(HR.has_edge(u + 1, u))
98

    
99
    def test_reverse3(self):
100
        H = nx.DiGraph()
101
        H.add_nodes_from([1, 2, 3, 4])
102
        HR = H.reverse()
103
        assert_equal(sorted(HR.nodes()), [1, 2, 3, 4])