[Python-checkins] python/dist/src/Lib/test test_sort.py,1.5,1.6

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Oct 29 01:54:44 EST 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv6236/Lib/test
Modified Files:
	test_sort.py 
Log Message:
Add list.sorted() classmethod.
Index: test_sort.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_sort.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** test_sort.py	16 Oct 2003 03:41:09 -0000	1.5
--- test_sort.py	29 Oct 2003 06:54:42 -0000	1.6
***************
*** 1,4 ****
--- 1,6 ----
 from test.test_support import verbose
 import random
+ from UserList import UserList
+ from sets import Set
 
 nerrors = 0
***************
*** 191,194 ****
--- 193,197 ----
 data.sort(reverse=True)
 self.assertEqual(data, range(99,-1,-1))
+ self.assertRaises(TypeError, data.sort, "wrong type")
 
 def test_reverse_stability(self):
***************
*** 202,205 ****
--- 205,262 ----
 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, 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)
+ 
 #==============================================================================
 
***************
*** 207,210 ****
--- 264,268 ----
 test_classes = (
 TestDecorateSortUndecorate,
+ TestSorted,
 TestBugs,
 )


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /