|
1 | | -from test.test_support import verbose |
| 1 | +from test import test_support |
2 | 2 | import random |
3 | | -from UserList import UserList |
| 3 | +import sys |
| 4 | +import unittest |
4 | 5 |
|
| 6 | +verbose = test_support.verbose |
5 | 7 | nerrors = 0 |
6 | 8 |
|
7 | 9 | def check(tag, expected, raw, compare=None): |
@@ -36,91 +38,88 @@ def check(tag, expected, raw, compare=None): |
36 | 38 | nerrors += 1 |
37 | 39 | return |
38 | 40 |
|
39 | | -# Try a variety of sizes at and around powers of 2, and at powers of 10. |
40 | | -sizes = [0] |
41 | | -for power in range(1, 10): |
42 | | - n = 2 ** power |
43 | | - sizes.extend(range(n-1, n+2)) |
44 | | -sizes.extend([10, 100, 1000]) |
45 | | - |
46 | | -class Complains(object): |
47 | | - maybe_complain = True |
| 41 | +class TestBase(unittest.TestCase): |
| 42 | + def testStressfully(self): |
| 43 | + # Try a variety of sizes at and around powers of 2, and at powers of 10. |
| 44 | + sizes = [0] |
| 45 | + for power in range(1, 10): |
| 46 | + n = 2 ** power |
| 47 | + sizes.extend(range(n-1, n+2)) |
| 48 | + sizes.extend([10, 100, 1000]) |
48 | 49 |
|
49 | | - def __init__(self, i): |
50 | | - self.i = i |
| 50 | + class Complains(object): |
| 51 | + maybe_complain = True |
51 | 52 |
|
52 | | - def __lt__(self, other): |
53 | | - if Complains.maybe_complain and random.random() < 0.001: |
54 | | - if verbose: |
55 | | - print " complaining at", self, other |
56 | | - raise RuntimeError |
57 | | - return self.i < other.i |
| 53 | + def __init__(self, i): |
| 54 | + self.i = i |
58 | 55 |
|
59 | | - def __repr__(self): |
60 | | - return "Complains(%d)" % self.i |
| 56 | + def __lt__(self, other): |
| 57 | + if Complains.maybe_complain and random.random() < 0.001: |
| 58 | + if verbose: |
| 59 | + print " complaining at", self, other |
| 60 | + raise RuntimeError |
| 61 | + return self.i < other.i |
61 | 62 |
|
62 | | -class Stable(object): |
63 | | - def __init__(self, key, i): |
64 | | - self.key = key |
65 | | - self.index = i |
| 63 | + def __repr__(self): |
| 64 | + return "Complains(%d)" % self.i |
66 | 65 |
|
67 | | - def __cmp__(self, other): |
68 | | - return cmp(self.key, other.key) |
| 66 | + class Stable(object): |
| 67 | + def __init__(self, key, i): |
| 68 | + self.key = key |
| 69 | + self.index = i |
69 | 70 |
|
70 | | - def __repr__(self): |
71 | | - return "Stable(%d, %d)" % (self.key, self.index) |
| 71 | + def __cmp__(self, other): |
| 72 | + return cmp(self.key, other.key) |
72 | 73 |
|
73 | | -for n in sizes: |
74 | | - x = range(n) |
75 | | - if verbose: |
76 | | - print "Testing size", n |
| 74 | + def __repr__(self): |
| 75 | + return "Stable(%d, %d)" % (self.key, self.index) |
77 | 76 |
|
78 | | - s = x[:] |
79 | | - check("identity", x, s) |
80 | | - |
81 | | - s = x[:] |
82 | | - s.reverse() |
83 | | - check("reversed", x, s) |
| 77 | + for n in sizes: |
| 78 | + x = range(n) |
| 79 | + if verbose: |
| 80 | + print "Testing size", n |
84 | 81 |
|
85 | | - s = x[:] |
86 | | - random.shuffle(s) |
87 | | - check("random permutation", x, s) |
| 82 | + s = x[:] |
| 83 | + check("identity", x, s) |
88 | 84 |
|
89 | | - y = x[:] |
90 | | - y.reverse() |
91 | | - s = x[:] |
92 | | - check("reversed via function", y, s, lambda a, b: cmp(b, a)) |
| 85 | + s = x[:] |
| 86 | + s.reverse() |
| 87 | + check("reversed", x, s) |
93 | 88 |
|
94 | | - if verbose: |
95 | | - print " Checking against an insane comparison function." |
96 | | - print " If the implementation isn't careful, this may segfault." |
97 | | - s = x[:] |
98 | | - s.sort(lambda a, b: int(random.random() * 3) - 1) |
99 | | - check("an insane function left some permutation", x, s) |
100 | | - |
101 | | - x = [Complains(i) for i in x] |
102 | | - s = x[:] |
103 | | - random.shuffle(s) |
104 | | - Complains.maybe_complain = True |
105 | | - it_complained = False |
106 | | - try: |
107 | | - s.sort() |
108 | | - except RuntimeError: |
109 | | - it_complained = True |
110 | | - if it_complained: |
111 | | - Complains.maybe_complain = False |
112 | | - check("exception during sort left some permutation", x, s) |
113 | | - |
114 | | - s = [Stable(random.randrange(10), i) for i in xrange(n)] |
115 | | - augmented = [(e, e.index) for e in s] |
116 | | - augmented.sort() # forced stable because ties broken by index |
117 | | - x = [e for e, i in augmented] # a stable sort of s |
118 | | - check("stability", x, s) |
| 89 | + s = x[:] |
| 90 | + random.shuffle(s) |
| 91 | + check("random permutation", x, s) |
119 | 92 |
|
| 93 | + y = x[:] |
| 94 | + y.reverse() |
| 95 | + s = x[:] |
| 96 | + check("reversed via function", y, s, lambda a, b: cmp(b, a)) |
120 | 97 |
|
121 | | -import unittest |
122 | | -from test import test_support |
123 | | -import sys |
| 98 | + if verbose: |
| 99 | + print " Checking against an insane comparison function." |
| 100 | + print " If the implementation isn't careful, this may segfault." |
| 101 | + s = x[:] |
| 102 | + s.sort(lambda a, b: int(random.random() * 3) - 1) |
| 103 | + check("an insane function left some permutation", x, s) |
| 104 | + |
| 105 | + x = [Complains(i) for i in x] |
| 106 | + s = x[:] |
| 107 | + random.shuffle(s) |
| 108 | + Complains.maybe_complain = True |
| 109 | + it_complained = False |
| 110 | + try: |
| 111 | + s.sort() |
| 112 | + except RuntimeError: |
| 113 | + it_complained = True |
| 114 | + if it_complained: |
| 115 | + Complains.maybe_complain = False |
| 116 | + check("exception during sort left some permutation", x, s) |
| 117 | + |
| 118 | + s = [Stable(random.randrange(10), i) for i in xrange(n)] |
| 119 | + augmented = [(e, e.index) for e in s] |
| 120 | + augmented.sort() # forced stable because ties broken by index |
| 121 | + x = [e for e, i in augmented] # a stable sort of s |
| 122 | + check("stability", x, s) |
124 | 123 |
|
125 | 124 | #============================================================================== |
126 | 125 |
|
@@ -269,6 +268,7 @@ def test_reverse_stability(self): |
269 | 268 |
|
270 | 269 | def test_main(verbose=None): |
271 | 270 | test_classes = ( |
| 271 | + TestBase, |
272 | 272 | TestDecorateSortUndecorate, |
273 | 273 | TestBugs, |
274 | 274 | ) |
|
0 commit comments