Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_deque.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | b0911b5a002e |
| children | 6768b2350c57 |
| 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 |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
9 import os |
|
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 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
117 def test_comparisons(self): |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
118 d = deque('xabc'); d.popleft() |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
123 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
|
124 for x in args: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
125 for y in args: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
126 self.assertEqual(x == y, list(x) == list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
127 self.assertEqual(x != y, list(x) != list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
128 self.assertEqual(x < y, list(x) < list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
129 self.assertEqual(x <= y, list(x) <= list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
130 self.assertEqual(x > y, list(x) > list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
131 self.assertEqual(x >= y, list(x) >= list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
132 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
|
133 |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
134 def test_extend(self): |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
135 d = deque('a') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
136 self.assertRaises(TypeError, d.extend, 1) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
137 d.extend('bcd') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
138 self.assertEqual(list(d), list('abcd')) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
139 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
140 def test_extendleft(self): |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
141 d = deque('a') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
142 self.assertRaises(TypeError, d.extendleft, 1) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
143 d.extendleft('bcd') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
144 self.assertEqual(list(d), list(reversed('abcd'))) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
145 d = deque() |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
146 d.extendleft(range(1000)) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
147 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
|
148 self.assertRaises(SyntaxError, d.extendleft, fail()) |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
149 |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
150 def test_getitem(self): |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
151 n = 200 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
152 d = deque(xrange(n)) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
153 l = range(n) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
154 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
|
155 d.popleft() |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
156 l.pop(0) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
157 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
|
158 d.append(i) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
159 l.append(i) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
160 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
|
161 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
|
162 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
163 d = deque('superman') |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
164 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
|
165 self.assertEqual(d[-1], 'n') |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
166 d = deque() |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
167 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
|
168 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
|
169 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
170 def test_setitem(self): |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
171 n = 200 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
172 d = deque(xrange(n)) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
173 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
|
174 d[i] = 10 * i |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
175 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
|
176 l = list(d) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
177 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
|
178 d[i] = 7*i |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
179 l[i] = 7*i |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
180 self.assertEqual(list(d), l) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
181 |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 val = d[j] |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
191 self.assert_(val in d) |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
192 del d[j] |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
193 self.assert_(val not in d) |
|
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
194 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
|
195 |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
196 def test_rotate(self): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
197 s = tuple('abcde') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
198 n = len(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
199 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
200 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
201 d.rotate(1) # verify rot(1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
202 self.assertEqual(''.join(d), 'eabcd') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
203 |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
204 d = deque(s) |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
205 d.rotate(-1) # verify rot(-1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
206 self.assertEqual(''.join(d), 'bcdea') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
207 d.rotate() # check default to 1 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
208 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
209 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
210 for i in xrange(n*3): |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
211 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
212 e = deque(d) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
213 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
|
214 for j in xrange(i): |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
215 e.rotate(1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
216 self.assertEqual(tuple(d), tuple(e)) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
217 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
|
218 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
219 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
|
220 self.assertEqual(tuple(e), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
221 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
222 for i in xrange(n*3): |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
223 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
224 e = deque(d) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
225 d.rotate(-i) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
226 for j in xrange(i): |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
227 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
|
228 self.assertEqual(tuple(d), tuple(e)) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
229 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
|
230 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
231 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
|
232 self.assertEqual(tuple(e), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
233 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
234 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
235 e = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
236 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
|
237 dr = d.rotate |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
238 for i in xrange(BIG+17): |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
239 dr() |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
240 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
|
241 |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
242 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
|
243 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
|
244 |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
245 d = deque() |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
246 d.rotate() # rotate an empty deque |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
247 self.assertEqual(d, deque()) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
248 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
249 def test_len(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
250 d = deque('ab') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
251 self.assertEqual(len(d), 2) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
252 d.popleft() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
253 self.assertEqual(len(d), 1) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
254 d.pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
255 self.assertEqual(len(d), 0) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
256 self.assertRaises(IndexError, d.pop) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
257 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
258 d.append('c') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
259 self.assertEqual(len(d), 1) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
260 d.appendleft('d') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
261 self.assertEqual(len(d), 2) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
262 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
263 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
264 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
265 def test_underflow(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
266 d = deque() |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
267 self.assertRaises(IndexError, d.pop) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
268 self.assertRaises(IndexError, d.popleft) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
269 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
270 def test_clear(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
271 d = deque(xrange(100)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
272 self.assertEqual(len(d), 100) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
273 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
274 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
275 self.assertEqual(list(d), []) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
276 d.clear() # clear an emtpy deque |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
277 self.assertEqual(list(d), []) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
278 |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
279 def test_remove(self): |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
280 d = deque('abcdefghcij') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
281 d.remove('c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
282 self.assertEqual(d, deque('abdefghcij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
283 d.remove('c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
284 self.assertEqual(d, deque('abdefghij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
285 self.assertRaises(ValueError, d.remove, 'c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
286 self.assertEqual(d, deque('abdefghij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
287 |
| 34798 | 288 # Handle comparison errors |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
289 d = deque(['a', 'b', BadCmp(), 'c']) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
290 e = deque(d) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
291 self.assertRaises(RuntimeError, d.remove, 'c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
292 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
|
293 # verify that original order and values are retained. |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
294 self.assert_(x is y) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
295 |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
296 # Handle evil mutator |
|
34772
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
297 for match in (True, False): |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
298 d = deque(['ab']) |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
299 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
|
300 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
|
301 self.assertEqual(d, deque()) |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
302 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
303 def test_repr(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
304 d = deque(xrange(200)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
305 e = eval(repr(d)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
306 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
307 d.append(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
308 self.assert_('...' in repr(d)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
309 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
310 def test_print(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
311 d = deque(xrange(200)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
312 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
|
313 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
|
314 fo = open(test_support.TESTFN, "wb") |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
315 try: |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
316 print >> fo, d, |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
317 fo.close() |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
318 fo = open(test_support.TESTFN, "rb") |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
319 self.assertEqual(fo.read(), repr(d)) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
320 finally: |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
321 fo.close() |
|
46061
f3e648fee4c1
Fix a bunch of UnboundLocalErrors when the tests fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
45468
diff
changeset
|
322 test_support.unlink(test_support.TESTFN) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
323 |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
324 def test_init(self): |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
325 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
|
326 self.assertRaises(TypeError, deque, 1); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
327 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
328 def test_hash(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
329 self.assertRaises(TypeError, hash, deque('abc')) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
330 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
331 def test_long_steadystate_queue_popleft(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
332 for size in (0, 1, 2, 100, 1000): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
333 d = deque(xrange(size)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
334 append, pop = d.append, d.popleft |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
335 for i in xrange(size, BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
336 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
337 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
338 if x != i - size: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
339 self.assertEqual(x, i-size) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
340 self.assertEqual(list(d), range(BIG-size, BIG)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
341 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
342 def test_long_steadystate_queue_popright(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
343 for size in (0, 1, 2, 100, 1000): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
344 d = deque(reversed(xrange(size))) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
345 append, pop = d.appendleft, d.pop |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
346 for i in xrange(size, BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
347 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
348 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
349 if x != i - size: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
350 self.assertEqual(x, i-size) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
351 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
|
352 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
353 def test_big_queue_popleft(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
354 pass |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
355 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
356 append, pop = d.append, d.popleft |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
357 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
358 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
359 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
360 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
361 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
362 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
363 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
364 def test_big_queue_popright(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
365 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
366 append, pop = d.appendleft, d.pop |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
367 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
368 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
369 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
370 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
371 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
372 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
373 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
374 def test_big_stack_right(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
375 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
376 append, pop = d.append, d.pop |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
377 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
378 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
379 for i in reversed(xrange(BIG)): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
380 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
381 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
382 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
383 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
384 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
385 def test_big_stack_left(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
386 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
387 append, pop = d.appendleft, d.popleft |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
388 for i in xrange(BIG): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
389 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
390 for i in reversed(xrange(BIG)): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
391 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
392 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
393 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
394 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
395 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
396 def test_roundtrip_iter_init(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
397 d = deque(xrange(200)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
398 e = deque(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
399 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
400 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
401 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
402 def test_pickle(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
403 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
|
404 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
|
405 s = pickle.dumps(d, i) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
406 e = pickle.loads(s) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
407 self.assertNotEqual(id(d), id(e)) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
408 self.assertEqual(list(d), list(e)) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
409 |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
410 ## def test_pickle_recursive(self): |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
411 ## d = deque('abc') |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
412 ## 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
|
413 ## 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
|
414 ## 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
|
415 ## self.assertNotEqual(id(d), id(e)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
416 ## self.assertEqual(id(e), id(e[-1])) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
417 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
418 def test_deepcopy(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
419 mut = [10] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
420 d = deque([mut]) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
421 e = copy.deepcopy(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
422 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
423 mut[0] = 11 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
424 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
425 self.assertNotEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
426 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
427 def test_copy(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
428 mut = [10] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
429 d = deque([mut]) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
430 e = copy.copy(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
431 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
432 mut[0] = 11 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
433 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
434 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
435 |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
436 def test_reversed(self): |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
437 for s in ('abcd', xrange(2000)): |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
438 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
|
439 |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
440 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
|
441 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
|
442 # 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
|
443 # 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 |
|
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
|
449 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
|
450 # 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 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
|
461 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
|
462 gc.collect() |
|
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
|
463 self.assert_(ref() is None, "Cycle was not collected") |
|
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
|
464 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
465 class TestVariousIteratorArgs(unittest.TestCase): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
466 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
467 def test_constructor(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
468 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
|
469 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
|
470 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
|
471 seq_tests.itermulti, seq_tests.iterfunc): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
472 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
|
473 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
|
474 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
|
475 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
|
476 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
477 def test_iter_with_altered_data(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
478 d = deque('abcdefg') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
479 it = iter(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
480 d.pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
481 self.assertRaises(RuntimeError, it.next) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
482 |
|
40573
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
34802
diff
changeset
|
483 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
|
484 d = deque() |
|
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
34802
diff
changeset
|
485 it = iter(d) |
|
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
34802
diff
changeset
|
486 d.append(10) |
|
bd7e41de982f
Fix zero-length corner case for iterating over a mutating deque.
Raymond Hettinger <python@rcn.com>
parents:
34802
diff
changeset
|
487 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
|
488 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
489 class Deque(deque): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
490 pass |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
491 |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
492 class DequeWithBadIter(deque): |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
493 def __iter__(self): |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
494 raise TypeError |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
495 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
496 class TestSubclass(unittest.TestCase): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
497 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
498 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
|
499 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
|
500 d.__init__(xrange(200)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
501 for i in xrange(200, 400): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
502 d.append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
503 for i in reversed(xrange(-200, 0)): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
504 d.appendleft(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
505 self.assertEqual(list(d), range(-200, 400)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
506 self.assertEqual(len(d), 600) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
507 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
508 left = [d.popleft() for i in xrange(250)] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
509 self.assertEqual(left, range(-200, 50)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
510 self.assertEqual(list(d), range(50, 400)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
511 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
512 right = [d.pop() for i in xrange(250)] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
513 right.reverse() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
514 self.assertEqual(right, range(150, 400)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
515 self.assertEqual(list(d), range(50, 150)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
516 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
517 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
518 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
519 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
520 def test_copy_pickle(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
521 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
522 d = Deque('abc') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
523 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
524 e = d.__copy__() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
525 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
526 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
527 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
528 e = Deque(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
529 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
530 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
531 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
532 s = pickle.dumps(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
533 e = pickle.loads(s) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
534 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
535 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
536 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
537 |
|
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
|
538 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
|
539 |
|
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
|
540 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
|
541 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
|
542 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
|
543 |
|
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
|
544 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
|
545 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
|
546 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
|
547 |
|
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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 |
|
43466
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
554 ## def test_pickle(self): |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
555 ## d = Deque('abc') |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
556 ## d.append(d) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
557 ## |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
558 ## e = pickle.loads(pickle.dumps(d)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
559 ## self.assertNotEqual(id(d), id(e)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
560 ## self.assertEqual(type(d), type(e)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
561 ## dd = d.pop() |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
562 ## ee = e.pop() |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
563 ## self.assertEqual(id(e), id(ee)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
564 ## self.assertEqual(d, e) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
565 ## |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
566 ## d.x = d |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
567 ## e = pickle.loads(pickle.dumps(d)) |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
568 ## 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
|
569 ## |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
570 ## d = DequeWithBadIter('abc') |
|
73f49656420b
Add __asdict__() to NamedTuple and refine the docs.
Raymond Hettinger <python@rcn.com>
parents:
40682
diff
changeset
|
571 ## self.assertRaises(TypeError, pickle.dumps, d) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
572 |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
573 def test_weakref(self): |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
574 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
|
575 p = weakref.proxy(d) |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
576 self.assertEqual(str(p), str(d)) |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
577 d = None |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
578 self.assertRaises(ReferenceError, str, p) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
579 |
|
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
|
580 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
|
581 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
|
582 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
|
583 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
|
584 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
|
585 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
|
586 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
|
587 # 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
|
588 |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
589 |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
590 class SubclassWithKwargs(deque): |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
591 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
|
592 deque.__init__(self) |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
593 |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
594 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
|
595 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
|
596 # 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
|
597 SubclassWithKwargs(newarg=1) |
|
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
598 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
599 #============================================================================== |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
600 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
601 libreftest = """ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
602 Example from the Library Reference: Doc/lib/libcollections.tex |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
603 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
604 >>> from collections import deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
605 >>> d = deque('ghi') # make a new deque with three items |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
606 >>> 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
|
607 ... print elem.upper() |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
608 G |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
609 H |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
610 I |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
611 >>> 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
|
612 >>> 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
|
613 >>> d # show the representation of the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
614 deque(['f', 'g', 'h', 'i', 'j']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
615 >>> d.pop() # return and remove the rightmost item |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
616 'j' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
617 >>> d.popleft() # return and remove the leftmost item |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
618 'f' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
619 >>> list(d) # list the contents of the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
620 ['g', 'h', 'i'] |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
621 >>> d[0] # peek at leftmost item |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
622 'g' |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
623 >>> d[-1] # peek at rightmost item |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
624 'i' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
625 >>> 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
|
626 ['i', 'h', 'g'] |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
627 >>> 'h' in d # search the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
628 True |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
629 >>> d.extend('jkl') # add multiple elements at once |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
630 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
631 deque(['g', 'h', 'i', 'j', 'k', 'l']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
632 >>> d.rotate(1) # right rotation |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
633 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
634 deque(['l', 'g', 'h', 'i', 'j', 'k']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
635 >>> d.rotate(-1) # left rotation |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
636 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
637 deque(['g', 'h', 'i', 'j', 'k', 'l']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
638 >>> deque(reversed(d)) # make a new deque in reverse order |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
639 deque(['l', 'k', 'j', 'i', 'h', 'g']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
640 >>> d.clear() # empty the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
641 >>> d.pop() # cannot pop from an empty deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
642 Traceback (most recent call last): |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
643 File "<pyshell#6>", line 1, in -toplevel- |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
644 d.pop() |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
645 IndexError: pop from an empty deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
646 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
647 >>> d.extendleft('abc') # extendleft() reverses the input order |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
648 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
649 deque(['c', 'b', 'a']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
650 |
| 32014 | 651 |
| 652 | |
| 653 >>> def delete_nth(d, n): | |
| 654 ... d.rotate(-n) | |
| 655 ... d.popleft() | |
| 656 ... d.rotate(n) | |
| 657 ... | |
| 658 >>> d = deque('abcdef') | |
| 659 >>> delete_nth(d, 2) # remove the entry at d[2] | |
| 660 >>> d | |
| 661 deque(['a', 'b', 'd', 'e', 'f']) | |
| 662 | |
| 663 | |
| 664 | |
| 665 >>> def roundrobin(*iterables): | |
| 666 ... pending = deque(iter(i) for i in iterables) | |
| 667 ... while pending: | |
| 668 ... task = pending.popleft() | |
| 669 ... try: | |
| 670 ... yield task.next() | |
| 671 ... except StopIteration: | |
| 672 ... continue | |
| 673 ... pending.append(task) | |
| 674 ... | |
| 675 | |
| 676 >>> for value in roundrobin('abc', 'd', 'efgh'): | |
| 677 ... print value | |
| 678 ... | |
| 679 a | |
| 680 d | |
| 681 e | |
| 682 b | |
| 683 f | |
| 684 c | |
| 685 g | |
| 686 h | |
| 687 | |
| 688 | |
| 689 >>> def maketree(iterable): | |
| 690 ... d = deque(iterable) | |
| 691 ... while len(d) > 1: | |
| 692 ... pair = [d.popleft(), d.popleft()] | |
| 693 ... d.append(pair) | |
| 694 ... return list(d) | |
| 695 ... | |
| 696 >>> print maketree('abcdefgh') | |
| 697 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] | |
| 698 | |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
699 """ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
700 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
701 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
702 #============================================================================== |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
703 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
704 __test__ = {'libreftest' : libreftest} |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
705 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
706 def test_main(verbose=None): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
707 import sys |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
708 test_classes = ( |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
709 TestBasic, |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
710 TestVariousIteratorArgs, |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
711 TestSubclass, |
|
40682
77e0f4718a7c
Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents:
40573
diff
changeset
|
712 TestSubclassWithKwargs, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
713 ) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
714 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
715 test_support.run_unittest(*test_classes) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
716 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
717 # verify reference counting |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
718 if verbose and hasattr(sys, "gettotalrefcount"): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
719 import gc |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
720 counts = [None] * 5 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
721 for i in xrange(len(counts)): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
722 test_support.run_unittest(*test_classes) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
723 gc.collect() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
724 counts[i] = sys.gettotalrefcount() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
725 print counts |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
726 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
727 # doctests |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
728 from test import test_deque |
|
32121
302d399cfd37
SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents:
32084
diff
changeset
|
729 test_support.run_doctest(test_deque, verbose) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
730 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
731 if __name__ == "__main__": |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
732 test_main(verbose=True) |
