Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / generators / tests / test_directed.py @ 5cef0f13

History | View | Annotate | Download (4.3 KB)

1
"""Generators - Directed Graphs
2
----------------------------
3
"""
4
from nose.tools import assert_equal
5
from nose.tools import assert_false
6
from nose.tools import assert_raises
7
from nose.tools import assert_true
8

    
9
import networkx as nx
10
from networkx.classes import Graph
11
from networkx.classes import MultiDiGraph
12
from networkx.generators.directed import gn_graph
13
from networkx.generators.directed import gnr_graph
14
from networkx.generators.directed import gnc_graph
15
from networkx.generators.directed import random_k_out_graph
16
from networkx.generators.directed import random_uniform_k_out_graph
17
from networkx.generators.directed import scale_free_graph
18

    
19

    
20
class TestGeneratorsDirected(object):
21
    def test_smoke_test_random_graphs(self):
22
        gn_graph(100)
23
        gnr_graph(100, 0.5)
24
        gnc_graph(100)
25
        scale_free_graph(100)
26

    
27
        gn_graph(100, seed=42)
28
        gnr_graph(100, 0.5, seed=42)
29
        gnc_graph(100, seed=42)
30
        scale_free_graph(100, seed=42)
31

    
32
    def test_create_using_keyword_arguments(self):
33
        assert_raises(nx.NetworkXError,
34
                      gn_graph, 100, create_using=Graph())
35
        assert_raises(nx.NetworkXError,
36
                      gnr_graph, 100, 0.5, create_using=Graph())
37
        assert_raises(nx.NetworkXError,
38
                      gnc_graph, 100, create_using=Graph())
39
        assert_raises(nx.NetworkXError,
40
                      scale_free_graph, 100, create_using=Graph())
41
        G = gn_graph(100, seed=1)
42
        MG = gn_graph(100, create_using=MultiDiGraph(), seed=1)
43
        assert_equal(sorted(G.edges()), sorted(MG.edges()))
44
        G = gnr_graph(100, 0.5, seed=1)
45
        MG = gnr_graph(100, 0.5, create_using=MultiDiGraph(), seed=1)
46
        assert_equal(sorted(G.edges()), sorted(MG.edges()))
47
        G = gnc_graph(100, seed=1)
48
        MG = gnc_graph(100, create_using=MultiDiGraph(), seed=1)
49
        assert_equal(sorted(G.edges()), sorted(MG.edges()))
50

    
51
        G = scale_free_graph(100, alpha=0.3, beta=0.4, gamma=0.3,
52
                             delta_in=0.3, delta_out=0.1,
53
                             create_using=MultiDiGraph, seed=1)
54
        assert_raises(ValueError, scale_free_graph, 100, 0.5, 0.4, 0.3)
55
        assert_raises(ValueError, scale_free_graph, 100, alpha=-0.3)
56
        assert_raises(ValueError, scale_free_graph, 100, beta=-0.3)
57
        assert_raises(ValueError, scale_free_graph, 100, gamma=-0.3)
58

    
59

    
60
class TestRandomKOutGraph(object):
61
    """Unit tests for the
62
    :func:`~networkx.generators.directed.random_k_out_graph` function.
63

64
    """
65

    
66
    def test_regularity(self):
67
        """Tests that the generated graph is `k`-out-regular."""
68
        n = 10
69
        k = 3
70
        alpha = 1
71
        G = random_k_out_graph(n, k, alpha)
72
        assert_true(all(d == k for v, d in G.out_degree()))
73
        G = random_k_out_graph(n, k, alpha, seed=42)
74
        assert_true(all(d == k for v, d in G.out_degree()))
75

    
76
    def test_no_self_loops(self):
77
        """Tests for forbidding self-loops."""
78
        n = 10
79
        k = 3
80
        alpha = 1
81
        G = random_k_out_graph(n, k, alpha, self_loops=False)
82
        assert_equal(nx.number_of_selfloops(G), 0)
83

    
84

    
85
class TestUniformRandomKOutGraph(object):
86
    """Unit tests for the
87
    :func:`~networkx.generators.directed.random_uniform_k_out_graph`
88
    function.
89

90
    """
91
    def test_regularity(self):
92
        """Tests that the generated graph is `k`-out-regular."""
93
        n = 10
94
        k = 3
95
        G = random_uniform_k_out_graph(n, k)
96
        assert_true(all(d == k for v, d in G.out_degree()))
97
        G = random_uniform_k_out_graph(n, k, seed=42)
98
        assert_true(all(d == k for v, d in G.out_degree()))
99

    
100
    def test_no_self_loops(self):
101
        """Tests for forbidding self-loops."""
102
        n = 10
103
        k = 3
104
        G = random_uniform_k_out_graph(n, k, self_loops=False)
105
        assert_equal(nx.number_of_selfloops(G), 0)
106
        assert_true(all(d == k for v, d in G.out_degree()))
107

    
108
    def test_with_replacement(self):
109
        n = 10
110
        k = 3
111
        G = random_uniform_k_out_graph(n, k, with_replacement=True)
112
        assert_true(G.is_multigraph())
113
        assert_true(all(d == k for v, d in G.out_degree()))
114

    
115
    def test_without_replacement(self):
116
        n = 10
117
        k = 3
118
        G = random_uniform_k_out_graph(n, k, with_replacement=False)
119
        assert_false(G.is_multigraph())
120
        assert_true(all(d == k for v, d in G.out_degree()))