Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a98e769

Browse files
committed
move test into a unittest.TestCase, no functional changes
1 parent f06e30a commit a98e769

1 file changed

Lines changed: 74 additions & 74 deletions

File tree

Lib/test/test_sort.py

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from test.test_support import verbose
1+
from test import test_support
22
import random
3-
from UserList import UserList
3+
import sys
4+
import unittest
45

6+
verbose = test_support.verbose
57
nerrors = 0
68

79
def check(tag, expected, raw, compare=None):
@@ -36,91 +38,88 @@ def check(tag, expected, raw, compare=None):
3638
nerrors += 1
3739
return
3840

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])
4849

49-
def __init__(self, i):
50-
self.i = i
50+
class Complains(object):
51+
maybe_complain = True
5152

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
5855

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
6162

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
6665

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
6970

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)
7273

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)
7776

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
8481

85-
s = x[:]
86-
random.shuffle(s)
87-
check("random permutation", x, s)
82+
s = x[:]
83+
check("identity", x, s)
8884

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)
9388

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)
11992

93+
y = x[:]
94+
y.reverse()
95+
s = x[:]
96+
check("reversed via function", y, s, lambda a, b: cmp(b, a))
12097

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)
124123

125124
#==============================================================================
126125

@@ -269,6 +268,7 @@ def test_reverse_stability(self):
269268

270269
def test_main(verbose=None):
271270
test_classes = (
271+
TestBase,
272272
TestDecorateSortUndecorate,
273273
TestBugs,
274274
)

0 commit comments

Comments
 (0)