Skip to content

Commit d82f697

Browse files
committed
Add more test cases
1 parent 3e2f2d9 commit d82f697

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Lib/test/test_functools.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import typing
1414
import unittest
1515
import unittest.mock
16+
import os
1617
from weakref import proxy
1718
import contextlib
1819

20+
from test.support.script_helper import assert_python_ok
21+
1922
import functools
2023

2124
py_functools = support.import_fresh_module('functools', blocked=['_functools'])
@@ -1204,6 +1207,46 @@ def test_simple_cases(self):
12041207
self._test_graph({x: {x+1} for x in range(10)},
12051208
[(x,) for x in range(10, -1, -1)])
12061209

1210+
self._test_graph({2: {3}, 3: {4}, 4: {5}, 5: {1},
1211+
11: {12}, 12: {13}, 13: {14}, 14: {15}},
1212+
[(1, 15), (5, 14), (4, 13), (3, 12), (2, 11)])
1213+
1214+
self._test_graph({
1215+
0: [1, 2],
1216+
1: [3],
1217+
2: [5, 6],
1218+
3: [4],
1219+
4: [9],
1220+
5: [3],
1221+
6: [7],
1222+
7: [8],
1223+
8: [4],
1224+
9: []
1225+
},
1226+
[(9,), (4,), (3, 8), (1, 5, 7), (6,), (2,), (0,)]
1227+
)
1228+
1229+
self._test_graph({
1230+
0: [1, 2],
1231+
1: [],
1232+
2: [3],
1233+
3: []
1234+
},
1235+
[(1, 3), (2,), (0,)]
1236+
)
1237+
1238+
self._test_graph({
1239+
0: [1, 2],
1240+
1: [],
1241+
2: [3],
1242+
3: [],
1243+
4: [5],
1244+
5: [6],
1245+
6: []
1246+
},
1247+
[(1, 3, 6), (2, 5), (0, 4)]
1248+
)
1249+
12071250
def test_no_dependencies(self):
12081251
self._test_graph(
12091252
{1: {2},
@@ -1308,6 +1351,45 @@ def test_not_hashable_nodes(self):
13081351
self.assertRaises(TypeError, ts.add, 1, dict())
13091352
self.assertRaises(TypeError, ts.add, dict(), dict())
13101353

1354+
def test_order_of_insertion_does_not_matter(self):
1355+
ts = functools.TopologicalSorter()
1356+
ts.add(3, 2, 1)
1357+
ts.add(2, 1)
1358+
ts.add(1, 0)
1359+
1360+
ts2 = functools.TopologicalSorter()
1361+
ts2.add(2, 1)
1362+
ts2.add(1, 0)
1363+
ts2.add(3, 2, 1)
1364+
1365+
self.assertEqual([*ts.static_order()], [*ts2.static_order()])
1366+
1367+
def test_static_order_does_not_change_with_the_hash_seed(self):
1368+
def check_order_with_hash_seed(seed):
1369+
code = """if 1:
1370+
import functools
1371+
ts = functools.TopologicalSorter()
1372+
ts.add(1, 2, 3, 4, 5)
1373+
ts.add(2, 3, 4, 5, 6)
1374+
ts.add(4, 11, 45, 3)
1375+
ts.add(0, 11, 2, 3, 4)
1376+
print(list(ts.static_order()))
1377+
"""
1378+
env = os.environ.copy()
1379+
# signal to assert_python not to do a copy
1380+
# of os.environ on its own
1381+
env['__cleanenv'] = True
1382+
env['PYTHONHASHSEED'] = str(seed)
1383+
out = assert_python_ok('-c', code, **env)
1384+
return out
1385+
1386+
run1 = check_order_with_hash_seed(1234)
1387+
run2 = check_order_with_hash_seed(31415)
1388+
1389+
self.assertNotEqual(run1, "")
1390+
self.assertNotEqual(run2, "")
1391+
self.assertEqual(run1, run2)
1392+
13111393

13121394
class TestLRU:
13131395

0 commit comments

Comments
 (0)