Guido grants a Christmas wish:

sorted() becomes a regular function instead of a classmethod.
This commit is contained in:
Raymond Hettinger 2003-12-17 20:43:33 +00:00
parent df38ea9c29
commit 64958a15d7
15 changed files with 120 additions and 138 deletions

View file

@ -3,7 +3,7 @@
import test.test_support, unittest
from test.test_support import fcmp, have_unicode, TESTFN, unlink
import sys, warnings, cStringIO
import sys, warnings, cStringIO, random
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
FutureWarning, __name__)
warnings.filterwarnings("ignore", "integer argument expected",
@ -1153,8 +1153,41 @@ class BuiltinTest(unittest.TestCase):
return i
self.assertRaises(ValueError, zip, BadSeq(), BadSeq())
class TestSorted(unittest.TestCase):
def test_basic(self):
data = range(100)
copy = data[:]
random.shuffle(copy)
self.assertEqual(data, sorted(copy))
self.assertNotEqual(data, copy)
data.reverse()
random.shuffle(copy)
self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x)))
self.assertNotEqual(data, copy)
random.shuffle(copy)
self.assertEqual(data, sorted(copy, key=lambda x: -x))
self.assertNotEqual(data, copy)
random.shuffle(copy)
self.assertEqual(data, sorted(copy, reverse=1))
self.assertNotEqual(data, copy)
def test_inputtypes(self):
s = 'abracadabra'
for T in [unicode, list, tuple]:
self.assertEqual(sorted(s), sorted(T(s)))
s = ''.join(dict.fromkeys(s).keys()) # unique letters only
for T in [unicode, set, frozenset, list, tuple, dict.fromkeys]:
self.assertEqual(sorted(s), sorted(T(s)))
def test_baddecorator(self):
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
def test_main():
test.test_support.run_unittest(BuiltinTest)
test.test_support.run_unittest(BuiltinTest, TestSorted)
if __name__ == "__main__":
test_main()