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