Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1 | from collections import deque |
| 2 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support, seq_tests |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 4 | import gc |
| 5 | import weakref |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 6 | import copy |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 7 | import pickle |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 8 | from io import StringIO |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 9 | import random |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 10 | import struct |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 11 | |
| 12 | BIG = 100000 |
| 13 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 14 | def fail(): |
| 15 | raise SyntaxError |
| 16 | yield 1 |
| 17 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 18 | class BadCmp: |
| 19 | def __eq__(self, other): |
| 20 | raise RuntimeError |
| 21 | |
| 22 | class MutateCmp: |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 23 | def __init__(self, deque, result): |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 24 | self.deque = deque |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 25 | self.result = result |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 26 | def __eq__(self, other): |
| 27 | self.deque.clear() |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 28 | return self.result |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 29 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 30 | class TestBasic(unittest.TestCase): |
| 31 | |
| 32 | def test_basics(self): |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 33 | d = deque(range(-5125, -5000)) |
| 34 | d.__init__(range(200)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 35 | for i in range(200, 400): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 36 | d.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 37 | for i in reversed(range(-200, 0)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 38 | d.appendleft(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 39 | self.assertEqual(list(d), list(range(-200, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 40 | self.assertEqual(len(d), 600) |
| 41 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 42 | left = [d.popleft() for i in range(250)] |
| 43 | self.assertEqual(left, list(range(-200, 50))) |
| 44 | self.assertEqual(list(d), list(range(50, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 46 | right = [d.pop() for i in range(250)] |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 47 | right.reverse() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 48 | self.assertEqual(right, list(range(150, 400))) |
| 49 | self.assertEqual(list(d), list(range(50, 150))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 51 | def test_maxlen(self): |
| 52 | self.assertRaises(ValueError, deque, 'abc', -1) |
| 53 | self.assertRaises(ValueError, deque, 'abc', -2) |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 54 | it = iter(range(10)) |
| 55 | d = deque(it, maxlen=3) |
| 56 | self.assertEqual(list(it), []) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 57 | self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)') |
| 58 | self.assertEqual(list(d), [7, 8, 9]) |
| 59 | self.assertEqual(d, deque(range(10), 3)) |
| 60 | d.append(10) |
| 61 | self.assertEqual(list(d), [8, 9, 10]) |
| 62 | d.appendleft(7) |
| 63 | self.assertEqual(list(d), [7, 8, 9]) |
| 64 | d.extend([10, 11]) |
| 65 | self.assertEqual(list(d), [9, 10, 11]) |
| 66 | d.extendleft([8, 7]) |
| 67 | self.assertEqual(list(d), [7, 8, 9]) |
| 68 | d = deque(range(200), maxlen=10) |
| 69 | d.append(d) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 70 | support.unlink(support.TESTFN) |
| 71 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 72 | try: |
| 73 | fo.write(str(d)) |
| 74 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 75 | fo = open(support.TESTFN, "r") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 76 | self.assertEqual(fo.read(), repr(d)) |
| 77 | finally: |
| 78 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 79 | support.unlink(support.TESTFN) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 81 | d = deque(range(10), maxlen=None) |
| 82 | self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])') |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 83 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 84 | try: |
| 85 | fo.write(str(d)) |
| 86 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 87 | fo = open(support.TESTFN, "r") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 88 | self.assertEqual(fo.read(), repr(d)) |
| 89 | finally: |
| 90 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 91 | support.unlink(support.TESTFN) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 92 | |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 93 | def test_maxlen_zero(self): |
| 94 | it = iter(range(100)) |
| 95 | deque(it, maxlen=0) |
| 96 | self.assertEqual(list(it), []) |
| 97 | |
| 98 | it = iter(range(100)) |
| 99 | d = deque(maxlen=0) |
| 100 | d.extend(it) |
| 101 | self.assertEqual(list(it), []) |
| 102 | |
| 103 | it = iter(range(100)) |
| 104 | d = deque(maxlen=0) |
| 105 | d.extendleft(it) |
| 106 | self.assertEqual(list(it), []) |
| 107 | |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 108 | def test_maxlen_attribute(self): |
| 109 | self.assertEqual(deque().maxlen, None) |
| 110 | self.assertEqual(deque('abc').maxlen, None) |
| 111 | self.assertEqual(deque('abc', maxlen=4).maxlen, 4) |
| 112 | self.assertEqual(deque('abc', maxlen=2).maxlen, 2) |
| 113 | self.assertEqual(deque('abc', maxlen=0).maxlen, 0) |
| 114 | with self.assertRaises(AttributeError): |
| 115 | d = deque('abc') |
| 116 | d.maxlen = 10 |
| 117 | |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 118 | def test_count(self): |
| 119 | for s in ('', 'abracadabra', 'simsalabim'*500+'abc'): |
| 120 | s = list(s) |
| 121 | d = deque(s) |
| 122 | for letter in 'abcdefghijklmnopqrstuvwxyz': |
| 123 | self.assertEqual(s.count(letter), d.count(letter), (s, d, letter)) |
| 124 | self.assertRaises(TypeError, d.count) # too few args |
| 125 | self.assertRaises(TypeError, d.count, 1, 2) # too many args |
| 126 | class BadCompare: |
| 127 | def __eq__(self, other): |
| 128 | raise ArithmeticError |
| 129 | d = deque([1, 2, BadCompare(), 3]) |
| 130 | self.assertRaises(ArithmeticError, d.count, 2) |
| 131 | d = deque([1, 2, 3]) |
| 132 | self.assertRaises(ArithmeticError, d.count, BadCompare()) |
| 133 | class MutatingCompare: |
| 134 | def __eq__(self, other): |
| 135 | self.d.pop() |
| 136 | return True |
| 137 | m = MutatingCompare() |
| 138 | d = deque([1, 2, 3, m, 4, 5]) |
| 139 | m.d = d |
| 140 | self.assertRaises(RuntimeError, d.count, 3) |
| 141 | |
Raymond Hettinger | 512d2cc | 2011-01-25 21:32:39 +0000 | [diff] [blame] | 142 | # test issue11004 |
| 143 | # block advance failed after rotation aligned elements on right side of block |
| 144 | d = deque([None]*16) |
| 145 | for i in range(len(d)): |
| 146 | d.rotate(-1) |
| 147 | d.rotate(1) |
| 148 | self.assertEqual(d.count(1), 0) |
| 149 | self.assertEqual(d.count(None), 16) |
| 150 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 151 | def test_comparisons(self): |
| 152 | d = deque('xabc'); d.popleft() |
| 153 | for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: |
| 154 | self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e)) |
| 155 | self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e))) |
| 156 | |
| 157 | args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba')) |
| 158 | for x in args: |
| 159 | for y in args: |
| 160 | self.assertEqual(x == y, list(x) == list(y), (x,y)) |
| 161 | self.assertEqual(x != y, list(x) != list(y), (x,y)) |
| 162 | self.assertEqual(x < y, list(x) < list(y), (x,y)) |
| 163 | self.assertEqual(x <= y, list(x) <= list(y), (x,y)) |
| 164 | self.assertEqual(x > y, list(x) > list(y), (x,y)) |
| 165 | self.assertEqual(x >= y, list(x) >= list(y), (x,y)) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 166 | |
Raymond Hettinger | 39dadf7 | 2015-03-20 16:38:56 -0700 | [diff] [blame] | 167 | def test_contains(self): |
| 168 | n = 200 |
| 169 | |
| 170 | d = deque(range(n)) |
| 171 | for i in range(n): |
| 172 | self.assertTrue(i in d) |
| 173 | self.assertTrue((n+1) not in d) |
| 174 | |
| 175 | # Test detection of mutation during iteration |
| 176 | d = deque(range(n)) |
| 177 | d[n//2] = MutateCmp(d, False) |
| 178 | with self.assertRaises(RuntimeError): |
| 179 | n in d |
| 180 | |
| 181 | # Test detection of comparison exceptions |
| 182 | d = deque(range(n)) |
| 183 | d[n//2] = BadCmp() |
| 184 | with self.assertRaises(RuntimeError): |
| 185 | n in d |
| 186 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 187 | def test_extend(self): |
| 188 | d = deque('a') |
| 189 | self.assertRaises(TypeError, d.extend, 1) |
| 190 | d.extend('bcd') |
| 191 | self.assertEqual(list(d), list('abcd')) |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 192 | d.extend(d) |
| 193 | self.assertEqual(list(d), list('abcdabcd')) |
| 194 | |
Raymond Hettinger | 41290a6 | 2015-03-31 08:12:23 -0700 | [diff] [blame] | 195 | def test_add(self): |
| 196 | d = deque() |
| 197 | e = deque('abc') |
| 198 | f = deque('def') |
| 199 | self.assertEqual(d + d, deque()) |
| 200 | self.assertEqual(e + f, deque('abcdef')) |
| 201 | self.assertEqual(e + e, deque('abcabc')) |
| 202 | self.assertEqual(e + d, deque('abc')) |
| 203 | self.assertEqual(d + e, deque('abc')) |
| 204 | self.assertIsNot(d + d, deque()) |
| 205 | self.assertIsNot(e + d, deque('abc')) |
| 206 | self.assertIsNot(d + e, deque('abc')) |
| 207 | |
| 208 | g = deque('abcdef', maxlen=4) |
| 209 | h = deque('gh') |
| 210 | self.assertEqual(g + h, deque('efgh')) |
| 211 | |
| 212 | with self.assertRaises(TypeError): |
| 213 | deque('abc') + 'def' |
| 214 | |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 215 | def test_iadd(self): |
| 216 | d = deque('a') |
| 217 | d += 'bcd' |
| 218 | self.assertEqual(list(d), list('abcd')) |
| 219 | d += d |
| 220 | self.assertEqual(list(d), list('abcdabcd')) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 221 | |
| 222 | def test_extendleft(self): |
| 223 | d = deque('a') |
| 224 | self.assertRaises(TypeError, d.extendleft, 1) |
| 225 | d.extendleft('bcd') |
| 226 | self.assertEqual(list(d), list(reversed('abcd'))) |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 227 | d.extendleft(d) |
| 228 | self.assertEqual(list(d), list('abcddcba')) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 229 | d = deque() |
| 230 | d.extendleft(range(1000)) |
| 231 | self.assertEqual(list(d), list(reversed(range(1000)))) |
| 232 | self.assertRaises(SyntaxError, d.extendleft, fail()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 233 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 234 | def test_getitem(self): |
| 235 | n = 200 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 236 | d = deque(range(n)) |
| 237 | l = list(range(n)) |
| 238 | for i in range(n): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 239 | d.popleft() |
| 240 | l.pop(0) |
| 241 | if random.random() < 0.5: |
| 242 | d.append(i) |
| 243 | l.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 244 | for j in range(1-len(l), len(l)): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 245 | assert d[j] == l[j] |
| 246 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 247 | d = deque('superman') |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 248 | self.assertEqual(d[0], 's') |
| 249 | self.assertEqual(d[-1], 'n') |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 250 | d = deque() |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 251 | self.assertRaises(IndexError, d.__getitem__, 0) |
| 252 | self.assertRaises(IndexError, d.__getitem__, -1) |
| 253 | |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 254 | def test_index(self): |
| 255 | for n in 1, 2, 30, 40, 200: |
| 256 | |
| 257 | d = deque(range(n)) |
| 258 | for i in range(n): |
| 259 | self.assertEqual(d.index(i), i) |
| 260 | |
| 261 | with self.assertRaises(ValueError): |
| 262 | d.index(n+1) |
| 263 | |
| 264 | # Test detection of mutation during iteration |
| 265 | d = deque(range(n)) |
| 266 | d[n//2] = MutateCmp(d, False) |
| 267 | with self.assertRaises(RuntimeError): |
| 268 | d.index(n) |
| 269 | |
| 270 | # Test detection of comparison exceptions |
| 271 | d = deque(range(n)) |
| 272 | d[n//2] = BadCmp() |
| 273 | with self.assertRaises(RuntimeError): |
| 274 | d.index(n) |
| 275 | |
| 276 | # Test start and stop arguments behavior matches list.index() |
| 277 | elements = 'ABCDEFGHI' |
| 278 | nonelement = 'Z' |
| 279 | d = deque(elements * 2) |
| 280 | s = list(elements * 2) |
| 281 | for start in range(-5 - len(s)*2, 5 + len(s) * 2): |
| 282 | for stop in range(-5 - len(s)*2, 5 + len(s) * 2): |
| 283 | for element in elements + 'Z': |
| 284 | try: |
| 285 | target = s.index(element, start, stop) |
| 286 | except ValueError: |
| 287 | with self.assertRaises(ValueError): |
| 288 | d.index(element, start, stop) |
| 289 | else: |
| 290 | self.assertEqual(d.index(element, start, stop), target) |
| 291 | |
Brett Cannon | df6b544 | 2015-09-03 10:15:03 -0700 | [diff] [blame] | 292 | def test_insert_bug_24913(self): |
| 293 | d = deque('A' * 3) |
| 294 | with self.assertRaises(ValueError): |
| 295 | i = d.index("Hello world", 0, 4) |
| 296 | |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 297 | def test_insert(self): |
| 298 | # Test to make sure insert behaves like lists |
| 299 | elements = 'ABCDEFGHI' |
| 300 | for i in range(-5 - len(elements)*2, 5 + len(elements) * 2): |
| 301 | d = deque('ABCDEFGHI') |
| 302 | s = list('ABCDEFGHI') |
| 303 | d.insert(i, 'Z') |
| 304 | s.insert(i, 'Z') |
| 305 | self.assertEqual(list(d), s) |
| 306 | |
Raymond Hettinger | b00da57 | 2016-02-01 21:19:22 -0800 | [diff] [blame] | 307 | def test_insert_bug_26194(self): |
Raymond Hettinger | 3743432 | 2016-01-26 21:44:16 -0800 | [diff] [blame] | 308 | data = 'ABC' |
Raymond Hettinger | b00da57 | 2016-02-01 21:19:22 -0800 | [diff] [blame] | 309 | d = deque(data, maxlen=len(data)) |
| 310 | with self.assertRaises(IndexError): |
| 311 | d.insert(2, None) |
| 312 | |
| 313 | elements = 'ABCDEFGHI' |
| 314 | for i in range(-len(elements), len(elements)): |
| 315 | d = deque(elements, maxlen=len(elements)+1) |
| 316 | d.insert(i, 'Z') |
| 317 | if i >= 0: |
| 318 | self.assertEqual(d[i], 'Z') |
Raymond Hettinger | 3743432 | 2016-01-26 21:44:16 -0800 | [diff] [blame] | 319 | else: |
Raymond Hettinger | b00da57 | 2016-02-01 21:19:22 -0800 | [diff] [blame] | 320 | self.assertEqual(d[i-1], 'Z') |
Raymond Hettinger | 3743432 | 2016-01-26 21:44:16 -0800 | [diff] [blame] | 321 | |
Raymond Hettinger | 41290a6 | 2015-03-31 08:12:23 -0700 | [diff] [blame] | 322 | def test_imul(self): |
| 323 | for n in (-10, -1, 0, 1, 2, 10, 1000): |
| 324 | d = deque() |
| 325 | d *= n |
| 326 | self.assertEqual(d, deque()) |
| 327 | self.assertIsNone(d.maxlen) |
| 328 | |
| 329 | for n in (-10, -1, 0, 1, 2, 10, 1000): |
| 330 | d = deque('a') |
| 331 | d *= n |
| 332 | self.assertEqual(d, deque('a' * n)) |
| 333 | self.assertIsNone(d.maxlen) |
| 334 | |
| 335 | for n in (-10, -1, 0, 1, 2, 10, 499, 500, 501, 1000): |
| 336 | d = deque('a', 500) |
| 337 | d *= n |
| 338 | self.assertEqual(d, deque('a' * min(n, 500))) |
| 339 | self.assertEqual(d.maxlen, 500) |
| 340 | |
| 341 | for n in (-10, -1, 0, 1, 2, 10, 1000): |
| 342 | d = deque('abcdef') |
| 343 | d *= n |
| 344 | self.assertEqual(d, deque('abcdef' * n)) |
| 345 | self.assertIsNone(d.maxlen) |
| 346 | |
| 347 | for n in (-10, -1, 0, 1, 2, 10, 499, 500, 501, 1000): |
| 348 | d = deque('abcdef', 500) |
| 349 | d *= n |
| 350 | self.assertEqual(d, deque(('abcdef' * n)[-500:])) |
| 351 | self.assertEqual(d.maxlen, 500) |
| 352 | |
| 353 | def test_mul(self): |
| 354 | d = deque('abc') |
| 355 | self.assertEqual(d * -5, deque()) |
| 356 | self.assertEqual(d * 0, deque()) |
| 357 | self.assertEqual(d * 1, deque('abc')) |
| 358 | self.assertEqual(d * 2, deque('abcabc')) |
| 359 | self.assertEqual(d * 3, deque('abcabcabc')) |
| 360 | self.assertIsNot(d * 1, d) |
| 361 | |
| 362 | self.assertEqual(deque() * 0, deque()) |
| 363 | self.assertEqual(deque() * 1, deque()) |
| 364 | self.assertEqual(deque() * 5, deque()) |
| 365 | |
| 366 | self.assertEqual(-5 * d, deque()) |
| 367 | self.assertEqual(0 * d, deque()) |
| 368 | self.assertEqual(1 * d, deque('abc')) |
| 369 | self.assertEqual(2 * d, deque('abcabc')) |
| 370 | self.assertEqual(3 * d, deque('abcabcabc')) |
| 371 | |
| 372 | d = deque('abc', maxlen=5) |
| 373 | self.assertEqual(d * -5, deque()) |
| 374 | self.assertEqual(d * 0, deque()) |
| 375 | self.assertEqual(d * 1, deque('abc')) |
| 376 | self.assertEqual(d * 2, deque('bcabc')) |
| 377 | self.assertEqual(d * 30, deque('bcabc')) |
| 378 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 379 | def test_setitem(self): |
| 380 | n = 200 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 381 | d = deque(range(n)) |
| 382 | for i in range(n): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 383 | d[i] = 10 * i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 384 | self.assertEqual(list(d), [10*i for i in range(n)]) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 385 | l = list(d) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 386 | for i in range(1-n, 0, -1): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 387 | d[i] = 7*i |
| 388 | l[i] = 7*i |
| 389 | self.assertEqual(list(d), l) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 390 | |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 391 | def test_delitem(self): |
| 392 | n = 500 # O(n**2) test, don't make this too big |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 393 | d = deque(range(n)) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 394 | self.assertRaises(IndexError, d.__delitem__, -n-1) |
| 395 | self.assertRaises(IndexError, d.__delitem__, n) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 396 | for i in range(n): |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 397 | self.assertEqual(len(d), n-i) |
| 398 | j = random.randrange(-len(d), len(d)) |
| 399 | val = d[j] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 400 | self.assertIn(val, d) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 401 | del d[j] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 402 | self.assertNotIn(val, d) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 403 | self.assertEqual(len(d), 0) |
| 404 | |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 405 | def test_reverse(self): |
| 406 | n = 500 # O(n**2) test, don't make this too big |
| 407 | data = [random.random() for i in range(n)] |
| 408 | for i in range(n): |
| 409 | d = deque(data[:i]) |
| 410 | r = d.reverse() |
| 411 | self.assertEqual(list(d), list(reversed(data[:i]))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 412 | self.assertIs(r, None) |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 413 | d.reverse() |
| 414 | self.assertEqual(list(d), data[:i]) |
| 415 | self.assertRaises(TypeError, d.reverse, 1) # Arity is zero |
| 416 | |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 417 | def test_rotate(self): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 418 | s = tuple('abcde') |
| 419 | n = len(s) |
| 420 | |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 421 | d = deque(s) |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 422 | d.rotate(1) # verify rot(1) |
| 423 | self.assertEqual(''.join(d), 'eabcd') |
| 424 | |
| 425 | d = deque(s) |
| 426 | d.rotate(-1) # verify rot(-1) |
| 427 | self.assertEqual(''.join(d), 'bcdea') |
| 428 | d.rotate() # check default to 1 |
| 429 | self.assertEqual(tuple(d), s) |
| 430 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 431 | for i in range(n*3): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 432 | d = deque(s) |
| 433 | e = deque(d) |
| 434 | d.rotate(i) # check vs. rot(1) n times |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 435 | for j in range(i): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 436 | e.rotate(1) |
| 437 | self.assertEqual(tuple(d), tuple(e)) |
| 438 | d.rotate(-i) # check that it works in reverse |
| 439 | self.assertEqual(tuple(d), s) |
| 440 | e.rotate(n-i) # check that it wraps forward |
| 441 | self.assertEqual(tuple(e), s) |
| 442 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 443 | for i in range(n*3): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 444 | d = deque(s) |
| 445 | e = deque(d) |
| 446 | d.rotate(-i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 447 | for j in range(i): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 448 | e.rotate(-1) # check vs. rot(-1) n times |
| 449 | self.assertEqual(tuple(d), tuple(e)) |
| 450 | d.rotate(i) # check that it works in reverse |
| 451 | self.assertEqual(tuple(d), s) |
| 452 | e.rotate(i-n) # check that it wraps backaround |
| 453 | self.assertEqual(tuple(e), s) |
| 454 | |
| 455 | d = deque(s) |
| 456 | e = deque(s) |
| 457 | e.rotate(BIG+17) # verify on long series of rotates |
| 458 | dr = d.rotate |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 459 | for i in range(BIG+17): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 460 | dr() |
| 461 | self.assertEqual(tuple(d), tuple(e)) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 462 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 463 | self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type |
| 464 | self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args |
| 465 | |
| 466 | d = deque() |
| 467 | d.rotate() # rotate an empty deque |
| 468 | self.assertEqual(d, deque()) |
| 469 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 470 | def test_len(self): |
| 471 | d = deque('ab') |
| 472 | self.assertEqual(len(d), 2) |
| 473 | d.popleft() |
| 474 | self.assertEqual(len(d), 1) |
| 475 | d.pop() |
| 476 | self.assertEqual(len(d), 0) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 477 | self.assertRaises(IndexError, d.pop) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 478 | self.assertEqual(len(d), 0) |
| 479 | d.append('c') |
| 480 | self.assertEqual(len(d), 1) |
| 481 | d.appendleft('d') |
| 482 | self.assertEqual(len(d), 2) |
| 483 | d.clear() |
| 484 | self.assertEqual(len(d), 0) |
| 485 | |
| 486 | def test_underflow(self): |
| 487 | d = deque() |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 488 | self.assertRaises(IndexError, d.pop) |
| 489 | self.assertRaises(IndexError, d.popleft) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 490 | |
| 491 | def test_clear(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 492 | d = deque(range(100)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 493 | self.assertEqual(len(d), 100) |
| 494 | d.clear() |
| 495 | self.assertEqual(len(d), 0) |
| 496 | self.assertEqual(list(d), []) |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 497 | d.clear() # clear an empty deque |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 498 | self.assertEqual(list(d), []) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 499 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 500 | def test_remove(self): |
| 501 | d = deque('abcdefghcij') |
| 502 | d.remove('c') |
| 503 | self.assertEqual(d, deque('abdefghcij')) |
| 504 | d.remove('c') |
| 505 | self.assertEqual(d, deque('abdefghij')) |
| 506 | self.assertRaises(ValueError, d.remove, 'c') |
| 507 | self.assertEqual(d, deque('abdefghij')) |
| 508 | |
Walter Dörwald | c448a91 | 2005-03-22 11:22:38 +0000 | [diff] [blame] | 509 | # Handle comparison errors |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 510 | d = deque(['a', 'b', BadCmp(), 'c']) |
| 511 | e = deque(d) |
| 512 | self.assertRaises(RuntimeError, d.remove, 'c') |
| 513 | for x, y in zip(d, e): |
| 514 | # verify that original order and values are retained. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 515 | self.assertTrue(x is y) |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 516 | |
| 517 | # Handle evil mutator |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 518 | for match in (True, False): |
| 519 | d = deque(['ab']) |
| 520 | d.extend([MutateCmp(d, match), 'c']) |
| 521 | self.assertRaises(IndexError, d.remove, 'c') |
| 522 | self.assertEqual(d, deque()) |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 523 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 524 | def test_repr(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 525 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 526 | e = eval(repr(d)) |
| 527 | self.assertEqual(list(d), list(e)) |
| 528 | d.append(d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 529 | self.assertIn('...', repr(d)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 530 | |
| 531 | def test_print(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 532 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 533 | d.append(d) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 534 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 535 | support.unlink(support.TESTFN) |
| 536 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 537 | print(d, file=fo, end='') |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 538 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 539 | fo = open(support.TESTFN, "r") |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 540 | self.assertEqual(fo.read(), repr(d)) |
| 541 | finally: |
| 542 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 543 | support.unlink(support.TESTFN) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 544 | |
| 545 | def test_init(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 546 | self.assertRaises(TypeError, deque, 'abc', 2, 3); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 547 | self.assertRaises(TypeError, deque, 1); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 548 | |
| 549 | def test_hash(self): |
| 550 | self.assertRaises(TypeError, hash, deque('abc')) |
| 551 | |
| 552 | def test_long_steadystate_queue_popleft(self): |
| 553 | for size in (0, 1, 2, 100, 1000): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 554 | d = deque(range(size)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 555 | append, pop = d.append, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 556 | for i in range(size, BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 557 | append(i) |
| 558 | x = pop() |
| 559 | if x != i - size: |
| 560 | self.assertEqual(x, i-size) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 561 | self.assertEqual(list(d), list(range(BIG-size, BIG))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 562 | |
| 563 | def test_long_steadystate_queue_popright(self): |
| 564 | for size in (0, 1, 2, 100, 1000): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 565 | d = deque(reversed(range(size))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 566 | append, pop = d.appendleft, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 567 | for i in range(size, BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 568 | append(i) |
| 569 | x = pop() |
| 570 | if x != i - size: |
| 571 | self.assertEqual(x, i-size) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 572 | self.assertEqual(list(reversed(list(d))), |
| 573 | list(range(BIG-size, BIG))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 574 | |
| 575 | def test_big_queue_popleft(self): |
| 576 | pass |
| 577 | d = deque() |
| 578 | append, pop = d.append, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 579 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 580 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 581 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 582 | x = pop() |
| 583 | if x != i: |
| 584 | self.assertEqual(x, i) |
| 585 | |
| 586 | def test_big_queue_popright(self): |
| 587 | d = deque() |
| 588 | append, pop = d.appendleft, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 589 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 590 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 591 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 592 | x = pop() |
| 593 | if x != i: |
| 594 | self.assertEqual(x, i) |
| 595 | |
| 596 | def test_big_stack_right(self): |
| 597 | d = deque() |
| 598 | append, pop = d.append, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 599 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 600 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 601 | for i in reversed(range(BIG)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 602 | x = pop() |
| 603 | if x != i: |
| 604 | self.assertEqual(x, i) |
| 605 | self.assertEqual(len(d), 0) |
| 606 | |
| 607 | def test_big_stack_left(self): |
| 608 | d = deque() |
| 609 | append, pop = d.appendleft, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 610 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 611 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 612 | for i in reversed(range(BIG)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 613 | x = pop() |
| 614 | if x != i: |
| 615 | self.assertEqual(x, i) |
| 616 | self.assertEqual(len(d), 0) |
| 617 | |
| 618 | def test_roundtrip_iter_init(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 619 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 620 | e = deque(d) |
| 621 | self.assertNotEqual(id(d), id(e)) |
| 622 | self.assertEqual(list(d), list(e)) |
| 623 | |
| 624 | def test_pickle(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 625 | d = deque(range(200)) |
Hirokazu Yamamoto | 801f9d3 | 2008-12-27 04:21:44 +0000 | [diff] [blame] | 626 | for i in range(pickle.HIGHEST_PROTOCOL + 1): |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 627 | s = pickle.dumps(d, i) |
| 628 | e = pickle.loads(s) |
| 629 | self.assertNotEqual(id(d), id(e)) |
| 630 | self.assertEqual(list(d), list(e)) |
| 631 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 632 | ## def test_pickle_recursive(self): |
| 633 | ## d = deque('abc') |
| 634 | ## d.append(d) |
Hirokazu Yamamoto | 801f9d3 | 2008-12-27 04:21:44 +0000 | [diff] [blame] | 635 | ## for i in range(pickle.HIGHEST_PROTOCOL + 1): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 636 | ## e = pickle.loads(pickle.dumps(d, i)) |
| 637 | ## self.assertNotEqual(id(d), id(e)) |
| 638 | ## self.assertEqual(id(e), id(e[-1])) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 639 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 640 | def test_iterator_pickle(self): |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 641 | orig = deque(range(200)) |
| 642 | data = [i*1.01 for i in orig] |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 643 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 644 | # initial iterator |
| 645 | itorg = iter(orig) |
| 646 | dump = pickle.dumps((itorg, orig), proto) |
| 647 | it, d = pickle.loads(dump) |
| 648 | for i, x in enumerate(data): |
| 649 | d[i] = x |
| 650 | self.assertEqual(type(it), type(itorg)) |
| 651 | self.assertEqual(list(it), data) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 652 | |
Serhiy Storchaka | aabafe7 | 2016-03-06 14:10:24 +0200 | [diff] [blame] | 653 | # running iterator |
| 654 | next(itorg) |
| 655 | dump = pickle.dumps((itorg, orig), proto) |
| 656 | it, d = pickle.loads(dump) |
| 657 | for i, x in enumerate(data): |
| 658 | d[i] = x |
| 659 | self.assertEqual(type(it), type(itorg)) |
| 660 | self.assertEqual(list(it), data[1:]) |
| 661 | |
| 662 | # empty iterator |
| 663 | for i in range(1, len(data)): |
| 664 | next(itorg) |
| 665 | dump = pickle.dumps((itorg, orig), proto) |
| 666 | it, d = pickle.loads(dump) |
| 667 | for i, x in enumerate(data): |
| 668 | d[i] = x |
| 669 | self.assertEqual(type(it), type(itorg)) |
| 670 | self.assertEqual(list(it), []) |
| 671 | |
| 672 | # exhausted iterator |
| 673 | self.assertRaises(StopIteration, next, itorg) |
| 674 | dump = pickle.dumps((itorg, orig), proto) |
| 675 | it, d = pickle.loads(dump) |
| 676 | for i, x in enumerate(data): |
| 677 | d[i] = x |
| 678 | self.assertEqual(type(it), type(itorg)) |
| 679 | self.assertEqual(list(it), []) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 680 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 681 | def test_deepcopy(self): |
| 682 | mut = [10] |
| 683 | d = deque([mut]) |
| 684 | e = copy.deepcopy(d) |
| 685 | self.assertEqual(list(d), list(e)) |
| 686 | mut[0] = 11 |
| 687 | self.assertNotEqual(id(d), id(e)) |
| 688 | self.assertNotEqual(list(d), list(e)) |
| 689 | |
| 690 | def test_copy(self): |
| 691 | mut = [10] |
| 692 | d = deque([mut]) |
| 693 | e = copy.copy(d) |
| 694 | self.assertEqual(list(d), list(e)) |
| 695 | mut[0] = 11 |
| 696 | self.assertNotEqual(id(d), id(e)) |
| 697 | self.assertEqual(list(d), list(e)) |
| 698 | |
Raymond Hettinger | 32ea165 | 2015-03-21 01:37:37 -0700 | [diff] [blame] | 699 | def test_copy_method(self): |
| 700 | mut = [10] |
| 701 | d = deque([mut]) |
| 702 | e = d.copy() |
| 703 | self.assertEqual(list(d), list(e)) |
| 704 | mut[0] = 11 |
| 705 | self.assertNotEqual(id(d), id(e)) |
| 706 | self.assertEqual(list(d), list(e)) |
| 707 | |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 708 | def test_reversed(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 709 | for s in ('abcd', range(2000)): |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 710 | self.assertEqual(list(reversed(deque(s))), list(reversed(s))) |
| 711 | |
Raymond Hettinger | badf5d8 | 2014-06-14 20:41:22 -0700 | [diff] [blame] | 712 | def test_reversed_new(self): |
| 713 | klass = type(reversed(deque())) |
| 714 | for s in ('abcd', range(2000)): |
| 715 | self.assertEqual(list(klass(deque(s))), list(reversed(s))) |
| 716 | |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 717 | def test_gc_doesnt_blowup(self): |
| 718 | import gc |
| 719 | # This used to assert-fail in deque_traverse() under a debug |
| 720 | # build, or run wild with a NULL pointer in a release build. |
| 721 | d = deque() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 722 | for i in range(100): |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 723 | d.append(1) |
| 724 | gc.collect() |
| 725 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 726 | def test_container_iterator(self): |
| 727 | # Bug #3680: tp_traverse was not implemented for deque iterator objects |
| 728 | class C(object): |
| 729 | pass |
| 730 | for i in range(2): |
| 731 | obj = C() |
| 732 | ref = weakref.ref(obj) |
| 733 | if i == 0: |
| 734 | container = deque([obj, 1]) |
| 735 | else: |
| 736 | container = reversed(deque([obj, 1])) |
| 737 | obj.x = iter(container) |
| 738 | del obj, container |
| 739 | gc.collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 740 | self.assertTrue(ref() is None, "Cycle was not collected") |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 741 | |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 742 | check_sizeof = support.check_sizeof |
| 743 | |
| 744 | @support.cpython_only |
| 745 | def test_sizeof(self): |
Raymond Hettinger | daf57f2 | 2015-02-26 23:21:29 -0800 | [diff] [blame] | 746 | BLOCKLEN = 64 |
Serhiy Storchaka | e23c90c | 2016-05-18 13:00:56 +0300 | [diff] [blame] | 747 | basesize = support.calcvobjsize('2P4nP') |
| 748 | blocksize = struct.calcsize('P%dPP' % BLOCKLEN) |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 749 | self.assertEqual(object.__sizeof__(deque()), basesize) |
| 750 | check = self.check_sizeof |
| 751 | check(deque(), basesize + blocksize) |
| 752 | check(deque('a'), basesize + blocksize) |
Raymond Hettinger | d9c116c | 2013-07-09 00:13:21 -0700 | [diff] [blame] | 753 | check(deque('a' * (BLOCKLEN - 1)), basesize + blocksize) |
| 754 | check(deque('a' * BLOCKLEN), basesize + 2 * blocksize) |
Jesus Cea | 16e2fca | 2012-08-03 14:49:42 +0200 | [diff] [blame] | 755 | check(deque('a' * (42 * BLOCKLEN)), basesize + 43 * blocksize) |
| 756 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 757 | class TestVariousIteratorArgs(unittest.TestCase): |
| 758 | |
| 759 | def test_constructor(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 760 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 761 | for g in (seq_tests.Sequence, seq_tests.IterFunc, |
| 762 | seq_tests.IterGen, seq_tests.IterFuncStop, |
| 763 | seq_tests.itermulti, seq_tests.iterfunc): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 764 | self.assertEqual(list(deque(g(s))), list(g(s))) |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 765 | self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s)) |
| 766 | self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s)) |
| 767 | self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 768 | |
| 769 | def test_iter_with_altered_data(self): |
| 770 | d = deque('abcdefg') |
| 771 | it = iter(d) |
| 772 | d.pop() |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 773 | self.assertRaises(RuntimeError, next, it) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 774 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 775 | def test_runtime_error_on_empty_deque(self): |
| 776 | d = deque() |
| 777 | it = iter(d) |
| 778 | d.append(10) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 779 | self.assertRaises(RuntimeError, next, it) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 780 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 781 | class Deque(deque): |
| 782 | pass |
| 783 | |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 784 | class DequeWithBadIter(deque): |
| 785 | def __iter__(self): |
| 786 | raise TypeError |
| 787 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 788 | class TestSubclass(unittest.TestCase): |
| 789 | |
| 790 | def test_basics(self): |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 791 | d = Deque(range(25)) |
| 792 | d.__init__(range(200)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 793 | for i in range(200, 400): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 794 | d.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 795 | for i in reversed(range(-200, 0)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 796 | d.appendleft(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 797 | self.assertEqual(list(d), list(range(-200, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 798 | self.assertEqual(len(d), 600) |
| 799 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 800 | left = [d.popleft() for i in range(250)] |
| 801 | self.assertEqual(left, list(range(-200, 50))) |
| 802 | self.assertEqual(list(d), list(range(50, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 804 | right = [d.pop() for i in range(250)] |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 805 | right.reverse() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 806 | self.assertEqual(right, list(range(150, 400))) |
| 807 | self.assertEqual(list(d), list(range(50, 150))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 808 | |
| 809 | d.clear() |
| 810 | self.assertEqual(len(d), 0) |
| 811 | |
| 812 | def test_copy_pickle(self): |
| 813 | |
| 814 | d = Deque('abc') |
| 815 | |
| 816 | e = d.__copy__() |
| 817 | self.assertEqual(type(d), type(e)) |
| 818 | self.assertEqual(list(d), list(e)) |
| 819 | |
| 820 | e = Deque(d) |
| 821 | self.assertEqual(type(d), type(e)) |
| 822 | self.assertEqual(list(d), list(e)) |
| 823 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 824 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 825 | s = pickle.dumps(d, proto) |
| 826 | e = pickle.loads(s) |
| 827 | self.assertNotEqual(id(d), id(e)) |
| 828 | self.assertEqual(type(d), type(e)) |
| 829 | self.assertEqual(list(d), list(e)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 831 | d = Deque('abcde', maxlen=4) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 832 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 833 | e = d.__copy__() |
| 834 | self.assertEqual(type(d), type(e)) |
| 835 | self.assertEqual(list(d), list(e)) |
| 836 | |
| 837 | e = Deque(d) |
| 838 | self.assertEqual(type(d), type(e)) |
| 839 | self.assertEqual(list(d), list(e)) |
| 840 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame] | 841 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 842 | s = pickle.dumps(d, proto) |
| 843 | e = pickle.loads(s) |
| 844 | self.assertNotEqual(id(d), id(e)) |
| 845 | self.assertEqual(type(d), type(e)) |
| 846 | self.assertEqual(list(d), list(e)) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 848 | ## def test_pickle(self): |
| 849 | ## d = Deque('abc') |
| 850 | ## d.append(d) |
| 851 | ## |
| 852 | ## e = pickle.loads(pickle.dumps(d)) |
| 853 | ## self.assertNotEqual(id(d), id(e)) |
| 854 | ## self.assertEqual(type(d), type(e)) |
| 855 | ## dd = d.pop() |
| 856 | ## ee = e.pop() |
| 857 | ## self.assertEqual(id(e), id(ee)) |
| 858 | ## self.assertEqual(d, e) |
| 859 | ## |
| 860 | ## d.x = d |
| 861 | ## e = pickle.loads(pickle.dumps(d)) |
| 862 | ## self.assertEqual(id(e), id(e.x)) |
| 863 | ## |
| 864 | ## d = DequeWithBadIter('abc') |
| 865 | ## self.assertRaises(TypeError, pickle.dumps, d) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 866 | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 867 | def test_weakref(self): |
| 868 | d = deque('gallahad') |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 869 | p = weakref.proxy(d) |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 870 | self.assertEqual(str(p), str(d)) |
| 871 | d = None |
| 872 | self.assertRaises(ReferenceError, str, p) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 873 | |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 874 | def test_strange_subclass(self): |
| 875 | class X(deque): |
| 876 | def __iter__(self): |
| 877 | return iter([]) |
| 878 | d1 = X([1,2,3]) |
| 879 | d2 = X([4,5,6]) |
| 880 | d1 == d2 # not clear if this is supposed to be True or False, |
| 881 | # but it used to give a SystemError |
| 882 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 883 | |
| 884 | class SubclassWithKwargs(deque): |
| 885 | def __init__(self, newarg=1): |
| 886 | deque.__init__(self) |
| 887 | |
| 888 | class TestSubclassWithKwargs(unittest.TestCase): |
| 889 | def test_subclass_with_kwargs(self): |
| 890 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 891 | SubclassWithKwargs(newarg=1) |
| 892 | |
Raymond Hettinger | 067bbba | 2015-04-01 08:11:09 -0700 | [diff] [blame] | 893 | class TestSequence(seq_tests.CommonTest): |
| 894 | type2test = deque |
| 895 | |
| 896 | def test_getitem(self): |
| 897 | # For now, bypass tests that require slicing |
| 898 | pass |
| 899 | |
| 900 | def test_getslice(self): |
| 901 | # For now, bypass tests that require slicing |
| 902 | pass |
| 903 | |
| 904 | def test_subscript(self): |
| 905 | # For now, bypass tests that require slicing |
| 906 | pass |
| 907 | |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 908 | def test_free_after_iterating(self): |
| 909 | # For now, bypass tests that require slicing |
| 910 | self.skipTest("Exhausted deque iterator doesn't free a deque") |
| 911 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 912 | #============================================================================== |
| 913 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 914 | libreftest = """ |
| 915 | Example from the Library Reference: Doc/lib/libcollections.tex |
| 916 | |
| 917 | >>> from collections import deque |
| 918 | >>> d = deque('ghi') # make a new deque with three items |
| 919 | >>> for elem in d: # iterate over the deque's elements |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 920 | ... print(elem.upper()) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 921 | G |
| 922 | H |
| 923 | I |
| 924 | >>> d.append('j') # add a new entry to the right side |
| 925 | >>> d.appendleft('f') # add a new entry to the left side |
| 926 | >>> d # show the representation of the deque |
| 927 | deque(['f', 'g', 'h', 'i', 'j']) |
| 928 | >>> d.pop() # return and remove the rightmost item |
| 929 | 'j' |
| 930 | >>> d.popleft() # return and remove the leftmost item |
| 931 | 'f' |
| 932 | >>> list(d) # list the contents of the deque |
| 933 | ['g', 'h', 'i'] |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 934 | >>> d[0] # peek at leftmost item |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 935 | 'g' |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 936 | >>> d[-1] # peek at rightmost item |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 937 | 'i' |
| 938 | >>> list(reversed(d)) # list the contents of a deque in reverse |
| 939 | ['i', 'h', 'g'] |
| 940 | >>> 'h' in d # search the deque |
| 941 | True |
| 942 | >>> d.extend('jkl') # add multiple elements at once |
| 943 | >>> d |
| 944 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 945 | >>> d.rotate(1) # right rotation |
| 946 | >>> d |
| 947 | deque(['l', 'g', 'h', 'i', 'j', 'k']) |
| 948 | >>> d.rotate(-1) # left rotation |
| 949 | >>> d |
| 950 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 951 | >>> deque(reversed(d)) # make a new deque in reverse order |
| 952 | deque(['l', 'k', 'j', 'i', 'h', 'g']) |
| 953 | >>> d.clear() # empty the deque |
| 954 | >>> d.pop() # cannot pop from an empty deque |
| 955 | Traceback (most recent call last): |
| 956 | File "<pyshell#6>", line 1, in -toplevel- |
| 957 | d.pop() |
| 958 | IndexError: pop from an empty deque |
| 959 | |
| 960 | >>> d.extendleft('abc') # extendleft() reverses the input order |
| 961 | >>> d |
| 962 | deque(['c', 'b', 'a']) |
| 963 | |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 964 | |
| 965 | |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 966 | >>> def delete_nth(d, n): |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 967 | ... d.rotate(-n) |
| 968 | ... d.popleft() |
| 969 | ... d.rotate(n) |
| 970 | ... |
| 971 | >>> d = deque('abcdef') |
| 972 | >>> delete_nth(d, 2) # remove the entry at d[2] |
| 973 | >>> d |
| 974 | deque(['a', 'b', 'd', 'e', 'f']) |
| 975 | |
| 976 | |
| 977 | |
| 978 | >>> def roundrobin(*iterables): |
| 979 | ... pending = deque(iter(i) for i in iterables) |
| 980 | ... while pending: |
| 981 | ... task = pending.popleft() |
| 982 | ... try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 983 | ... yield next(task) |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 984 | ... except StopIteration: |
| 985 | ... continue |
| 986 | ... pending.append(task) |
| 987 | ... |
| 988 | |
| 989 | >>> for value in roundrobin('abc', 'd', 'efgh'): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 990 | ... print(value) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 991 | ... |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 992 | a |
| 993 | d |
| 994 | e |
| 995 | b |
| 996 | f |
| 997 | c |
| 998 | g |
| 999 | h |
| 1000 | |
| 1001 | |
| 1002 | >>> def maketree(iterable): |
| 1003 | ... d = deque(iterable) |
| 1004 | ... while len(d) > 1: |
| 1005 | ... pair = [d.popleft(), d.popleft()] |
| 1006 | ... d.append(pair) |
| 1007 | ... return list(d) |
| 1008 | ... |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1009 | >>> print(maketree('abcdefgh')) |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 1010 | [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] |
| 1011 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1012 | """ |
| 1013 | |
| 1014 | |
| 1015 | #============================================================================== |
| 1016 | |
| 1017 | __test__ = {'libreftest' : libreftest} |
| 1018 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1019 | def test_main(verbose=None): |
| 1020 | import sys |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1021 | test_classes = ( |
| 1022 | TestBasic, |
| 1023 | TestVariousIteratorArgs, |
| 1024 | TestSubclass, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1025 | TestSubclassWithKwargs, |
Raymond Hettinger | 067bbba | 2015-04-01 08:11:09 -0700 | [diff] [blame] | 1026 | TestSequence, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1027 | ) |
| 1028 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1029 | support.run_unittest(*test_classes) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1030 | |
| 1031 | # verify reference counting |
| 1032 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 1033 | import gc |
| 1034 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1035 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1036 | support.run_unittest(*test_classes) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1037 | gc.collect() |
| 1038 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1039 | print(counts) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 1040 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 1041 | # doctests |
| 1042 | from test import test_deque |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1043 | support.run_doctest(test_deque, verbose) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 1044 | |
| 1045 | if __name__ == "__main__": |
| 1046 | test_main(verbose=True) |