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