annotate Lib/test/test_deque.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents a3d49f1de893
children 5accb0ac8bfb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 from collections import deque
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 import unittest
34802
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
3 from test import test_support, seq_tests
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
4 import gc
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
5 import weakref
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 import copy
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 import cPickle as pickle
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
8 import random
78393
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
9 import struct
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 BIG = 100000
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
13 def fail():
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
14 raise SyntaxError
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
15 yield 1
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
16
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
17 class BadCmp:
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
18 def __eq__(self, other):
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
19 raise RuntimeError
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
20
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
21 class MutateCmp:
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
22 def __init__(self, deque, result):
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
23 self.deque = deque
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
24 self.result = result
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
25 def __eq__(self, other):
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
26 self.deque.clear()
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
27 return self.result
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
28
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 class TestBasic(unittest.TestCase):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 def test_basics(self):
44281
c2e72feb53b3 Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
32 d = deque(xrange(-5125, -5000))
c2e72feb53b3 Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
33 d.__init__(xrange(200))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 for i in xrange(200, 400):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35 d.append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 for i in reversed(xrange(-200, 0)):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 d.appendleft(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 self.assertEqual(list(d), range(-200, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 self.assertEqual(len(d), 600)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 left = [d.popleft() for i in xrange(250)]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 self.assertEqual(left, range(-200, 50))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 self.assertEqual(list(d), range(50, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 right = [d.pop() for i in xrange(250)]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 right.reverse()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 self.assertEqual(right, range(150, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 self.assertEqual(list(d), range(50, 150))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
50 def test_maxlen(self):
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
51 self.assertRaises(ValueError, deque, 'abc', -1)
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
52 self.assertRaises(ValueError, deque, 'abc', -2)
52117
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
53 it = iter(range(10))
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
54 d = deque(it, maxlen=3)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
55 self.assertEqual(list(it), [])
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
56 self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)')
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
57 self.assertEqual(list(d), range(7, 10))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
58 self.assertEqual(d, deque(range(10), 3))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
59 d.append(10)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
60 self.assertEqual(list(d), range(8, 11))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
61 d.appendleft(7)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
62 self.assertEqual(list(d), range(7, 10))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
63 d.extend([10, 11])
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
64 self.assertEqual(list(d), range(9, 12))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
65 d.extendleft([8, 7])
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
66 self.assertEqual(list(d), range(7, 10))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
67 d = deque(xrange(200), maxlen=10)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
68 d.append(d)
46388
8d555850f73d Remove the test file before writing it in case there is no write permission.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46061
diff changeset
69 test_support.unlink(test_support.TESTFN)
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
70 fo = open(test_support.TESTFN, "wb")
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
71 try:
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
72 print >> fo, d,
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
73 fo.close()
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
74 fo = open(test_support.TESTFN, "rb")
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
75 self.assertEqual(fo.read(), repr(d))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
76 finally:
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
77 fo.close()
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
78 test_support.unlink(test_support.TESTFN)
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
79
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
80 d = deque(range(10), maxlen=None)
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
81 self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])')
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
82 fo = open(test_support.TESTFN, "wb")
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
83 try:
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
84 print >> fo, d,
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
85 fo.close()
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
86 fo = open(test_support.TESTFN, "rb")
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
87 self.assertEqual(fo.read(), repr(d))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
88 finally:
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
89 fo.close()
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
90 test_support.unlink(test_support.TESTFN)
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
91
52117
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
92 def test_maxlen_zero(self):
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
93 it = iter(range(100))
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
94 deque(it, maxlen=0)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
95 self.assertEqual(list(it), [])
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
96
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
97 it = iter(range(100))
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
98 d = deque(maxlen=0)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
99 d.extend(it)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
100 self.assertEqual(list(it), [])
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
101
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
102 it = iter(range(100))
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
103 d = deque(maxlen=0)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
104 d.extendleft(it)
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
105 self.assertEqual(list(it), [])
ab7a7215e463 Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
106
52119
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
107 def test_maxlen_attribute(self):
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
108 self.assertEqual(deque().maxlen, None)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
109 self.assertEqual(deque('abc').maxlen, None)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
110 self.assertEqual(deque('abc', maxlen=4).maxlen, 4)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
111 self.assertEqual(deque('abc', maxlen=2).maxlen, 2)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
112 self.assertEqual(deque('abc', maxlen=0).maxlen, 0)
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
113 with self.assertRaises(AttributeError):
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
114 d = deque('abc')
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
115 d.maxlen = 10
b0911b5a002e For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents: 52117
diff changeset
116
60277
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
117 def test_count(self):
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
118 for s in ('', 'abracadabra', 'simsalabim'*500+'abc'):
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
119 s = list(s)
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
120 d = deque(s)
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
121 for letter in 'abcdefghijklmnopqrstuvwxyz':
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
122 self.assertEqual(s.count(letter), d.count(letter), (s, d, letter))
60286
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
123 self.assertRaises(TypeError, d.count) # too few args
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
124 self.assertRaises(TypeError, d.count, 1, 2) # too many args
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
125 class BadCompare:
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
126 def __eq__(self, other):
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
127 raise ArithmeticError
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
128 d = deque([1, 2, BadCompare(), 3])
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
129 self.assertRaises(ArithmeticError, d.count, 2)
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
130 d = deque([1, 2, 3])
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
131 self.assertRaises(ArithmeticError, d.count, BadCompare())
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
132 class MutatingCompare:
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
133 def __eq__(self, other):
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
134 self.d.pop()
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
135 return True
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
136 m = MutatingCompare()
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
137 d = deque([1, 2, 3, m, 4, 5])
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
138 m.d = d
6a24defcc00b Expand test coverage for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 60277
diff changeset
139 self.assertRaises(RuntimeError, d.count, 3)
60277
c73a658da6fa Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 58817
diff changeset
140
67765
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
141 # test issue11004
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
142 # block advance failed after rotation aligned elements on right side of block
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
143 d = deque([None]*16)
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
144 for i in range(len(d)):
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
145 d.rotate(-1)
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
146 d.rotate(1)
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
147 self.assertEqual(d.count(1), 0)
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
148 self.assertEqual(d.count(None), 16)
f6392c4ce619 Issue 11004: Fix edge case for deque.count().
Raymond Hettinger <python@rcn.com>
parents: 66348
diff changeset
149
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
150 def test_comparisons(self):
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
151 d = deque('xabc'); d.popleft()
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
152 for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
153 self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
154 self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e)))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
155
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
156 args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba'))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
157 for x in args:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
158 for y in args:
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
159 self.assertEqual(x == y, list(x) == list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
160 self.assertEqual(x != y, list(x) != list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
161 self.assertEqual(x < y, list(x) < list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
162 self.assertEqual(x <= y, list(x) <= list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
163 self.assertEqual(x > y, list(x) > list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
164 self.assertEqual(x >= y, list(x) >= list(y), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
165 self.assertEqual(cmp(x,y), cmp(list(x),list(y)), (x,y))
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
166
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
167 def test_extend(self):
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
168 d = deque('a')
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
169 self.assertRaises(TypeError, d.extend, 1)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
170 d.extend('bcd')
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
171 self.assertEqual(list(d), list('abcd'))
57633
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
172 d.extend(d)
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
173 self.assertEqual(list(d), list('abcdabcd'))
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
174
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
175 def test_iadd(self):
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
176 d = deque('a')
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
177 d += 'bcd'
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
178 self.assertEqual(list(d), list('abcd'))
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
179 d += d
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
180 self.assertEqual(list(d), list('abcdabcd'))
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
181
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
182 def test_extendleft(self):
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
183 d = deque('a')
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
184 self.assertRaises(TypeError, d.extendleft, 1)
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
185 d.extendleft('bcd')
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
186 self.assertEqual(list(d), list(reversed('abcd')))
57633
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
187 d.extendleft(d)
b5a4acbeaaa1 Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
188 self.assertEqual(list(d), list('abcddcba'))
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
189 d = deque()
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
190 d.extendleft(range(1000))
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
191 self.assertEqual(list(d), list(reversed(range(1000))))
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
192 self.assertRaises(SyntaxError, d.extendleft, fail())
31464
aabd9b54e469 Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents: 31436
diff changeset
193
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
194 def test_getitem(self):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
195 n = 200
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
196 d = deque(xrange(n))
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
197 l = range(n)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
198 for i in xrange(n):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
199 d.popleft()
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
200 l.pop(0)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
201 if random.random() < 0.5:
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
202 d.append(i)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
203 l.append(i)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
204 for j in xrange(1-len(l), len(l)):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
205 assert d[j] == l[j]
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
206
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
207 d = deque('superman')
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
208 self.assertEqual(d[0], 's')
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
209 self.assertEqual(d[-1], 'n')
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
210 d = deque()
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
211 self.assertRaises(IndexError, d.__getitem__, 0)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
212 self.assertRaises(IndexError, d.__getitem__, -1)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
213
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
214 def test_setitem(self):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
215 n = 200
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
216 d = deque(xrange(n))
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
217 for i in xrange(n):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
218 d[i] = 10 * i
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
219 self.assertEqual(list(d), [10*i for i in xrange(n)])
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
220 l = list(d)
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
221 for i in xrange(1-n, 0, -1):
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
222 d[i] = 7*i
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
223 l[i] = 7*i
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
224 self.assertEqual(list(d), l)
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
225
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
226 def test_delitem(self):
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
227 n = 500 # O(n**2) test, don't make this too big
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
228 d = deque(xrange(n))
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
229 self.assertRaises(IndexError, d.__delitem__, -n-1)
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
230 self.assertRaises(IndexError, d.__delitem__, n)
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
231 for i in xrange(n):
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
232 self.assertEqual(len(d), n-i)
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
233 j = random.randrange(-len(d), len(d))
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
234 val = d[j]
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57635
diff changeset
235 self.assertIn(val, d)
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
236 del d[j]
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57635
diff changeset
237 self.assertNotIn(val, d)
32084
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
238 self.assertEqual(len(d), 0)
245835ed8de8 Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents: 32035
diff changeset
239
57635
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
240 def test_reverse(self):
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
241 n = 500 # O(n**2) test, don't make this too big
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
242 data = [random.random() for i in range(n)]
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
243 for i in range(n):
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
244 d = deque(data[:i])
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
245 r = d.reverse()
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
246 self.assertEqual(list(d), list(reversed(data[:i])))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60286
diff changeset
247 self.assertIs(r, None)
57635
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
248 d.reverse()
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
249 self.assertEqual(list(d), data[:i])
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
250 self.assertRaises(TypeError, d.reverse, 1) # Arity is zero
8276d00d3100 Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents: 57634
diff changeset
251
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
252 def test_rotate(self):
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
253 s = tuple('abcde')
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
254 n = len(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
255
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
256 d = deque(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
257 d.rotate(1) # verify rot(1)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
258 self.assertEqual(''.join(d), 'eabcd')
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
259
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
260 d = deque(s)
31476
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
261 d.rotate(-1) # verify rot(-1)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
262 self.assertEqual(''.join(d), 'bcdea')
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
263 d.rotate() # check default to 1
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
264 self.assertEqual(tuple(d), s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
265
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
266 for i in xrange(n*3):
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
267 d = deque(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
268 e = deque(d)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
269 d.rotate(i) # check vs. rot(1) n times
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
270 for j in xrange(i):
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
271 e.rotate(1)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
272 self.assertEqual(tuple(d), tuple(e))
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
273 d.rotate(-i) # check that it works in reverse
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
274 self.assertEqual(tuple(d), s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
275 e.rotate(n-i) # check that it wraps forward
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
276 self.assertEqual(tuple(e), s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
277
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
278 for i in xrange(n*3):
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
279 d = deque(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
280 e = deque(d)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
281 d.rotate(-i)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
282 for j in xrange(i):
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
283 e.rotate(-1) # check vs. rot(-1) n times
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
284 self.assertEqual(tuple(d), tuple(e))
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
285 d.rotate(i) # check that it works in reverse
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
286 self.assertEqual(tuple(d), s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
287 e.rotate(i-n) # check that it wraps backaround
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
288 self.assertEqual(tuple(e), s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
289
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
290 d = deque(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
291 e = deque(s)
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
292 e.rotate(BIG+17) # verify on long series of rotates
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
293 dr = d.rotate
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
294 for i in xrange(BIG+17):
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
295 dr()
25b30439fe22 Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents: 31474
diff changeset
296 self.assertEqual(tuple(d), tuple(e))
31474
3ce4ffac27ab * Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents: 31466
diff changeset
297
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
298 self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
299 self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
300
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
301 d = deque()
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
302 d.rotate() # rotate an empty deque
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
303 self.assertEqual(d, deque())
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
304
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 def test_len(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 d = deque('ab')
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 self.assertEqual(len(d), 2)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 d.popleft()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
309 self.assertEqual(len(d), 1)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310 d.pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 self.assertEqual(len(d), 0)
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
312 self.assertRaises(IndexError, d.pop)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 d.append('c')
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 self.assertEqual(len(d), 1)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 d.appendleft('d')
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 self.assertEqual(len(d), 2)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 d.clear()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 def test_underflow(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 d = deque()
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
323 self.assertRaises(IndexError, d.pop)
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
324 self.assertRaises(IndexError, d.popleft)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 def test_clear(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 d = deque(xrange(100))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 self.assertEqual(len(d), 100)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 d.clear()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 self.assertEqual(list(d), [])
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
332 d.clear() # clear an emtpy deque
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
333 self.assertEqual(list(d), [])
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
335 def test_remove(self):
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
336 d = deque('abcdefghcij')
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
337 d.remove('c')
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
338 self.assertEqual(d, deque('abdefghcij'))
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
339 d.remove('c')
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
340 self.assertEqual(d, deque('abdefghij'))
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
341 self.assertRaises(ValueError, d.remove, 'c')
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
342 self.assertEqual(d, deque('abdefghij'))
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
343
34798
0852d196b590 Fix typo.
Walter Dörwald <walter@livinglogic.de>
parents: 34772
diff changeset
344 # Handle comparison errors
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
345 d = deque(['a', 'b', BadCmp(), 'c'])
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
346 e = deque(d)
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
347 self.assertRaises(RuntimeError, d.remove, 'c')
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
348 for x, y in zip(d, e):
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
349 # verify that original order and values are retained.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52119
diff changeset
350 self.assertTrue(x is y)
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
351
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
352 # Handle evil mutator
34772
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
353 for match in (True, False):
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
354 d = deque(['ab'])
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
355 d.extend([MutateCmp(d, match), 'c'])
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
356 self.assertRaises(IndexError, d.remove, 'c')
2ee575b9b60e Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents: 34771
diff changeset
357 self.assertEqual(d, deque())
34771
fb39f4eeaea4 Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents: 33949
diff changeset
358
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359 def test_repr(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 d = deque(xrange(200))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 e = eval(repr(d))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363 d.append(d)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57635
diff changeset
364 self.assertIn('...', repr(d))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
366 def test_print(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367 d = deque(xrange(200))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 d.append(d)
46388
8d555850f73d Remove the test file before writing it in case there is no write permission.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46061
diff changeset
369 test_support.unlink(test_support.TESTFN)
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
370 fo = open(test_support.TESTFN, "wb")
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
371 try:
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
372 print >> fo, d,
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
373 fo.close()
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
374 fo = open(test_support.TESTFN, "rb")
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
375 self.assertEqual(fo.read(), repr(d))
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
376 finally:
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
377 fo.close()
46061
f3e648fee4c1 Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
378 test_support.unlink(test_support.TESTFN)
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
379
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
380 def test_init(self):
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
381 self.assertRaises(TypeError, deque, 'abc', 2, 3);
32554
74c3b713e35a * balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
382 self.assertRaises(TypeError, deque, 1);
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 def test_hash(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385 self.assertRaises(TypeError, hash, deque('abc'))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 def test_long_steadystate_queue_popleft(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 for size in (0, 1, 2, 100, 1000):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 d = deque(xrange(size))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 append, pop = d.append, d.popleft
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 for i in xrange(size, BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394 if x != i - size:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 self.assertEqual(x, i-size)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 self.assertEqual(list(d), range(BIG-size, BIG))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
397
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
398 def test_long_steadystate_queue_popright(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 for size in (0, 1, 2, 100, 1000):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 d = deque(reversed(xrange(size)))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 append, pop = d.appendleft, d.pop
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 for i in xrange(size, BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 if x != i - size:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
406 self.assertEqual(x, i-size)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 self.assertEqual(list(reversed(list(d))), range(BIG-size, BIG))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 def test_big_queue_popleft(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 pass
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411 d = deque()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 append, pop = d.append, d.popleft
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 if x != i:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 self.assertEqual(x, i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
420 def test_big_queue_popright(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
421 d = deque()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422 append, pop = d.appendleft, d.pop
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427 if x != i:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
428 self.assertEqual(x, i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
430 def test_big_stack_right(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
431 d = deque()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
432 append, pop = d.append, d.pop
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
435 for i in reversed(xrange(BIG)):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
436 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
437 if x != i:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
438 self.assertEqual(x, i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
439 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
440
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
441 def test_big_stack_left(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
442 d = deque()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
443 append, pop = d.appendleft, d.popleft
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
444 for i in xrange(BIG):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
445 append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
446 for i in reversed(xrange(BIG)):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
447 x = pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
448 if x != i:
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 self.assertEqual(x, i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 def test_roundtrip_iter_init(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 d = deque(xrange(200))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 e = deque(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 self.assertNotEqual(id(d), id(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 def test_pickle(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 d = deque(xrange(200))
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46388
diff changeset
460 for i in range(pickle.HIGHEST_PROTOCOL + 1):
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
461 s = pickle.dumps(d, i)
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
462 e = pickle.loads(s)
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
463 self.assertNotEqual(id(d), id(e))
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
464 self.assertEqual(list(d), list(e))
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
465
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
466 ## def test_pickle_recursive(self):
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
467 ## d = deque('abc')
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
468 ## d.append(d)
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46388
diff changeset
469 ## for i in range(pickle.HIGHEST_PROTOCOL + 1):
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
470 ## e = pickle.loads(pickle.dumps(d, i))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
471 ## self.assertNotEqual(id(d), id(e))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
472 ## self.assertEqual(id(e), id(e[-1]))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
473
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
474 def test_deepcopy(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475 mut = [10]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 d = deque([mut])
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 e = copy.deepcopy(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 mut[0] = 11
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 self.assertNotEqual(id(d), id(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 self.assertNotEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 def test_copy(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 mut = [10]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 d = deque([mut])
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 e = copy.copy(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 mut[0] = 11
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 self.assertNotEqual(id(d), id(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491
31466
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
492 def test_reversed(self):
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
493 for s in ('abcd', xrange(2000)):
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
494 self.assertEqual(list(reversed(deque(s))), list(reversed(s)))
f04ccc288542 * Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents: 31464
diff changeset
495
33639
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
496 def test_gc_doesnt_blowup(self):
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
497 import gc
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
498 # This used to assert-fail in deque_traverse() under a debug
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
499 # build, or run wild with a NULL pointer in a release build.
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
500 d = deque()
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
501 for i in xrange(100):
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
502 d.append(1)
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
503 gc.collect()
40350fbe665d deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents: 33597
diff changeset
504
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
505 def test_container_iterator(self):
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
506 # Bug #3680: tp_traverse was not implemented for deque iterator objects
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
507 class C(object):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
508 pass
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
509 for i in range(2):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
510 obj = C()
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
511 ref = weakref.ref(obj)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
512 if i == 0:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
513 container = deque([obj, 1])
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
514 else:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
515 container = reversed(deque([obj, 1]))
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
516 obj.x = iter(container)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
517 del obj, container
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
518 gc.collect()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52119
diff changeset
519 self.assertTrue(ref() is None, "Cycle was not collected")
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
520
78393
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
521 check_sizeof = test_support.check_sizeof
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
522
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
523 @test_support.cpython_only
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
524 def test_sizeof(self):
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
525 BLOCKLEN = 62
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
526 basesize = test_support.calcobjsize('2P4PlP')
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
527 blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
528 self.assertEqual(object.__sizeof__(deque()), basesize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
529 check = self.check_sizeof
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
530 check(deque(), basesize + blocksize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
531 check(deque('a'), basesize + blocksize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
532 check(deque('a' * (BLOCKLEN // 2)), basesize + blocksize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
533 check(deque('a' * (BLOCKLEN // 2 + 1)), basesize + 2 * blocksize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
534 check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize)
a3d49f1de893 Closes #15469: Correct __sizeof__ support for deque
Jesus Cea <jcea@jcea.es>
parents: 68256
diff changeset
535
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 class TestVariousIteratorArgs(unittest.TestCase):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 def test_constructor(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
539 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
34802
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
540 for g in (seq_tests.Sequence, seq_tests.IterFunc,
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
541 seq_tests.IterGen, seq_tests.IterFuncStop,
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
542 seq_tests.itermulti, seq_tests.iterfunc):
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543 self.assertEqual(list(deque(g(s))), list(g(s)))
34802
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
544 self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
545 self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s))
718b0b5a6419 Add tests for tuple, list and UserList that initialize the object from
Walter Dörwald <walter@livinglogic.de>
parents: 34798
diff changeset
546 self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
547
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 def test_iter_with_altered_data(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549 d = deque('abcdefg')
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 it = iter(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 d.pop()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 self.assertRaises(RuntimeError, it.next)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
553
40573
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
554 def test_runtime_error_on_empty_deque(self):
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
555 d = deque()
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
556 it = iter(d)
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
557 d.append(10)
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
558 self.assertRaises(RuntimeError, it.next)
bd7e41de982f Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents: 34802
diff changeset
559
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
560 class Deque(deque):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
561 pass
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
563 class DequeWithBadIter(deque):
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
564 def __iter__(self):
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
565 raise TypeError
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
566
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
567 class TestSubclass(unittest.TestCase):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
569 def test_basics(self):
44281
c2e72feb53b3 Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
570 d = Deque(xrange(25))
c2e72feb53b3 Fix bug 1604. deque.__init__() did not clear existing contents like list.__init__. Not a backport candidate.
Raymond Hettinger <python@rcn.com>
parents: 43517
diff changeset
571 d.__init__(xrange(200))
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572 for i in xrange(200, 400):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 d.append(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 for i in reversed(xrange(-200, 0)):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 d.appendleft(i)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 self.assertEqual(list(d), range(-200, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577 self.assertEqual(len(d), 600)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 left = [d.popleft() for i in xrange(250)]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 self.assertEqual(left, range(-200, 50))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 self.assertEqual(list(d), range(50, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
582
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 right = [d.pop() for i in xrange(250)]
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 right.reverse()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 self.assertEqual(right, range(150, 400))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 self.assertEqual(list(d), range(50, 150))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588 d.clear()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 self.assertEqual(len(d), 0)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
590
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
591 def test_copy_pickle(self):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
592
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
593 d = Deque('abc')
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
594
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
595 e = d.__copy__()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
596 self.assertEqual(type(d), type(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
597 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
598
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
599 e = Deque(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
600 self.assertEqual(type(d), type(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
601 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
602
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
603 s = pickle.dumps(d)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
604 e = pickle.loads(s)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
605 self.assertNotEqual(id(d), id(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
606 self.assertEqual(type(d), type(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
607 self.assertEqual(list(d), list(e))
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
608
43517
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
609 d = Deque('abcde', maxlen=4)
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
610
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
611 e = d.__copy__()
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
612 self.assertEqual(type(d), type(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
613 self.assertEqual(list(d), list(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
614
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
615 e = Deque(d)
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
616 self.assertEqual(type(d), type(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
617 self.assertEqual(list(d), list(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
618
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
619 s = pickle.dumps(d)
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
620 e = pickle.loads(s)
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
621 self.assertNotEqual(id(d), id(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
622 self.assertEqual(type(d), type(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
623 self.assertEqual(list(d), list(e))
da52b4c776a4 Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
Raymond Hettinger <python@rcn.com>
parents: 43466
diff changeset
624
43466
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
625 ## def test_pickle(self):
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
626 ## d = Deque('abc')
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
627 ## d.append(d)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
628 ##
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
629 ## e = pickle.loads(pickle.dumps(d))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
630 ## self.assertNotEqual(id(d), id(e))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
631 ## self.assertEqual(type(d), type(e))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
632 ## dd = d.pop()
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
633 ## ee = e.pop()
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
634 ## self.assertEqual(id(e), id(ee))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
635 ## self.assertEqual(d, e)
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
636 ##
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
637 ## d.x = d
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
638 ## e = pickle.loads(pickle.dumps(d))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
639 ## self.assertEqual(id(e), id(e.x))
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
640 ##
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
641 ## d = DequeWithBadIter('abc')
73f49656420b Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents: 40682
diff changeset
642 ## self.assertRaises(TypeError, pickle.dumps, d)
33949
b9819b9a0dad SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33648
diff changeset
643
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32121
diff changeset
644 def test_weakref(self):
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32121
diff changeset
645 d = deque('gallahad')
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
646 p = weakref.proxy(d)
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32121
diff changeset
647 self.assertEqual(str(p), str(d))
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32121
diff changeset
648 d = None
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 32121
diff changeset
649 self.assertRaises(ReferenceError, str, p)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650
33648
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
651 def test_strange_subclass(self):
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
652 class X(deque):
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
653 def __iter__(self):
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
654 return iter([])
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
655 d1 = X([1,2,3])
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
656 d2 = X([4,5,6])
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
657 d1 == d2 # not clear if this is supposed to be True or False,
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
658 # but it used to give a SystemError
7c50cde1e4fd Upon insertion, if memory runs out, the deque was left in a corrupted state.
Armin Rigo <arigo@tunes.org>
parents: 33639
diff changeset
659
40682
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
660
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
661 class SubclassWithKwargs(deque):
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
662 def __init__(self, newarg=1):
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
663 deque.__init__(self)
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
664
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
665 class TestSubclassWithKwargs(unittest.TestCase):
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
666 def test_subclass_with_kwargs(self):
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
667 # SF bug #1486663 -- this used to erroneously raise a TypeError
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
668 SubclassWithKwargs(newarg=1)
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
669
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670 #==============================================================================
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
672 libreftest = """
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
673 Example from the Library Reference: Doc/lib/libcollections.tex
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
674
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
675 >>> from collections import deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
676 >>> d = deque('ghi') # make a new deque with three items
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
677 >>> for elem in d: # iterate over the deque's elements
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
678 ... print elem.upper()
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
679 G
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
680 H
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
681 I
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
682 >>> d.append('j') # add a new entry to the right side
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
683 >>> d.appendleft('f') # add a new entry to the left side
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
684 >>> d # show the representation of the deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
685 deque(['f', 'g', 'h', 'i', 'j'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
686 >>> d.pop() # return and remove the rightmost item
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
687 'j'
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
688 >>> d.popleft() # return and remove the leftmost item
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
689 'f'
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
690 >>> list(d) # list the contents of the deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
691 ['g', 'h', 'i']
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
692 >>> d[0] # peek at leftmost item
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
693 'g'
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
694 >>> d[-1] # peek at rightmost item
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
695 'i'
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
696 >>> list(reversed(d)) # list the contents of a deque in reverse
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
697 ['i', 'h', 'g']
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
698 >>> 'h' in d # search the deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
699 True
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
700 >>> d.extend('jkl') # add multiple elements at once
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
701 >>> d
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
702 deque(['g', 'h', 'i', 'j', 'k', 'l'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
703 >>> d.rotate(1) # right rotation
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
704 >>> d
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
705 deque(['l', 'g', 'h', 'i', 'j', 'k'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
706 >>> d.rotate(-1) # left rotation
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
707 >>> d
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
708 deque(['g', 'h', 'i', 'j', 'k', 'l'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
709 >>> deque(reversed(d)) # make a new deque in reverse order
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
710 deque(['l', 'k', 'j', 'i', 'h', 'g'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
711 >>> d.clear() # empty the deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
712 >>> d.pop() # cannot pop from an empty deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
713 Traceback (most recent call last):
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
714 File "<pyshell#6>", line 1, in -toplevel-
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
715 d.pop()
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
716 IndexError: pop from an empty deque
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
717
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
718 >>> d.extendleft('abc') # extendleft() reverses the input order
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
719 >>> d
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
720 deque(['c', 'b', 'a'])
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
721
32014
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
722
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
723
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
724 >>> def delete_nth(d, n):
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
725 ... d.rotate(-n)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
726 ... d.popleft()
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
727 ... d.rotate(n)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
728 ...
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
729 >>> d = deque('abcdef')
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
730 >>> delete_nth(d, 2) # remove the entry at d[2]
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
731 >>> d
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
732 deque(['a', 'b', 'd', 'e', 'f'])
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
733
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
734
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
735
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
736 >>> def roundrobin(*iterables):
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
737 ... pending = deque(iter(i) for i in iterables)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
738 ... while pending:
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
739 ... task = pending.popleft()
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
740 ... try:
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
741 ... yield task.next()
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
742 ... except StopIteration:
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
743 ... continue
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
744 ... pending.append(task)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
745 ...
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
746
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
747 >>> for value in roundrobin('abc', 'd', 'efgh'):
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
748 ... print value
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
749 ...
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
750 a
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
751 d
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
752 e
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
753 b
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
754 f
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
755 c
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
756 g
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
757 h
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
758
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
759
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
760 >>> def maketree(iterable):
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
761 ... d = deque(iterable)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
762 ... while len(d) > 1:
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
763 ... pair = [d.popleft(), d.popleft()]
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
764 ... d.append(pair)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
765 ... return list(d)
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
766 ...
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
767 >>> print maketree('abcdefgh')
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
768 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
582dca02e60f Add more examples.
Raymond Hettinger <python@rcn.com>
parents: 31607
diff changeset
769
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
770 """
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
771
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
772
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
773 #==============================================================================
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
774
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
775 __test__ = {'libreftest' : libreftest}
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
776
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777 def test_main(verbose=None):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 import sys
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 test_classes = (
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 TestBasic,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 TestVariousIteratorArgs,
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 TestSubclass,
40682
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 40573
diff changeset
783 TestSubclassWithKwargs,
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784 )
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786 test_support.run_unittest(*test_classes)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 # verify reference counting
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 if verbose and hasattr(sys, "gettotalrefcount"):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 import gc
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
791 counts = [None] * 5
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 for i in xrange(len(counts)):
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 test_support.run_unittest(*test_classes)
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 gc.collect()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795 counts[i] = sys.gettotalrefcount()
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 print counts
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
797
31603
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
798 # doctests
15553a1b7a17 Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents: 31476
diff changeset
799 from test import test_deque
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 32084
diff changeset
800 test_support.run_doctest(test_deque, verbose)
31436
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
801
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 if __name__ == "__main__":
63fa60f2d61c * Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803 test_main(verbose=True)