mirror of
https://github.com/python/cpython.git
synced 2025-10-13 10:23:28 +00:00
Guido grants a Christmas wish:
sorted() becomes a regular function instead of a classmethod.
This commit is contained in:
parent
df38ea9c29
commit
64958a15d7
15 changed files with 120 additions and 138 deletions
|
@ -248,66 +248,11 @@ class TestDecorateSortUndecorate(unittest.TestCase):
|
|||
copy2.sort(key=lambda x: x[0], reverse=True)
|
||||
self.assertEqual(data, copy2)
|
||||
|
||||
class TestSorted(unittest.TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
data = range(100)
|
||||
copy = data[:]
|
||||
random.shuffle(copy)
|
||||
self.assertEqual(data, list.sorted(copy))
|
||||
self.assertNotEqual(data, copy)
|
||||
|
||||
data.reverse()
|
||||
random.shuffle(copy)
|
||||
self.assertEqual(data, list.sorted(copy, cmp=lambda x, y: cmp(y,x)))
|
||||
self.assertNotEqual(data, copy)
|
||||
random.shuffle(copy)
|
||||
self.assertEqual(data, list.sorted(copy, key=lambda x: -x))
|
||||
self.assertNotEqual(data, copy)
|
||||
random.shuffle(copy)
|
||||
self.assertEqual(data, list.sorted(copy, reverse=1))
|
||||
self.assertNotEqual(data, copy)
|
||||
|
||||
def test_inputtypes(self):
|
||||
s = 'abracadabra'
|
||||
for T in [unicode, list, tuple]:
|
||||
self.assertEqual(list.sorted(s), list.sorted(T(s)))
|
||||
|
||||
s = ''.join(dict.fromkeys(s).keys()) # unique letters only
|
||||
for T in [unicode, set, frozenset, list, tuple, dict.fromkeys]:
|
||||
self.assertEqual(list.sorted(s), list.sorted(T(s)))
|
||||
|
||||
def test_baddecorator(self):
|
||||
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
||||
self.assertRaises(TypeError, list.sorted, data, None, lambda x,y: 0)
|
||||
|
||||
def classmethods(self):
|
||||
s = "hello world"
|
||||
a = list.sorted(s)
|
||||
b = UserList.sorted(s)
|
||||
c = [].sorted(s)
|
||||
d = UserList().sorted(s)
|
||||
class Mylist(list):
|
||||
def __new__(cls):
|
||||
return UserList()
|
||||
e = MyList.sorted(s)
|
||||
f = MyList().sorted(s)
|
||||
class Myuserlist(UserList):
|
||||
def __new__(cls):
|
||||
return []
|
||||
g = MyList.sorted(s)
|
||||
h = MyList().sorted(s)
|
||||
self.assert_(a == b == c == d == e == f == g == h)
|
||||
self.assert_(b.__class__ == d.__class__ == UserList)
|
||||
self.assert_(e.__class__ == f.__class__ == MyList)
|
||||
self.assert_(g.__class__ == h.__class__ == Myuserlist)
|
||||
|
||||
#==============================================================================
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_classes = (
|
||||
TestDecorateSortUndecorate,
|
||||
TestSorted,
|
||||
TestBugs,
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue