Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2 KB)

1
# test_stochastic.py - unit tests for the stochastic module
2
#
3
# Copyright 2010, 2011, 2012, 2013, 2014, 2015 NetworkX developers.
4
#
5
# This file is part of NetworkX.
6
#
7
# NetworkX is distributed under a BSD license; see LICENSE.txt for more
8
# information.
9
"""Unit tests for the :mod:`networkx.generators.stochastic` module."""
10
from nose.tools import assert_true, assert_equal, raises
11
import networkx as nx
12

    
13

    
14
class TestStochasticGraph(object):
15
    """Unit tests for the :func:`~networkx.stochastic_graph` function.
16

17
    """
18

    
19
    def test_default_weights(self):
20
        G = nx.DiGraph()
21
        G.add_edge(0, 1)
22
        G.add_edge(0, 2)
23
        S = nx.stochastic_graph(G)
24
        assert_true(nx.is_isomorphic(G, S))
25
        assert_equal(sorted(S.edges(data=True)),
26
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})])
27

    
28
    def test_in_place(self):
29
        """Tests for an in-place reweighting of the edges of the graph.
30

31
        """
32
        G = nx.DiGraph()
33
        G.add_edge(0, 1, weight=1)
34
        G.add_edge(0, 2, weight=1)
35
        nx.stochastic_graph(G, copy=False)
36
        assert_equal(sorted(G.edges(data=True)),
37
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})])
38

    
39
    def test_arbitrary_weights(self):
40
        G = nx.DiGraph()
41
        G.add_edge(0, 1, weight=1)
42
        G.add_edge(0, 2, weight=1)
43
        S = nx.stochastic_graph(G)
44
        assert_equal(sorted(S.edges(data=True)),
45
                     [(0, 1, {'weight': 0.5}), (0, 2, {'weight': 0.5})])
46

    
47
    def test_multidigraph(self):
48
        G = nx.MultiDiGraph()
49
        G.add_edges_from([(0, 1), (0, 1), (0, 2), (0, 2)])
50
        S = nx.stochastic_graph(G)
51
        d = dict(weight=0.25)
52
        assert_equal(sorted(S.edges(data=True)),
53
                     [(0, 1, d), (0, 1, d), (0, 2, d), (0, 2, d)])
54

    
55
    @raises(nx.NetworkXNotImplemented)
56
    def test_graph_disallowed(self):
57
        nx.stochastic_graph(nx.Graph())
58

    
59
    @raises(nx.NetworkXNotImplemented)
60
    def test_multigraph_disallowed(self):
61
        nx.stochastic_graph(nx.MultiGraph())