|
13 | 13 | import typing
|
14 | 14 | import unittest
|
15 | 15 | import unittest.mock
|
| 16 | +import os |
16 | 17 | from weakref import proxy
|
17 | 18 | import contextlib
|
18 | 19 |
|
| 20 | +from test.support.script_helper import assert_python_ok |
| 21 | + |
19 | 22 | import functools
|
20 | 23 |
|
21 | 24 | py_functools = support.import_fresh_module('functools', blocked=['_functools'])
|
@@ -1204,6 +1207,46 @@ def test_simple_cases(self):
|
1204 | 1207 | self._test_graph({x: {x+1} for x in range(10)},
|
1205 | 1208 | [(x,) for x in range(10, -1, -1)])
|
1206 | 1209 |
|
| 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 | + |
1207 | 1250 | def test_no_dependencies(self):
|
1208 | 1251 | self._test_graph(
|
1209 | 1252 | {1: {2},
|
@@ -1308,6 +1351,45 @@ def test_not_hashable_nodes(self):
|
1308 | 1351 | self.assertRaises(TypeError, ts.add, 1, dict())
|
1309 | 1352 | self.assertRaises(TypeError, ts.add, dict(), dict())
|
1310 | 1353 |
|
| 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 | + |
1311 | 1393 |
|
1312 | 1394 | class TestLRU:
|
1313 | 1395 |
|
|
0 commit comments