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