annotate Lib/test/test_sort.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 41f7726e3e51
children 1345c23a0591
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 from test.test_support import verbose
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
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 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
5
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 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
7 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
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 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
10 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
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 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
13 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
14 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
15 else:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16 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
17
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
26
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 return
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
37
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
38 # Try a variety of sizes at and around powers of 2, and at powers of 10.
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
39 sizes = [0]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40 for power in range(1, 10):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
41 n = 2 ** power
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
42 sizes.extend(range(n-1, n+2))
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
43 sizes.extend([10, 100, 1000])
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
44
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
45 class Complains(object):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
46 maybe_complain = True
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
47
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
48 def __init__(self, i):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
49 self.i = i
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
50
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
51 def __lt__(self, other):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52 if Complains.maybe_complain and random.random() < 0.001:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
53 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
54 print " complaining at", self, other
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55 raise RuntimeError
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
56 return self.i < other.i
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
57
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
58 def __repr__(self):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
59 return "Complains(%d)" % self.i
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
60
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
61 class Stable(object):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62 def __init__(self, key, i):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
63 self.key = key
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
64 self.index = i
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
65
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
66 def __cmp__(self, other):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
67 return cmp(self.key, other.key)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
69 def __repr__(self):
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
70 return "Stable(%d, %d)" % (self.key, self.index)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
71
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
72 for n in sizes:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
73 x = range(n)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
74 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
75 print "Testing size", n
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
76
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
77 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
78 check("identity", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
79
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
80 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
81 s.reverse()
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
82 check("reversed", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
83
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
84 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
85 random.shuffle(s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
86 check("random permutation", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
87
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
88 y = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
89 y.reverse()
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
90 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
91 check("reversed via function", y, s, lambda a, b: cmp(b, a))
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
92
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
93 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
94 print " Checking against an insane comparison function."
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
95 print " If the implementation isn't careful, this may segfault."
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
96 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
97 s.sort(lambda a, b: int(random.random() * 3) - 1)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
98 check("an insane function left some permutation", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
99
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
100 x = [Complains(i) for i in x]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
101 s = x[:]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
102 random.shuffle(s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
103 Complains.maybe_complain = True
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
104 it_complained = False
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
105 try:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
106 s.sort()
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
107 except RuntimeError:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
108 it_complained = True
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
109 if it_complained:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
110 Complains.maybe_complain = False
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
111 check("exception during sort left some permutation", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
112
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
113 s = [Stable(random.randrange(10), i) for i in xrange(n)]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
114 augmented = [(e, e.index) for e in s]
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
115 augmented.sort() # forced stable because ties broken by index
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
116 x = [e for e, i in augmented] # a stable sort of s
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
117 check("stability", x, s)
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
118
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
119 def bug453523():
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
120 global nerrors
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
121 from random import random
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
122
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
123 # If this fails, the most likely outcome is a core dump.
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
124 if verbose:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
125 print "Testing bug 453523 -- list.sort() crasher."
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
126
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
127 class C:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
128 def __lt__(self, other):
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
129 if L and random() < 0.75:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
130 pop()
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
131 else:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
132 push(3)
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
133 return random() < 0.5
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
134
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
135 L = [C() for i in range(50)]
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
136 pop = L.pop
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
137 push = L.append
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
138 try:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
139 L.sort()
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
140 except ValueError:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
141 pass
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
142 else:
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
143 print " Mutation during list.sort() wasn't caught."
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
144 nerrors += 1
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
145
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
146 bug453523()
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 24514
diff changeset
147
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
148 def cmpNone():
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
149 global nerrors
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
150
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
151 if verbose:
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
152 print "Testing None as a comparison function."
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
153
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
154 L = range(50)
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
155 random.shuffle(L)
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
156 try:
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
157 L.sort(None)
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
158 except TypeError:
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
159 print " Passing None as cmpfunc failed."
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
160 nerrors += 1
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
161 else:
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
162 if L != range(50):
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
163 print " Passing None as cmpfunc failed."
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
164 nerrors += 1
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
165
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
166 cmpNone()
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26161
diff changeset
167
24457
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
168 if nerrors:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
169 print "Test failed", nerrors
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
170 elif verbose:
27825d268317 New test for sorting sanity. Note that this will fail in earlier Pythons,
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
171 print "Test passed -- no errors."