1
"""Unit test for the graph library"""
3
__author__ = "Gustav Magnus Vikstrom (gustavv@ifi.uio.no)"
4
__date__ = "2007-02-12 -- 2007-03-21"
5
__copyright__ = "Copyright (C) 2007 Gustav Magnus Vikstrom"
6
__license__ = "GNU LGPL Version 2.1"
14
class Editor(unittest.TestCase):
16
def testUndirected(self):
17
"""Create undirected graph with edges added out of order (should pass)"""
19
editor = GraphEditor()
20
editor.open(graph, "undirected")
21
editor.initVertices(4)
22
editor.addVertex(0, 2)
23
editor.addVertex(1, 3)
24
editor.addVertex(2, 2)
25
editor.addVertex(3, 3)
34
self.assertEqual(graph.numVertices(), 4)
35
self.assertEqual(graph.numEdges(), 5)
36
self.assertEqual(graph.numArches(), 10)
38
def testDirected(self):
39
"""Create directed graph with edges added out of order (should pass)"""
41
editor = GraphEditor()
42
editor.open(graph, "directed")
43
editor.initVertices(4)
44
editor.addVertex(0, 2)
45
editor.addVertex(1, 1)
46
editor.addVertex(2, 1)
47
editor.addVertex(3, 1)
56
self.assertEqual(graph.numVertices(), 4)
57
self.assertEqual(graph.numEdges(), 5)
58
self.assertEqual(graph.numArches(), 5)
60
class InputOutput(unittest.TestCase):
62
def testUndirectedGraphXML(self):
63
"""Write and read undirected graph to/from file"""
64
graph = UndirectedClique(4)
66
# Create temp file from graph, read file and delete tempfile
67
fd, tmp_name = tempfile.mkstemp(suffix=".xml")
74
self.assertEqual(graph.numVertices(), graph2.numVertices())
75
self.assertEqual(graph.numEdges(), graph2.numEdges())
77
def testDirectedGraphXML(self):
78
"""Write and read directed graph to/from file"""
79
graph = DirectedClique(4)
81
# Create temp file from graph, read file and delete tempfile
82
fd, tmp_name = tempfile.mkstemp(suffix=".xml")
90
self.assertEqual(graph.numVertices(), graph2.numVertices())
91
self.assertEqual(graph.numEdges(), graph2.numEdges())
93
def testMetisGraphConvertion(self):
94
"""Create metis graph file, convert to dolfin xml and read xml file"""
96
fd, tmp_name = tempfile.mkstemp(suffix=".gra")
97
mfile = open(tmp_name, "w")
100
mfile.write(" 0 2\n")
101
mfile.write(" 0 1\n")
105
# Create temp dolfin xml file from metis graph
106
fd, tmp_name = tempfile.mkstemp(suffix=".xml")
107
os.system('dolfin-convert %s %s' % (mfile.name, tmp_name))
109
# Read dolfin xml file
111
file = File(tmp_name)
116
os.remove(mfile.name)
118
self.assertEqual(graph.numVertices(), 3)
119
self.assertEqual(graph.numEdges(), 3)
121
def testScotchGraphConvertion(self):
122
"""Create scotch graph file, convert to dolfin xml and read xml file"""
123
fd, tmp_name = tempfile.mkstemp(suffix=".grf")
124
sfile = open(tmp_name, "w")
127
sfile.write("1 000\n")
128
sfile.write("2 1 2\n")
129
sfile.write("2 0 2\n")
130
sfile.write("2 0 1\n")
134
# Create temp dolfin xml file from scotch graph
135
fd, tmp_name = tempfile.mkstemp(suffix=".xml")
136
os.system('dolfin-convert %s %s' % (sfile.name, tmp_name))
138
# Read dolfin xml file
140
file = File(tmp_name)
145
os.remove(sfile.name)
147
self.assertEqual(graph.numVertices(), 3)
148
self.assertEqual(graph.numEdges(), 3)
150
class Partitioning(unittest.TestCase):
152
def testPartition(self):
153
"""Create a graph and partition it"""
168
editor = GraphEditor()
169
editor.open(graph, "undirected")
170
editor.initVertices(nn)
171
editor.addVertex(0, 1)
172
editor.addVertex(1, 3)
173
editor.addVertex(2, 2)
174
editor.addVertex(3, 5)
175
editor.addVertex(4, 2)
176
editor.addVertex(5, 2)
177
editor.addVertex(6, 2)
178
editor.addVertex(7, 1)
179
editor.addVertex(8, 2)
193
parts = numpy.array(0, 'L')
196
GraphPartition.partition(graph, num_part, parts)
197
GraphPartition.eval(graph, num_part, parts)
198
GraphPartition.disp(graph, num_part, parts)
199
edgecut = GraphPartition.edgecut(graph, num_part, parts)
200
GraphPartition.check(graph, num_part, parts)
202
if __name__ == "__main__":