summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-30 20:15:17 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-30 20:15:17 (GMT)
commit70b64fce96e894dfa8af5afd1f8b3fe863ba16e0 (patch)
tree15d549753edea1f11c0a3990ea0419426220c445 /Lib/test
parent4f066126d616d35aa8c0911a915ad64a09aaf16e (diff)
downloadcpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.zip
cpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.tar.gz
cpython-70b64fce96e894dfa8af5afd1f8b3fe863ba16e0.tar.bz2
Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/list_tests.py17
-rw-r--r--Lib/test/test_builtin.py3
-rw-r--r--Lib/test/test_sort.py50
3 files changed, 30 insertions, 40 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index f03cdfe..49283e4 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -8,6 +8,15 @@ import os
import unittest
from test import test_support, seq_tests
+def CmpToKey(mycmp):
+ 'Convert a cmp= function into a key= function'
+ class K(object):
+ def __init__(self, obj):
+ self.obj = obj
+ def __lt__(self, other):
+ return mycmp(self.obj, other.obj) == -1
+ return K
+
class CommonTest(seq_tests.CommonTest):
def test_init(self):
@@ -430,23 +439,21 @@ class CommonTest(seq_tests.CommonTest):
def revcmp(a, b):
return cmp(b, a)
- u.sort(revcmp)
+ u.sort(key=CmpToKey(revcmp))
self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
# The following dumps core in unpatched Python 1.5:
def myComparison(x,y):
return cmp(x%3, y%7)
z = self.type2test(range(12))
- z.sort(myComparison)
+ z.sort(key=CmpToKey(myComparison))
self.assertRaises(TypeError, z.sort, 2)
def selfmodifyingComparison(x,y):
z.append(1)
return cmp(x, y)
- self.assertRaises(ValueError, z.sort, selfmodifyingComparison)
-
- self.assertRaises(TypeError, z.sort, lambda x, y: 's')
+ self.assertRaises(ValueError, z.sort, key=CmpToKey(selfmodifyingComparison))
self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 92c1d00..8582862 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1764,9 +1764,6 @@ class TestSorted(unittest.TestCase):
data.reverse()
random.shuffle(copy)
- self.assertEqual(data, sorted(copy, cmp=lambda x, y: (x < y) - (x > y)))
- self.assertNotEqual(data, copy)
- random.shuffle(copy)
self.assertEqual(data, sorted(copy, key=lambda x: -x))
self.assertNotEqual(data, copy)
random.shuffle(copy)
diff --git a/Lib/test/test_sort.py b/Lib/test/test_sort.py
index 8ef98c7..8301c31 100644
--- a/Lib/test/test_sort.py
+++ b/Lib/test/test_sort.py
@@ -6,6 +6,15 @@ import unittest
verbose = test_support.verbose
nerrors = 0
+def CmpToKey(mycmp):
+ 'Convert a cmp= function into a key= function'
+ class K(object):
+ def __init__(self, obj):
+ self.obj = obj
+ def __lt__(self, other):
+ return mycmp(self.obj, other.obj) == -1
+ return K
+
def check(tag, expected, raw, compare=None):
global nerrors
@@ -14,7 +23,7 @@ def check(tag, expected, raw, compare=None):
orig = raw[:] # save input in case of error
if compare:
- raw.sort(compare)
+ raw.sort(key=CmpToKey(compare))
else:
raw.sort()
@@ -99,7 +108,7 @@ class TestBase(unittest.TestCase):
print(" Checking against an insane comparison function.")
print(" If the implementation isn't careful, this may segfault.")
s = x[:]
- s.sort(lambda a, b: int(random.random() * 3) - 1)
+ s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1))
check("an insane function left some permutation", x, s)
x = [Complains(i) for i in x]
@@ -141,14 +150,6 @@ class TestBugs(unittest.TestCase):
L = [C() for i in range(50)]
self.assertRaises(ValueError, L.sort)
- def test_cmpNone(self):
- # Testing None as a comparison function.
-
- L = list(range(50))
- random.shuffle(L)
- L.sort(None)
- self.assertEqual(L, list(range(50)))
-
def test_undetected_mutation(self):
# Python 2.4a1 did not always detect mutation
memorywaster = []
@@ -158,12 +159,12 @@ class TestBugs(unittest.TestCase):
L.pop()
return cmp(x, y)
L = [1,2]
- self.assertRaises(ValueError, L.sort, mutating_cmp)
+ self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp))
def mutating_cmp(x, y):
L.append(3)
del L[:]
return cmp(x, y)
- self.assertRaises(ValueError, L.sort, mutating_cmp)
+ self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp))
memorywaster = [memorywaster]
#==============================================================================
@@ -175,11 +176,11 @@ class TestDecorateSortUndecorate(unittest.TestCase):
copy = data[:]
random.shuffle(data)
data.sort(key=str.lower)
- copy.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
+ copy.sort(key=CmpToKey(lambda x,y: cmp(x.lower(), y.lower())))
def test_baddecorator(self):
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
- self.assertRaises(TypeError, data.sort, None, lambda x,y: 0)
+ self.assertRaises(TypeError, data.sort, key=lambda x,y: 0)
def test_stability(self):
data = [(random.randrange(100), i) for i in range(200)]
@@ -188,25 +189,11 @@ class TestDecorateSortUndecorate(unittest.TestCase):
copy.sort() # sort using both fields
self.assertEqual(data, copy) # should get the same result
- def test_cmp_and_key_combination(self):
- # Verify that the wrapper has been removed
- def compare(x, y):
- self.assertEqual(type(x), str)
- self.assertEqual(type(x), str)
- return cmp(x, y)
- data = 'The quick Brown fox Jumped over The lazy Dog'.split()
- data.sort(cmp=compare, key=str.lower)
-
- def test_badcmp_with_key(self):
- # Verify that the wrapper has been removed
- data = 'The quick Brown fox Jumped over The lazy Dog'.split()
- self.assertRaises(TypeError, data.sort, "bad", str.lower)
-
def test_key_with_exception(self):
# Verify that the wrapper has been removed
data = list(range(-2, 2))
dup = data[:]
- self.assertRaises(ZeroDivisionError, data.sort, None, lambda x: 1/x)
+ self.assertRaises(ZeroDivisionError, data.sort, key=lambda x: 1/x)
self.assertEqual(data, dup)
def test_key_with_mutation(self):
@@ -254,14 +241,13 @@ class TestDecorateSortUndecorate(unittest.TestCase):
random.shuffle(data)
data.sort(reverse=True)
self.assertEqual(data, list(range(99,-1,-1)))
- self.assertRaises(TypeError, data.sort, "wrong type")
def test_reverse_stability(self):
data = [(random.randrange(100), i) for i in range(200)]
copy1 = data[:]
copy2 = data[:]
- data.sort(cmp=lambda x,y: cmp(x[0],y[0]), reverse=True)
- copy1.sort(cmp=lambda x,y: cmp(y[0],x[0]))
+ data.sort(key=CmpToKey(lambda x,y: cmp(x[0],y[0])), reverse=True)
+ copy1.sort(key=CmpToKey(lambda x,y: cmp(y[0],x[0])))
self.assertEqual(data, copy1)
copy2.sort(key=lambda x: x[0], reverse=True)
self.assertEqual(data, copy2)