test_resource_graph.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #
  2. # testresources: extensions to python unittest to allow declaritive use
  3. # of resources by test cases.
  4. #
  5. # Copyright (c) 2005-2010 Testresources Contributors
  6. #
  7. # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
  8. # license at the users choice. A copy of both licenses are available in the
  9. # project source as Apache-2.0 and BSD. You may not use this file except in
  10. # compliance with one of these two licences.
  11. #
  12. # Unless required by applicable law or agreed to in writing, software distributed
  13. # under these licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  14. # CONDITIONS OF ANY KIND, either express or implied. See the license you chose
  15. # for the specific language governing permissions and limitations under that
  16. # license.
  17. #
  18. """Test _resource_graph(resource_sets)."""
  19. import testtools
  20. import testresources
  21. from testresources import split_by_resources, _resource_graph
  22. from testresources.tests import ResultWithResourceExtensions
  23. import unittest
  24. def test_suite():
  25. from testresources.tests import TestUtil
  26. loader = TestUtil.TestLoader()
  27. result = loader.loadTestsFromName(__name__)
  28. return result
  29. class TestResourceGraph(testtools.TestCase):
  30. def test_empty(self):
  31. no_resources = frozenset()
  32. resource_sets = [no_resources]
  33. self.assertEqual({no_resources:set([])}, _resource_graph(resource_sets))
  34. def test_discrete(self):
  35. resset1 = frozenset([testresources.TestResourceManager()])
  36. resset2 = frozenset([testresources.TestResourceManager()])
  37. resource_sets = [resset1, resset2]
  38. result = _resource_graph(resource_sets)
  39. self.assertEqual({resset1:set([]), resset2:set([])}, result)
  40. def test_overlapping(self):
  41. res1 = testresources.TestResourceManager()
  42. res2 = testresources.TestResourceManager()
  43. resset1 = frozenset([res1])
  44. resset2 = frozenset([res2])
  45. resset3 = frozenset([res1, res2])
  46. resource_sets = [resset1, resset2, resset3]
  47. result = _resource_graph(resource_sets)
  48. self.assertEqual(
  49. {resset1:set([resset3]),
  50. resset2:set([resset3]),
  51. resset3:set([resset1, resset2])},
  52. result)
  53. class TestDigraphToGraph(testtools.TestCase):
  54. def test_wikipedia_example(self):
  55. """Converting a digraph mirrors it in the XZ axis (matrix view).
  56. See http://en.wikipedia.org/wiki/Travelling_salesman_problem \
  57. #Solving_by_conversion_to_Symmetric_TSP
  58. """
  59. # A B C
  60. # A 1 2
  61. # B 6 3
  62. # C 5 4
  63. A = "A"
  64. Ap = "A'"
  65. B = "B"
  66. Bp = "B'"
  67. C = "C"
  68. Cp = "C'"
  69. digraph = {A:{ B:1, C:2},
  70. B:{A:6, C:3},
  71. C:{A:5, B:4 }}
  72. # and the output
  73. # A B C A' B' C'
  74. # A 0 6 5
  75. # B 1 0 4
  76. # C 2 3 0
  77. # A' 0 1 2
  78. # B' 6 0 3
  79. # C' 5 4 0
  80. expected = {
  81. A :{ Ap:0, Bp:6, Cp:5},
  82. B :{ Ap:1, Bp:0, Cp:4},
  83. C :{ Ap:2, Bp:3, Cp:0},
  84. Ap:{A:0, B:1, C:2 },
  85. Bp:{A:6, B:0, C:3 },
  86. Cp:{A:5, B:4, C:0 }}
  87. self.assertEqual(expected,
  88. testresources._digraph_to_graph(digraph, {A:Ap, B:Bp, C:Cp}))
  89. class TestKruskalsMST(testtools.TestCase):
  90. def test_wikipedia_example(self):
  91. """Performing KruskalsMST on a graph returns a spanning tree.
  92. See http://en.wikipedia.org/wiki/Kruskal%27s_algorithm.
  93. """
  94. A = "A"
  95. B = "B"
  96. C = "C"
  97. D = "D"
  98. E = "E"
  99. F = "F"
  100. G = "G"
  101. graph = {
  102. A:{ B:7, D:5},
  103. B:{A:7, C:8, D:9, E:7},
  104. C:{ B:8, E:5},
  105. D:{A:5, B:9, E:15, F:6},
  106. E:{ B:7, C:5, D:15, F:8, G:9},
  107. F:{ D:6, E:8, G:11},
  108. G:{ E:9, F:11}}
  109. expected = {
  110. A:{ B:7, D:5},
  111. B:{A:7, E:7},
  112. C:{ E:5},
  113. D:{A:5, F:6},
  114. E:{ B:7, C:5, G:9},
  115. F:{ D:6},
  116. G:{ E:9}}
  117. result = testresources._kruskals_graph_MST(graph)
  118. e_weight = sum(sum(row.values()) for row in expected.values())
  119. r_weight = sum(sum(row.values()) for row in result.values())
  120. self.assertEqual(e_weight, r_weight)
  121. self.assertEqual(expected,
  122. testresources._kruskals_graph_MST(graph))