~ubuntu-branches/ubuntu/trusty/python-networkx/trusty-proposed

« back to all changes in this revision

Viewing changes to networkx/classes/tests/test_multigraph.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2009-02-28 13:36:24 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090228133624-9l5rwi1ftlzc7b0l
* Upload to unstable now that lenny is released (yay).
* Fix FTBFS with python-support 0.90.3: no longer rely on its internal
  behaviour, and xnow set tests/test.py executable right after “setup.py
  install” (Closes: #517065).
* Drop executable bits from bz2 files.
* Update Vcs-* fields: move from DPMT's svn to collab-maint's git.
* Remote DPMT from Uploaders, following Piotr Ożarowski's request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import copy
 
3
import unittest
 
4
from nose.tools import *
 
5
import networkx
 
6
from test_graph import TestGraph
 
7
 
 
8
 
 
9
class TestMultiGraph(TestGraph):
 
10
    def setUp(self):
 
11
        self.Graph=networkx.MultiGraph
 
12
        # build K3
 
13
        self.k3adj={0: {1: [1], 2: [1]}, 
 
14
                    1: {0: [1], 2: [1]}, 
 
15
                    2: {0: [1], 1: [1]}}
 
16
        self.k3edges=[(0, 1), (0, 2), (1, 2)]
 
17
        self.k3nodes=[0, 1, 2]
 
18
        self.K3=self.Graph()
 
19
        self.K3.adj=self.k3adj
 
20
 
 
21
 
 
22
    def test_data_input(self):
 
23
        pass
 
24
#        G=self.Graph(data={1:[2],2:[1]}, name="test")
 
25
#        assert_equal(G.name,"test")
 
26
#        assert_equal(sorted(G.adj.items()),[(1, {2: [1]}), (2, {1: [1]})])
 
27
 
 
28
 
 
29
    def test_contains(self):
 
30
        G=self.K3
 
31
        assert(1 in G )
 
32
        assert(4 not in G )
 
33
        assert('b' not in G )
 
34
        assert([] not in G )   # no exception for nonhashable
 
35
        assert({1:1} not in G) # no exception for nonhashable
 
36
 
 
37
    def test_order(self):
 
38
        G=self.K3
 
39
        assert_equal(len(G),3)
 
40
        assert_equal(G.order(),3)
 
41
        assert_equal(G.number_of_nodes(),3)
 
42
 
 
43
    def test_getitem(self):
 
44
        G=self.K3
 
45
        assert_equal(G[0],{1: [1], 2: [1]})
 
46
        assert_raises(KeyError, G.__getitem__, 'j')
 
47
        assert_raises((TypeError,networkx.NetworkXError), G.__getitem__, ['A'])
 
48
 
 
49
    def test_remove_node(self):
 
50
        G=self.K3
 
51
        G.remove_node(0)
 
52
        assert_equal(G.adj,{1:{2:[1]},2:{1:[1]}})
 
53
        assert_raises((KeyError,networkx.NetworkXError), G.remove_node,-1)
 
54
 
 
55
 
 
56
    def test_add_edge(self):
 
57
        G=self.Graph()
 
58
        G.add_edge(0,1)
 
59
        assert_equal(G.adj,{0: {1: [1]}, 1: {0: [1]}})
 
60
        G=self.Graph()
 
61
        G.add_edge(*(0,1))
 
62
        assert_equal(G.adj,{0: {1: [1]}, 1: {0: [1]}})
 
63
    
 
64
    def test_add_edges_from(self):
 
65
        G=self.Graph()
 
66
        G.add_edges_from([(0,1)])
 
67
        assert_equal(G.adj,{0: {1: [1]}, 1: {0: [1]}})
 
68
 
 
69
 
 
70
    def test_remove_edge(self):
 
71
        G=self.K3
 
72
        G.remove_edge(0,1)
 
73
        assert_equal(G.adj,{0:{2:[1]},1:{2:[1]},2:{0:[1],1:[1]}})
 
74
        assert_raises((KeyError,networkx.NetworkXError), G.remove_edge,-1,0)
 
75
 
 
76
    def test_remove_edges_from(self):
 
77
        G=self.K3
 
78
        G.remove_edges_from([(0,1)])
 
79
        assert_equal(G.adj,{0:{2:[1]},1:{2:[1]},2:{0:[1],1:[1]}})
 
80
        G.remove_edges_from([(0,0)]) # silent fail
 
81
 
 
82
    def test_get_edge(self):
 
83
        G=self.K3
 
84
        assert_equal(G.get_edge(0,1),[1])
 
85
        assert_equal(G[0][1],[1])
 
86
        assert_raises((KeyError,networkx.NetworkXError), G.get_edge,-1,0)
 
87
 
 
88
    def test_adjacency_iter(self):
 
89
        G=self.K3
 
90
        assert_equal(dict(G.adjacency_iter()),
 
91
                          {0: {1: [1], 2: [1]}, 
 
92
                           1: {0: [1], 2: [1]},
 
93
                           2: {0: [1], 1: [1]}})
 
94
                          
 
95
    def test_to_directed(self):
 
96
        G=self.K3
 
97
        H=networkx.MultiDiGraph(G)
 
98
        assert_equal(G.adj,H.adj)
 
99
        assert_equal(G.adj,H.succ)
 
100
        assert_equal(G.adj,H.pred)
 
101
        H=G.to_directed()
 
102
        assert_equal(G.adj,H.adj)
 
103
        assert_equal(G.adj,H.succ)
 
104
        assert_equal(G.adj,H.pred)
 
105
 
 
106
    def test_selfloops(self):
 
107
        G=self.K3
 
108
        G.add_edge(0,0)
 
109
        assert_equal(G.nodes_with_selfloops(),[0])
 
110
        assert_equal(G.selfloop_edges(),[(0,0,1)])
 
111
        assert_equal(G.number_of_selfloops(),1)