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 |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 10 | |
| 11 | BIG = 100000 |
| 12 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 13 | def fail(): |
| 14 | raise SyntaxError |
| 15 | yield 1 |
| 16 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 17 | class BadCmp: |
| 18 | def __eq__(self, other): |
| 19 | raise RuntimeError |
| 20 | |
| 21 | class MutateCmp: |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 22 | def __init__(self, deque, result): |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 23 | self.deque = deque |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 24 | self.result = result |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 25 | def __eq__(self, other): |
| 26 | self.deque.clear() |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 27 | return self.result |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 28 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 29 | class TestBasic(unittest.TestCase): |
| 30 | |
| 31 | def test_basics(self): |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 32 | d = deque(range(-5125, -5000)) |
| 33 | d.__init__(range(200)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 34 | for i in range(200, 400): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 35 | d.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 36 | for i in reversed(range(-200, 0)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 37 | d.appendleft(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 38 | self.assertEqual(list(d), list(range(-200, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 39 | self.assertEqual(len(d), 600) |
| 40 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 41 | left = [d.popleft() for i in range(250)] |
| 42 | self.assertEqual(left, list(range(-200, 50))) |
| 43 | self.assertEqual(list(d), list(range(50, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 45 | right = [d.pop() for i in range(250)] |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 46 | right.reverse() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 47 | self.assertEqual(right, list(range(150, 400))) |
| 48 | self.assertEqual(list(d), list(range(50, 150))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 50 | def test_maxlen(self): |
| 51 | self.assertRaises(ValueError, deque, 'abc', -1) |
| 52 | self.assertRaises(ValueError, deque, 'abc', -2) |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 53 | it = iter(range(10)) |
| 54 | d = deque(it, maxlen=3) |
| 55 | self.assertEqual(list(it), []) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 56 | self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)') |
| 57 | self.assertEqual(list(d), [7, 8, 9]) |
| 58 | self.assertEqual(d, deque(range(10), 3)) |
| 59 | d.append(10) |
| 60 | self.assertEqual(list(d), [8, 9, 10]) |
| 61 | d.appendleft(7) |
| 62 | self.assertEqual(list(d), [7, 8, 9]) |
| 63 | d.extend([10, 11]) |
| 64 | self.assertEqual(list(d), [9, 10, 11]) |
| 65 | d.extendleft([8, 7]) |
| 66 | self.assertEqual(list(d), [7, 8, 9]) |
| 67 | d = deque(range(200), maxlen=10) |
| 68 | d.append(d) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 69 | support.unlink(support.TESTFN) |
| 70 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 71 | try: |
| 72 | fo.write(str(d)) |
| 73 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 74 | fo = open(support.TESTFN, "r") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 75 | self.assertEqual(fo.read(), repr(d)) |
| 76 | finally: |
| 77 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 78 | support.unlink(support.TESTFN) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 80 | d = deque(range(10), maxlen=None) |
| 81 | 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] | 82 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 83 | try: |
| 84 | fo.write(str(d)) |
| 85 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 86 | fo = open(support.TESTFN, "r") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 87 | self.assertEqual(fo.read(), repr(d)) |
| 88 | finally: |
| 89 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 90 | support.unlink(support.TESTFN) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 91 | |
Raymond Hettinger | 060c7f6 | 2009-03-10 09:36:07 +0000 | [diff] [blame] | 92 | def test_maxlen_zero(self): |
| 93 | it = iter(range(100)) |
| 94 | deque(it, maxlen=0) |
| 95 | self.assertEqual(list(it), []) |
| 96 | |
| 97 | it = iter(range(100)) |
| 98 | d = deque(maxlen=0) |
| 99 | d.extend(it) |
| 100 | self.assertEqual(list(it), []) |
| 101 | |
| 102 | it = iter(range(100)) |
| 103 | d = deque(maxlen=0) |
| 104 | d.extendleft(it) |
| 105 | self.assertEqual(list(it), []) |
| 106 | |
Raymond Hettinger | 5bb0f0e | 2009-03-10 12:56:32 +0000 | [diff] [blame] | 107 | def test_maxlen_attribute(self): |
| 108 | self.assertEqual(deque().maxlen, None) |
| 109 | self.assertEqual(deque('abc').maxlen, None) |
| 110 | self.assertEqual(deque('abc', maxlen=4).maxlen, 4) |
| 111 | self.assertEqual(deque('abc', maxlen=2).maxlen, 2) |
| 112 | self.assertEqual(deque('abc', maxlen=0).maxlen, 0) |
| 113 | with self.assertRaises(AttributeError): |
| 114 | d = deque('abc') |
| 115 | d.maxlen = 10 |
| 116 | |
Raymond Hettinger | 44459de | 2010-04-03 23:20:46 +0000 | [diff] [blame] | 117 | def test_count(self): |
| 118 | for s in ('', 'abracadabra', 'simsalabim'*500+'abc'): |
| 119 | s = list(s) |
| 120 | d = deque(s) |
| 121 | for letter in 'abcdefghijklmnopqrstuvwxyz': |
| 122 | self.assertEqual(s.count(letter), d.count(letter), (s, d, letter)) |
| 123 | self.assertRaises(TypeError, d.count) # too few args |
| 124 | self.assertRaises(TypeError, d.count, 1, 2) # too many args |
| 125 | class BadCompare: |
| 126 | def __eq__(self, other): |
| 127 | raise ArithmeticError |
| 128 | d = deque([1, 2, BadCompare(), 3]) |
| 129 | self.assertRaises(ArithmeticError, d.count, 2) |
| 130 | d = deque([1, 2, 3]) |
| 131 | self.assertRaises(ArithmeticError, d.count, BadCompare()) |
| 132 | class MutatingCompare: |
| 133 | def __eq__(self, other): |
| 134 | self.d.pop() |
| 135 | return True |
| 136 | m = MutatingCompare() |
| 137 | d = deque([1, 2, 3, m, 4, 5]) |
| 138 | m.d = d |
| 139 | self.assertRaises(RuntimeError, d.count, 3) |
| 140 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 141 | def test_comparisons(self): |
| 142 | d = deque('xabc'); d.popleft() |
| 143 | for e in [d, deque('abc'), deque('ab'), deque(), list(d)]: |
| 144 | self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e)) |
| 145 | self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e))) |
| 146 | |
| 147 | args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba')) |
| 148 | for x in args: |
| 149 | for y in args: |
| 150 | self.assertEqual(x == y, list(x) == list(y), (x,y)) |
| 151 | self.assertEqual(x != y, list(x) != list(y), (x,y)) |
| 152 | self.assertEqual(x < y, list(x) < list(y), (x,y)) |
| 153 | self.assertEqual(x <= y, list(x) <= list(y), (x,y)) |
| 154 | self.assertEqual(x > y, list(x) > list(y), (x,y)) |
| 155 | self.assertEqual(x >= y, list(x) >= list(y), (x,y)) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 156 | |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 157 | def test_extend(self): |
| 158 | d = deque('a') |
| 159 | self.assertRaises(TypeError, d.extend, 1) |
| 160 | d.extend('bcd') |
| 161 | self.assertEqual(list(d), list('abcd')) |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 162 | d.extend(d) |
| 163 | self.assertEqual(list(d), list('abcdabcd')) |
| 164 | |
| 165 | def test_iadd(self): |
| 166 | d = deque('a') |
| 167 | d += 'bcd' |
| 168 | self.assertEqual(list(d), list('abcd')) |
| 169 | d += d |
| 170 | self.assertEqual(list(d), list('abcdabcd')) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 171 | |
| 172 | def test_extendleft(self): |
| 173 | d = deque('a') |
| 174 | self.assertRaises(TypeError, d.extendleft, 1) |
| 175 | d.extendleft('bcd') |
| 176 | self.assertEqual(list(d), list(reversed('abcd'))) |
Raymond Hettinger | 3f9afd8 | 2009-12-10 03:03:02 +0000 | [diff] [blame] | 177 | d.extendleft(d) |
| 178 | self.assertEqual(list(d), list('abcddcba')) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 179 | d = deque() |
| 180 | d.extendleft(range(1000)) |
| 181 | self.assertEqual(list(d), list(reversed(range(1000)))) |
| 182 | self.assertRaises(SyntaxError, d.extendleft, fail()) |
Raymond Hettinger | 3ba85c2 | 2004-02-06 19:04:56 +0000 | [diff] [blame] | 183 | |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 184 | def test_getitem(self): |
| 185 | n = 200 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 186 | d = deque(range(n)) |
| 187 | l = list(range(n)) |
| 188 | for i in range(n): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 189 | d.popleft() |
| 190 | l.pop(0) |
| 191 | if random.random() < 0.5: |
| 192 | d.append(i) |
| 193 | l.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 194 | for j in range(1-len(l), len(l)): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 195 | assert d[j] == l[j] |
| 196 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 197 | d = deque('superman') |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 198 | self.assertEqual(d[0], 's') |
| 199 | self.assertEqual(d[-1], 'n') |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 200 | d = deque() |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 201 | self.assertRaises(IndexError, d.__getitem__, 0) |
| 202 | self.assertRaises(IndexError, d.__getitem__, -1) |
| 203 | |
| 204 | def test_setitem(self): |
| 205 | n = 200 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 206 | d = deque(range(n)) |
| 207 | for i in range(n): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 208 | d[i] = 10 * i |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 209 | self.assertEqual(list(d), [10*i for i in range(n)]) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 210 | l = list(d) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 211 | for i in range(1-n, 0, -1): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 212 | d[i] = 7*i |
| 213 | l[i] = 7*i |
| 214 | self.assertEqual(list(d), l) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 215 | |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 216 | def test_delitem(self): |
| 217 | 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] | 218 | d = deque(range(n)) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 219 | self.assertRaises(IndexError, d.__delitem__, -n-1) |
| 220 | self.assertRaises(IndexError, d.__delitem__, n) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 221 | for i in range(n): |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 222 | self.assertEqual(len(d), n-i) |
| 223 | j = random.randrange(-len(d), len(d)) |
| 224 | val = d[j] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 225 | self.assertIn(val, d) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 226 | del d[j] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 227 | self.assertNotIn(val, d) |
Raymond Hettinger | 0e371f2 | 2004-05-12 20:55:56 +0000 | [diff] [blame] | 228 | self.assertEqual(len(d), 0) |
| 229 | |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 230 | def test_reverse(self): |
| 231 | n = 500 # O(n**2) test, don't make this too big |
| 232 | data = [random.random() for i in range(n)] |
| 233 | for i in range(n): |
| 234 | d = deque(data[:i]) |
| 235 | r = d.reverse() |
| 236 | self.assertEqual(list(d), list(reversed(data[:i]))) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 237 | self.assertIs(r, None) |
Raymond Hettinger | e5fdedb | 2009-12-10 00:47:21 +0000 | [diff] [blame] | 238 | d.reverse() |
| 239 | self.assertEqual(list(d), data[:i]) |
| 240 | self.assertRaises(TypeError, d.reverse, 1) # Arity is zero |
| 241 | |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 242 | def test_rotate(self): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 243 | s = tuple('abcde') |
| 244 | n = len(s) |
| 245 | |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 246 | d = deque(s) |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 247 | d.rotate(1) # verify rot(1) |
| 248 | self.assertEqual(''.join(d), 'eabcd') |
| 249 | |
| 250 | d = deque(s) |
| 251 | d.rotate(-1) # verify rot(-1) |
| 252 | self.assertEqual(''.join(d), 'bcdea') |
| 253 | d.rotate() # check default to 1 |
| 254 | self.assertEqual(tuple(d), s) |
| 255 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 256 | for i in range(n*3): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 257 | d = deque(s) |
| 258 | e = deque(d) |
| 259 | d.rotate(i) # check vs. rot(1) n times |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 260 | for j in range(i): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 261 | e.rotate(1) |
| 262 | self.assertEqual(tuple(d), tuple(e)) |
| 263 | d.rotate(-i) # check that it works in reverse |
| 264 | self.assertEqual(tuple(d), s) |
| 265 | e.rotate(n-i) # check that it wraps forward |
| 266 | self.assertEqual(tuple(e), s) |
| 267 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 268 | for i in range(n*3): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 269 | d = deque(s) |
| 270 | e = deque(d) |
| 271 | d.rotate(-i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 272 | for j in range(i): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 273 | e.rotate(-1) # check vs. rot(-1) n times |
| 274 | self.assertEqual(tuple(d), tuple(e)) |
| 275 | d.rotate(i) # check that it works in reverse |
| 276 | self.assertEqual(tuple(d), s) |
| 277 | e.rotate(i-n) # check that it wraps backaround |
| 278 | self.assertEqual(tuple(e), s) |
| 279 | |
| 280 | d = deque(s) |
| 281 | e = deque(s) |
| 282 | e.rotate(BIG+17) # verify on long series of rotates |
| 283 | dr = d.rotate |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 284 | for i in range(BIG+17): |
Raymond Hettinger | ee33b27 | 2004-02-08 04:05:26 +0000 | [diff] [blame] | 285 | dr() |
| 286 | self.assertEqual(tuple(d), tuple(e)) |
Raymond Hettinger | 5c5eb86 | 2004-02-07 21:13:00 +0000 | [diff] [blame] | 287 | |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 288 | self.assertRaises(TypeError, d.rotate, 'x') # Wrong arg type |
| 289 | self.assertRaises(TypeError, d.rotate, 1, 10) # Too many args |
| 290 | |
| 291 | d = deque() |
| 292 | d.rotate() # rotate an empty deque |
| 293 | self.assertEqual(d, deque()) |
| 294 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 295 | def test_len(self): |
| 296 | d = deque('ab') |
| 297 | self.assertEqual(len(d), 2) |
| 298 | d.popleft() |
| 299 | self.assertEqual(len(d), 1) |
| 300 | d.pop() |
| 301 | self.assertEqual(len(d), 0) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 302 | self.assertRaises(IndexError, d.pop) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 303 | self.assertEqual(len(d), 0) |
| 304 | d.append('c') |
| 305 | self.assertEqual(len(d), 1) |
| 306 | d.appendleft('d') |
| 307 | self.assertEqual(len(d), 2) |
| 308 | d.clear() |
| 309 | self.assertEqual(len(d), 0) |
| 310 | |
| 311 | def test_underflow(self): |
| 312 | d = deque() |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 313 | self.assertRaises(IndexError, d.pop) |
| 314 | self.assertRaises(IndexError, d.popleft) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 315 | |
| 316 | def test_clear(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 317 | d = deque(range(100)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 318 | self.assertEqual(len(d), 100) |
| 319 | d.clear() |
| 320 | self.assertEqual(len(d), 0) |
| 321 | self.assertEqual(list(d), []) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 322 | d.clear() # clear an emtpy deque |
| 323 | self.assertEqual(list(d), []) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 324 | |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 325 | def test_remove(self): |
| 326 | d = deque('abcdefghcij') |
| 327 | d.remove('c') |
| 328 | self.assertEqual(d, deque('abdefghcij')) |
| 329 | d.remove('c') |
| 330 | self.assertEqual(d, deque('abdefghij')) |
| 331 | self.assertRaises(ValueError, d.remove, 'c') |
| 332 | self.assertEqual(d, deque('abdefghij')) |
| 333 | |
Walter Dörwald | c448a91 | 2005-03-22 11:22:38 +0000 | [diff] [blame] | 334 | # Handle comparison errors |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 335 | d = deque(['a', 'b', BadCmp(), 'c']) |
| 336 | e = deque(d) |
| 337 | self.assertRaises(RuntimeError, d.remove, 'c') |
| 338 | for x, y in zip(d, e): |
| 339 | # verify that original order and values are retained. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 340 | self.assertTrue(x is y) |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 341 | |
| 342 | # Handle evil mutator |
Raymond Hettinger | d73202c | 2005-03-19 00:00:51 +0000 | [diff] [blame] | 343 | for match in (True, False): |
| 344 | d = deque(['ab']) |
| 345 | d.extend([MutateCmp(d, match), 'c']) |
| 346 | self.assertRaises(IndexError, d.remove, 'c') |
| 347 | self.assertEqual(d, deque()) |
Raymond Hettinger | 4aec61e | 2005-03-18 21:20:23 +0000 | [diff] [blame] | 348 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 349 | def test_repr(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 350 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 351 | e = eval(repr(d)) |
| 352 | self.assertEqual(list(d), list(e)) |
| 353 | d.append(d) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 354 | self.assertIn('...', repr(d)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 355 | |
| 356 | def test_print(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 357 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 358 | d.append(d) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 359 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 360 | support.unlink(support.TESTFN) |
| 361 | fo = open(support.TESTFN, "w") |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 362 | print(d, file=fo, end='') |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 363 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 364 | fo = open(support.TESTFN, "r") |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 365 | self.assertEqual(fo.read(), repr(d)) |
| 366 | finally: |
| 367 | fo.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 368 | support.unlink(support.TESTFN) |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 369 | |
| 370 | def test_init(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 371 | self.assertRaises(TypeError, deque, 'abc', 2, 3); |
Raymond Hettinger | a435c53 | 2004-07-09 04:10:20 +0000 | [diff] [blame] | 372 | self.assertRaises(TypeError, deque, 1); |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 373 | |
| 374 | def test_hash(self): |
| 375 | self.assertRaises(TypeError, hash, deque('abc')) |
| 376 | |
| 377 | def test_long_steadystate_queue_popleft(self): |
| 378 | for size in (0, 1, 2, 100, 1000): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 379 | d = deque(range(size)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 380 | append, pop = d.append, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 381 | for i in range(size, BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 382 | append(i) |
| 383 | x = pop() |
| 384 | if x != i - size: |
| 385 | self.assertEqual(x, i-size) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 386 | self.assertEqual(list(d), list(range(BIG-size, BIG))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 387 | |
| 388 | def test_long_steadystate_queue_popright(self): |
| 389 | for size in (0, 1, 2, 100, 1000): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 390 | d = deque(reversed(range(size))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 391 | append, pop = d.appendleft, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 392 | for i in range(size, BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 393 | append(i) |
| 394 | x = pop() |
| 395 | if x != i - size: |
| 396 | self.assertEqual(x, i-size) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 397 | self.assertEqual(list(reversed(list(d))), |
| 398 | list(range(BIG-size, BIG))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 399 | |
| 400 | def test_big_queue_popleft(self): |
| 401 | pass |
| 402 | d = deque() |
| 403 | append, pop = d.append, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 404 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 405 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 406 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 407 | x = pop() |
| 408 | if x != i: |
| 409 | self.assertEqual(x, i) |
| 410 | |
| 411 | def test_big_queue_popright(self): |
| 412 | d = deque() |
| 413 | append, pop = d.appendleft, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 414 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 415 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 416 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 417 | x = pop() |
| 418 | if x != i: |
| 419 | self.assertEqual(x, i) |
| 420 | |
| 421 | def test_big_stack_right(self): |
| 422 | d = deque() |
| 423 | append, pop = d.append, d.pop |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 424 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 425 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 426 | for i in reversed(range(BIG)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 427 | x = pop() |
| 428 | if x != i: |
| 429 | self.assertEqual(x, i) |
| 430 | self.assertEqual(len(d), 0) |
| 431 | |
| 432 | def test_big_stack_left(self): |
| 433 | d = deque() |
| 434 | append, pop = d.appendleft, d.popleft |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 435 | for i in range(BIG): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 436 | append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 437 | for i in reversed(range(BIG)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 438 | x = pop() |
| 439 | if x != i: |
| 440 | self.assertEqual(x, i) |
| 441 | self.assertEqual(len(d), 0) |
| 442 | |
| 443 | def test_roundtrip_iter_init(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 444 | d = deque(range(200)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 445 | e = deque(d) |
| 446 | self.assertNotEqual(id(d), id(e)) |
| 447 | self.assertEqual(list(d), list(e)) |
| 448 | |
| 449 | def test_pickle(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 450 | d = deque(range(200)) |
Hirokazu Yamamoto | 801f9d3 | 2008-12-27 04:21:44 +0000 | [diff] [blame] | 451 | for i in range(pickle.HIGHEST_PROTOCOL + 1): |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 452 | s = pickle.dumps(d, i) |
| 453 | e = pickle.loads(s) |
| 454 | self.assertNotEqual(id(d), id(e)) |
| 455 | self.assertEqual(list(d), list(e)) |
| 456 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 457 | ## def test_pickle_recursive(self): |
| 458 | ## d = deque('abc') |
| 459 | ## d.append(d) |
Hirokazu Yamamoto | 801f9d3 | 2008-12-27 04:21:44 +0000 | [diff] [blame] | 460 | ## for i in range(pickle.HIGHEST_PROTOCOL + 1): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 461 | ## e = pickle.loads(pickle.dumps(d, i)) |
| 462 | ## self.assertNotEqual(id(d), id(e)) |
| 463 | ## self.assertEqual(id(e), id(e[-1])) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 464 | |
| 465 | def test_deepcopy(self): |
| 466 | mut = [10] |
| 467 | d = deque([mut]) |
| 468 | e = copy.deepcopy(d) |
| 469 | self.assertEqual(list(d), list(e)) |
| 470 | mut[0] = 11 |
| 471 | self.assertNotEqual(id(d), id(e)) |
| 472 | self.assertNotEqual(list(d), list(e)) |
| 473 | |
| 474 | def test_copy(self): |
| 475 | mut = [10] |
| 476 | d = deque([mut]) |
| 477 | e = copy.copy(d) |
| 478 | self.assertEqual(list(d), list(e)) |
| 479 | mut[0] = 11 |
| 480 | self.assertNotEqual(id(d), id(e)) |
| 481 | self.assertEqual(list(d), list(e)) |
| 482 | |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 483 | def test_reversed(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 484 | for s in ('abcd', range(2000)): |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 485 | self.assertEqual(list(reversed(deque(s))), list(reversed(s))) |
| 486 | |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 487 | def test_gc_doesnt_blowup(self): |
| 488 | import gc |
| 489 | # This used to assert-fail in deque_traverse() under a debug |
| 490 | # build, or run wild with a NULL pointer in a release build. |
| 491 | d = deque() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 492 | for i in range(100): |
Tim Peters | 10c7e86 | 2004-10-01 02:01:04 +0000 | [diff] [blame] | 493 | d.append(1) |
| 494 | gc.collect() |
| 495 | |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 496 | def test_container_iterator(self): |
| 497 | # Bug #3680: tp_traverse was not implemented for deque iterator objects |
| 498 | class C(object): |
| 499 | pass |
| 500 | for i in range(2): |
| 501 | obj = C() |
| 502 | ref = weakref.ref(obj) |
| 503 | if i == 0: |
| 504 | container = deque([obj, 1]) |
| 505 | else: |
| 506 | container = reversed(deque([obj, 1])) |
| 507 | obj.x = iter(container) |
| 508 | del obj, container |
| 509 | gc.collect() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 510 | self.assertTrue(ref() is None, "Cycle was not collected") |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 511 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 512 | class TestVariousIteratorArgs(unittest.TestCase): |
| 513 | |
| 514 | def test_constructor(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 515 | 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] | 516 | for g in (seq_tests.Sequence, seq_tests.IterFunc, |
| 517 | seq_tests.IterGen, seq_tests.IterFuncStop, |
| 518 | seq_tests.itermulti, seq_tests.iterfunc): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 519 | self.assertEqual(list(deque(g(s))), list(g(s))) |
Walter Dörwald | 09a3f2c | 2005-03-22 22:43:28 +0000 | [diff] [blame] | 520 | self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s)) |
| 521 | self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s)) |
| 522 | self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 523 | |
| 524 | def test_iter_with_altered_data(self): |
| 525 | d = deque('abcdefg') |
| 526 | it = iter(d) |
| 527 | d.pop() |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 528 | self.assertRaises(RuntimeError, next, it) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 529 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 530 | def test_runtime_error_on_empty_deque(self): |
| 531 | d = deque() |
| 532 | it = iter(d) |
| 533 | d.append(10) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 534 | self.assertRaises(RuntimeError, next, it) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 535 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 536 | class Deque(deque): |
| 537 | pass |
| 538 | |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 539 | class DequeWithBadIter(deque): |
| 540 | def __iter__(self): |
| 541 | raise TypeError |
| 542 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 543 | class TestSubclass(unittest.TestCase): |
| 544 | |
| 545 | def test_basics(self): |
Christian Heimes | 3805321 | 2007-12-14 01:24:44 +0000 | [diff] [blame] | 546 | d = Deque(range(25)) |
| 547 | d.__init__(range(200)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 548 | for i in range(200, 400): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 549 | d.append(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 550 | for i in reversed(range(-200, 0)): |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 551 | d.appendleft(i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 552 | self.assertEqual(list(d), list(range(-200, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 553 | self.assertEqual(len(d), 600) |
| 554 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 555 | left = [d.popleft() for i in range(250)] |
| 556 | self.assertEqual(left, list(range(-200, 50))) |
| 557 | self.assertEqual(list(d), list(range(50, 400))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 558 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 559 | right = [d.pop() for i in range(250)] |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 560 | right.reverse() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 561 | self.assertEqual(right, list(range(150, 400))) |
| 562 | self.assertEqual(list(d), list(range(50, 150))) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 563 | |
| 564 | d.clear() |
| 565 | self.assertEqual(len(d), 0) |
| 566 | |
| 567 | def test_copy_pickle(self): |
| 568 | |
| 569 | d = Deque('abc') |
| 570 | |
| 571 | e = d.__copy__() |
| 572 | self.assertEqual(type(d), type(e)) |
| 573 | self.assertEqual(list(d), list(e)) |
| 574 | |
| 575 | e = Deque(d) |
| 576 | self.assertEqual(type(d), type(e)) |
| 577 | self.assertEqual(list(d), list(e)) |
| 578 | |
| 579 | s = pickle.dumps(d) |
| 580 | e = pickle.loads(s) |
| 581 | self.assertNotEqual(id(d), id(e)) |
| 582 | self.assertEqual(type(d), type(e)) |
| 583 | self.assertEqual(list(d), list(e)) |
| 584 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 585 | d = Deque('abcde', maxlen=4) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 586 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 587 | e = d.__copy__() |
| 588 | self.assertEqual(type(d), type(e)) |
| 589 | self.assertEqual(list(d), list(e)) |
| 590 | |
| 591 | e = Deque(d) |
| 592 | self.assertEqual(type(d), type(e)) |
| 593 | self.assertEqual(list(d), list(e)) |
| 594 | |
| 595 | s = pickle.dumps(d) |
| 596 | e = pickle.loads(s) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 597 | self.assertNotEqual(id(d), id(e)) |
| 598 | self.assertEqual(type(d), type(e)) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 599 | self.assertEqual(list(d), list(e)) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 600 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 601 | ## def test_pickle(self): |
| 602 | ## d = Deque('abc') |
| 603 | ## d.append(d) |
| 604 | ## |
| 605 | ## e = pickle.loads(pickle.dumps(d)) |
| 606 | ## self.assertNotEqual(id(d), id(e)) |
| 607 | ## self.assertEqual(type(d), type(e)) |
| 608 | ## dd = d.pop() |
| 609 | ## ee = e.pop() |
| 610 | ## self.assertEqual(id(e), id(ee)) |
| 611 | ## self.assertEqual(d, e) |
| 612 | ## |
| 613 | ## d.x = d |
| 614 | ## e = pickle.loads(pickle.dumps(d)) |
| 615 | ## self.assertEqual(id(e), id(e.x)) |
| 616 | ## |
| 617 | ## d = DequeWithBadIter('abc') |
| 618 | ## self.assertRaises(TypeError, pickle.dumps, d) |
Raymond Hettinger | 952f880 | 2004-11-09 07:27:35 +0000 | [diff] [blame] | 619 | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 620 | def test_weakref(self): |
| 621 | d = deque('gallahad') |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 622 | p = weakref.proxy(d) |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 623 | self.assertEqual(str(p), str(d)) |
| 624 | d = None |
| 625 | self.assertRaises(ReferenceError, str, p) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 626 | |
Armin Rigo | 974d757 | 2004-10-02 13:59:34 +0000 | [diff] [blame] | 627 | def test_strange_subclass(self): |
| 628 | class X(deque): |
| 629 | def __iter__(self): |
| 630 | return iter([]) |
| 631 | d1 = X([1,2,3]) |
| 632 | d2 = X([4,5,6]) |
| 633 | d1 == d2 # not clear if this is supposed to be True or False, |
| 634 | # but it used to give a SystemError |
| 635 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 636 | |
| 637 | class SubclassWithKwargs(deque): |
| 638 | def __init__(self, newarg=1): |
| 639 | deque.__init__(self) |
| 640 | |
| 641 | class TestSubclassWithKwargs(unittest.TestCase): |
| 642 | def test_subclass_with_kwargs(self): |
| 643 | # SF bug #1486663 -- this used to erroneously raise a TypeError |
| 644 | SubclassWithKwargs(newarg=1) |
| 645 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 646 | #============================================================================== |
| 647 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 648 | libreftest = """ |
| 649 | Example from the Library Reference: Doc/lib/libcollections.tex |
| 650 | |
| 651 | >>> from collections import deque |
| 652 | >>> d = deque('ghi') # make a new deque with three items |
| 653 | >>> for elem in d: # iterate over the deque's elements |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 654 | ... print(elem.upper()) |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 655 | G |
| 656 | H |
| 657 | I |
| 658 | >>> d.append('j') # add a new entry to the right side |
| 659 | >>> d.appendleft('f') # add a new entry to the left side |
| 660 | >>> d # show the representation of the deque |
| 661 | deque(['f', 'g', 'h', 'i', 'j']) |
| 662 | >>> d.pop() # return and remove the rightmost item |
| 663 | 'j' |
| 664 | >>> d.popleft() # return and remove the leftmost item |
| 665 | 'f' |
| 666 | >>> list(d) # list the contents of the deque |
| 667 | ['g', 'h', 'i'] |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 668 | >>> d[0] # peek at leftmost item |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 669 | 'g' |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 670 | >>> d[-1] # peek at rightmost item |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 671 | 'i' |
| 672 | >>> list(reversed(d)) # list the contents of a deque in reverse |
| 673 | ['i', 'h', 'g'] |
| 674 | >>> 'h' in d # search the deque |
| 675 | True |
| 676 | >>> d.extend('jkl') # add multiple elements at once |
| 677 | >>> d |
| 678 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 679 | >>> d.rotate(1) # right rotation |
| 680 | >>> d |
| 681 | deque(['l', 'g', 'h', 'i', 'j', 'k']) |
| 682 | >>> d.rotate(-1) # left rotation |
| 683 | >>> d |
| 684 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 685 | >>> deque(reversed(d)) # make a new deque in reverse order |
| 686 | deque(['l', 'k', 'j', 'i', 'h', 'g']) |
| 687 | >>> d.clear() # empty the deque |
| 688 | >>> d.pop() # cannot pop from an empty deque |
| 689 | Traceback (most recent call last): |
| 690 | File "<pyshell#6>", line 1, in -toplevel- |
| 691 | d.pop() |
| 692 | IndexError: pop from an empty deque |
| 693 | |
| 694 | >>> d.extendleft('abc') # extendleft() reverses the input order |
| 695 | >>> d |
| 696 | deque(['c', 'b', 'a']) |
| 697 | |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 698 | |
| 699 | |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 700 | >>> def delete_nth(d, n): |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 701 | ... d.rotate(-n) |
| 702 | ... d.popleft() |
| 703 | ... d.rotate(n) |
| 704 | ... |
| 705 | >>> d = deque('abcdef') |
| 706 | >>> delete_nth(d, 2) # remove the entry at d[2] |
| 707 | >>> d |
| 708 | deque(['a', 'b', 'd', 'e', 'f']) |
| 709 | |
| 710 | |
| 711 | |
| 712 | >>> def roundrobin(*iterables): |
| 713 | ... pending = deque(iter(i) for i in iterables) |
| 714 | ... while pending: |
| 715 | ... task = pending.popleft() |
| 716 | ... try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 717 | ... yield next(task) |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 718 | ... except StopIteration: |
| 719 | ... continue |
| 720 | ... pending.append(task) |
| 721 | ... |
| 722 | |
| 723 | >>> for value in roundrobin('abc', 'd', 'efgh'): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 724 | ... print(value) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 725 | ... |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 726 | a |
| 727 | d |
| 728 | e |
| 729 | b |
| 730 | f |
| 731 | c |
| 732 | g |
| 733 | h |
| 734 | |
| 735 | |
| 736 | >>> def maketree(iterable): |
| 737 | ... d = deque(iterable) |
| 738 | ... while len(d) > 1: |
| 739 | ... pair = [d.popleft(), d.popleft()] |
| 740 | ... d.append(pair) |
| 741 | ... return list(d) |
| 742 | ... |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 743 | >>> print(maketree('abcdefgh')) |
Raymond Hettinger | e7169eb | 2004-05-09 01:15:01 +0000 | [diff] [blame] | 744 | [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] |
| 745 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 746 | """ |
| 747 | |
| 748 | |
| 749 | #============================================================================== |
| 750 | |
| 751 | __test__ = {'libreftest' : libreftest} |
| 752 | |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 753 | def test_main(verbose=None): |
| 754 | import sys |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 755 | test_classes = ( |
| 756 | TestBasic, |
| 757 | TestVariousIteratorArgs, |
| 758 | TestSubclass, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 759 | TestSubclassWithKwargs, |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 760 | ) |
| 761 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 762 | support.run_unittest(*test_classes) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 763 | |
| 764 | # verify reference counting |
| 765 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 766 | import gc |
| 767 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 768 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 769 | support.run_unittest(*test_classes) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 770 | gc.collect() |
| 771 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 772 | print(counts) |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 773 | |
Raymond Hettinger | 738ec90 | 2004-02-29 02:15:56 +0000 | [diff] [blame] | 774 | # doctests |
| 775 | from test import test_deque |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 776 | support.run_doctest(test_deque, verbose) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 777 | |
| 778 | if __name__ == "__main__": |
| 779 | test_main(verbose=True) |