~ubuntu-branches/ubuntu/trusty/python-igraph/trusty

« back to all changes in this revision

Viewing changes to igraph/test/operators.py

  • Committer: Package Import Robot
  • Author(s): TANIGUCHI Takaki
  • Date: 2012-03-17 17:23:55 UTC
  • Revision ID: package-import@ubuntu.com-20120317172355-e9iki37igmxnlq38
Tags: upstream-0.5.4
ImportĀ upstreamĀ versionĀ 0.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
from igraph import *
 
3
 
 
4
class OperatorTests(unittest.TestCase):
 
5
    def testMultiplication(self):
 
6
        g = Graph.Full(3)*3
 
7
        self.failUnless(g.vcount() == 9 and g.ecount() == 9
 
8
                        and g.clusters().membership == [0,0,0,1,1,1,2,2,2])
 
9
 
 
10
    def testIntersection(self):
 
11
        g = Graph.Tree(7, 2) & Graph.Lattice([7])
 
12
        self.failUnless(g.get_edgelist() == [(0, 1)])
 
13
 
 
14
    def testUnion(self):
 
15
        g = Graph.Tree(7, 2) | Graph.Lattice([7])
 
16
        self.failUnless(g.vcount() == 7 and g.ecount() == 12)
 
17
 
 
18
    def testInPlaceAddition(self):
 
19
        g = Graph.Full(3)
 
20
        orig = g
 
21
 
 
22
        # Adding vertices
 
23
        g += 2
 
24
        self.failUnless(g.vcount() == 5 and g.ecount() == 3
 
25
                        and g.clusters().membership == [0,0,0,1,2])
 
26
 
 
27
        # Adding a single edge
 
28
        g += (2, 3)
 
29
        self.failUnless(g.vcount() == 5 and g.ecount() == 4
 
30
                        and g.clusters().membership == [0,0,0,0,1])
 
31
 
 
32
        # Adding two edges
 
33
        g += [(3, 4), (2, 4)]
 
34
        self.failUnless(g.vcount() == 5 and g.ecount() == 6
 
35
                        and g.clusters().membership == [0]*5)
 
36
 
 
37
        # Did we really use the original graph so far?
 
38
        # TODO: disjoint union should be modified so that this assertion
 
39
        # could be moved to the end
 
40
        self.assert_(id(g) == id(orig))
 
41
 
 
42
        # Adding another graph
 
43
        g += Graph.Full(3)
 
44
        self.failUnless(g.vcount() == 8 and g.ecount() == 9
 
45
                        and g.clusters().membership == [0,0,0,0,0,1,1,1])
 
46
 
 
47
        # Adding two graphs
 
48
        g += [Graph.Full(3), Graph.Full(2)]
 
49
        self.failUnless(g.vcount() == 13 and g.ecount() == 13
 
50
                        and g.clusters().membership == [0,0,0,0,0,1,1,1,2,2,2,3,3])
 
51
 
 
52
    def testAddition(self):
 
53
        g0 = Graph.Full(3)
 
54
 
 
55
        # Adding vertices
 
56
        g = g0+2
 
57
        self.failUnless(g.vcount() == 5 and g.ecount() == 3
 
58
                        and g.clusters().membership == [0,0,0,1,2])
 
59
        g0 = g
 
60
 
 
61
        # Adding a single edge
 
62
        g = g0+(2,3)
 
63
        self.failUnless(g.vcount() == 5 and g.ecount() == 4
 
64
                        and g.clusters().membership == [0,0,0,0,1])
 
65
        g0 = g
 
66
 
 
67
        # Adding two edges
 
68
        g = g0+[(3, 4), (2, 4)]
 
69
        self.failUnless(g.vcount() == 5 and g.ecount() == 6
 
70
                        and g.clusters().membership == [0]*5)
 
71
        g0 = g
 
72
 
 
73
        # Adding another graph
 
74
        g = g0+Graph.Full(3)
 
75
        self.failUnless(g.vcount() == 8 and g.ecount() == 9
 
76
                        and g.clusters().membership == [0,0,0,0,0,1,1,1])
 
77
 
 
78
    def testInPlaceSubtraction(self):
 
79
        g = Graph.Full(8)
 
80
        orig = g
 
81
 
 
82
        # Deleting a vertex by vertex selector
 
83
        g -= 7
 
84
        self.failUnless(g.vcount() == 7 and g.ecount() == 21
 
85
                        and g.clusters().membership == [0,0,0,0,0,0,0])
 
86
 
 
87
        # Deleting a vertex
 
88
        g -= g.vs[6]
 
89
        self.failUnless(g.vcount() == 6 and g.ecount() == 15
 
90
                        and g.clusters().membership == [0,0,0,0,0,0])
 
91
 
 
92
        # Deleting two vertices
 
93
        g -= [4, 5]
 
94
        self.failUnless(g.vcount() == 4 and g.ecount() == 6
 
95
                        and g.clusters().membership == [0,0,0,0])
 
96
 
 
97
        # Deleting an edge
 
98
        g -= (1, 2)
 
99
        self.failUnless(g.vcount() == 4 and g.ecount() == 5
 
100
                        and g.clusters().membership == [0,0,0,0])
 
101
 
 
102
        # Deleting three more edges
 
103
        g -= [(1, 3), (0, 2), (0, 3)]
 
104
        self.failUnless(g.vcount() == 4 and g.ecount() == 2
 
105
                        and g.clusters().membership == [0,0,1,1])
 
106
        
 
107
        # Did we really use the original graph so far?
 
108
        self.assert_(id(g) == id(orig))
 
109
 
 
110
        # Subtracting a graph
 
111
        g2 = Graph.Tree(3, 2)
 
112
        g -= g2
 
113
        self.failUnless(g.vcount() == 4 and g.ecount() == 1
 
114
                        and g.clusters().membership == [0,1,2,2])
 
115
 
 
116
 
 
117
    def testSimplify(self):
 
118
        el = [(0,1), (1,0), (1,2), (2,3), (2,3), (2,3), (3,3)] 
 
119
        g = Graph(el)
 
120
        g.es["weight"] = [1, 2, 3, 4, 5, 6, 7]
 
121
 
 
122
        g2 = g.copy()
 
123
        g2.simplify()
 
124
        self.failUnless(g2.vcount() == g.vcount())
 
125
        self.failUnless(g2.ecount() == 3)
 
126
 
 
127
        g2 = g.copy()
 
128
        g2.simplify(loops=False)
 
129
        self.failUnless(g2.vcount() == g.vcount())
 
130
        self.failUnless(g2.ecount() == 4)
 
131
 
 
132
        g2 = g.copy()
 
133
        g2.simplify(multiple=False)
 
134
        self.failUnless(g2.vcount() == g.vcount())
 
135
        self.failUnless(g2.ecount() == g.ecount() - 1)
 
136
 
 
137
    def testSimplifyAttributes(self):
 
138
        el = [(0,1), (1,0), (1,2), (2,3), (2,3), (2,3), (3,3)] 
 
139
        g = Graph(el)
 
140
        g.es["weight"] = [1, 2, 3, 4, 5, 6, 7]
 
141
        g.es["weight2"] = [1, 2, 3, 4, 5, 6, 7]
 
142
 
 
143
        g2 = g.copy()
 
144
        g2.simplify(reduce_attributes=max)
 
145
        self.failUnless(g2.es["weight"] == [2, 3, 6])
 
146
        self.failUnless(g2.es["weight2"] == [2, 3, 6])
 
147
 
 
148
        g2 = g.copy()
 
149
        g2.simplify(reduce_attributes={"weight": max})
 
150
        self.failUnless(g2.es["weight"] == [2, 3, 6])
 
151
 
 
152
        g2 = g.copy()
 
153
        g2.simplify(reduce_attributes={"weight": max, "weight2": sum})
 
154
        self.failUnless(g2.es["weight"] == [2, 3, 6])
 
155
        self.failUnless(g2.es["weight2"] == [3, 3, 15])
 
156
 
 
157
        g = Graph(el, directed=True)
 
158
        g.es["weight"] = [1, 2, 3, 4, 5, 6, 7]
 
159
        g.es["weight2"] = [1, 2, 3, 4, 5, 6, 7]
 
160
 
 
161
        g2 = g.copy()
 
162
        g2.simplify(reduce_attributes={"weight": max, "weight2": sum})
 
163
        self.failUnless(g2.es["weight"] == [1, 2, 3, 6])
 
164
        self.failUnless(g2.es["weight2"] == [1, 2, 3, 15])
 
165
 
 
166
 
 
167
 
 
168
def suite():
 
169
    operator_suite = unittest.makeSuite(OperatorTests)
 
170
    return unittest.TestSuite([operator_suite])
 
171
 
 
172
def test():
 
173
    runner = unittest.TextTestRunner()
 
174
    runner.run(suite())
 
175
    
 
176
if __name__ == "__main__":
 
177
    test()
 
178