Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_sort.TestBase Class Reference

List of all members.

Public Member Functions

def testStressfully

Public Attributes

 i
 key
 index

Detailed Description

Definition at line 43 of file test_sort.py.


Member Function Documentation

Definition at line 44 of file test_sort.py.

00044 
00045     def testStressfully(self):
00046         # Try a variety of sizes at and around powers of 2, and at powers of 10.
00047         sizes = [0]
00048         for power in range(1, 10):
00049             n = 2 ** power
00050             sizes.extend(range(n-1, n+2))
00051         sizes.extend([10, 100, 1000])
00052 
00053         class Complains(object):
00054             maybe_complain = True
00055 
00056             def __init__(self, i):
00057                 self.i = i
00058 
00059             def __lt__(self, other):
00060                 if Complains.maybe_complain and random.random() < 0.001:
00061                     if verbose:
00062                         print("        complaining at", self, other)
00063                     raise RuntimeError
00064                 return self.i < other.i
00065 
00066             def __repr__(self):
00067                 return "Complains(%d)" % self.i
00068 
00069         class Stable(object):
00070             def __init__(self, key, i):
00071                 self.key = key
00072                 self.index = i
00073 
00074             def __lt__(self, other):
00075                 return self.key < other.key
00076 
00077             def __repr__(self):
00078                 return "Stable(%d, %d)" % (self.key, self.index)
00079 
00080         for n in sizes:
00081             x = list(range(n))
00082             if verbose:
00083                 print("Testing size", n)
00084 
00085             s = x[:]
00086             check("identity", x, s)
00087 
00088             s = x[:]
00089             s.reverse()
00090             check("reversed", x, s)
00091 
00092             s = x[:]
00093             random.shuffle(s)
00094             check("random permutation", x, s)
00095 
00096             y = x[:]
00097             y.reverse()
00098             s = x[:]
00099             check("reversed via function", y, s, lambda a, b: (b>a)-(b<a))
00100 
00101             if verbose:
00102                 print("    Checking against an insane comparison function.")
00103                 print("        If the implementation isn't careful, this may segfault.")
00104             s = x[:]
00105             s.sort(key=cmp_to_key(lambda a, b:  int(random.random() * 3) - 1))
00106             check("an insane function left some permutation", x, s)
00107 
00108             if len(x) >= 2:
00109                 def bad_key(x):
00110                     raise RuntimeError
00111                 s = x[:]
00112                 self.assertRaises(RuntimeError, s.sort, key=bad_key)
00113 
00114             x = [Complains(i) for i in x]
00115             s = x[:]
00116             random.shuffle(s)
00117             Complains.maybe_complain = True
00118             it_complained = False
00119             try:
00120                 s.sort()
00121             except RuntimeError:
00122                 it_complained = True
00123             if it_complained:
00124                 Complains.maybe_complain = False
00125                 check("exception during sort left some permutation", x, s)
00126 
00127             s = [Stable(random.randrange(10), i) for i in range(n)]
00128             augmented = [(e, e.index) for e in s]
00129             augmented.sort()    # forced stable because ties broken by index
00130             x = [e for e, i in augmented] # a stable sort of s
00131             check("stability", x, s)
00132 
00133 #==============================================================================

Here is the call graph for this function:


Member Data Documentation

Definition at line 56 of file test_sort.py.

Definition at line 71 of file test_sort.py.

Definition at line 70 of file test_sort.py.


The documentation for this class was generated from the following file: