diff options
author | Raymond Hettinger <python@rcn.com> | 2004-03-01 23:16:22 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-03-01 23:16:22 (GMT) |
commit | 0a4977c2f3b8b3cd80f326f44e87076b2578b1b6 (patch) | |
tree | 25342a34c69da22a41fe9964852de92ab6c7ff8b /Lib/test/test_deque.py | |
parent | 786ea6bc23c26a0ec98d5cbc80633615f9fa8cb1 (diff) | |
download | cpython-0a4977c2f3b8b3cd80f326f44e87076b2578b1b6.zip cpython-0a4977c2f3b8b3cd80f326f44e87076b2578b1b6.tar.gz cpython-0a4977c2f3b8b3cd80f326f44e87076b2578b1b6.tar.bz2 |
Replace left(), right(), and __reversed__() with the more general purpose
__getitem__() and __setitem__().
Simplifies the API, reduces the code size, adds flexibility, and makes
deques work with bisect.bisect(), random.shuffle(), and random.sample().
Diffstat (limited to 'Lib/test/test_deque.py')
-rw-r--r-- | Lib/test/test_deque.py | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index db9733e..a99de03 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -4,6 +4,7 @@ from test import test_support import copy import cPickle as pickle from cStringIO import StringIO +import random BIG = 100000 @@ -57,13 +58,37 @@ class TestBasic(unittest.TestCase): d.extendleft('bcd') self.assertEqual(list(d), list(reversed('abcd'))) - def test_leftright(self): + def test_getitem(self): + n = 200 + d = deque(xrange(n)) + l = range(n) + for i in xrange(n): + d.popleft() + l.pop(0) + if random.random() < 0.5: + d.append(i) + l.append(i) + for j in xrange(1-len(l), len(l)): + assert d[j] == l[j] + d = deque('superman') - self.assertEqual(d.left(), 's') - self.assertEqual(d.right(), 'n') + self.assertEqual(d[0], 's') + self.assertEqual(d[-1], 'n') d = deque() - self.assertRaises(IndexError, d.left) - self.assertRaises(IndexError, d.right) + self.assertRaises(IndexError, d.__getitem__, 0) + self.assertRaises(IndexError, d.__getitem__, -1) + + def test_setitem(self): + n = 200 + d = deque(xrange(n)) + for i in xrange(n): + d[i] = 10 * i + self.assertEqual(list(d), [10*i for i in xrange(n)]) + l = list(d) + for i in xrange(1-n, 0, -1): + d[i] = 7*i + l[i] = 7*i + self.assertEqual(list(d), l) def test_rotate(self): s = tuple('abcde') @@ -405,7 +430,7 @@ 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() +... print elem.upper() G H I @@ -419,9 +444,9 @@ deque(['f', 'g', 'h', 'i', 'j']) 'f' >>> list(d) # list the contents of the deque ['g', 'h', 'i'] ->>> d.left() # peek at leftmost item +>>> d[0] # peek at leftmost item 'g' ->>> d.right() # peek at rightmost item +>>> d[-1] # peek at rightmost item 'i' >>> list(reversed(d)) # list the contents of a deque in reverse ['i', 'h', 'g'] @@ -476,7 +501,7 @@ def test_main(verbose=None): gc.collect() counts[i] = sys.gettotalrefcount() print counts - + # doctests from test import test_deque test_support.run_doctest(test_deque, verbose) |