summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/seq_tests.py90
-rw-r--r--Lib/test/test_deque.py90
2 files changed, 97 insertions, 83 deletions
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py
index aeb15c0..89996a3 100644
--- a/Lib/test/seq_tests.py
+++ b/Lib/test/seq_tests.py
@@ -5,6 +5,85 @@ Tests common to tuple, list and UserList.UserList
import unittest
from test import test_support
+# Various iterables
+# This is used for checking the constructor (here and in test_deque.py)
+def iterfunc(seqn):
+ 'Regular generator'
+ for i in seqn:
+ yield i
+
+class Sequence:
+ 'Sequence using __getitem__'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ def __getitem__(self, i):
+ return self.seqn[i]
+
+class IterFunc:
+ 'Sequence using iterator protocol'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ self.i = 0
+ def __iter__(self):
+ return self
+ def next(self):
+ if self.i >= len(self.seqn): raise StopIteration
+ v = self.seqn[self.i]
+ self.i += 1
+ return v
+
+class IterGen:
+ 'Sequence using iterator protocol defined with a generator'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ self.i = 0
+ def __iter__(self):
+ for val in self.seqn:
+ yield val
+
+class IterNextOnly:
+ 'Missing __getitem__ and __iter__'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ self.i = 0
+ def next(self):
+ if self.i >= len(self.seqn): raise StopIteration
+ v = self.seqn[self.i]
+ self.i += 1
+ return v
+
+class IterNoNext:
+ 'Iterator missing next()'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ self.i = 0
+ def __iter__(self):
+ return self
+
+class IterGenExc:
+ 'Test propagation of exceptions'
+ def __init__(self, seqn):
+ self.seqn = seqn
+ self.i = 0
+ def __iter__(self):
+ return self
+ def next(self):
+ 3 // 0
+
+class IterFuncStop:
+ 'Test immediate stop'
+ def __init__(self, seqn):
+ pass
+ def __iter__(self):
+ return self
+ def next(self):
+ raise StopIteration
+
+from itertools import chain, imap
+def itermulti(seqn):
+ 'Test multiple tiers of iterators'
+ return chain(imap(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
+
class CommonTest(unittest.TestCase):
# The type to be tested
type2test = None
@@ -40,6 +119,17 @@ class CommonTest(unittest.TestCase):
vv = self.type2test(s)
self.assertEqual(len(vv), len(s))
+ # Create from various iteratables
+ for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
+ for g in (Sequence, IterFunc, IterGen,
+ itermulti, iterfunc):
+ self.assertEqual(self.type2test(g(s)), self.type2test(s))
+ self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
+ self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
+ self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
+ self.assertRaises(TypeError, self.type2test, IterNoNext(s))
+ self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
+
def test_truth(self):
self.assert_(not self.type2test())
self.assert_(self.type2test([42]))
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index 61919cb..a562922 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -1,6 +1,6 @@
from collections import deque
import unittest
-from test import test_support
+from test import test_support, seq_tests
from weakref import proxy
import copy
import cPickle as pickle
@@ -378,93 +378,17 @@ class TestBasic(unittest.TestCase):
d.append(1)
gc.collect()
-def R(seqn):
- 'Regular generator'
- for i in seqn:
- yield i
-
-class G:
- 'Sequence using __getitem__'
- def __init__(self, seqn):
- self.seqn = seqn
- def __getitem__(self, i):
- return self.seqn[i]
-
-class I:
- 'Sequence using iterator protocol'
- def __init__(self, seqn):
- self.seqn = seqn
- self.i = 0
- def __iter__(self):
- return self
- def next(self):
- if self.i >= len(self.seqn): raise StopIteration
- v = self.seqn[self.i]
- self.i += 1
- return v
-
-class Ig:
- 'Sequence using iterator protocol defined with a generator'
- def __init__(self, seqn):
- self.seqn = seqn
- self.i = 0
- def __iter__(self):
- for val in self.seqn:
- yield val
-
-class X:
- 'Missing __getitem__ and __iter__'
- def __init__(self, seqn):
- self.seqn = seqn
- self.i = 0
- def next(self):
- if self.i >= len(self.seqn): raise StopIteration
- v = self.seqn[self.i]
- self.i += 1
- return v
-
-class N:
- 'Iterator missing next()'
- def __init__(self, seqn):
- self.seqn = seqn
- self.i = 0
- def __iter__(self):
- return self
-
-class E:
- 'Test propagation of exceptions'
- def __init__(self, seqn):
- self.seqn = seqn
- self.i = 0
- def __iter__(self):
- return self
- def next(self):
- 3 // 0
-
-class S:
- 'Test immediate stop'
- def __init__(self, seqn):
- pass
- def __iter__(self):
- return self
- def next(self):
- raise StopIteration
-
-from itertools import chain, imap
-def L(seqn):
- 'Test multiple tiers of iterators'
- return chain(imap(lambda x:x, R(Ig(G(seqn)))))
-
-
class TestVariousIteratorArgs(unittest.TestCase):
def test_constructor(self):
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
- for g in (G, I, Ig, S, L, R):
+ for g in (seq_tests.Sequence, seq_tests.IterFunc,
+ seq_tests.IterGen, seq_tests.IterFuncStop,
+ seq_tests.itermulti, seq_tests.iterfunc):
self.assertEqual(list(deque(g(s))), list(g(s)))
- self.assertRaises(TypeError, deque, X(s))
- self.assertRaises(TypeError, deque, N(s))
- self.assertRaises(ZeroDivisionError, deque, E(s))
+ self.assertRaises(TypeError, deque, seq_tests.IterNextOnly(s))
+ self.assertRaises(TypeError, deque, seq_tests.IterNoNext(s))
+ self.assertRaises(ZeroDivisionError, deque, seq_tests.IterGenExc(s))
def test_iter_with_altered_data(self):
d = deque('abcdefg')