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