summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_graphlib.py
diff options
context:
space:
mode:
authorCarl Friedrich Bolz-Tereick <cfbolz@gmx.de>2021-10-28 20:01:35 (GMT)
committerGitHub <noreply@github.com>2021-10-28 20:01:35 (GMT)
commit7401694807fc6b5f7b35ff73c06f4bb852e02946 (patch)
tree251e1548d8f1070c9b7c939c8301cb0a66b4d753 /Lib/test/test_graphlib.py
parent03db1bbfd2d3f5a343c293b2f0e09a1e962df7ea (diff)
downloadcpython-7401694807fc6b5f7b35ff73c06f4bb852e02946.zip
cpython-7401694807fc6b5f7b35ff73c06f4bb852e02946.tar.gz
cpython-7401694807fc6b5f7b35ff73c06f4bb852e02946.tar.bz2
bpo-45624: make test_graphlib not depend on the iteration order of sets (GH-29233)
the current test depended on integer sets being iterated on in a certain fixed order. That order is different on PyPy (insertion based) and could change in CPython in the future in theory. Make the test robust against a different iteration order by sorting.
Diffstat (limited to 'Lib/test/test_graphlib.py')
-rw-r--r--Lib/test/test_graphlib.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/test/test_graphlib.py b/Lib/test/test_graphlib.py
index 0043253..86246a6 100644
--- a/Lib/test/test_graphlib.py
+++ b/Lib/test/test_graphlib.py
@@ -13,13 +13,19 @@ class TestTopologicalSort(unittest.TestCase):
nodes = ts.get_ready()
for node in nodes:
ts.done(node)
- yield nodes
+ yield tuple(sorted(nodes))
ts = graphlib.TopologicalSorter(graph)
self.assertEqual(list(static_order_with_groups(ts)), list(expected))
ts = graphlib.TopologicalSorter(graph)
- self.assertEqual(list(ts.static_order()), list(chain(*expected)))
+ # need to be a bit careful comparing the result of ts.static_order and
+ # expected, because the order within a group is dependent on set
+ # iteration order
+ it = iter(ts.static_order())
+ for group in expected:
+ tsgroup = {next(it) for element in group}
+ self.assertEqual(set(group), tsgroup)
def _assert_cycle(self, graph, cycle):
ts = graphlib.TopologicalSorter()
@@ -36,7 +42,7 @@ class TestTopologicalSort(unittest.TestCase):
def test_simple_cases(self):
self._test_graph(
{2: {11}, 9: {11, 8}, 10: {11, 3}, 11: {7, 5}, 8: {7, 3}},
- [(3, 5, 7), (11, 8), (2, 10, 9)],
+ [(3, 5, 7), (8, 11), (2, 9, 10)],
)
self._test_graph({1: {}}, [(1,)])
@@ -80,7 +86,7 @@ class TestTopologicalSort(unittest.TestCase):
def test_the_node_multiple_times(self):
# Test same node multiple times in dependencies
- self._test_graph({1: {2}, 3: {4}, 0: [2, 4, 4, 4, 4, 4]}, [(2, 4), (1, 3, 0)])
+ self._test_graph({1: {2}, 3: {4}, 0: [2, 4, 4, 4, 4, 4]}, [(2, 4), (0, 1, 3)])
# Test adding the same dependency multiple times
ts = graphlib.TopologicalSorter()
@@ -242,3 +248,6 @@ class TestTopologicalSort(unittest.TestCase):
self.assertNotEqual(run1, "")
self.assertNotEqual(run2, "")
self.assertEqual(run1, run2)
+
+if __name__ == "__main__":
+ unittest.main()