summaryrefslogtreecommitdiff
path: root/Lib/test/test_deque.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_deque.py')
-rw-r--r--Lib/test/test_deque.py177
1 files changed, 176 insertions, 1 deletions
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 5ecbc73c99..b858509684 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -164,6 +164,26 @@ class TestBasic(unittest.TestCase):
self.assertEqual(x > y, list(x) > list(y), (x,y))
self.assertEqual(x >= y, list(x) >= list(y), (x,y))
+ def test_contains(self):
+ n = 200
+
+ d = deque(range(n))
+ for i in range(n):
+ self.assertTrue(i in d)
+ self.assertTrue((n+1) not in d)
+
+ # Test detection of mutation during iteration
+ d = deque(range(n))
+ d[n//2] = MutateCmp(d, False)
+ with self.assertRaises(RuntimeError):
+ n in d
+
+ # Test detection of comparison exceptions
+ d = deque(range(n))
+ d[n//2] = BadCmp()
+ with self.assertRaises(RuntimeError):
+ n in d
+
def test_extend(self):
d = deque('a')
self.assertRaises(TypeError, d.extend, 1)
@@ -172,6 +192,26 @@ class TestBasic(unittest.TestCase):
d.extend(d)
self.assertEqual(list(d), list('abcdabcd'))
+ def test_add(self):
+ d = deque()
+ e = deque('abc')
+ f = deque('def')
+ self.assertEqual(d + d, deque())
+ self.assertEqual(e + f, deque('abcdef'))
+ self.assertEqual(e + e, deque('abcabc'))
+ self.assertEqual(e + d, deque('abc'))
+ self.assertEqual(d + e, deque('abc'))
+ self.assertIsNot(d + d, deque())
+ self.assertIsNot(e + d, deque('abc'))
+ self.assertIsNot(d + e, deque('abc'))
+
+ g = deque('abcdef', maxlen=4)
+ h = deque('gh')
+ self.assertEqual(g + h, deque('efgh'))
+
+ with self.assertRaises(TypeError):
+ deque('abc') + 'def'
+
def test_iadd(self):
d = deque('a')
d += 'bcd'
@@ -211,6 +251,111 @@ class TestBasic(unittest.TestCase):
self.assertRaises(IndexError, d.__getitem__, 0)
self.assertRaises(IndexError, d.__getitem__, -1)
+ def test_index(self):
+ for n in 1, 2, 30, 40, 200:
+
+ d = deque(range(n))
+ for i in range(n):
+ self.assertEqual(d.index(i), i)
+
+ with self.assertRaises(ValueError):
+ d.index(n+1)
+
+ # Test detection of mutation during iteration
+ d = deque(range(n))
+ d[n//2] = MutateCmp(d, False)
+ with self.assertRaises(RuntimeError):
+ d.index(n)
+
+ # Test detection of comparison exceptions
+ d = deque(range(n))
+ d[n//2] = BadCmp()
+ with self.assertRaises(RuntimeError):
+ d.index(n)
+
+ # Test start and stop arguments behavior matches list.index()
+ elements = 'ABCDEFGHI'
+ nonelement = 'Z'
+ d = deque(elements * 2)
+ s = list(elements * 2)
+ for start in range(-5 - len(s)*2, 5 + len(s) * 2):
+ for stop in range(-5 - len(s)*2, 5 + len(s) * 2):
+ for element in elements + 'Z':
+ try:
+ target = s.index(element, start, stop)
+ except ValueError:
+ with self.assertRaises(ValueError):
+ d.index(element, start, stop)
+ else:
+ self.assertEqual(d.index(element, start, stop), target)
+
+ def test_insert(self):
+ # Test to make sure insert behaves like lists
+ elements = 'ABCDEFGHI'
+ for i in range(-5 - len(elements)*2, 5 + len(elements) * 2):
+ d = deque('ABCDEFGHI')
+ s = list('ABCDEFGHI')
+ d.insert(i, 'Z')
+ s.insert(i, 'Z')
+ self.assertEqual(list(d), s)
+
+ def test_imul(self):
+ for n in (-10, -1, 0, 1, 2, 10, 1000):
+ d = deque()
+ d *= n
+ self.assertEqual(d, deque())
+ self.assertIsNone(d.maxlen)
+
+ for n in (-10, -1, 0, 1, 2, 10, 1000):
+ d = deque('a')
+ d *= n
+ self.assertEqual(d, deque('a' * n))
+ self.assertIsNone(d.maxlen)
+
+ for n in (-10, -1, 0, 1, 2, 10, 499, 500, 501, 1000):
+ d = deque('a', 500)
+ d *= n
+ self.assertEqual(d, deque('a' * min(n, 500)))
+ self.assertEqual(d.maxlen, 500)
+
+ for n in (-10, -1, 0, 1, 2, 10, 1000):
+ d = deque('abcdef')
+ d *= n
+ self.assertEqual(d, deque('abcdef' * n))
+ self.assertIsNone(d.maxlen)
+
+ for n in (-10, -1, 0, 1, 2, 10, 499, 500, 501, 1000):
+ d = deque('abcdef', 500)
+ d *= n
+ self.assertEqual(d, deque(('abcdef' * n)[-500:]))
+ self.assertEqual(d.maxlen, 500)
+
+ def test_mul(self):
+ d = deque('abc')
+ self.assertEqual(d * -5, deque())
+ self.assertEqual(d * 0, deque())
+ self.assertEqual(d * 1, deque('abc'))
+ self.assertEqual(d * 2, deque('abcabc'))
+ self.assertEqual(d * 3, deque('abcabcabc'))
+ self.assertIsNot(d * 1, d)
+
+ self.assertEqual(deque() * 0, deque())
+ self.assertEqual(deque() * 1, deque())
+ self.assertEqual(deque() * 5, deque())
+
+ self.assertEqual(-5 * d, deque())
+ self.assertEqual(0 * d, deque())
+ self.assertEqual(1 * d, deque('abc'))
+ self.assertEqual(2 * d, deque('abcabc'))
+ self.assertEqual(3 * d, deque('abcabcabc'))
+
+ d = deque('abc', maxlen=5)
+ self.assertEqual(d * -5, deque())
+ self.assertEqual(d * 0, deque())
+ self.assertEqual(d * 1, deque('abc'))
+ self.assertEqual(d * 2, deque('bcabc'))
+ self.assertEqual(d * 30, deque('bcabc'))
+
def test_setitem(self):
n = 200
d = deque(range(n))
@@ -504,10 +649,24 @@ class TestBasic(unittest.TestCase):
self.assertNotEqual(id(d), id(e))
self.assertEqual(list(d), list(e))
+ def test_copy_method(self):
+ mut = [10]
+ d = deque([mut])
+ e = d.copy()
+ self.assertEqual(list(d), list(e))
+ mut[0] = 11
+ self.assertNotEqual(id(d), id(e))
+ self.assertEqual(list(d), list(e))
+
def test_reversed(self):
for s in ('abcd', range(2000)):
self.assertEqual(list(reversed(deque(s))), list(reversed(s)))
+ def test_reversed_new(self):
+ klass = type(reversed(deque()))
+ for s in ('abcd', range(2000)):
+ self.assertEqual(list(klass(deque(s))), list(reversed(s)))
+
def test_gc_doesnt_blowup(self):
import gc
# This used to assert-fail in deque_traverse() under a debug
@@ -537,7 +696,7 @@ class TestBasic(unittest.TestCase):
@support.cpython_only
def test_sizeof(self):
- BLOCKLEN = 62
+ BLOCKLEN = 64
basesize = support.calcobjsize('2P4nlP')
blocksize = struct.calcsize('2P%dP' % BLOCKLEN)
self.assertEqual(object.__sizeof__(deque()), basesize)
@@ -684,6 +843,21 @@ class TestSubclassWithKwargs(unittest.TestCase):
# SF bug #1486663 -- this used to erroneously raise a TypeError
SubclassWithKwargs(newarg=1)
+class TestSequence(seq_tests.CommonTest):
+ type2test = deque
+
+ def test_getitem(self):
+ # For now, bypass tests that require slicing
+ pass
+
+ def test_getslice(self):
+ # For now, bypass tests that require slicing
+ pass
+
+ def test_subscript(self):
+ # For now, bypass tests that require slicing
+ pass
+
#==============================================================================
libreftest = """
@@ -798,6 +972,7 @@ def test_main(verbose=None):
TestVariousIteratorArgs,
TestSubclass,
TestSubclassWithKwargs,
+ TestSequence,
)
support.run_unittest(*test_classes)