Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.6 KB)

1
from itertools import groupby
2

    
3
from nose.tools import assert_equal
4
from nose.tools import assert_less_equal
5
from nose.tools import raises
6

    
7
import networkx as nx
8
from networkx.testing import *
9
from networkx import graph_atlas
10
from networkx import graph_atlas_g
11
from networkx.generators.atlas import NUM_GRAPHS
12
from networkx.utils import pairwise
13

    
14

    
15
class TestAtlasGraph(object):
16
    """Unit tests for the :func:`~networkx.graph_atlas` function."""
17

    
18
    @raises(ValueError)
19
    def test_index_too_small(self):
20
        graph_atlas(-1)
21

    
22
    @raises(ValueError)
23
    def test_index_too_large(self):
24
        graph_atlas(NUM_GRAPHS)
25

    
26
    def test_graph(self):
27
        G = graph_atlas(6)
28
        assert_nodes_equal(G.nodes(), range(3))
29
        assert_edges_equal(G.edges(), [(0, 1), (0, 2)])
30

    
31

    
32
class TestAtlasGraphG(object):
33
    """Unit tests for the :func:`~networkx.graph_atlas_g` function."""
34

    
35
    def setUp(self):
36
        self.GAG = graph_atlas_g()
37

    
38
    def test_sizes(self):
39
        G = self.GAG[0]
40
        assert_equal(G.number_of_nodes(), 0)
41
        assert_equal(G.number_of_edges(), 0)
42

    
43
        G = self.GAG[7]
44
        assert_equal(G.number_of_nodes(), 3)
45
        assert_equal(G.number_of_edges(), 3)
46

    
47
    def test_names(self):
48
        for i, G in enumerate(self.GAG):
49
            assert_equal(int(G.name[1:]), i)
50

    
51
    def test_nondecreasing_nodes(self):
52
        # check for nondecreasing number of nodes
53
        for n1, n2 in pairwise(map(len, self.GAG)):
54
            assert_less_equal(n2, n1 + 1)
55

    
56
    def test_nondecreasing_edges(self):
57
        # check for nondecreasing number of edges (for fixed number of
58
        # nodes)
59
        for n, group in groupby(self.GAG, key=nx.number_of_nodes):
60
            for m1, m2 in pairwise(map(nx.number_of_edges, group)):
61
                assert_less_equal(m2, m1 + 1)
62

    
63
    def test_nondecreasing_degree_sequence(self):
64
        # Check for lexicographically nondecreasing degree sequences
65
        # (for fixed number of nodes and edges).
66
        #
67
        # There are three exceptions to this rule in the order given in
68
        # the "Atlas of Graphs" book, so we need to manually exclude
69
        # those.
70
        exceptions = [('G55', 'G56'), ('G1007', 'G1008'), ('G1012', 'G1013')]
71
        for n, group in groupby(self.GAG, key=nx.number_of_nodes):
72
            for m, group in groupby(group, key=nx.number_of_edges):
73
                for G1, G2 in pairwise(group):
74
                    if (G1.name, G2.name) in exceptions:
75
                        continue
76
                    d1 = sorted(d for v, d in G1.degree())
77
                    d2 = sorted(d for v, d in G2.degree())
78
                    assert_less_equal(d1, d2)