Back to index

python3.2  3.2.2
Public Member Functions
test.test_sort.TestDecorateSortUndecorate Class Reference

List of all members.

Public Member Functions

def test_decorated
def test_baddecorator
def test_stability
def test_key_with_exception
def test_key_with_mutation
def test_key_with_mutating_del
def test_key_with_mutating_del_and_exception
 self.assertEqual(data, dup)
def test_reverse
def test_reverse_stability

Detailed Description

Definition at line 171 of file test_sort.py.


Member Function Documentation

Definition at line 183 of file test_sort.py.

00183 
00184     def test_baddecorator(self):
00185         data = 'The quick Brown fox Jumped over The lazy Dog'.split()
00186         self.assertRaises(TypeError, data.sort, key=lambda x,y: 0)

Here is the call graph for this function:

Definition at line 173 of file test_sort.py.

00173 
00174     def test_decorated(self):
00175         data = 'The quick Brown fox Jumped over The lazy Dog'.split()
00176         copy = data[:]
00177         random.shuffle(data)
00178         data.sort(key=str.lower)
00179         def my_cmp(x, y):
00180             xlower, ylower = x.lower(), y.lower()
00181             return (xlower > ylower) - (xlower < ylower)
00182         copy.sort(key=cmp_to_key(my_cmp))

Here is the call graph for this function:

Definition at line 194 of file test_sort.py.

00194 
00195     def test_key_with_exception(self):
00196         # Verify that the wrapper has been removed
00197         data = list(range(-2, 2))
00198         dup = data[:]
00199         self.assertRaises(ZeroDivisionError, data.sort, key=lambda x: 1/x)
00200         self.assertEqual(data, dup)

Here is the call graph for this function:

Definition at line 209 of file test_sort.py.

00209 
00210     def test_key_with_mutating_del(self):
00211         data = list(range(10))
00212         class SortKiller(object):
00213             def __init__(self, x):
00214                 pass
00215             def __del__(self):
00216                 del data[:]
00217                 data[:] = range(20)
00218             def __lt__(self, other):
00219                 return id(self) < id(other)
00220         self.assertRaises(ValueError, data.sort, key=SortKiller)

Here is the call graph for this function:

self.assertEqual(data, dup)

because there is a reference to a SortKiller in the traceback and by the time it dies we're outside the call to .sort() and so the list protection gimmicks are out of date (this cost some brain cells to figure out...).

Definition at line 221 of file test_sort.py.

00221 
00222     def test_key_with_mutating_del_and_exception(self):
00223         data = list(range(10))
00224         ## dup = data[:]
00225         class SortKiller(object):
00226             def __init__(self, x):
00227                 if x > 2:
00228                     raise RuntimeError
00229             def __del__(self):
00230                 del data[:]
00231                 data[:] = list(range(20))
00232         self.assertRaises(RuntimeError, data.sort, key=SortKiller)
00233         ## major honking subtlety: we *can't* do:
00234         ##
00235         ## self.assertEqual(data, dup)
00236         ##
00237         ## because there is a reference to a SortKiller in the
00238         ## traceback and by the time it dies we're outside the call to
00239         ## .sort() and so the list protection gimmicks are out of
00240         ## date (this cost some brain cells to figure out...).

Here is the call graph for this function:

Definition at line 201 of file test_sort.py.

00201 
00202     def test_key_with_mutation(self):
00203         data = list(range(10))
00204         def k(x):
00205             del data[:]
00206             data[:] = range(20)
00207             return x
00208         self.assertRaises(ValueError, data.sort, key=k)

Here is the call graph for this function:

Definition at line 241 of file test_sort.py.

00241 
00242     def test_reverse(self):
00243         data = list(range(100))
00244         random.shuffle(data)
00245         data.sort(reverse=True)
00246         self.assertEqual(data, list(range(99,-1,-1)))

Here is the call graph for this function:

Definition at line 247 of file test_sort.py.

00247 
00248     def test_reverse_stability(self):
00249         data = [(random.randrange(100), i) for i in range(200)]
00250         copy1 = data[:]
00251         copy2 = data[:]
00252         def my_cmp(x, y):
00253             x0, y0 = x[0], y[0]
00254             return (x0 > y0) - (x0 < y0)
00255         def my_cmp_reversed(x, y):
00256             x0, y0 = x[0], y[0]
00257             return (y0 > x0) - (y0 < x0)
00258         data.sort(key=cmp_to_key(my_cmp), reverse=True)
00259         copy1.sort(key=cmp_to_key(my_cmp_reversed))
00260         self.assertEqual(data, copy1)
00261         copy2.sort(key=lambda x: x[0], reverse=True)
00262         self.assertEqual(data, copy2)
00263 
00264 #==============================================================================

Here is the call graph for this function:

Definition at line 187 of file test_sort.py.

00187 
00188     def test_stability(self):
00189         data = [(random.randrange(100), i) for i in range(200)]
00190         copy = data[:]
00191         data.sort(key=lambda t: t[0])   # sort on the random first field
00192         copy.sort()                     # sort using both fields
00193         self.assertEqual(data, copy)    # should get the same result

Here is the call graph for this function:


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