[Python-checkins] python/dist/src/Lib/test test_deque.py,1.5,1.6
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Sat Feb 28 21:15:58 EST 2004
Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24444/Lib/test
Modified Files:
test_deque.py
Log Message:
Improvements to collections.deque():
* Add doctests for the examples in the library reference.
* Add two methods, left() and right(), modeled after deques in C++ STL.
* Apply the new method to asynchat.py.
* Add comparison operators to make deques more substitutable for lists.
* Replace the LookupErrors with IndexErrors to more closely match lists.
Index: test_deque.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_deque.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** test_deque.py 8 Feb 2004 04:05:26 -0000 1.5
--- test_deque.py 29 Feb 2004 02:15:56 -0000 1.6
***************
*** 29,32 ****
--- 29,49 ----
self.assertEqual(list(d), range(50, 150))
+ def test_comparisons(self):
+ d = deque('xabc'); d.popleft()
+ for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
+ self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e))
+ self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e)))
+
+ args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba'))
+ for x in args:
+ for y in args:
+ self.assertEqual(x == y, list(x) == list(y), (x,y))
+ self.assertEqual(x != y, list(x) != list(y), (x,y))
+ self.assertEqual(x < y, list(x) < list(y), (x,y))
+ self.assertEqual(x <= y, list(x) <= list(y), (x,y))
+ self.assertEqual(x > y, list(x) > list(y), (x,y))
+ self.assertEqual(x >= y, list(x) >= list(y), (x,y))
+ self.assertEqual(cmp(x,y), cmp(list(x),list(y)), (x,y))
+
def test_extend(self):
d = deque('a')
***************
*** 41,44 ****
--- 58,69 ----
self.assertEqual(list(d), list(reversed('abcd')))
+ def test_leftright(self):
+ d = deque('superman')
+ self.assertEqual(d.left(), 's')
+ self.assertEqual(d.right(), 'n')
+ d = deque()
+ self.assertRaises(IndexError, d.left)
+ self.assertRaises(IndexError, d.right)
+
def test_rotate(self):
s = tuple('abcde')
***************
*** 94,98 ****
d.pop()
self.assertEqual(len(d), 0)
! self.assertRaises(LookupError, d.pop)
self.assertEqual(len(d), 0)
d.append('c')
--- 119,123 ----
d.pop()
self.assertEqual(len(d), 0)
! self.assertRaises(IndexError, d.pop)
self.assertEqual(len(d), 0)
d.append('c')
***************
*** 105,110 ****
def test_underflow(self):
d = deque()
! self.assertRaises(LookupError, d.pop)
! self.assertRaises(LookupError, d.popleft)
def test_clear(self):
--- 130,135 ----
def test_underflow(self):
d = deque()
! self.assertRaises(IndexError, d.pop)
! self.assertRaises(IndexError, d.popleft)
def test_clear(self):
***************
*** 375,378 ****
--- 400,460 ----
#==============================================================================
+ libreftest = """
+ Example from the Library Reference: Doc/lib/libcollections.tex
+
+ >>> from collections import deque
+ >>> d = deque('ghi') # make a new deque with three items
+ >>> for elem in d: # iterate over the deque's elements
+ ... print elem.upper()
+ G
+ H
+ I
+ >>> d.append('j') # add a new entry to the right side
+ >>> d.appendleft('f') # add a new entry to the left side
+ >>> d # show the representation of the deque
+ deque(['f', 'g', 'h', 'i', 'j'])
+ >>> d.pop() # return and remove the rightmost item
+ 'j'
+ >>> d.popleft() # return and remove the leftmost item
+ 'f'
+ >>> list(d) # list the contents of the deque
+ ['g', 'h', 'i']
+ >>> d.left() # peek at leftmost item
+ 'g'
+ >>> d.right() # peek at rightmost item
+ 'i'
+ >>> list(reversed(d)) # list the contents of a deque in reverse
+ ['i', 'h', 'g']
+ >>> 'h' in d # search the deque
+ True
+ >>> d.extend('jkl') # add multiple elements at once
+ >>> d
+ deque(['g', 'h', 'i', 'j', 'k', 'l'])
+ >>> d.rotate(1) # right rotation
+ >>> d
+ deque(['l', 'g', 'h', 'i', 'j', 'k'])
+ >>> d.rotate(-1) # left rotation
+ >>> d
+ deque(['g', 'h', 'i', 'j', 'k', 'l'])
+ >>> deque(reversed(d)) # make a new deque in reverse order
+ deque(['l', 'k', 'j', 'i', 'h', 'g'])
+ >>> d.clear() # empty the deque
+ >>> d.pop() # cannot pop from an empty deque
+ Traceback (most recent call last):
+ File "<pyshell#6>", line 1, in -toplevel-
+ d.pop()
+ IndexError: pop from an empty deque
+
+ >>> d.extendleft('abc') # extendleft() reverses the input order
+ >>> d
+ deque(['c', 'b', 'a'])
+
+ """
+
+
+ #==============================================================================
+
+ __test__ = {'libreftest' : libreftest}
+
def test_main(verbose=None):
import sys
***************
*** 395,398 ****
--- 477,484 ----
counts[i] = sys.gettotalrefcount()
print counts
+
+ # doctests
+ from test import test_deque
+ test_support.run_doctest(test_deque, verbose)
if __name__ == "__main__":
More information about the Python-checkins
mailing list