blob: 89996a36e4cc6f1c8fa06db1fbfcb069918a0989 [file] [log] [blame]
Walter Dörwald1dde95d2003-12-08 11:38:45 +00001"""
2Tests common to tuple, list and UserList.UserList
3"""
4
5import unittest
6from test import test_support
7
Walter Dörwald09a3f2c2005-03-22 22:43:28 +00008# 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
Walter Dörwald1dde95d2003-12-08 11:38:45 +000087class 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
Walter Dörwald09a3f2c2005-03-22 22:43:28 +0000122 # 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
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000133 def test_truth(self):
134 self.assert_(not self.type2test())
135 self.assert_(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)
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000141 self.assertEqual(u[long(i)], i)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000142 for i in xrange(-len(u), -1):
143 self.assertEqual(u[i], len(u)+i)
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000144 self.assertEqual(u[long(i)], len(u)+i)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000145 self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
146 self.assertRaises(IndexError, u.__getitem__, len(u))
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000147 self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000148
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
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000155 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
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000163 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.assert_(i in u)
205 for i in min(u)-1, max(u)+1:
206 self.assert_(i not in u)
207
208 self.assertRaises(TypeError, u.__contains__)
209
210 def test_len(self):
211 self.assertEqual(len(self.type2test()), 0)
212 self.assertEqual(len(self.type2test([])), 0)
213 self.assertEqual(len(self.type2test([0])), 1)
214 self.assertEqual(len(self.type2test([0, 1, 2])), 3)
215
216 def test_minmax(self):
217 u = self.type2test([0, 1, 2])
218 self.assertEqual(min(u), 0)
219 self.assertEqual(max(u), 2)
220
221 def test_addmul(self):
222 u1 = self.type2test([0])
223 u2 = self.type2test([0, 1])
224 self.assertEqual(u1, u1 + self.type2test())
225 self.assertEqual(u1, self.type2test() + u1)
226 self.assertEqual(u1 + self.type2test([1]), u2)
227 self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
228 self.assertEqual(self.type2test(), u2*0)
229 self.assertEqual(self.type2test(), 0*u2)
230 self.assertEqual(self.type2test(), u2*0L)
231 self.assertEqual(self.type2test(), 0L*u2)
232 self.assertEqual(u2, u2*1)
233 self.assertEqual(u2, 1*u2)
234 self.assertEqual(u2, u2*1L)
235 self.assertEqual(u2, 1L*u2)
236 self.assertEqual(u2+u2, u2*2)
237 self.assertEqual(u2+u2, 2*u2)
238 self.assertEqual(u2+u2, u2*2L)
239 self.assertEqual(u2+u2, 2L*u2)
240 self.assertEqual(u2+u2+u2, u2*3)
241 self.assertEqual(u2+u2+u2, 3*u2)
242
243 class subclass(self.type2test):
244 pass
245 u3 = subclass([0, 1])
246 self.assertEqual(u3, u3*1)
247 self.assert_(u3 is not u3*1)
248
249 def test_iadd(self):
250 u = self.type2test([0, 1])
251 u += self.type2test()
252 self.assertEqual(u, self.type2test([0, 1]))
253 u += self.type2test([2, 3])
254 self.assertEqual(u, self.type2test([0, 1, 2, 3]))
255 u += self.type2test([4, 5])
256 self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
257
258 u = self.type2test("spam")
259 u += self.type2test("eggs")
260 self.assertEqual(u, self.type2test("spameggs"))
261
262 def test_imul(self):
263 u = self.type2test([0, 1])
264 u *= 3
265 self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
266
267 def test_getitemoverwriteiter(self):
268 # Verify that __getitem__ overrides are not recognized by __iter__
269 class T(self.type2test):
270 def __getitem__(self, key):
271 return str(key) + '!!!'
272 self.assertEqual(iter(T((1,2))).next(), 1)
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000273
274 def test_repeat(self):
275 for m in xrange(4):
276 s = tuple(range(m))
277 for n in xrange(-3, 5):
278 self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
279 self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
280 self.assertEqual(id(s), id(s*1))
281
282 def test_subscript(self):
283 a = self.type2test([10, 11])
284 self.assertEqual(a.__getitem__(0L), 10)
285 self.assertEqual(a.__getitem__(1L), 11)
286 self.assertEqual(a.__getitem__(-2L), 10)
287 self.assertEqual(a.__getitem__(-1L), 11)
288 self.assertRaises(IndexError, a.__getitem__, -3)
289 self.assertRaises(IndexError, a.__getitem__, 3)
290 self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
291 self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
292 self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
293 self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
294 self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
295 self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
296 self.assertRaises(TypeError, a.__getitem__, 'x')