Skip to content

bpo-45624: make test_graphlib not depend on the iteration order of sets #29233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Lib/test/test_graphlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ def static_order_with_groups(ts):
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()
Expand All @@ -36,7 +42,7 @@ def _assert_cycle(self, graph, cycle):
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,)])
Expand Down Expand Up @@ -80,7 +86,7 @@ def test_no_dependencies(self):

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()
Expand Down Expand Up @@ -242,3 +248,6 @@ def check_order_with_hash_seed(seed):
self.assertNotEqual(run1, "")
self.assertNotEqual(run2, "")
self.assertEqual(run1, run2)

if __name__ == "__main__":
unittest.main()