1 | n/a | """ |
---|
2 | n/a | Tests common to list and UserList.UserList |
---|
3 | n/a | """ |
---|
4 | n/a | |
---|
5 | n/a | import sys |
---|
6 | n/a | import os |
---|
7 | n/a | from functools import cmp_to_key |
---|
8 | n/a | |
---|
9 | n/a | from test import support, seq_tests |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class CommonTest(seq_tests.CommonTest): |
---|
13 | n/a | |
---|
14 | n/a | def test_init(self): |
---|
15 | n/a | # Iterable arg is optional |
---|
16 | n/a | self.assertEqual(self.type2test([]), self.type2test()) |
---|
17 | n/a | |
---|
18 | n/a | # Init clears previous values |
---|
19 | n/a | a = self.type2test([1, 2, 3]) |
---|
20 | n/a | a.__init__() |
---|
21 | n/a | self.assertEqual(a, self.type2test([])) |
---|
22 | n/a | |
---|
23 | n/a | # Init overwrites previous values |
---|
24 | n/a | a = self.type2test([1, 2, 3]) |
---|
25 | n/a | a.__init__([4, 5, 6]) |
---|
26 | n/a | self.assertEqual(a, self.type2test([4, 5, 6])) |
---|
27 | n/a | |
---|
28 | n/a | # Mutables always return a new object |
---|
29 | n/a | b = self.type2test(a) |
---|
30 | n/a | self.assertNotEqual(id(a), id(b)) |
---|
31 | n/a | self.assertEqual(a, b) |
---|
32 | n/a | |
---|
33 | n/a | def test_getitem_error(self): |
---|
34 | n/a | msg = "list indices must be integers or slices" |
---|
35 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
36 | n/a | a = [] |
---|
37 | n/a | a['a'] = "python" |
---|
38 | n/a | |
---|
39 | n/a | def test_repr(self): |
---|
40 | n/a | l0 = [] |
---|
41 | n/a | l2 = [0, 1, 2] |
---|
42 | n/a | a0 = self.type2test(l0) |
---|
43 | n/a | a2 = self.type2test(l2) |
---|
44 | n/a | |
---|
45 | n/a | self.assertEqual(str(a0), str(l0)) |
---|
46 | n/a | self.assertEqual(repr(a0), repr(l0)) |
---|
47 | n/a | self.assertEqual(repr(a2), repr(l2)) |
---|
48 | n/a | self.assertEqual(str(a2), "[0, 1, 2]") |
---|
49 | n/a | self.assertEqual(repr(a2), "[0, 1, 2]") |
---|
50 | n/a | |
---|
51 | n/a | a2.append(a2) |
---|
52 | n/a | a2.append(3) |
---|
53 | n/a | self.assertEqual(str(a2), "[0, 1, 2, [...], 3]") |
---|
54 | n/a | self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]") |
---|
55 | n/a | |
---|
56 | n/a | l0 = [] |
---|
57 | n/a | for i in range(sys.getrecursionlimit() + 100): |
---|
58 | n/a | l0 = [l0] |
---|
59 | n/a | self.assertRaises(RecursionError, repr, l0) |
---|
60 | n/a | |
---|
61 | n/a | def test_print(self): |
---|
62 | n/a | d = self.type2test(range(200)) |
---|
63 | n/a | d.append(d) |
---|
64 | n/a | d.extend(range(200,400)) |
---|
65 | n/a | d.append(d) |
---|
66 | n/a | d.append(400) |
---|
67 | n/a | try: |
---|
68 | n/a | with open(support.TESTFN, "w") as fo: |
---|
69 | n/a | fo.write(str(d)) |
---|
70 | n/a | with open(support.TESTFN, "r") as fo: |
---|
71 | n/a | self.assertEqual(fo.read(), repr(d)) |
---|
72 | n/a | finally: |
---|
73 | n/a | os.remove(support.TESTFN) |
---|
74 | n/a | |
---|
75 | n/a | def test_set_subscript(self): |
---|
76 | n/a | a = self.type2test(range(20)) |
---|
77 | n/a | self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3]) |
---|
78 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 10), 1) |
---|
79 | n/a | self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2]) |
---|
80 | n/a | self.assertRaises(TypeError, a.__getitem__, 'x', 1) |
---|
81 | n/a | a[slice(2,10,3)] = [1,2,3] |
---|
82 | n/a | self.assertEqual(a, self.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3, |
---|
83 | n/a | 9, 10, 11, 12, 13, 14, 15, |
---|
84 | n/a | 16, 17, 18, 19])) |
---|
85 | n/a | |
---|
86 | n/a | def test_reversed(self): |
---|
87 | n/a | a = self.type2test(range(20)) |
---|
88 | n/a | r = reversed(a) |
---|
89 | n/a | self.assertEqual(list(r), self.type2test(range(19, -1, -1))) |
---|
90 | n/a | self.assertRaises(StopIteration, next, r) |
---|
91 | n/a | self.assertEqual(list(reversed(self.type2test())), |
---|
92 | n/a | self.type2test()) |
---|
93 | n/a | # Bug 3689: make sure list-reversed-iterator doesn't have __len__ |
---|
94 | n/a | self.assertRaises(TypeError, len, reversed([1,2,3])) |
---|
95 | n/a | |
---|
96 | n/a | def test_setitem(self): |
---|
97 | n/a | a = self.type2test([0, 1]) |
---|
98 | n/a | a[0] = 0 |
---|
99 | n/a | a[1] = 100 |
---|
100 | n/a | self.assertEqual(a, self.type2test([0, 100])) |
---|
101 | n/a | a[-1] = 200 |
---|
102 | n/a | self.assertEqual(a, self.type2test([0, 200])) |
---|
103 | n/a | a[-2] = 100 |
---|
104 | n/a | self.assertEqual(a, self.type2test([100, 200])) |
---|
105 | n/a | self.assertRaises(IndexError, a.__setitem__, -3, 200) |
---|
106 | n/a | self.assertRaises(IndexError, a.__setitem__, 2, 200) |
---|
107 | n/a | |
---|
108 | n/a | a = self.type2test([]) |
---|
109 | n/a | self.assertRaises(IndexError, a.__setitem__, 0, 200) |
---|
110 | n/a | self.assertRaises(IndexError, a.__setitem__, -1, 200) |
---|
111 | n/a | self.assertRaises(TypeError, a.__setitem__) |
---|
112 | n/a | |
---|
113 | n/a | a = self.type2test([0,1,2,3,4]) |
---|
114 | n/a | a[0] = 1 |
---|
115 | n/a | a[1] = 2 |
---|
116 | n/a | a[2] = 3 |
---|
117 | n/a | self.assertEqual(a, self.type2test([1,2,3,3,4])) |
---|
118 | n/a | a[0] = 5 |
---|
119 | n/a | a[1] = 6 |
---|
120 | n/a | a[2] = 7 |
---|
121 | n/a | self.assertEqual(a, self.type2test([5,6,7,3,4])) |
---|
122 | n/a | a[-2] = 88 |
---|
123 | n/a | a[-1] = 99 |
---|
124 | n/a | self.assertEqual(a, self.type2test([5,6,7,88,99])) |
---|
125 | n/a | a[-2] = 8 |
---|
126 | n/a | a[-1] = 9 |
---|
127 | n/a | self.assertEqual(a, self.type2test([5,6,7,8,9])) |
---|
128 | n/a | |
---|
129 | n/a | msg = "list indices must be integers or slices" |
---|
130 | n/a | with self.assertRaisesRegex(TypeError, msg): |
---|
131 | n/a | a['a'] = "python" |
---|
132 | n/a | |
---|
133 | n/a | def test_delitem(self): |
---|
134 | n/a | a = self.type2test([0, 1]) |
---|
135 | n/a | del a[1] |
---|
136 | n/a | self.assertEqual(a, [0]) |
---|
137 | n/a | del a[0] |
---|
138 | n/a | self.assertEqual(a, []) |
---|
139 | n/a | |
---|
140 | n/a | a = self.type2test([0, 1]) |
---|
141 | n/a | del a[-2] |
---|
142 | n/a | self.assertEqual(a, [1]) |
---|
143 | n/a | del a[-1] |
---|
144 | n/a | self.assertEqual(a, []) |
---|
145 | n/a | |
---|
146 | n/a | a = self.type2test([0, 1]) |
---|
147 | n/a | self.assertRaises(IndexError, a.__delitem__, -3) |
---|
148 | n/a | self.assertRaises(IndexError, a.__delitem__, 2) |
---|
149 | n/a | |
---|
150 | n/a | a = self.type2test([]) |
---|
151 | n/a | self.assertRaises(IndexError, a.__delitem__, 0) |
---|
152 | n/a | |
---|
153 | n/a | self.assertRaises(TypeError, a.__delitem__) |
---|
154 | n/a | |
---|
155 | n/a | def test_setslice(self): |
---|
156 | n/a | l = [0, 1] |
---|
157 | n/a | a = self.type2test(l) |
---|
158 | n/a | |
---|
159 | n/a | for i in range(-3, 4): |
---|
160 | n/a | a[:i] = l[:i] |
---|
161 | n/a | self.assertEqual(a, l) |
---|
162 | n/a | a2 = a[:] |
---|
163 | n/a | a2[:i] = a[:i] |
---|
164 | n/a | self.assertEqual(a2, a) |
---|
165 | n/a | a[i:] = l[i:] |
---|
166 | n/a | self.assertEqual(a, l) |
---|
167 | n/a | a2 = a[:] |
---|
168 | n/a | a2[i:] = a[i:] |
---|
169 | n/a | self.assertEqual(a2, a) |
---|
170 | n/a | for j in range(-3, 4): |
---|
171 | n/a | a[i:j] = l[i:j] |
---|
172 | n/a | self.assertEqual(a, l) |
---|
173 | n/a | a2 = a[:] |
---|
174 | n/a | a2[i:j] = a[i:j] |
---|
175 | n/a | self.assertEqual(a2, a) |
---|
176 | n/a | |
---|
177 | n/a | aa2 = a2[:] |
---|
178 | n/a | aa2[:0] = [-2, -1] |
---|
179 | n/a | self.assertEqual(aa2, [-2, -1, 0, 1]) |
---|
180 | n/a | aa2[0:] = [] |
---|
181 | n/a | self.assertEqual(aa2, []) |
---|
182 | n/a | |
---|
183 | n/a | a = self.type2test([1, 2, 3, 4, 5]) |
---|
184 | n/a | a[:-1] = a |
---|
185 | n/a | self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 5])) |
---|
186 | n/a | a = self.type2test([1, 2, 3, 4, 5]) |
---|
187 | n/a | a[1:] = a |
---|
188 | n/a | self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5])) |
---|
189 | n/a | a = self.type2test([1, 2, 3, 4, 5]) |
---|
190 | n/a | a[1:-1] = a |
---|
191 | n/a | self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5, 5])) |
---|
192 | n/a | |
---|
193 | n/a | a = self.type2test([]) |
---|
194 | n/a | a[:] = tuple(range(10)) |
---|
195 | n/a | self.assertEqual(a, self.type2test(range(10))) |
---|
196 | n/a | |
---|
197 | n/a | self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5)) |
---|
198 | n/a | |
---|
199 | n/a | self.assertRaises(TypeError, a.__setitem__) |
---|
200 | n/a | |
---|
201 | n/a | def test_delslice(self): |
---|
202 | n/a | a = self.type2test([0, 1]) |
---|
203 | n/a | del a[1:2] |
---|
204 | n/a | del a[0:1] |
---|
205 | n/a | self.assertEqual(a, self.type2test([])) |
---|
206 | n/a | |
---|
207 | n/a | a = self.type2test([0, 1]) |
---|
208 | n/a | del a[1:2] |
---|
209 | n/a | del a[0:1] |
---|
210 | n/a | self.assertEqual(a, self.type2test([])) |
---|
211 | n/a | |
---|
212 | n/a | a = self.type2test([0, 1]) |
---|
213 | n/a | del a[-2:-1] |
---|
214 | n/a | self.assertEqual(a, self.type2test([1])) |
---|
215 | n/a | |
---|
216 | n/a | a = self.type2test([0, 1]) |
---|
217 | n/a | del a[-2:-1] |
---|
218 | n/a | self.assertEqual(a, self.type2test([1])) |
---|
219 | n/a | |
---|
220 | n/a | a = self.type2test([0, 1]) |
---|
221 | n/a | del a[1:] |
---|
222 | n/a | del a[:1] |
---|
223 | n/a | self.assertEqual(a, self.type2test([])) |
---|
224 | n/a | |
---|
225 | n/a | a = self.type2test([0, 1]) |
---|
226 | n/a | del a[1:] |
---|
227 | n/a | del a[:1] |
---|
228 | n/a | self.assertEqual(a, self.type2test([])) |
---|
229 | n/a | |
---|
230 | n/a | a = self.type2test([0, 1]) |
---|
231 | n/a | del a[-1:] |
---|
232 | n/a | self.assertEqual(a, self.type2test([0])) |
---|
233 | n/a | |
---|
234 | n/a | a = self.type2test([0, 1]) |
---|
235 | n/a | del a[-1:] |
---|
236 | n/a | self.assertEqual(a, self.type2test([0])) |
---|
237 | n/a | |
---|
238 | n/a | a = self.type2test([0, 1]) |
---|
239 | n/a | del a[:] |
---|
240 | n/a | self.assertEqual(a, self.type2test([])) |
---|
241 | n/a | |
---|
242 | n/a | def test_append(self): |
---|
243 | n/a | a = self.type2test([]) |
---|
244 | n/a | a.append(0) |
---|
245 | n/a | a.append(1) |
---|
246 | n/a | a.append(2) |
---|
247 | n/a | self.assertEqual(a, self.type2test([0, 1, 2])) |
---|
248 | n/a | |
---|
249 | n/a | self.assertRaises(TypeError, a.append) |
---|
250 | n/a | |
---|
251 | n/a | def test_extend(self): |
---|
252 | n/a | a1 = self.type2test([0]) |
---|
253 | n/a | a2 = self.type2test((0, 1)) |
---|
254 | n/a | a = a1[:] |
---|
255 | n/a | a.extend(a2) |
---|
256 | n/a | self.assertEqual(a, a1 + a2) |
---|
257 | n/a | |
---|
258 | n/a | a.extend(self.type2test([])) |
---|
259 | n/a | self.assertEqual(a, a1 + a2) |
---|
260 | n/a | |
---|
261 | n/a | a.extend(a) |
---|
262 | n/a | self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1])) |
---|
263 | n/a | |
---|
264 | n/a | a = self.type2test("spam") |
---|
265 | n/a | a.extend("eggs") |
---|
266 | n/a | self.assertEqual(a, list("spameggs")) |
---|
267 | n/a | |
---|
268 | n/a | self.assertRaises(TypeError, a.extend, None) |
---|
269 | n/a | self.assertRaises(TypeError, a.extend) |
---|
270 | n/a | |
---|
271 | n/a | # overflow test. issue1621 |
---|
272 | n/a | class CustomIter: |
---|
273 | n/a | def __iter__(self): |
---|
274 | n/a | return self |
---|
275 | n/a | def __next__(self): |
---|
276 | n/a | raise StopIteration |
---|
277 | n/a | def __length_hint__(self): |
---|
278 | n/a | return sys.maxsize |
---|
279 | n/a | a = self.type2test([1,2,3,4]) |
---|
280 | n/a | a.extend(CustomIter()) |
---|
281 | n/a | self.assertEqual(a, [1,2,3,4]) |
---|
282 | n/a | |
---|
283 | n/a | |
---|
284 | n/a | def test_insert(self): |
---|
285 | n/a | a = self.type2test([0, 1, 2]) |
---|
286 | n/a | a.insert(0, -2) |
---|
287 | n/a | a.insert(1, -1) |
---|
288 | n/a | a.insert(2, 0) |
---|
289 | n/a | self.assertEqual(a, [-2, -1, 0, 0, 1, 2]) |
---|
290 | n/a | |
---|
291 | n/a | b = a[:] |
---|
292 | n/a | b.insert(-2, "foo") |
---|
293 | n/a | b.insert(-200, "left") |
---|
294 | n/a | b.insert(200, "right") |
---|
295 | n/a | self.assertEqual(b, self.type2test(["left",-2,-1,0,0,"foo",1,2,"right"])) |
---|
296 | n/a | |
---|
297 | n/a | self.assertRaises(TypeError, a.insert) |
---|
298 | n/a | |
---|
299 | n/a | def test_pop(self): |
---|
300 | n/a | a = self.type2test([-1, 0, 1]) |
---|
301 | n/a | a.pop() |
---|
302 | n/a | self.assertEqual(a, [-1, 0]) |
---|
303 | n/a | a.pop(0) |
---|
304 | n/a | self.assertEqual(a, [0]) |
---|
305 | n/a | self.assertRaises(IndexError, a.pop, 5) |
---|
306 | n/a | a.pop(0) |
---|
307 | n/a | self.assertEqual(a, []) |
---|
308 | n/a | self.assertRaises(IndexError, a.pop) |
---|
309 | n/a | self.assertRaises(TypeError, a.pop, 42, 42) |
---|
310 | n/a | a = self.type2test([0, 10, 20, 30, 40]) |
---|
311 | n/a | |
---|
312 | n/a | def test_remove(self): |
---|
313 | n/a | a = self.type2test([0, 0, 1]) |
---|
314 | n/a | a.remove(1) |
---|
315 | n/a | self.assertEqual(a, [0, 0]) |
---|
316 | n/a | a.remove(0) |
---|
317 | n/a | self.assertEqual(a, [0]) |
---|
318 | n/a | a.remove(0) |
---|
319 | n/a | self.assertEqual(a, []) |
---|
320 | n/a | |
---|
321 | n/a | self.assertRaises(ValueError, a.remove, 0) |
---|
322 | n/a | |
---|
323 | n/a | self.assertRaises(TypeError, a.remove) |
---|
324 | n/a | |
---|
325 | n/a | class BadExc(Exception): |
---|
326 | n/a | pass |
---|
327 | n/a | |
---|
328 | n/a | class BadCmp: |
---|
329 | n/a | def __eq__(self, other): |
---|
330 | n/a | if other == 2: |
---|
331 | n/a | raise BadExc() |
---|
332 | n/a | return False |
---|
333 | n/a | |
---|
334 | n/a | a = self.type2test([0, 1, 2, 3]) |
---|
335 | n/a | self.assertRaises(BadExc, a.remove, BadCmp()) |
---|
336 | n/a | |
---|
337 | n/a | class BadCmp2: |
---|
338 | n/a | def __eq__(self, other): |
---|
339 | n/a | raise BadExc() |
---|
340 | n/a | |
---|
341 | n/a | d = self.type2test('abcdefghcij') |
---|
342 | n/a | d.remove('c') |
---|
343 | n/a | self.assertEqual(d, self.type2test('abdefghcij')) |
---|
344 | n/a | d.remove('c') |
---|
345 | n/a | self.assertEqual(d, self.type2test('abdefghij')) |
---|
346 | n/a | self.assertRaises(ValueError, d.remove, 'c') |
---|
347 | n/a | self.assertEqual(d, self.type2test('abdefghij')) |
---|
348 | n/a | |
---|
349 | n/a | # Handle comparison errors |
---|
350 | n/a | d = self.type2test(['a', 'b', BadCmp2(), 'c']) |
---|
351 | n/a | e = self.type2test(d) |
---|
352 | n/a | self.assertRaises(BadExc, d.remove, 'c') |
---|
353 | n/a | for x, y in zip(d, e): |
---|
354 | n/a | # verify that original order and values are retained. |
---|
355 | n/a | self.assertIs(x, y) |
---|
356 | n/a | |
---|
357 | n/a | def test_count(self): |
---|
358 | n/a | a = self.type2test([0, 1, 2])*3 |
---|
359 | n/a | self.assertEqual(a.count(0), 3) |
---|
360 | n/a | self.assertEqual(a.count(1), 3) |
---|
361 | n/a | self.assertEqual(a.count(3), 0) |
---|
362 | n/a | |
---|
363 | n/a | self.assertRaises(TypeError, a.count) |
---|
364 | n/a | |
---|
365 | n/a | class BadExc(Exception): |
---|
366 | n/a | pass |
---|
367 | n/a | |
---|
368 | n/a | class BadCmp: |
---|
369 | n/a | def __eq__(self, other): |
---|
370 | n/a | if other == 2: |
---|
371 | n/a | raise BadExc() |
---|
372 | n/a | return False |
---|
373 | n/a | |
---|
374 | n/a | self.assertRaises(BadExc, a.count, BadCmp()) |
---|
375 | n/a | |
---|
376 | n/a | def test_index(self): |
---|
377 | n/a | u = self.type2test([0, 1]) |
---|
378 | n/a | self.assertEqual(u.index(0), 0) |
---|
379 | n/a | self.assertEqual(u.index(1), 1) |
---|
380 | n/a | self.assertRaises(ValueError, u.index, 2) |
---|
381 | n/a | |
---|
382 | n/a | u = self.type2test([-2, -1, 0, 0, 1, 2]) |
---|
383 | n/a | self.assertEqual(u.count(0), 2) |
---|
384 | n/a | self.assertEqual(u.index(0), 2) |
---|
385 | n/a | self.assertEqual(u.index(0, 2), 2) |
---|
386 | n/a | self.assertEqual(u.index(-2, -10), 0) |
---|
387 | n/a | self.assertEqual(u.index(0, 3), 3) |
---|
388 | n/a | self.assertEqual(u.index(0, 3, 4), 3) |
---|
389 | n/a | self.assertRaises(ValueError, u.index, 2, 0, -10) |
---|
390 | n/a | |
---|
391 | n/a | self.assertRaises(TypeError, u.index) |
---|
392 | n/a | |
---|
393 | n/a | class BadExc(Exception): |
---|
394 | n/a | pass |
---|
395 | n/a | |
---|
396 | n/a | class BadCmp: |
---|
397 | n/a | def __eq__(self, other): |
---|
398 | n/a | if other == 2: |
---|
399 | n/a | raise BadExc() |
---|
400 | n/a | return False |
---|
401 | n/a | |
---|
402 | n/a | a = self.type2test([0, 1, 2, 3]) |
---|
403 | n/a | self.assertRaises(BadExc, a.index, BadCmp()) |
---|
404 | n/a | |
---|
405 | n/a | a = self.type2test([-2, -1, 0, 0, 1, 2]) |
---|
406 | n/a | self.assertEqual(a.index(0), 2) |
---|
407 | n/a | self.assertEqual(a.index(0, 2), 2) |
---|
408 | n/a | self.assertEqual(a.index(0, -4), 2) |
---|
409 | n/a | self.assertEqual(a.index(-2, -10), 0) |
---|
410 | n/a | self.assertEqual(a.index(0, 3), 3) |
---|
411 | n/a | self.assertEqual(a.index(0, -3), 3) |
---|
412 | n/a | self.assertEqual(a.index(0, 3, 4), 3) |
---|
413 | n/a | self.assertEqual(a.index(0, -3, -2), 3) |
---|
414 | n/a | self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2) |
---|
415 | n/a | self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize) |
---|
416 | n/a | self.assertRaises(ValueError, a.index, 2, 0, -10) |
---|
417 | n/a | a.remove(0) |
---|
418 | n/a | self.assertRaises(ValueError, a.index, 2, 0, 4) |
---|
419 | n/a | self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2])) |
---|
420 | n/a | |
---|
421 | n/a | # Test modifying the list during index's iteration |
---|
422 | n/a | class EvilCmp: |
---|
423 | n/a | def __init__(self, victim): |
---|
424 | n/a | self.victim = victim |
---|
425 | n/a | def __eq__(self, other): |
---|
426 | n/a | del self.victim[:] |
---|
427 | n/a | return False |
---|
428 | n/a | a = self.type2test() |
---|
429 | n/a | a[:] = [EvilCmp(a) for _ in range(100)] |
---|
430 | n/a | # This used to seg fault before patch #1005778 |
---|
431 | n/a | self.assertRaises(ValueError, a.index, None) |
---|
432 | n/a | |
---|
433 | n/a | def test_reverse(self): |
---|
434 | n/a | u = self.type2test([-2, -1, 0, 1, 2]) |
---|
435 | n/a | u2 = u[:] |
---|
436 | n/a | u.reverse() |
---|
437 | n/a | self.assertEqual(u, [2, 1, 0, -1, -2]) |
---|
438 | n/a | u.reverse() |
---|
439 | n/a | self.assertEqual(u, u2) |
---|
440 | n/a | |
---|
441 | n/a | self.assertRaises(TypeError, u.reverse, 42) |
---|
442 | n/a | |
---|
443 | n/a | def test_clear(self): |
---|
444 | n/a | u = self.type2test([2, 3, 4]) |
---|
445 | n/a | u.clear() |
---|
446 | n/a | self.assertEqual(u, []) |
---|
447 | n/a | |
---|
448 | n/a | u = self.type2test([]) |
---|
449 | n/a | u.clear() |
---|
450 | n/a | self.assertEqual(u, []) |
---|
451 | n/a | |
---|
452 | n/a | u = self.type2test([]) |
---|
453 | n/a | u.append(1) |
---|
454 | n/a | u.clear() |
---|
455 | n/a | u.append(2) |
---|
456 | n/a | self.assertEqual(u, [2]) |
---|
457 | n/a | |
---|
458 | n/a | self.assertRaises(TypeError, u.clear, None) |
---|
459 | n/a | |
---|
460 | n/a | def test_copy(self): |
---|
461 | n/a | u = self.type2test([1, 2, 3]) |
---|
462 | n/a | v = u.copy() |
---|
463 | n/a | self.assertEqual(v, [1, 2, 3]) |
---|
464 | n/a | |
---|
465 | n/a | u = self.type2test([]) |
---|
466 | n/a | v = u.copy() |
---|
467 | n/a | self.assertEqual(v, []) |
---|
468 | n/a | |
---|
469 | n/a | # test that it's indeed a copy and not a reference |
---|
470 | n/a | u = self.type2test(['a', 'b']) |
---|
471 | n/a | v = u.copy() |
---|
472 | n/a | v.append('i') |
---|
473 | n/a | self.assertEqual(u, ['a', 'b']) |
---|
474 | n/a | self.assertEqual(v, u + ['i']) |
---|
475 | n/a | |
---|
476 | n/a | # test that it's a shallow, not a deep copy |
---|
477 | n/a | u = self.type2test([1, 2, [3, 4], 5]) |
---|
478 | n/a | v = u.copy() |
---|
479 | n/a | self.assertEqual(u, v) |
---|
480 | n/a | self.assertIs(v[3], u[3]) |
---|
481 | n/a | |
---|
482 | n/a | self.assertRaises(TypeError, u.copy, None) |
---|
483 | n/a | |
---|
484 | n/a | def test_sort(self): |
---|
485 | n/a | u = self.type2test([1, 0]) |
---|
486 | n/a | u.sort() |
---|
487 | n/a | self.assertEqual(u, [0, 1]) |
---|
488 | n/a | |
---|
489 | n/a | u = self.type2test([2,1,0,-1,-2]) |
---|
490 | n/a | u.sort() |
---|
491 | n/a | self.assertEqual(u, self.type2test([-2,-1,0,1,2])) |
---|
492 | n/a | |
---|
493 | n/a | self.assertRaises(TypeError, u.sort, 42, 42) |
---|
494 | n/a | |
---|
495 | n/a | def revcmp(a, b): |
---|
496 | n/a | if a == b: |
---|
497 | n/a | return 0 |
---|
498 | n/a | elif a < b: |
---|
499 | n/a | return 1 |
---|
500 | n/a | else: # a > b |
---|
501 | n/a | return -1 |
---|
502 | n/a | u.sort(key=cmp_to_key(revcmp)) |
---|
503 | n/a | self.assertEqual(u, self.type2test([2,1,0,-1,-2])) |
---|
504 | n/a | |
---|
505 | n/a | # The following dumps core in unpatched Python 1.5: |
---|
506 | n/a | def myComparison(x,y): |
---|
507 | n/a | xmod, ymod = x%3, y%7 |
---|
508 | n/a | if xmod == ymod: |
---|
509 | n/a | return 0 |
---|
510 | n/a | elif xmod < ymod: |
---|
511 | n/a | return -1 |
---|
512 | n/a | else: # xmod > ymod |
---|
513 | n/a | return 1 |
---|
514 | n/a | z = self.type2test(range(12)) |
---|
515 | n/a | z.sort(key=cmp_to_key(myComparison)) |
---|
516 | n/a | |
---|
517 | n/a | self.assertRaises(TypeError, z.sort, 2) |
---|
518 | n/a | |
---|
519 | n/a | def selfmodifyingComparison(x,y): |
---|
520 | n/a | z.append(1) |
---|
521 | n/a | if x == y: |
---|
522 | n/a | return 0 |
---|
523 | n/a | elif x < y: |
---|
524 | n/a | return -1 |
---|
525 | n/a | else: # x > y |
---|
526 | n/a | return 1 |
---|
527 | n/a | self.assertRaises(ValueError, z.sort, |
---|
528 | n/a | key=cmp_to_key(selfmodifyingComparison)) |
---|
529 | n/a | |
---|
530 | n/a | self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) |
---|
531 | n/a | |
---|
532 | n/a | def test_slice(self): |
---|
533 | n/a | u = self.type2test("spam") |
---|
534 | n/a | u[:2] = "h" |
---|
535 | n/a | self.assertEqual(u, list("ham")) |
---|
536 | n/a | |
---|
537 | n/a | def test_iadd(self): |
---|
538 | n/a | super().test_iadd() |
---|
539 | n/a | u = self.type2test([0, 1]) |
---|
540 | n/a | u2 = u |
---|
541 | n/a | u += [2, 3] |
---|
542 | n/a | self.assertIs(u, u2) |
---|
543 | n/a | |
---|
544 | n/a | u = self.type2test("spam") |
---|
545 | n/a | u += "eggs" |
---|
546 | n/a | self.assertEqual(u, self.type2test("spameggs")) |
---|
547 | n/a | |
---|
548 | n/a | self.assertRaises(TypeError, u.__iadd__, None) |
---|
549 | n/a | |
---|
550 | n/a | def test_imul(self): |
---|
551 | n/a | u = self.type2test([0, 1]) |
---|
552 | n/a | u *= 3 |
---|
553 | n/a | self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) |
---|
554 | n/a | u *= 0 |
---|
555 | n/a | self.assertEqual(u, self.type2test([])) |
---|
556 | n/a | s = self.type2test([]) |
---|
557 | n/a | oldid = id(s) |
---|
558 | n/a | s *= 10 |
---|
559 | n/a | self.assertEqual(id(s), oldid) |
---|
560 | n/a | |
---|
561 | n/a | def test_extendedslicing(self): |
---|
562 | n/a | # subscript |
---|
563 | n/a | a = self.type2test([0,1,2,3,4]) |
---|
564 | n/a | |
---|
565 | n/a | # deletion |
---|
566 | n/a | del a[::2] |
---|
567 | n/a | self.assertEqual(a, self.type2test([1,3])) |
---|
568 | n/a | a = self.type2test(range(5)) |
---|
569 | n/a | del a[1::2] |
---|
570 | n/a | self.assertEqual(a, self.type2test([0,2,4])) |
---|
571 | n/a | a = self.type2test(range(5)) |
---|
572 | n/a | del a[1::-2] |
---|
573 | n/a | self.assertEqual(a, self.type2test([0,2,3,4])) |
---|
574 | n/a | a = self.type2test(range(10)) |
---|
575 | n/a | del a[::1000] |
---|
576 | n/a | self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9])) |
---|
577 | n/a | # assignment |
---|
578 | n/a | a = self.type2test(range(10)) |
---|
579 | n/a | a[::2] = [-1]*5 |
---|
580 | n/a | self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])) |
---|
581 | n/a | a = self.type2test(range(10)) |
---|
582 | n/a | a[::-4] = [10]*3 |
---|
583 | n/a | self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
---|
584 | n/a | a = self.type2test(range(4)) |
---|
585 | n/a | a[::-1] = a |
---|
586 | n/a | self.assertEqual(a, self.type2test([3, 2, 1, 0])) |
---|
587 | n/a | a = self.type2test(range(10)) |
---|
588 | n/a | b = a[:] |
---|
589 | n/a | c = a[:] |
---|
590 | n/a | a[2:3] = self.type2test(["two", "elements"]) |
---|
591 | n/a | b[slice(2,3)] = self.type2test(["two", "elements"]) |
---|
592 | n/a | c[2:3:] = self.type2test(["two", "elements"]) |
---|
593 | n/a | self.assertEqual(a, b) |
---|
594 | n/a | self.assertEqual(a, c) |
---|
595 | n/a | a = self.type2test(range(10)) |
---|
596 | n/a | a[::2] = tuple(range(5)) |
---|
597 | n/a | self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9])) |
---|
598 | n/a | # test issue7788 |
---|
599 | n/a | a = self.type2test(range(10)) |
---|
600 | n/a | del a[9::1<<333] |
---|
601 | n/a | |
---|
602 | n/a | def test_constructor_exception_handling(self): |
---|
603 | n/a | # Bug #1242657 |
---|
604 | n/a | class F(object): |
---|
605 | n/a | def __iter__(self): |
---|
606 | n/a | raise KeyboardInterrupt |
---|
607 | n/a | self.assertRaises(KeyboardInterrupt, list, F()) |
---|
608 | n/a | |
---|
609 | n/a | def test_exhausted_iterator(self): |
---|
610 | n/a | a = self.type2test([1, 2, 3]) |
---|
611 | n/a | exhit = iter(a) |
---|
612 | n/a | empit = iter(a) |
---|
613 | n/a | for x in exhit: # exhaust the iterator |
---|
614 | n/a | next(empit) # not exhausted |
---|
615 | n/a | a.append(9) |
---|
616 | n/a | self.assertEqual(list(exhit), []) |
---|
617 | n/a | self.assertEqual(list(empit), [9]) |
---|
618 | n/a | self.assertEqual(a, self.type2test([1, 2, 3, 9])) |
---|