Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_deque.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 3c9232a684ae |
| children | b0725c1b3068 4ff234337e24 |
| 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 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
3 from test import support, seq_tests |
|
50553
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
4 import gc |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
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 |
|
39543
9bc0bc0ad1bf
Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents:
34802
diff
changeset
|
7 import pickle |
|
42501
f6069f9dd52d
SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents:
41972
diff
changeset
|
8 from io import StringIO |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
9 import random |
|
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): |
|
44291
71623878057b
Merged revisions 59465-59487 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43734
diff
changeset
|
32 d = deque(range(-5125, -5000)) |
|
71623878057b
Merged revisions 59465-59487 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43734
diff
changeset
|
33 d.__init__(range(200)) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
34 for i in range(200, 400): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
35 d.append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
36 for i in reversed(range(-200, 0)): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
37 d.appendleft(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
38 self.assertEqual(list(d), list(range(-200, 400))) |
|
31436
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 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
41 left = [d.popleft() for i in range(250)] |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
42 self.assertEqual(left, list(range(-200, 50))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
43 self.assertEqual(list(d), list(range(50, 400))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
44 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
45 right = [d.pop() for i in range(250)] |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
46 right.reverse() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
47 self.assertEqual(right, list(range(150, 400))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
48 self.assertEqual(list(d), list(range(50, 150))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
49 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
50 def test_maxlen(self): |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
51 self.assertRaises(ValueError, deque, 'abc', -1) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
52 self.assertRaises(ValueError, deque, 'abc', -2) |
|
52118
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
53 it = iter(range(10)) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
54 d = deque(it, maxlen=3) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
55 self.assertEqual(list(it), []) |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
56 self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
57 self.assertEqual(list(d), [7, 8, 9]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
58 self.assertEqual(d, deque(range(10), 3)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
59 d.append(10) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
60 self.assertEqual(list(d), [8, 9, 10]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
61 d.appendleft(7) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
62 self.assertEqual(list(d), [7, 8, 9]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
63 d.extend([10, 11]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
64 self.assertEqual(list(d), [9, 10, 11]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
65 d.extendleft([8, 7]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
66 self.assertEqual(list(d), [7, 8, 9]) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
67 d = deque(range(200), maxlen=10) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
68 d.append(d) |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
69 support.unlink(support.TESTFN) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
70 fo = open(support.TESTFN, "w") |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
71 try: |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
72 fo.write(str(d)) |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
73 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
74 fo = open(support.TESTFN, "r") |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
75 self.assertEqual(fo.read(), repr(d)) |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
76 finally: |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
77 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
78 support.unlink(support.TESTFN) |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
79 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
80 d = deque(range(10), maxlen=None) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
81 self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])') |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
82 fo = open(support.TESTFN, "w") |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
83 try: |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
84 fo.write(str(d)) |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
85 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
86 fo = open(support.TESTFN, "r") |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
87 self.assertEqual(fo.read(), repr(d)) |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
88 finally: |
|
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
89 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
90 support.unlink(support.TESTFN) |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
91 |
|
52118
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
92 def test_maxlen_zero(self): |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
93 it = iter(range(100)) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
94 deque(it, maxlen=0) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
95 self.assertEqual(list(it), []) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
96 |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
97 it = iter(range(100)) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
98 d = deque(maxlen=0) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
99 d.extend(it) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
100 self.assertEqual(list(it), []) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
101 |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
102 it = iter(range(100)) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
103 d = deque(maxlen=0) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
104 d.extendleft(it) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
105 self.assertEqual(list(it), []) |
|
c5e23b9cedcc
Small optimization for corner case where maxlen==0.
Raymond Hettinger <python@rcn.com>
parents:
51122
diff
changeset
|
106 |
|
52120
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
107 def test_maxlen_attribute(self): |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
108 self.assertEqual(deque().maxlen, None) |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
109 self.assertEqual(deque('abc').maxlen, None) |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
110 self.assertEqual(deque('abc', maxlen=4).maxlen, 4) |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
111 self.assertEqual(deque('abc', maxlen=2).maxlen, 2) |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
112 self.assertEqual(deque('abc', maxlen=0).maxlen, 0) |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
113 with self.assertRaises(AttributeError): |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
114 d = deque('abc') |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
115 d.maxlen = 10 |
|
21850d6d5b4d
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Raymond Hettinger <python@rcn.com>
parents:
52118
diff
changeset
|
116 |
|
60291
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
117 def test_count(self): |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
118 for s in ('', 'abracadabra', 'simsalabim'*500+'abc'): |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
119 s = list(s) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
120 d = deque(s) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
121 for letter in 'abcdefghijklmnopqrstuvwxyz': |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
122 self.assertEqual(s.count(letter), d.count(letter), (s, d, letter)) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
123 self.assertRaises(TypeError, d.count) # too few args |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
124 self.assertRaises(TypeError, d.count, 1, 2) # too many args |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
125 class BadCompare: |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
126 def __eq__(self, other): |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
127 raise ArithmeticError |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
128 d = deque([1, 2, BadCompare(), 3]) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
129 self.assertRaises(ArithmeticError, d.count, 2) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
130 d = deque([1, 2, 3]) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
131 self.assertRaises(ArithmeticError, d.count, BadCompare()) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
132 class MutatingCompare: |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
133 def __eq__(self, other): |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
134 self.d.pop() |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
135 return True |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
136 m = MutatingCompare() |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
137 d = deque([1, 2, 3, m, 4, 5]) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
138 m.d = d |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
139 self.assertRaises(RuntimeError, d.count, 3) |
|
da939ab2bfe2
Add count() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
59615
diff
changeset
|
140 |
|
67764
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
141 # test issue11004 |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
142 # block advance failed after rotation aligned elements on right side of block |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
143 d = deque([None]*16) |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
144 for i in range(len(d)): |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
145 d.rotate(-1) |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
146 d.rotate(1) |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
147 self.assertEqual(d.count(1), 0) |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
148 self.assertEqual(d.count(None), 16) |
|
776c3c294bb7
Issue #11004: Repair edge case in deque.count().
Raymond Hettinger <python@rcn.com>
parents:
66310
diff
changeset
|
149 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
150 def test_comparisons(self): |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
151 d = deque('xabc'); d.popleft() |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
152 for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
153 self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
154 self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e))) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
155 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
156 args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba')) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
157 for x in args: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
158 for y in args: |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
159 self.assertEqual(x == y, list(x) == list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
160 self.assertEqual(x != y, list(x) != list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
161 self.assertEqual(x < y, list(x) < list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
162 self.assertEqual(x <= y, list(x) <= list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
163 self.assertEqual(x > y, list(x) > list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
164 self.assertEqual(x >= y, list(x) >= list(y), (x,y)) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
165 |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
166 def test_extend(self): |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
167 d = deque('a') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
168 self.assertRaises(TypeError, d.extend, 1) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
169 d.extend('bcd') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
170 self.assertEqual(list(d), list('abcd')) |
|
57632
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
171 d.extend(d) |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
172 self.assertEqual(list(d), list('abcdabcd')) |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
173 |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
174 def test_iadd(self): |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
175 d = deque('a') |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
176 d += 'bcd' |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
177 self.assertEqual(list(d), list('abcd')) |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
178 d += d |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
179 self.assertEqual(list(d), list('abcdabcd')) |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
180 |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
181 def test_extendleft(self): |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
182 d = deque('a') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
183 self.assertRaises(TypeError, d.extendleft, 1) |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
184 d.extendleft('bcd') |
|
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
185 self.assertEqual(list(d), list(reversed('abcd'))) |
|
57632
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
186 d.extendleft(d) |
|
bc716d8f4345
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
187 self.assertEqual(list(d), list('abcddcba')) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
188 d = deque() |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
189 d.extendleft(range(1000)) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
190 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
|
191 self.assertRaises(SyntaxError, d.extendleft, fail()) |
|
31464
aabd9b54e469
Have deques support high volume loads.
Raymond Hettinger <python@rcn.com>
parents:
31436
diff
changeset
|
192 |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
193 def test_getitem(self): |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
194 n = 200 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
195 d = deque(range(n)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
196 l = list(range(n)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
197 for i in range(n): |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
198 d.popleft() |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
199 l.pop(0) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
200 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
|
201 d.append(i) |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
202 l.append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
203 for j in range(1-len(l), len(l)): |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
204 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
|
205 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
206 d = deque('superman') |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
207 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
|
208 self.assertEqual(d[-1], 'n') |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
209 d = deque() |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
210 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
|
211 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
|
212 |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
213 def test_setitem(self): |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
214 n = 200 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
215 d = deque(range(n)) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
216 for i in range(n): |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
217 d[i] = 10 * i |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
218 self.assertEqual(list(d), [10*i for i in range(n)]) |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
219 l = list(d) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
220 for i in range(1-n, 0, -1): |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
221 d[i] = 7*i |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
222 l[i] = 7*i |
|
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
223 self.assertEqual(list(d), l) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
224 |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
225 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
|
226 n = 500 # O(n**2) test, don't make this too big |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
227 d = deque(range(n)) |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
228 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
|
229 self.assertRaises(IndexError, d.__delitem__, n) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
230 for i in range(n): |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
231 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
|
232 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
|
233 val = d[j] |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57630
diff
changeset
|
234 self.assertIn(val, d) |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
235 del d[j] |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57630
diff
changeset
|
236 self.assertNotIn(val, d) |
|
32084
245835ed8de8
Make sure "del d[n]" is properly supported. Was necessary because the
Raymond Hettinger <python@rcn.com>
parents:
32035
diff
changeset
|
237 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
|
238 |
|
57627
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
239 def test_reverse(self): |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
240 n = 500 # O(n**2) test, don't make this too big |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
241 data = [random.random() for i in range(n)] |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
242 for i in range(n): |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
243 d = deque(data[:i]) |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
244 r = d.reverse() |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
245 self.assertEqual(list(d), list(reversed(data[:i]))) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
60291
diff
changeset
|
246 self.assertIs(r, None) |
|
57627
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
247 d.reverse() |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
248 self.assertEqual(list(d), data[:i]) |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
249 self.assertRaises(TypeError, d.reverse, 1) # Arity is zero |
|
816303a07762
Add a reverse() method to collections.deque().
Raymond Hettinger <python@rcn.com>
parents:
55593
diff
changeset
|
250 |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
251 def test_rotate(self): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
252 s = tuple('abcde') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
253 n = len(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
254 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
255 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
256 d.rotate(1) # verify rot(1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
257 self.assertEqual(''.join(d), 'eabcd') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
258 |
|
31474
3ce4ffac27ab
* Incorporate Skip's suggestions for documentation (explain the word deque
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
259 d = deque(s) |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
260 d.rotate(-1) # verify rot(-1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
261 self.assertEqual(''.join(d), 'bcdea') |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
262 d.rotate() # check default to 1 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
263 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
264 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
265 for i in range(n*3): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
266 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
267 e = deque(d) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
268 d.rotate(i) # check vs. rot(1) n times |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
269 for j in range(i): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
270 e.rotate(1) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
271 self.assertEqual(tuple(d), tuple(e)) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
272 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
|
273 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
274 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
|
275 self.assertEqual(tuple(e), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
276 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
277 for i in range(n*3): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
278 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
279 e = deque(d) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
280 d.rotate(-i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
281 for j in range(i): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
282 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
|
283 self.assertEqual(tuple(d), tuple(e)) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
284 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
|
285 self.assertEqual(tuple(d), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
286 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
|
287 self.assertEqual(tuple(e), s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
288 |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
289 d = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
290 e = deque(s) |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
291 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
|
292 dr = d.rotate |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
293 for i in range(BIG+17): |
|
31476
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
294 dr() |
|
25b30439fe22
Make deque.rotate() smarter. Beef-up related tests.
Raymond Hettinger <python@rcn.com>
parents:
31474
diff
changeset
|
295 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
|
296 |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
297 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
|
298 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
|
299 |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
300 d = deque() |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
301 d.rotate() # rotate an empty deque |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
302 self.assertEqual(d, deque()) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
303 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
304 def test_len(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
305 d = deque('ab') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
306 self.assertEqual(len(d), 2) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
307 d.popleft() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
308 self.assertEqual(len(d), 1) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
309 d.pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
310 self.assertEqual(len(d), 0) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
311 self.assertRaises(IndexError, d.pop) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
312 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
313 d.append('c') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
314 self.assertEqual(len(d), 1) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
315 d.appendleft('d') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
316 self.assertEqual(len(d), 2) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
317 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
318 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
319 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
320 def test_underflow(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
321 d = deque() |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
322 self.assertRaises(IndexError, d.pop) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
323 self.assertRaises(IndexError, d.popleft) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
324 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
325 def test_clear(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
326 d = deque(range(100)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
327 self.assertEqual(len(d), 100) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
328 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
329 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
330 self.assertEqual(list(d), []) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
331 d.clear() # clear an emtpy deque |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
332 self.assertEqual(list(d), []) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
333 |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
334 def test_remove(self): |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
335 d = deque('abcdefghcij') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
336 d.remove('c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
337 self.assertEqual(d, deque('abdefghcij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
338 d.remove('c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
339 self.assertEqual(d, deque('abdefghij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
340 self.assertRaises(ValueError, d.remove, 'c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
341 self.assertEqual(d, deque('abdefghij')) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
342 |
| 34798 | 343 # Handle comparison errors |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
344 d = deque(['a', 'b', BadCmp(), 'c']) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
345 e = deque(d) |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
346 self.assertRaises(RuntimeError, d.remove, 'c') |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
347 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
|
348 # verify that original order and values are retained. |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52120
diff
changeset
|
349 self.assertTrue(x is y) |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
350 |
|
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
351 # Handle evil mutator |
|
34772
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
352 for match in (True, False): |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
353 d = deque(['ab']) |
|
2ee575b9b60e
Apply remove's mutation test after every equality test.
Raymond Hettinger <python@rcn.com>
parents:
34771
diff
changeset
|
354 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
|
355 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
|
356 self.assertEqual(d, deque()) |
|
34771
fb39f4eeaea4
Add a remove() method to collections.deque objects.
Raymond Hettinger <python@rcn.com>
parents:
33949
diff
changeset
|
357 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
358 def test_repr(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
359 d = deque(range(200)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
360 e = eval(repr(d)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
361 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
362 d.append(d) |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
57630
diff
changeset
|
363 self.assertIn('...', repr(d)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
364 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
365 def test_print(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
366 d = deque(range(200)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
367 d.append(d) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
368 try: |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
369 support.unlink(support.TESTFN) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
370 fo = open(support.TESTFN, "w") |
|
46084
6bea2f1351b1
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44291
diff
changeset
|
371 print(d, file=fo, end='') |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
372 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
373 fo = open(support.TESTFN, "r") |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
374 self.assertEqual(fo.read(), repr(d)) |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
375 finally: |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
376 fo.close() |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
377 support.unlink(support.TESTFN) |
|
32554
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
378 |
|
74c3b713e35a
* balance the left/right search for getitem.
Raymond Hettinger <python@rcn.com>
parents:
32158
diff
changeset
|
379 def test_init(self): |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
380 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
|
381 self.assertRaises(TypeError, deque, 1); |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
382 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
383 def test_hash(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
384 self.assertRaises(TypeError, hash, deque('abc')) |
|
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_long_steadystate_queue_popleft(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
387 for size in (0, 1, 2, 100, 1000): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
388 d = deque(range(size)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
389 append, pop = d.append, d.popleft |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
390 for i in range(size, BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
391 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
392 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
393 if x != i - size: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
394 self.assertEqual(x, i-size) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
395 self.assertEqual(list(d), list(range(BIG-size, BIG))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
396 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
397 def test_long_steadystate_queue_popright(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
398 for size in (0, 1, 2, 100, 1000): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
399 d = deque(reversed(range(size))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
400 append, pop = d.appendleft, d.pop |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
401 for i in range(size, BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
402 append(i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
403 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
404 if x != i - size: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
405 self.assertEqual(x, i-size) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
406 self.assertEqual(list(reversed(list(d))), |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
407 list(range(BIG-size, BIG))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
408 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
409 def test_big_queue_popleft(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
410 pass |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
411 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
412 append, pop = d.append, d.popleft |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
413 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
414 append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
415 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
416 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
417 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
418 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
419 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
420 def test_big_queue_popright(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
421 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
422 append, pop = d.appendleft, d.pop |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
423 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
424 append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
425 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
426 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
427 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
428 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
429 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
430 def test_big_stack_right(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
431 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
432 append, pop = d.append, d.pop |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
433 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
434 append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
435 for i in reversed(range(BIG)): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
436 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
437 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
438 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
439 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
440 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
441 def test_big_stack_left(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
442 d = deque() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
443 append, pop = d.appendleft, d.popleft |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
444 for i in range(BIG): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
445 append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
446 for i in reversed(range(BIG)): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
447 x = pop() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
448 if x != i: |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
449 self.assertEqual(x, i) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
450 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
451 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
452 def test_roundtrip_iter_init(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
453 d = deque(range(200)) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
454 e = deque(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
455 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
456 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
457 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
458 def test_pickle(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
459 d = deque(range(200)) |
|
50540
4229a4b14f1d
Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
460 for i in range(pickle.HIGHEST_PROTOCOL + 1): |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
461 s = pickle.dumps(d, i) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
462 e = pickle.loads(s) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
463 self.assertNotEqual(id(d), id(e)) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
464 self.assertEqual(list(d), list(e)) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
465 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
466 ## def test_pickle_recursive(self): |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
467 ## d = deque('abc') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
468 ## d.append(d) |
|
50540
4229a4b14f1d
Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
469 ## for i in range(pickle.HIGHEST_PROTOCOL + 1): |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
470 ## e = pickle.loads(pickle.dumps(d, i)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
471 ## self.assertNotEqual(id(d), id(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
472 ## self.assertEqual(id(e), id(e[-1])) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
473 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
474 def test_deepcopy(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
475 mut = [10] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
476 d = deque([mut]) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
477 e = copy.deepcopy(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
478 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
479 mut[0] = 11 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
480 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
481 self.assertNotEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
482 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
483 def test_copy(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
484 mut = [10] |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
485 d = deque([mut]) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
486 e = copy.copy(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
487 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
488 mut[0] = 11 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
489 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
490 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
491 |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
492 def test_reversed(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
493 for s in ('abcd', range(2000)): |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
494 self.assertEqual(list(reversed(deque(s))), list(reversed(s))) |
|
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
31464
diff
changeset
|
495 |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
496 def test_gc_doesnt_blowup(self): |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
497 import gc |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
498 # This used to assert-fail in deque_traverse() under a debug |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
499 # build, or run wild with a NULL pointer in a release build. |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
500 d = deque() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
501 for i in range(100): |
|
33639
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
502 d.append(1) |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
503 gc.collect() |
|
40350fbe665d
deque_traverse(): If the deque had one block, and its rightindex was
Tim Peters <tim.peters@gmail.com>
parents:
33597
diff
changeset
|
504 |
|
50553
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
505 def test_container_iterator(self): |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
506 # Bug #3680: tp_traverse was not implemented for deque iterator objects |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
507 class C(object): |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
508 pass |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
509 for i in range(2): |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
510 obj = C() |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
511 ref = weakref.ref(obj) |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
512 if i == 0: |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
513 container = deque([obj, 1]) |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
514 else: |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
515 container = reversed(deque([obj, 1])) |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
516 obj.x = iter(container) |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
517 del obj, container |
|
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
518 gc.collect() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
52120
diff
changeset
|
519 self.assertTrue(ref() is None, "Cycle was not collected") |
|
50553
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
520 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
521 class TestVariousIteratorArgs(unittest.TestCase): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
522 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
523 def test_constructor(self): |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
524 for s in ("123", "", range(1000), ('do', 1.2), range(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
|
525 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
|
526 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
|
527 seq_tests.itermulti, seq_tests.iterfunc): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
528 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
|
529 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
|
530 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
|
531 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
|
532 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
533 def test_iter_with_altered_data(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
534 d = deque('abcdefg') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
535 it = iter(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
536 d.pop() |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40789
diff
changeset
|
537 self.assertRaises(RuntimeError, next, it) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
538 |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39543
diff
changeset
|
539 def test_runtime_error_on_empty_deque(self): |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39543
diff
changeset
|
540 d = deque() |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39543
diff
changeset
|
541 it = iter(d) |
|
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39543
diff
changeset
|
542 d.append(10) |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40789
diff
changeset
|
543 self.assertRaises(RuntimeError, next, it) |
|
40582
67287ca4e1e8
Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39543
diff
changeset
|
544 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
545 class Deque(deque): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
546 pass |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
547 |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
548 class DequeWithBadIter(deque): |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
549 def __iter__(self): |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
550 raise TypeError |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
551 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
552 class TestSubclass(unittest.TestCase): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
553 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
554 def test_basics(self): |
|
44291
71623878057b
Merged revisions 59465-59487 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43734
diff
changeset
|
555 d = Deque(range(25)) |
|
71623878057b
Merged revisions 59465-59487 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43734
diff
changeset
|
556 d.__init__(range(200)) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
557 for i in range(200, 400): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
558 d.append(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
559 for i in reversed(range(-200, 0)): |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
560 d.appendleft(i) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
561 self.assertEqual(list(d), list(range(-200, 400))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
562 self.assertEqual(len(d), 600) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
563 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
564 left = [d.popleft() for i in range(250)] |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
565 self.assertEqual(left, list(range(-200, 50))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
566 self.assertEqual(list(d), list(range(50, 400))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
567 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
568 right = [d.pop() for i in range(250)] |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
569 right.reverse() |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
570 self.assertEqual(right, list(range(150, 400))) |
|
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
571 self.assertEqual(list(d), list(range(50, 150))) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
572 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
573 d.clear() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
574 self.assertEqual(len(d), 0) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
575 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
576 def test_copy_pickle(self): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
577 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
578 d = Deque('abc') |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
579 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
580 e = d.__copy__() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
581 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
582 self.assertEqual(list(d), list(e)) |
|
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 e = Deque(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
585 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
586 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
587 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
588 s = pickle.dumps(d) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
589 e = pickle.loads(s) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
590 self.assertNotEqual(id(d), id(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
591 self.assertEqual(type(d), type(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
592 self.assertEqual(list(d), list(e)) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
593 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
594 d = Deque('abcde', maxlen=4) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
595 |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
596 e = d.__copy__() |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
597 self.assertEqual(type(d), type(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
598 self.assertEqual(list(d), list(e)) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
599 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
600 e = Deque(d) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
601 self.assertEqual(type(d), type(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
602 self.assertEqual(list(d), list(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
603 |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
604 s = pickle.dumps(d) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
605 e = pickle.loads(s) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
606 self.assertNotEqual(id(d), id(e)) |
|
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
607 self.assertEqual(type(d), type(e)) |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
608 self.assertEqual(list(d), list(e)) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
609 |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
610 ## def test_pickle(self): |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
611 ## d = Deque('abc') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
612 ## d.append(d) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
613 ## |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
614 ## e = pickle.loads(pickle.dumps(d)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
615 ## self.assertNotEqual(id(d), id(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
616 ## self.assertEqual(type(d), type(e)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
617 ## dd = d.pop() |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
618 ## ee = e.pop() |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
619 ## self.assertEqual(id(e), id(ee)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
620 ## self.assertEqual(d, e) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
621 ## |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
622 ## d.x = d |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
623 ## e = pickle.loads(pickle.dumps(d)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
624 ## self.assertEqual(id(e), id(e.x)) |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
625 ## |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
626 ## d = DequeWithBadIter('abc') |
|
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42501
diff
changeset
|
627 ## self.assertRaises(TypeError, pickle.dumps, d) |
|
33949
b9819b9a0dad
SF patch #1062279: deque pickling problems
Raymond Hettinger <python@rcn.com>
parents:
33648
diff
changeset
|
628 |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
629 def test_weakref(self): |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
630 d = deque('gallahad') |
|
50553
badb471dbaeb
Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50540
diff
changeset
|
631 p = weakref.proxy(d) |
|
32158
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
632 self.assertEqual(str(p), str(d)) |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
633 d = None |
|
679e5c8effae
Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents:
32121
diff
changeset
|
634 self.assertRaises(ReferenceError, str, p) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
635 |
|
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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 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
|
641 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
|
642 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
|
643 # 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
|
644 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
645 |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
646 class SubclassWithKwargs(deque): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
647 def __init__(self, newarg=1): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
648 deque.__init__(self) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
649 |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
650 class TestSubclassWithKwargs(unittest.TestCase): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
651 def test_subclass_with_kwargs(self): |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
652 # SF bug #1486663 -- this used to erroneously raise a TypeError |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
653 SubclassWithKwargs(newarg=1) |
|
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
654 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
655 #============================================================================== |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
656 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
657 libreftest = """ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
658 Example from the Library Reference: Doc/lib/libcollections.tex |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
659 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
660 >>> from collections import deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
661 >>> d = deque('ghi') # make a new deque with three items |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
662 >>> for elem in d: # iterate over the deque's elements |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
663 ... print(elem.upper()) |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
664 G |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
665 H |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
666 I |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
667 >>> 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
|
668 >>> 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
|
669 >>> d # show the representation of the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
670 deque(['f', 'g', 'h', 'i', 'j']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
671 >>> d.pop() # return and remove the rightmost item |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
672 'j' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
673 >>> d.popleft() # return and remove the leftmost item |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
674 'f' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
675 >>> list(d) # list the contents of the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
676 ['g', 'h', 'i'] |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
677 >>> d[0] # peek at leftmost item |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
678 'g' |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
679 >>> d[-1] # peek at rightmost item |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
680 'i' |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
681 >>> 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
|
682 ['i', 'h', 'g'] |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
683 >>> 'h' in d # search the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
684 True |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
685 >>> d.extend('jkl') # add multiple elements at once |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
686 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
687 deque(['g', 'h', 'i', 'j', 'k', 'l']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
688 >>> d.rotate(1) # right rotation |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
689 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
690 deque(['l', 'g', 'h', 'i', 'j', 'k']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
691 >>> d.rotate(-1) # left rotation |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
692 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
693 deque(['g', 'h', 'i', 'j', 'k', 'l']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
694 >>> deque(reversed(d)) # make a new deque in reverse order |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
695 deque(['l', 'k', 'j', 'i', 'h', 'g']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
696 >>> d.clear() # empty the deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
697 >>> d.pop() # cannot pop from an empty deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
698 Traceback (most recent call last): |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
699 File "<pyshell#6>", line 1, in -toplevel- |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
700 d.pop() |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
701 IndexError: pop from an empty deque |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
702 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
703 >>> d.extendleft('abc') # extendleft() reverses the input order |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
704 >>> d |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
705 deque(['c', 'b', 'a']) |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
706 |
| 32014 | 707 |
| 708 | |
| 709 >>> def delete_nth(d, n): | |
| 710 ... d.rotate(-n) | |
| 711 ... d.popleft() | |
| 712 ... d.rotate(n) | |
| 713 ... | |
| 714 >>> d = deque('abcdef') | |
| 715 >>> delete_nth(d, 2) # remove the entry at d[2] | |
| 716 >>> d | |
| 717 deque(['a', 'b', 'd', 'e', 'f']) | |
| 718 | |
| 719 | |
| 720 | |
| 721 >>> def roundrobin(*iterables): | |
| 722 ... pending = deque(iter(i) for i in iterables) | |
| 723 ... while pending: | |
| 724 ... task = pending.popleft() | |
| 725 ... try: | |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40789
diff
changeset
|
726 ... yield next(task) |
| 32014 | 727 ... except StopIteration: |
| 728 ... continue | |
| 729 ... pending.append(task) | |
| 730 ... | |
| 731 | |
| 732 >>> for value in roundrobin('abc', 'd', 'efgh'): | |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
733 ... print(value) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
734 ... |
| 32014 | 735 a |
| 736 d | |
| 737 e | |
| 738 b | |
| 739 f | |
| 740 c | |
| 741 g | |
| 742 h | |
| 743 | |
| 744 | |
| 745 >>> def maketree(iterable): | |
| 746 ... d = deque(iterable) | |
| 747 ... while len(d) > 1: | |
| 748 ... pair = [d.popleft(), d.popleft()] | |
| 749 ... d.append(pair) | |
| 750 ... return list(d) | |
| 751 ... | |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
752 >>> print(maketree('abcdefgh')) |
| 32014 | 753 [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] |
| 754 | |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
755 """ |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
756 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
757 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
758 #============================================================================== |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
759 |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
760 __test__ = {'libreftest' : libreftest} |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
761 |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
762 def test_main(verbose=None): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
763 import sys |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
764 test_classes = ( |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
765 TestBasic, |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
766 TestVariousIteratorArgs, |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
767 TestSubclass, |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40582
diff
changeset
|
768 TestSubclassWithKwargs, |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
769 ) |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
770 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
771 support.run_unittest(*test_classes) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
772 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
773 # verify reference counting |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
774 if verbose and hasattr(sys, "gettotalrefcount"): |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
775 import gc |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
776 counts = [None] * 5 |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
777 for i in range(len(counts)): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
778 support.run_unittest(*test_classes) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
779 gc.collect() |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
780 counts[i] = sys.gettotalrefcount() |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
781 print(counts) |
|
31607
8ecee8f47108
Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents:
31603
diff
changeset
|
782 |
|
31603
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
783 # doctests |
|
15553a1b7a17
Improvements to collections.deque():
Raymond Hettinger <python@rcn.com>
parents:
31476
diff
changeset
|
784 from test import test_deque |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46430
diff
changeset
|
785 support.run_doctest(test_deque, verbose) |
|
31436
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
786 |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
787 if __name__ == "__main__": |
|
63fa60f2d61c
* Move collections.deque() in from the sandbox
Raymond Hettinger <python@rcn.com>
parents:
diff
changeset
|
788 test_main(verbose=True) |
