annotate Lib/test/test_sort.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
1 from test import test_support
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2 import random
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
3 import sys
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
4 import unittest
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
6 verbose = test_support.verbose
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7 nerrors = 0
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
9 def check(tag, expected, raw, compare=None):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
10 global nerrors
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
11
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12 if verbose:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13 print " checking", tag
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
15 orig = raw[:] # save input in case of error
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16 if compare:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 raw.sort(compare)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 else:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19 raw.sort()
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
20
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21 if len(expected) != len(raw):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
22 print "error in", tag
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23 print "length mismatch;", len(expected), len(raw)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
24 print expected
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
25 print orig
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
26 print raw
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 nerrors += 1
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30 for i, good in enumerate(expected):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31 maybe = raw[i]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 if good is not maybe:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33 print "error in", tag
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34 print "out of order at index", i, good, maybe
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
35 print expected
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
36 print orig
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
37 print raw
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
38 nerrors += 1
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
39 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
41 class TestBase(unittest.TestCase):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
42 def testStressfully(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
43 # Try a variety of sizes at and around powers of 2, and at powers of 10.
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
44 sizes = [0]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
45 for power in range(1, 10):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
46 n = 2 ** power
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
47 sizes.extend(range(n-1, n+2))
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
48 sizes.extend([10, 100, 1000])
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
49
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
50 class Complains(object):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
51 maybe_complain = True
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
53 def __init__(self, i):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
54 self.i = i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
56 def __lt__(self, other):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
57 if Complains.maybe_complain and random.random() < 0.001:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
58 if verbose:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
59 print " complaining at", self, other
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
60 raise RuntimeError
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
61 return self.i < other.i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
63 def __repr__(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
64 return "Complains(%d)" % self.i
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
65
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
66 class Stable(object):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
67 def __init__(self, key, i):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
68 self.key = key
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
69 self.index = i
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
70
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
71 def __cmp__(self, other):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
72 return cmp(self.key, other.key)
48213
7527da2b05e8 Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 35937
diff changeset
73 __hash__ = None # Silence Py3k warning
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
74
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
75 def __repr__(self):
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
76 return "Stable(%d, %d)" % (self.key, self.index)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
77
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
78 for n in sizes:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
79 x = range(n)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
80 if verbose:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
81 print "Testing size", n
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
82
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
83 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
84 check("identity", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
85
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
86 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
87 s.reverse()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
88 check("reversed", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
89
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
90 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
91 random.shuffle(s)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
92 check("random permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
93
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
94 y = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
95 y.reverse()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
96 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
97 check("reversed via function", y, s, lambda a, b: cmp(b, a))
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
98
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
99 if verbose:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
100 print " Checking against an insane comparison function."
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
101 print " If the implementation isn't careful, this may segfault."
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
102 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
103 s.sort(lambda a, b: int(random.random() * 3) - 1)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
104 check("an insane function left some permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
105
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
106 x = [Complains(i) for i in x]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
107 s = x[:]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
108 random.shuffle(s)
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
109 Complains.maybe_complain = True
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
110 it_complained = False
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
111 try:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
112 s.sort()
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
113 except RuntimeError:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
114 it_complained = True
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
115 if it_complained:
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
116 Complains.maybe_complain = False
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
117 check("exception during sort left some permutation", x, s)
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
118
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
119 s = [Stable(random.randrange(10), i) for i in xrange(n)]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
120 augmented = [(e, e.index) for e in s]
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
121 augmented.sort() # forced stable because ties broken by index
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
122 x = [e for e, i in augmented] # a stable sort of s
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
123 check("stability", x, s)
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
124
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
125 #==============================================================================
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
126
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
127 class TestBugs(unittest.TestCase):
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
128
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
129 def test_bug453523(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
130 # bug 453523 -- list.sort() crasher.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
131 # If this fails, the most likely outcome is a core dump.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
132 # Mutations during a list sort should raise a ValueError.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
133
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
134 class C:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
135 def __lt__(self, other):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
136 if L and random.random() < 0.75:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
137 L.pop()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
138 else:
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
139 L.append(3)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
140 return random.random() < 0.5
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
141
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
142 L = [C() for i in range(50)]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
143 self.assertRaises(ValueError, L.sort)
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
144
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
145 def test_cmpNone(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
146 # Testing None as a comparison function.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
147
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
148 L = range(50)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
149 random.shuffle(L)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
150 L.sort(None)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
151 self.assertEqual(L, range(50))
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
152
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
153 def test_undetected_mutation(self):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
154 # Python 2.4a1 did not always detect mutation
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
155 memorywaster = []
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
156 for i in range(20):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
157 def mutating_cmp(x, y):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
158 L.append(3)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
159 L.pop()
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
160 return cmp(x, y)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
161 L = [1,2]
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
162 self.assertRaises(ValueError, L.sort, mutating_cmp)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
163 def mutating_cmp(x, y):
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
164 L.append(3)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
165 del L[:]
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
166 return cmp(x, y)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
167 self.assertRaises(ValueError, L.sort, mutating_cmp)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
168 memorywaster = [memorywaster]
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 31389
diff changeset
169
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
170 #==============================================================================
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
171
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
172 class TestDecorateSortUndecorate(unittest.TestCase):
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
173
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
174 def test_decorated(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
175 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
176 copy = data[:]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
177 random.shuffle(data)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
178 data.sort(key=str.lower)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
179 copy.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
180
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
181 def test_baddecorator(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
182 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
183 self.assertRaises(TypeError, data.sort, None, lambda x,y: 0)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
184
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
185 def test_stability(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
186 data = [(random.randrange(100), i) for i in xrange(200)]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
187 copy = data[:]
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
188 data.sort(key=lambda x: x[0]) # sort on the random first field
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
189 copy.sort() # sort using both fields
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
190 self.assertEqual(data, copy) # should get the same result
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
191
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
192 def test_cmp_and_key_combination(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
193 # Verify that the wrapper has been removed
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
194 def compare(x, y):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
195 self.assertEqual(type(x), str)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
196 self.assertEqual(type(x), str)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
197 return cmp(x, y)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
198 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
199 data.sort(cmp=compare, key=str.lower)
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
200
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
201 def test_badcmp_with_key(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
202 # Verify that the wrapper has been removed
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
203 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
204 self.assertRaises(TypeError, data.sort, "bad", str.lower)
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
205
31041
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
206 def test_key_with_exception(self):
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
207 # Verify that the wrapper has been removed
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
208 data = range(-2,2)
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
209 dup = data[:]
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
210 self.assertRaises(ZeroDivisionError, data.sort, None, lambda x: 1 // x)
31041
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
211 self.assertEqual(data, dup)
6f01700c1808 Make sure the list.sort's decorate step unwinds itself before returning
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
212
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
213 def test_key_with_mutation(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
214 data = range(10)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
215 def k(x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
216 del data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
217 data[:] = range(20)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
218 return x
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
219 self.assertRaises(ValueError, data.sort, key=k)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
220
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
221 def test_key_with_mutating_del(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
222 data = range(10)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
223 class SortKiller(object):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
224 def __init__(self, x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
225 pass
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
226 def __del__(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
227 del data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
228 data[:] = range(20)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
229 self.assertRaises(ValueError, data.sort, key=SortKiller)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
230
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
231 def test_key_with_mutating_del_and_exception(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
232 data = range(10)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
233 ## dup = data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
234 class SortKiller(object):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
235 def __init__(self, x):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
236 if x > 2:
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
237 raise RuntimeError
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
238 def __del__(self):
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
239 del data[:]
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
240 data[:] = range(20)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
241 self.assertRaises(RuntimeError, data.sort, key=SortKiller)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
242 ## major honking subtlety: we *can't* do:
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
243 ##
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
244 ## self.assertEqual(data, dup)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
245 ##
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
246 ## because there is a reference to a SortKiller in the
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
247 ## traceback and by the time it dies we're outside the call to
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
248 ## .sort() and so the list protection gimmicks are out of
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
249 ## date (this cost some brain cells to figure out...).
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
250
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
251 def test_reverse(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
252 data = range(100)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
253 random.shuffle(data)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
254 data.sort(reverse=True)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
255 self.assertEqual(data, range(99,-1,-1))
30810
2cba0de70df6 Add list.sorted() classmethod.
Raymond Hettinger <python@rcn.com>
parents: 30697
diff changeset
256 self.assertRaises(TypeError, data.sort, "wrong type")
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
257
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
258 def test_reverse_stability(self):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
259 data = [(random.randrange(100), i) for i in xrange(200)]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
260 copy1 = data[:]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
261 copy2 = data[:]
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
262 data.sort(cmp=lambda x,y: cmp(x[0],y[0]), reverse=True)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
263 copy1.sort(cmp=lambda x,y: cmp(y[0],x[0]))
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
264 self.assertEqual(data, copy1)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
265 copy2.sort(key=lambda x: x[0], reverse=True)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
266 self.assertEqual(data, copy2)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
267
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
268 #==============================================================================
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
269
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
270 def test_main(verbose=None):
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
271 test_classes = (
35937
e6d31cbf2ed3 move test into a unittest.TestCase, no functional changes
Neal Norwitz <nnorwitz@gmail.com>
parents: 32858
diff changeset
272 TestBase,
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
273 TestDecorateSortUndecorate,
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
274 TestBugs,
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
275 )
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
276
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58198
diff changeset
277 with test_support.check_py3k_warnings(
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
278 ("the cmp argument is not supported", DeprecationWarning)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
279 test_support.run_unittest(*test_classes)
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
280
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
281 # verify reference counting
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
282 if verbose and hasattr(sys, "gettotalrefcount"):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
283 import gc
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
284 counts = [None] * 5
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
285 for i in xrange(len(counts)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
286 test_support.run_unittest(*test_classes)
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
287 gc.collect()
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
288 counts[i] = sys.gettotalrefcount()
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48213
diff changeset
289 print counts
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
290
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
291 if __name__ == "__main__":
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 27036
diff changeset
292 test_main(verbose=True)