1"""
2Tests common to tuple, list and UserList.UserList
3"""
4
5import unittest
6import sys
7
8# Various iterables
9# This is used for checking the constructor (here and in test_deque.py)
10def iterfunc(seqn):
11    'Regular generator'
12    for i in seqn:
13        yield i
14
15class Sequence:
16    'Sequence using __getitem__'
17    def __init__(self, seqn):
18        self.seqn = seqn
19    def __getitem__(self, i):
20        return self.seqn[i]
21
22class IterFunc:
23    'Sequence using iterator protocol'
24    def __init__(self, seqn):
25        self.seqn = seqn
26        self.i = 0
27    def __iter__(self):
28        return self
29    def next(self):
30        if self.i >= len(self.seqn): raise StopIteration
31        v = self.seqn[self.i]
32        self.i += 1
33        return v
34
35class IterGen:
36    'Sequence using iterator protocol defined with a generator'
37    def __init__(self, seqn):
38        self.seqn = seqn
39        self.i = 0
40    def __iter__(self):
41        for val in self.seqn:
42            yield val
43
44class IterNextOnly:
45    'Missing __getitem__ and __iter__'
46    def __init__(self, seqn):
47        self.seqn = seqn
48        self.i = 0
49    def next(self):
50        if self.i >= len(self.seqn): raise StopIteration
51        v = self.seqn[self.i]
52        self.i += 1
53        return v
54
55class IterNoNext:
56    'Iterator missing next()'
57    def __init__(self, seqn):
58        self.seqn = seqn
59        self.i = 0
60    def __iter__(self):
61        return self
62
63class IterGenExc:
64    'Test propagation of exceptions'
65    def __init__(self, seqn):
66        self.seqn = seqn
67        self.i = 0
68    def __iter__(self):
69        return self
70    def next(self):
71        3 // 0
72
73class IterFuncStop:
74    'Test immediate stop'
75    def __init__(self, seqn):
76        pass
77    def __iter__(self):
78        return self
79    def next(self):
80        raise StopIteration
81
82from itertools import chain, imap
83def itermulti(seqn):
84    'Test multiple tiers of iterators'
85    return chain(imap(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
86
87class CommonTest(unittest.TestCase):
88    # The type to be tested
89    type2test = None
90
91    def test_constructors(self):
92        l0 = []
93        l1 = [0]
94        l2 = [0, 1]
95
96        u = self.type2test()
97        u0 = self.type2test(l0)
98        u1 = self.type2test(l1)
99        u2 = self.type2test(l2)
100
101        uu = self.type2test(u)
102        uu0 = self.type2test(u0)
103        uu1 = self.type2test(u1)
104        uu2 = self.type2test(u2)
105
106        v = self.type2test(tuple(u))
107        class OtherSeq:
108            def __init__(self, initseq):
109                self.__data = initseq
110            def __len__(self):
111                return len(self.__data)
112            def __getitem__(self, i):
113                return self.__data[i]
114        s = OtherSeq(u0)
115        v0 = self.type2test(s)
116        self.assertEqual(len(v0), len(s))
117
118        s = "this is also a sequence"
119        vv = self.type2test(s)
120        self.assertEqual(len(vv), len(s))
121
122        # Create from various iteratables
123        for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
124            for g in (Sequence, IterFunc, IterGen,
125                      itermulti, iterfunc):
126                self.assertEqual(self.type2test(g(s)), self.type2test(s))
127            self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
128            self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
129            self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
130            self.assertRaises(TypeError, self.type2test, IterNoNext(s))
131            self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
132
133    def test_truth(self):
134        self.assertFalse(self.type2test())
135        self.assertTrue(self.type2test([42]))
136
137    def test_getitem(self):
138        u = self.type2test([0, 1, 2, 3, 4])
139        for i in xrange(len(u)):
140            self.assertEqual(u[i], i)
141            self.assertEqual(u[long(i)], i)
142        for i in xrange(-len(u), -1):
143            self.assertEqual(u[i], len(u)+i)
144            self.assertEqual(u[long(i)], len(u)+i)
145        self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
146        self.assertRaises(IndexError, u.__getitem__, len(u))
147        self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
148
149        u = self.type2test()
150        self.assertRaises(IndexError, u.__getitem__, 0)
151        self.assertRaises(IndexError, u.__getitem__, -1)
152
153        self.assertRaises(TypeError, u.__getitem__)
154
155        a = self.type2test([10, 11])
156        self.assertEqual(a[0], 10)
157        self.assertEqual(a[1], 11)
158        self.assertEqual(a[-2], 10)
159        self.assertEqual(a[-1], 11)
160        self.assertRaises(IndexError, a.__getitem__, -3)
161        self.assertRaises(IndexError, a.__getitem__, 3)
162
163    def test_getslice(self):
164        l = [0, 1, 2, 3, 4]
165        u = self.type2test(l)
166
167        self.assertEqual(u[0:0], self.type2test())
168        self.assertEqual(u[1:2], self.type2test([1]))
169        self.assertEqual(u[-2:-1], self.type2test([3]))
170        self.assertEqual(u[-1000:1000], u)
171        self.assertEqual(u[1000:-1000], self.type2test([]))
172        self.assertEqual(u[:], u)
173        self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
174        self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
175
176        # Extended slices
177        self.assertEqual(u[::], u)
178        self.assertEqual(u[::2], self.type2test([0, 2, 4]))
179        self.assertEqual(u[1::2], self.type2test([1, 3]))
180        self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
181        self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
182        self.assertEqual(u[3::-2], self.type2test([3, 1]))
183        self.assertEqual(u[3:3:-2], self.type2test([]))
184        self.assertEqual(u[3:2:-2], self.type2test([3]))
185        self.assertEqual(u[3:1:-2], self.type2test([3]))
186        self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
187        self.assertEqual(u[::-100], self.type2test([4]))
188        self.assertEqual(u[100:-100:], self.type2test([]))
189        self.assertEqual(u[-100:100:], u)
190        self.assertEqual(u[100:-100:-1], u[::-1])
191        self.assertEqual(u[-100:100:-1], self.type2test([]))
192        self.assertEqual(u[-100L:100L:2L], self.type2test([0, 2, 4]))
193
194        # Test extreme cases with long ints
195        a = self.type2test([0,1,2,3,4])
196        self.assertEqual(a[ -pow(2,128L): 3 ], self.type2test([0,1,2]))
197        self.assertEqual(a[ 3: pow(2,145L) ], self.type2test([3,4]))
198
199        self.assertRaises(TypeError, u.__getslice__)
200
201    def test_contains(self):
202        u = self.type2test([0, 1, 2])
203        for i in u:
204            self.assertIn(i, u)
205        for i in min(u)-1, max(u)+1:
206            self.assertNotIn(i, u)
207
208        self.assertRaises(TypeError, u.__contains__)
209
210    def test_contains_fake(self):
211        class AllEq:
212            # Sequences must use rich comparison against each item
213            # (unless "is" is true, or an earlier item answered)
214            # So instances of AllEq must be found in all non-empty sequences.
215            def __eq__(self, other):
216                return True
217            __hash__ = None # Can't meet hash invariant requirements
218        self.assertNotIn(AllEq(), self.type2test([]))
219        self.assertIn(AllEq(), self.type2test([1]))
220
221    def test_contains_order(self):
222        # Sequences must test in-order.  If a rich comparison has side
223        # effects, these will be visible to tests against later members.
224        # In this test, the "side effect" is a short-circuiting raise.
225        class DoNotTestEq(Exception):
226            pass
227        class StopCompares:
228            def __eq__(self, other):
229                raise DoNotTestEq
230
231        checkfirst = self.type2test([1, StopCompares()])
232        self.assertIn(1, checkfirst)
233        checklast = self.type2test([StopCompares(), 1])
234        self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
235
236    def test_len(self):
237        self.assertEqual(len(self.type2test()), 0)
238        self.assertEqual(len(self.type2test([])), 0)
239        self.assertEqual(len(self.type2test([0])), 1)
240        self.assertEqual(len(self.type2test([0, 1, 2])), 3)
241
242    def test_minmax(self):
243        u = self.type2test([0, 1, 2])
244        self.assertEqual(min(u), 0)
245        self.assertEqual(max(u), 2)
246
247    def test_addmul(self):
248        u1 = self.type2test([0])
249        u2 = self.type2test([0, 1])
250        self.assertEqual(u1, u1 + self.type2test())
251        self.assertEqual(u1, self.type2test() + u1)
252        self.assertEqual(u1 + self.type2test([1]), u2)
253        self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
254        self.assertEqual(self.type2test(), u2*0)
255        self.assertEqual(self.type2test(), 0*u2)
256        self.assertEqual(self.type2test(), u2*0L)
257        self.assertEqual(self.type2test(), 0L*u2)
258        self.assertEqual(u2, u2*1)
259        self.assertEqual(u2, 1*u2)
260        self.assertEqual(u2, u2*1L)
261        self.assertEqual(u2, 1L*u2)
262        self.assertEqual(u2+u2, u2*2)
263        self.assertEqual(u2+u2, 2*u2)
264        self.assertEqual(u2+u2, u2*2L)
265        self.assertEqual(u2+u2, 2L*u2)
266        self.assertEqual(u2+u2+u2, u2*3)
267        self.assertEqual(u2+u2+u2, 3*u2)
268
269        class subclass(self.type2test):
270            pass
271        u3 = subclass([0, 1])
272        self.assertEqual(u3, u3*1)
273        self.assertIsNot(u3, u3*1)
274
275    def test_iadd(self):
276        u = self.type2test([0, 1])
277        u += self.type2test()
278        self.assertEqual(u, self.type2test([0, 1]))
279        u += self.type2test([2, 3])
280        self.assertEqual(u, self.type2test([0, 1, 2, 3]))
281        u += self.type2test([4, 5])
282        self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
283
284        u = self.type2test("spam")
285        u += self.type2test("eggs")
286        self.assertEqual(u, self.type2test("spameggs"))
287
288    def test_imul(self):
289        u = self.type2test([0, 1])
290        u *= 3
291        self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
292
293    def test_getitemoverwriteiter(self):
294        # Verify that __getitem__ overrides are not recognized by __iter__
295        class T(self.type2test):
296            def __getitem__(self, key):
297                return str(key) + '!!!'
298        self.assertEqual(iter(T((1,2))).next(), 1)
299
300    def test_repeat(self):
301        for m in xrange(4):
302            s = tuple(range(m))
303            for n in xrange(-3, 5):
304                self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
305            self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
306            self.assertEqual(id(s), id(s*1))
307
308    def test_bigrepeat(self):
309        import sys
310        if sys.maxint <= 2147483647:
311            x = self.type2test([0])
312            x *= 2**16
313            self.assertRaises(MemoryError, x.__mul__, 2**16)
314            if hasattr(x, '__imul__'):
315                self.assertRaises(MemoryError, x.__imul__, 2**16)
316
317    def test_subscript(self):
318        a = self.type2test([10, 11])
319        self.assertEqual(a.__getitem__(0L), 10)
320        self.assertEqual(a.__getitem__(1L), 11)
321        self.assertEqual(a.__getitem__(-2L), 10)
322        self.assertEqual(a.__getitem__(-1L), 11)
323        self.assertRaises(IndexError, a.__getitem__, -3)
324        self.assertRaises(IndexError, a.__getitem__, 3)
325        self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
326        self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
327        self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
328        self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
329        self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
330        self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
331        self.assertRaises(TypeError, a.__getitem__, 'x')
332
333    def test_count(self):
334        a = self.type2test([0, 1, 2])*3
335        self.assertEqual(a.count(0), 3)
336        self.assertEqual(a.count(1), 3)
337        self.assertEqual(a.count(3), 0)
338
339        self.assertRaises(TypeError, a.count)
340
341        class BadExc(Exception):
342            pass
343
344        class BadCmp:
345            def __eq__(self, other):
346                if other == 2:
347                    raise BadExc()
348                return False
349
350        self.assertRaises(BadExc, a.count, BadCmp())
351
352    def test_index(self):
353        u = self.type2test([0, 1])
354        self.assertEqual(u.index(0), 0)
355        self.assertEqual(u.index(1), 1)
356        self.assertRaises(ValueError, u.index, 2)
357
358        u = self.type2test([-2, -1, 0, 0, 1, 2])
359        self.assertEqual(u.count(0), 2)
360        self.assertEqual(u.index(0), 2)
361        self.assertEqual(u.index(0, 2), 2)
362        self.assertEqual(u.index(-2, -10), 0)
363        self.assertEqual(u.index(0, 3), 3)
364        self.assertEqual(u.index(0, 3, 4), 3)
365        self.assertRaises(ValueError, u.index, 2, 0, -10)
366
367        self.assertRaises(TypeError, u.index)
368
369        class BadExc(Exception):
370            pass
371
372        class BadCmp:
373            def __eq__(self, other):
374                if other == 2:
375                    raise BadExc()
376                return False
377
378        a = self.type2test([0, 1, 2, 3])
379        self.assertRaises(BadExc, a.index, BadCmp())
380
381        a = self.type2test([-2, -1, 0, 0, 1, 2])
382        self.assertEqual(a.index(0), 2)
383        self.assertEqual(a.index(0, 2), 2)
384        self.assertEqual(a.index(0, -4), 2)
385        self.assertEqual(a.index(-2, -10), 0)
386        self.assertEqual(a.index(0, 3), 3)
387        self.assertEqual(a.index(0, -3), 3)
388        self.assertEqual(a.index(0, 3, 4), 3)
389        self.assertEqual(a.index(0, -3, -2), 3)
390        self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2)
391        self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint)
392        self.assertRaises(ValueError, a.index, 2, 0, -10)
393