Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 2 | import random |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import unittest |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 5 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | verbose = support.verbose |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 7 | nerrors = 0 |
| 8 | |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 9 | def CmpToKey(mycmp): |
| 10 | 'Convert a cmp= function into a key= function' |
| 11 | class K(object): |
| 12 | def __init__(self, obj): |
| 13 | self.obj = obj |
| 14 | def __lt__(self, other): |
| 15 | return mycmp(self.obj, other.obj) == -1 |
| 16 | return K |
| 17 | |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 18 | def check(tag, expected, raw, compare=None): |
| 19 | global nerrors |
| 20 | |
| 21 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 22 | print(" checking", tag) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 23 | |
| 24 | orig = raw[:] # save input in case of error |
| 25 | if compare: |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 26 | raw.sort(key=CmpToKey(compare)) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 27 | else: |
| 28 | raw.sort() |
| 29 | |
| 30 | if len(expected) != len(raw): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 31 | print("error in", tag) |
| 32 | print("length mismatch;", len(expected), len(raw)) |
| 33 | print(expected) |
| 34 | print(orig) |
| 35 | print(raw) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 36 | nerrors += 1 |
| 37 | return |
| 38 | |
| 39 | for i, good in enumerate(expected): |
| 40 | maybe = raw[i] |
| 41 | if good is not maybe: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 42 | print("error in", tag) |
| 43 | print("out of order at index", i, good, maybe) |
| 44 | print(expected) |
| 45 | print(orig) |
| 46 | print(raw) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 47 | nerrors += 1 |
| 48 | return |
| 49 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 50 | class TestBase(unittest.TestCase): |
| 51 | def testStressfully(self): |
| 52 | # Try a variety of sizes at and around powers of 2, and at powers of 10. |
| 53 | sizes = [0] |
| 54 | for power in range(1, 10): |
| 55 | n = 2 ** power |
| 56 | sizes.extend(range(n-1, n+2)) |
| 57 | sizes.extend([10, 100, 1000]) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 58 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 59 | class Complains(object): |
| 60 | maybe_complain = True |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 61 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 62 | def __init__(self, i): |
| 63 | self.i = i |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 64 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 65 | def __lt__(self, other): |
| 66 | if Complains.maybe_complain and random.random() < 0.001: |
| 67 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 68 | print(" complaining at", self, other) |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 69 | raise RuntimeError |
| 70 | return self.i < other.i |
| 71 | |
| 72 | def __repr__(self): |
| 73 | return "Complains(%d)" % self.i |
| 74 | |
| 75 | class Stable(object): |
| 76 | def __init__(self, key, i): |
| 77 | self.key = key |
| 78 | self.index = i |
| 79 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 80 | def __lt__(self, other): |
| 81 | return self.key < other.key |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 82 | |
| 83 | def __repr__(self): |
| 84 | return "Stable(%d, %d)" % (self.key, self.index) |
| 85 | |
| 86 | for n in sizes: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 87 | x = list(range(n)) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 88 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 89 | print("Testing size", n) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 90 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 91 | s = x[:] |
| 92 | check("identity", x, s) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 93 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 94 | s = x[:] |
| 95 | s.reverse() |
| 96 | check("reversed", x, s) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 97 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 98 | s = x[:] |
| 99 | random.shuffle(s) |
| 100 | check("random permutation", x, s) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 101 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 102 | y = x[:] |
| 103 | y.reverse() |
| 104 | s = x[:] |
| 105 | check("reversed via function", y, s, lambda a, b: cmp(b, a)) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 106 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 107 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 108 | print(" Checking against an insane comparison function.") |
| 109 | print(" If the implementation isn't careful, this may segfault.") |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 110 | s = x[:] |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 111 | s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1)) |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 112 | check("an insane function left some permutation", x, s) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 113 | |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 114 | x = [Complains(i) for i in x] |
| 115 | s = x[:] |
| 116 | random.shuffle(s) |
| 117 | Complains.maybe_complain = True |
| 118 | it_complained = False |
| 119 | try: |
| 120 | s.sort() |
| 121 | except RuntimeError: |
| 122 | it_complained = True |
| 123 | if it_complained: |
| 124 | Complains.maybe_complain = False |
| 125 | check("exception during sort left some permutation", x, s) |
Tim Peters | 2d8b765 | 2002-08-01 02:23:06 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 127 | s = [Stable(random.randrange(10), i) for i in range(n)] |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 128 | augmented = [(e, e.index) for e in s] |
| 129 | augmented.sort() # forced stable because ties broken by index |
| 130 | x = [e for e, i in augmented] # a stable sort of s |
| 131 | check("stability", x, s) |
Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 132 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 133 | #============================================================================== |
Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 134 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 135 | class TestBugs(unittest.TestCase): |
Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 136 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 137 | def test_bug453523(self): |
| 138 | # bug 453523 -- list.sort() crasher. |
| 139 | # If this fails, the most likely outcome is a core dump. |
| 140 | # Mutations during a list sort should raise a ValueError. |
Tim Peters | b9099c3 | 2002-11-12 22:08:10 +0000 | [diff] [blame] | 141 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 142 | class C: |
| 143 | def __lt__(self, other): |
| 144 | if L and random.random() < 0.75: |
| 145 | L.pop() |
| 146 | else: |
| 147 | L.append(3) |
| 148 | return random.random() < 0.5 |
Skip Montanaro | 4abd5f0 | 2003-01-02 20:51:08 +0000 | [diff] [blame] | 149 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 150 | L = [C() for i in range(50)] |
| 151 | self.assertRaises(ValueError, L.sort) |
Skip Montanaro | 4abd5f0 | 2003-01-02 20:51:08 +0000 | [diff] [blame] | 152 | |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 153 | def test_undetected_mutation(self): |
| 154 | # Python 2.4a1 did not always detect mutation |
| 155 | memorywaster = [] |
| 156 | for i in range(20): |
| 157 | def mutating_cmp(x, y): |
| 158 | L.append(3) |
| 159 | L.pop() |
| 160 | return cmp(x, y) |
| 161 | L = [1,2] |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 162 | self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp)) |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 163 | def mutating_cmp(x, y): |
| 164 | L.append(3) |
| 165 | del L[:] |
| 166 | return cmp(x, y) |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 167 | self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp)) |
Armin Rigo | 93677f0 | 2004-07-29 12:40:23 +0000 | [diff] [blame] | 168 | memorywaster = [memorywaster] |
| 169 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 170 | #============================================================================== |
Skip Montanaro | 4abd5f0 | 2003-01-02 20:51:08 +0000 | [diff] [blame] | 171 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 172 | class TestDecorateSortUndecorate(unittest.TestCase): |
| 173 | |
| 174 | def test_decorated(self): |
| 175 | data = 'The quick Brown fox Jumped over The lazy Dog'.split() |
| 176 | copy = data[:] |
| 177 | random.shuffle(data) |
| 178 | data.sort(key=str.lower) |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 179 | copy.sort(key=CmpToKey(lambda x,y: cmp(x.lower(), y.lower()))) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 180 | |
| 181 | def test_baddecorator(self): |
| 182 | data = 'The quick Brown fox Jumped over The lazy Dog'.split() |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 183 | self.assertRaises(TypeError, data.sort, key=lambda x,y: 0) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 184 | |
| 185 | def test_stability(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 186 | data = [(random.randrange(100), i) for i in range(200)] |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 187 | copy = data[:] |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 188 | data.sort(key=lambda t: t[0]) # sort on the random first field |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 189 | copy.sort() # sort using both fields |
| 190 | self.assertEqual(data, copy) # should get the same result |
| 191 | |
Raymond Hettinger | 37e1363 | 2003-11-28 21:43:02 +0000 | [diff] [blame] | 192 | def test_key_with_exception(self): |
| 193 | # Verify that the wrapper has been removed |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 194 | data = list(range(-2, 2)) |
Raymond Hettinger | 37e1363 | 2003-11-28 21:43:02 +0000 | [diff] [blame] | 195 | dup = data[:] |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 196 | self.assertRaises(ZeroDivisionError, data.sort, key=lambda x: 1/x) |
Raymond Hettinger | 37e1363 | 2003-11-28 21:43:02 +0000 | [diff] [blame] | 197 | self.assertEqual(data, dup) |
| 198 | |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 199 | def test_key_with_mutation(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 200 | data = list(range(10)) |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 201 | def k(x): |
| 202 | del data[:] |
| 203 | data[:] = range(20) |
| 204 | return x |
| 205 | self.assertRaises(ValueError, data.sort, key=k) |
| 206 | |
| 207 | def test_key_with_mutating_del(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 208 | data = list(range(10)) |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 209 | class SortKiller(object): |
| 210 | def __init__(self, x): |
| 211 | pass |
| 212 | def __del__(self): |
| 213 | del data[:] |
| 214 | data[:] = range(20) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 215 | def __lt__(self, other): |
| 216 | return id(self) < id(other) |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 217 | self.assertRaises(ValueError, data.sort, key=SortKiller) |
| 218 | |
| 219 | def test_key_with_mutating_del_and_exception(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 220 | data = list(range(10)) |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 221 | ## dup = data[:] |
| 222 | class SortKiller(object): |
| 223 | def __init__(self, x): |
| 224 | if x > 2: |
| 225 | raise RuntimeError |
| 226 | def __del__(self): |
| 227 | del data[:] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 228 | data[:] = list(range(20)) |
Michael W. Hudson | 1df0f65 | 2003-12-04 11:25:46 +0000 | [diff] [blame] | 229 | self.assertRaises(RuntimeError, data.sort, key=SortKiller) |
| 230 | ## major honking subtlety: we *can't* do: |
| 231 | ## |
| 232 | ## self.assertEqual(data, dup) |
| 233 | ## |
| 234 | ## because there is a reference to a SortKiller in the |
| 235 | ## traceback and by the time it dies we're outside the call to |
| 236 | ## .sort() and so the list protection gimmicks are out of |
| 237 | ## date (this cost some brain cells to figure out...). |
| 238 | |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 239 | def test_reverse(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 240 | data = list(range(100)) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 241 | random.shuffle(data) |
| 242 | data.sort(reverse=True) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 243 | self.assertEqual(data, list(range(99,-1,-1))) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 244 | |
| 245 | def test_reverse_stability(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 246 | data = [(random.randrange(100), i) for i in range(200)] |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 247 | copy1 = data[:] |
| 248 | copy2 = data[:] |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 249 | data.sort(key=CmpToKey(lambda x,y: cmp(x[0],y[0])), reverse=True) |
| 250 | copy1.sort(key=CmpToKey(lambda x,y: cmp(y[0],x[0]))) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 251 | self.assertEqual(data, copy1) |
| 252 | copy2.sort(key=lambda x: x[0], reverse=True) |
| 253 | self.assertEqual(data, copy2) |
| 254 | |
| 255 | #============================================================================== |
| 256 | |
| 257 | def test_main(verbose=None): |
| 258 | test_classes = ( |
Neal Norwitz | a98e769 | 2005-11-24 21:58:51 +0000 | [diff] [blame] | 259 | TestBase, |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 260 | TestDecorateSortUndecorate, |
| 261 | TestBugs, |
| 262 | ) |
| 263 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 264 | support.run_unittest(*test_classes) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 265 | |
| 266 | # verify reference counting |
| 267 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 268 | import gc |
| 269 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 270 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 271 | support.run_unittest(*test_classes) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 272 | gc.collect() |
| 273 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 274 | print(counts) |
Raymond Hettinger | 42b1ba3 | 2003-10-16 03:41:09 +0000 | [diff] [blame] | 275 | |
| 276 | if __name__ == "__main__": |
| 277 | test_main(verbose=True) |