summaryrefslogtreecommitdiff
path: root/Lib/test/test_deque.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2015-03-21 01:37:37 -0700
committerRaymond Hettinger <python@rcn.com>2015-03-21 01:37:37 -0700
commit32ea16577d2fd8994730250572957888c3e48f84 (patch)
treeef64b1b9c8022d3eb3bf089330060199be61a212 /Lib/test/test_deque.py
parent0a9e2721fa8605e7e8957bf658718a63c286c8b1 (diff)
downloadcpython-git-32ea16577d2fd8994730250572957888c3e48f84.tar.gz
Issue 23704: Add index(), copy(), and insert() to deques. Register deques as a MutableSequence.
Diffstat (limited to 'Lib/test/test_deque.py')
-rw-r--r--Lib/test/test_deque.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 7d69448585..07e5ef98eb 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -231,6 +231,54 @@ 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_setitem(self):
n = 200
d = deque(range(n))
@@ -524,6 +572,15 @@ 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)))