From c00ccf7a6ca51461a2d5dc7f39cc49edc6fc4fc8 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 7 Feb 2026 22:52:48 +0200 Subject: [PATCH] Update `test_sort.py` from 3.14.3 --- Lib/test/test_sort.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_sort.py b/Lib/test/test_sort.py index 37ce489b7..a01f5fc9c 100644 --- a/Lib/test/test_sort.py +++ b/Lib/test/test_sort.py @@ -128,6 +128,27 @@ class TestBase(unittest.TestCase): x = [e for e, i in augmented] # a stable sort of s check("stability", x, s) + def test_small_stability(self): + from itertools import product + from operator import itemgetter + + # Exhaustively test stability across all lists of small lengths + # and only a few distinct elements. + # This can provoke edge cases that randomization is unlikely to find. + # But it can grow very expensive quickly, so don't overdo it. + NELTS = 3 + MAXSIZE = 9 + + pick0 = itemgetter(0) + for length in range(MAXSIZE + 1): + # There are NELTS ** length distinct lists. + for t in product(range(NELTS), repeat=length): + xs = list(zip(t, range(length))) + # Stability forced by index in each element. + forced = sorted(xs) + # Use key= to hide the index from compares. + native = sorted(xs, key=pick0) + self.assertEqual(forced, native) #============================================================================== class TestBugs(unittest.TestCase): @@ -149,7 +170,7 @@ class TestBugs(unittest.TestCase): L = [C() for i in range(50)] self.assertRaises(ValueError, L.sort) - @unittest.expectedFailure # TODO: RUSTPYTHON; figure out how to detect sort mutation that doesn't change list length + @unittest.expectedFailure # TODO: RUSTPYTHON; figure out how to detect sort mutation that doesn't change list length def test_undetected_mutation(self): # Python 2.4a1 did not always detect mutation memorywaster = [] @@ -307,8 +328,7 @@ class TestOptimizedCompares(unittest.TestCase): for L in float_int_lists: check_against_PyObject_RichCompareBool(self, L) - # XXX RUSTPYTHON: added by us but it seems like an implementation detail - @support.cpython_only + @support.cpython_only # XXX RUSTPYTHON: added by us but it seems like an implementation detail def test_unsafe_object_compare(self): # This test is by ppperry. It ensures that unsafe_object_compare is