thirdparty/python-graph/misc/unittests-digraph.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 26 Nov 2008 23:56:19 +0000
changeset 594 06c2228e39cb
permissions -rw-r--r--
Added the python-graph module http://code.google.com/p/python-graph/ Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
594
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com>
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Permission is hereby granted, free of charge, to any person
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# obtaining a copy of this software and associated documentation
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# files (the "Software"), to deal in the Software without
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
# restriction, including without limitation the rights to use,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
# copy, modify, merge, publish, distribute, sublicense, and/or sell
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
# copies of the Software, and to permit persons to whom the
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Software is furnished to do so, subject to the following
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# conditions:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# The above copyright notice and this permission notice shall be
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# included in all copies or substantial portions of the Software.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
# OTHER DEALINGS IN THE SOFTWARE.
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
python-graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
Unit tests for python-graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
"""
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
# Imports
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
import sys
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
sys.path.append('..')
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
import copy
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
import graph
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
import unittest
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
# Tests
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
class testGraph(unittest.TestCase):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
	def setUp(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
		pass
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
	def testRandomGraph(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
		gr.generate(100, 500)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
		self.assertEqual(gr.nodes(),range(100))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
		self.assertEqual(len(gr.edges()), 500)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
		for each, other in gr.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
			self.assertTrue(each in gr)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
			self.assertTrue(other in gr)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
	def testRandomEmptyGraph(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
		gr.generate(0,0)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
		self.assertTrue(gr.nodes() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
		self.assertTrue(gr.edges() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
	def testNodeRemoval(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
		gr.generate(10, 90)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
		gr.del_node(0)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
		self.assertTrue(0 not in gr)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
		for each, other in gr.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
			self.assertTrue(each in gr)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
			self.assertTrue(other in gr)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
	def testGraphInverse(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
		gr.generate(50, 300)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
		inv = gr.inverse()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
		for each in gr.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
			self.assertTrue(each not in inv.edges())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
		for each in inv.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    79
			self.assertTrue(each not in gr.edges())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
	def testEmptyGraphInverse(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
		inv = gr.inverse()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    84
		self.assertTrue(gr.nodes() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
		self.assertTrue(gr.edges() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
	def testGraphComplete(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
		gr.add_nodes(xrange(10))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
		gr.complete()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
		for i in xrange(10):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
			for j in range(10):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
				self.assertTrue((i, j) in gr.edges() or i == j)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
	def testEmptyGraphComplete(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
		gr.complete()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
		self.assertTrue(gr.nodes() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
		self.assertTrue(gr.edges() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
	def testGraphWithOneNodeComplete(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
		gr.add_node(0)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
		gr.complete()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
		self.assertTrue(gr.nodes() == [0])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
		self.assertTrue(gr.edges() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
	def testAddGraph(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
		gr1 = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
		gr1.generate(25, 100)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
		gr2 = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
		gr2.generate(40, 200)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
		gr1.add_graph(gr2)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
		for each in gr2.nodes():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
			self.assertTrue(each in gr1)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
		for each in gr2.edges():
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
			self.assertTrue(each in gr1.edges())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
	def testAddEmptyGraph(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
		gr1 = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
		gr1.generate(25, 100)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
		gr1c = copy.copy(gr1)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
		gr2 = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   124
		gr1.add_graph(gr2)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   125
		self.assertTrue(gr1.nodes() == gr1c.nodes())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   126
		self.assertTrue(gr1.edges() == gr1c.edges())
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
	
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   128
	def testAddSpanningTree(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   129
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   130
		st = {0: None, 1: 0, 2:0, 3: 1, 4: 2, 5: 3}
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   131
		gr.add_spanning_tree(st)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
		for each in st:
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
			self.assertTrue((st[each], each) in gr.edges() or (each, st[each]) == (0, None))
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   134
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   135
	def testAddEmptySpanningTree(self):
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
		gr = graph.digraph()
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
		st = {}
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
		gr.add_spanning_tree(st)
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
		self.assertTrue(gr.nodes() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
		self.assertTrue(gr.edges() == [])
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
		
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
# Run tests
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
if __name__ == '__main__':
06c2228e39cb Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
    unittest.main()