| 1 |
|
| 2 |
import py |
| 3 |
import random |
| 4 |
from pypy.objspace.std.listobject import W_ListObject, SizeListStrategy,\ |
| 5 |
IntegerListStrategy, BytesListStrategy, FloatListStrategy, \ |
| 6 |
ObjectListStrategy, IntOrFloatListStrategy, AsciiListStrategy |
| 7 |
from pypy.interpreter.error import OperationError |
| 8 |
from rpython.rlib.rarithmetic import is_valid_int |
| 9 |
|
| 10 |
|
| 11 |
class TestW_ListObject(object): |
| 12 |
def test_is_true(self): |
| 13 |
w = self.space.wrap |
| 14 |
w_list = W_ListObject(self.space, []) |
| 15 |
assert self.space.is_true(w_list) == False |
| 16 |
w_list = W_ListObject(self.space, [w(5)]) |
| 17 |
assert self.space.is_true(w_list) == True |
| 18 |
w_list = W_ListObject(self.space, [w(5), w(3)]) |
| 19 |
assert self.space.is_true(w_list) == True |
| 20 |
|
| 21 |
def test_len(self): |
| 22 |
w = self.space.wrap |
| 23 |
w_list = W_ListObject(self.space, []) |
| 24 |
assert self.space.eq_w(self.space.len(w_list), w(0)) |
| 25 |
w_list = W_ListObject(self.space, [w(5)]) |
| 26 |
assert self.space.eq_w(self.space.len(w_list), w(1)) |
| 27 |
w_list = W_ListObject(self.space, [w(5), w(3), w(99)]*111) |
| 28 |
assert self.space.eq_w(self.space.len(w_list), w(333)) |
| 29 |
w_list = W_ListObject(self.space, [w(u'\u2039')]) |
| 30 |
assert self.space.eq_w(self.space.len(w_list), w(1)) |
| 31 |
|
| 32 |
def test_getitem(self): |
| 33 |
w = self.space.wrap |
| 34 |
w_list = W_ListObject(self.space, [w(5), w(3)]) |
| 35 |
assert self.space.eq_w(self.space.getitem(w_list, w(0)), w(5)) |
| 36 |
assert self.space.eq_w(self.space.getitem(w_list, w(1)), w(3)) |
| 37 |
assert self.space.eq_w(self.space.getitem(w_list, w(-2)), w(5)) |
| 38 |
assert self.space.eq_w(self.space.getitem(w_list, w(-1)), w(3)) |
| 39 |
self.space.raises_w(self.space.w_IndexError, |
| 40 |
self.space.getitem, w_list, w(2)) |
| 41 |
self.space.raises_w(self.space.w_IndexError, |
| 42 |
self.space.getitem, w_list, w(42)) |
| 43 |
self.space.raises_w(self.space.w_IndexError, |
| 44 |
self.space.getitem, w_list, w(-3)) |
| 45 |
|
| 46 |
def test_getitems(self): |
| 47 |
w = self.space.wrap |
| 48 |
from pypy.objspace.std.listobject import make_range_list |
| 49 |
r = make_range_list(self.space, 1,1,7) |
| 50 |
l = [w(1),w(2),w(3),w(4),w(5),w(6),w(7)] |
| 51 |
l2 = r.getitems() |
| 52 |
for i in range(7): |
| 53 |
assert self.space.eq_w(l[i], l2[i]) |
| 54 |
|
| 55 |
def test_getitems_fixedsize(self): |
| 56 |
w = self.space.wrap |
| 57 |
from pypy.objspace.std.listobject import make_range_list |
| 58 |
rangelist = make_range_list(self.space, 1,1,7) |
| 59 |
emptylist = W_ListObject(self.space, []) |
| 60 |
intlist = W_ListObject(self.space, [w(1),w(2),w(3),w(4),w(5),w(6),w(7)]) |
| 61 |
strlist = W_ListObject(self.space, [w('1'),w('2'),w('3'),w('4'),w('5'),w('6'),w('7')]) |
| 62 |
floatlist = W_ListObject(self.space, [w(1.0),w(2.0),w(3.0),w(4.0),w(5.0),w(6.0),w(7.0)]) |
| 63 |
objlist = W_ListObject(self.space, [w(1),w('2'),w(3.0),w(4),w(5),w(6),w(7)]) |
| 64 |
|
| 65 |
emptylist_copy = emptylist.getitems_fixedsize() |
| 66 |
assert emptylist_copy == [] |
| 67 |
|
| 68 |
rangelist_copy = rangelist.getitems_fixedsize() |
| 69 |
intlist_copy = intlist.getitems_fixedsize() |
| 70 |
strlist_copy = strlist.getitems_fixedsize() |
| 71 |
floatlist_copy = floatlist.getitems_fixedsize() |
| 72 |
objlist_copy = objlist.getitems_fixedsize() |
| 73 |
for i in range(7): |
| 74 |
assert self.space.eq_w(rangelist_copy[i], rangelist.getitem(i)) |
| 75 |
assert self.space.eq_w(intlist_copy[i], intlist.getitem(i)) |
| 76 |
assert self.space.eq_w(strlist_copy[i], strlist.getitem(i)) |
| 77 |
assert self.space.eq_w(floatlist_copy[i], floatlist.getitem(i)) |
| 78 |
assert self.space.eq_w(objlist_copy[i], objlist.getitem(i)) |
| 79 |
|
| 80 |
emptylist_copy = emptylist.getitems_unroll() |
| 81 |
assert emptylist_copy == [] |
| 82 |
|
| 83 |
rangelist_copy = rangelist.getitems_unroll() |
| 84 |
intlist_copy = intlist.getitems_unroll() |
| 85 |
strlist_copy = strlist.getitems_unroll() |
| 86 |
floatlist_copy = floatlist.getitems_unroll() |
| 87 |
objlist_copy = objlist.getitems_unroll() |
| 88 |
for i in range(7): |
| 89 |
assert self.space.eq_w(rangelist_copy[i], rangelist.getitem(i)) |
| 90 |
assert self.space.eq_w(intlist_copy[i], intlist.getitem(i)) |
| 91 |
assert self.space.eq_w(strlist_copy[i], strlist.getitem(i)) |
| 92 |
assert self.space.eq_w(floatlist_copy[i], floatlist.getitem(i)) |
| 93 |
assert self.space.eq_w(objlist_copy[i], objlist.getitem(i)) |
| 94 |
|
| 95 |
def test_random_getitem(self): |
| 96 |
w = self.space.wrap |
| 97 |
s = list('qedx387tn3uixhvt 7fh387fymh3dh238 dwd-wq.dwq9') |
| 98 |
w_list = W_ListObject(self.space, map(w, s)) |
| 99 |
keys = range(-len(s)-5, len(s)+5) |
| 100 |
choices = keys + [None]*12 |
| 101 |
stepchoices = [None, None, None, 1, 1, -1, -1, 2, -2, |
| 102 |
len(s)-1, len(s), len(s)+1, |
| 103 |
-len(s)-1, -len(s), -len(s)+1] |
| 104 |
for i in range(40): |
| 105 |
keys.append(slice(random.choice(choices), |
| 106 |
random.choice(choices), |
| 107 |
random.choice(stepchoices))) |
| 108 |
random.shuffle(keys) |
| 109 |
for key in keys: |
| 110 |
try: |
| 111 |
expected = s[key] |
| 112 |
except IndexError: |
| 113 |
self.space.raises_w(self.space.w_IndexError, |
| 114 |
self.space.getitem, w_list, w(key)) |
| 115 |
else: |
| 116 |
w_result = self.space.getitem(w_list, w(key)) |
| 117 |
assert self.space.unwrap(w_result) == expected |
| 118 |
|
| 119 |
def test_iter(self): |
| 120 |
w = self.space.wrap |
| 121 |
w_list = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 122 |
w_iter = self.space.iter(w_list) |
| 123 |
assert self.space.eq_w(self.space.next(w_iter), w(5)) |
| 124 |
assert self.space.eq_w(self.space.next(w_iter), w(3)) |
| 125 |
assert self.space.eq_w(self.space.next(w_iter), w(99)) |
| 126 |
py.test.raises(OperationError, self.space.next, w_iter) |
| 127 |
py.test.raises(OperationError, self.space.next, w_iter) |
| 128 |
|
| 129 |
def test_contains(self): |
| 130 |
w = self.space.wrap |
| 131 |
w_list = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 132 |
assert self.space.eq_w(self.space.contains(w_list, w(5)), |
| 133 |
self.space.w_True) |
| 134 |
assert self.space.eq_w(self.space.contains(w_list, w(99)), |
| 135 |
self.space.w_True) |
| 136 |
assert self.space.eq_w(self.space.contains(w_list, w(11)), |
| 137 |
self.space.w_False) |
| 138 |
assert self.space.eq_w(self.space.contains(w_list, w_list), |
| 139 |
self.space.w_False) |
| 140 |
|
| 141 |
def test_getslice(self): |
| 142 |
w = self.space.wrap |
| 143 |
|
| 144 |
def test1(testlist, start, stop, step, expected): |
| 145 |
w_slice = self.space.newslice(w(start), w(stop), w(step)) |
| 146 |
w_list = W_ListObject(self.space, [w(i) for i in testlist]) |
| 147 |
w_result = self.space.getitem(w_list, w_slice) |
| 148 |
assert self.space.unwrap(w_result) == expected |
| 149 |
|
| 150 |
for testlist in [[], [5,3,99]]: |
| 151 |
for start in [-2, 0, 1, 10]: |
| 152 |
for end in [-1, 2, 999]: |
| 153 |
test1(testlist, start, end, 1, testlist[start:end]) |
| 154 |
|
| 155 |
test1([5,7,1,4], 3, 1, -2, [4,]) |
| 156 |
test1([5,7,1,4], 3, 0, -2, [4, 7]) |
| 157 |
test1([5,7,1,4], 3, -1, -2, []) |
| 158 |
test1([5,7,1,4], -2, 11, 2, [1,]) |
| 159 |
test1([5,7,1,4], -3, 11, 2, [7, 4]) |
| 160 |
test1([5,7,1,4], -5, 11, 2, [5, 1]) |
| 161 |
|
| 162 |
def test_setslice(self): |
| 163 |
w = self.space.wrap |
| 164 |
|
| 165 |
def test1(lhslist, start, stop, rhslist, expected): |
| 166 |
w_slice = self.space.newslice(w(start), w(stop), w(1)) |
| 167 |
w_lhslist = W_ListObject(self.space, [w(i) for i in lhslist]) |
| 168 |
w_rhslist = W_ListObject(self.space, [w(i) for i in rhslist]) |
| 169 |
self.space.setitem(w_lhslist, w_slice, w_rhslist) |
| 170 |
assert self.space.unwrap(w_lhslist) == expected |
| 171 |
|
| 172 |
test1([5,7,1,4], 1, 3, [9,8], [5,9,8,4]) |
| 173 |
test1([5,7,1,4], 1, 3, [9], [5,9,4]) |
| 174 |
test1([5,7,1,4], 1, 3, [9,8,6],[5,9,8,6,4]) |
| 175 |
test1([5,7,1,4], 1, 3, [], [5,4]) |
| 176 |
test1([5,7,1,4], 2, 2, [9], [5,7,9,1,4]) |
| 177 |
test1([5,7,1,4], 0, 99,[9,8], [9,8]) |
| 178 |
|
| 179 |
def test_add(self): |
| 180 |
w = self.space.wrap |
| 181 |
w_list0 = W_ListObject(self.space, []) |
| 182 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 183 |
w_list2 = W_ListObject(self.space, [w(-7)] * 111) |
| 184 |
assert self.space.eq_w(self.space.add(w_list1, w_list1), |
| 185 |
W_ListObject(self.space, [w(5), w(3), w(99), |
| 186 |
w(5), w(3), w(99)])) |
| 187 |
assert self.space.eq_w(self.space.add(w_list1, w_list2), |
| 188 |
W_ListObject(self.space, [w(5), w(3), w(99)] + |
| 189 |
[w(-7)] * 111)) |
| 190 |
assert self.space.eq_w(self.space.add(w_list1, w_list0), w_list1) |
| 191 |
assert self.space.eq_w(self.space.add(w_list0, w_list2), w_list2) |
| 192 |
|
| 193 |
def test_mul(self): |
| 194 |
|
| 195 |
w = self.space.wrap |
| 196 |
arg = w(2) |
| 197 |
n = 3 |
| 198 |
w_lis = W_ListObject(self.space, [arg]) |
| 199 |
w_lis3 = W_ListObject(self.space, [arg]*n) |
| 200 |
w_res = self.space.mul(w_lis, w(n)) |
| 201 |
assert self.space.eq_w(w_lis3, w_res) |
| 202 |
|
| 203 |
w_res = self.space.mul(w(n), w_lis) |
| 204 |
assert self.space.eq_w(w_lis3, w_res) |
| 205 |
|
| 206 |
def test_mul_does_not_clone(self): |
| 207 |
|
| 208 |
w = self.space.wrap |
| 209 |
arg = w(2) |
| 210 |
w_lis = W_ListObject(self.space, [arg]) |
| 211 |
w_lis.clone = None |
| 212 |
|
| 213 |
self.space.mul(w_lis, w(5)) |
| 214 |
|
| 215 |
def test_setitem(self): |
| 216 |
w = self.space.wrap |
| 217 |
w_list = W_ListObject(self.space, [w(5), w(3)]) |
| 218 |
w_exp1 = W_ListObject(self.space, [w(5), w(7)]) |
| 219 |
w_exp2 = W_ListObject(self.space, [w(8), w(7)]) |
| 220 |
self.space.setitem(w_list, w(1), w(7)) |
| 221 |
assert self.space.eq_w(w_exp1, w_list) |
| 222 |
self.space.setitem(w_list, w(-2), w(8)) |
| 223 |
assert self.space.eq_w(w_exp2, w_list) |
| 224 |
self.space.raises_w(self.space.w_IndexError, |
| 225 |
self.space.setitem, w_list, w(2), w(5)) |
| 226 |
self.space.raises_w(self.space.w_IndexError, |
| 227 |
self.space.setitem, w_list, w(-3), w(5)) |
| 228 |
|
| 229 |
def test_random_setitem_delitem(self): |
| 230 |
w = self.space.wrap |
| 231 |
s = range(39) |
| 232 |
w_list = W_ListObject(self.space, map(w, s)) |
| 233 |
expected = list(s) |
| 234 |
keys = range(-len(s)-5, len(s)+5) |
| 235 |
choices = keys + [None]*12 |
| 236 |
stepchoices = [None, None, None, 1, 1, -1, -1, 2, -2, |
| 237 |
len(s)-1, len(s), len(s)+1, |
| 238 |
-len(s)-1, -len(s), -len(s)+1] |
| 239 |
for i in range(50): |
| 240 |
keys.append(slice(random.choice(choices), |
| 241 |
random.choice(choices), |
| 242 |
random.choice(stepchoices))) |
| 243 |
random.shuffle(keys) |
| 244 |
n = len(s) |
| 245 |
for key in keys: |
| 246 |
if random.random() < 0.15: |
| 247 |
random.shuffle(s) |
| 248 |
w_list = W_ListObject(self.space, map(w, s)) |
| 249 |
expected = list(s) |
| 250 |
try: |
| 251 |
value = expected[key] |
| 252 |
except IndexError: |
| 253 |
self.space.raises_w(self.space.w_IndexError, |
| 254 |
self.space.setitem, w_list, w(key), w(42)) |
| 255 |
else: |
| 256 |
if is_valid_int(value): |
| 257 |
if random.random() < 0.25: |
| 258 |
self.space.delitem(w_list, w(key)) |
| 259 |
del expected[key] |
| 260 |
else: |
| 261 |
self.space.setitem(w_list, w(key), w(n)) |
| 262 |
expected[key] = n |
| 263 |
n += 1 |
| 264 |
else: |
| 265 |
mode = random.choice(['samesize', 'resize', 'delete']) |
| 266 |
if mode == 'delete': |
| 267 |
self.space.delitem(w_list, w(key)) |
| 268 |
del expected[key] |
| 269 |
elif mode == 'samesize': |
| 270 |
newvalue = range(n, n+len(value)) |
| 271 |
self.space.setitem(w_list, w(key), w(newvalue)) |
| 272 |
expected[key] = newvalue |
| 273 |
n += len(newvalue) |
| 274 |
elif mode == 'resize' and key.step is None: |
| 275 |
newvalue = range(n, n+random.randrange(0, 20)) |
| 276 |
self.space.setitem(w_list, w(key), w(newvalue)) |
| 277 |
expected[key] = newvalue |
| 278 |
n += len(newvalue) |
| 279 |
assert self.space.unwrap(w_list) == expected |
| 280 |
|
| 281 |
def test_eq(self): |
| 282 |
w = self.space.wrap |
| 283 |
|
| 284 |
w_list0 = W_ListObject(self.space, []) |
| 285 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 286 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 287 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 288 |
|
| 289 |
assert self.space.eq_w(self.space.eq(w_list0, w_list1), |
| 290 |
self.space.w_False) |
| 291 |
assert self.space.eq_w(self.space.eq(w_list1, w_list0), |
| 292 |
self.space.w_False) |
| 293 |
assert self.space.eq_w(self.space.eq(w_list1, w_list1), |
| 294 |
self.space.w_True) |
| 295 |
assert self.space.eq_w(self.space.eq(w_list1, w_list2), |
| 296 |
self.space.w_True) |
| 297 |
assert self.space.eq_w(self.space.eq(w_list2, w_list3), |
| 298 |
self.space.w_False) |
| 299 |
|
| 300 |
def test_ne(self): |
| 301 |
w = self.space.wrap |
| 302 |
|
| 303 |
w_list0 = W_ListObject(self.space, []) |
| 304 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 305 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 306 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 307 |
|
| 308 |
assert self.space.eq_w(self.space.ne(w_list0, w_list1), |
| 309 |
self.space.w_True) |
| 310 |
assert self.space.eq_w(self.space.ne(w_list1, w_list0), |
| 311 |
self.space.w_True) |
| 312 |
assert self.space.eq_w(self.space.ne(w_list1, w_list1), |
| 313 |
self.space.w_False) |
| 314 |
assert self.space.eq_w(self.space.ne(w_list1, w_list2), |
| 315 |
self.space.w_False) |
| 316 |
assert self.space.eq_w(self.space.ne(w_list2, w_list3), |
| 317 |
self.space.w_True) |
| 318 |
|
| 319 |
def test_lt(self): |
| 320 |
w = self.space.wrap |
| 321 |
|
| 322 |
w_list0 = W_ListObject(self.space, []) |
| 323 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 324 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 325 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 326 |
w_list4 = W_ListObject(self.space, [w(5), w(3), w(9), w(-1)]) |
| 327 |
|
| 328 |
assert self.space.eq_w(self.space.lt(w_list0, w_list1), |
| 329 |
self.space.w_True) |
| 330 |
assert self.space.eq_w(self.space.lt(w_list1, w_list0), |
| 331 |
self.space.w_False) |
| 332 |
assert self.space.eq_w(self.space.lt(w_list1, w_list1), |
| 333 |
self.space.w_False) |
| 334 |
assert self.space.eq_w(self.space.lt(w_list1, w_list2), |
| 335 |
self.space.w_False) |
| 336 |
assert self.space.eq_w(self.space.lt(w_list2, w_list3), |
| 337 |
self.space.w_True) |
| 338 |
assert self.space.eq_w(self.space.lt(w_list4, w_list3), |
| 339 |
self.space.w_True) |
| 340 |
|
| 341 |
def test_ge(self): |
| 342 |
w = self.space.wrap |
| 343 |
|
| 344 |
w_list0 = W_ListObject(self.space, []) |
| 345 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 346 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 347 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 348 |
w_list4 = W_ListObject(self.space, [w(5), w(3), w(9), w(-1)]) |
| 349 |
|
| 350 |
assert self.space.eq_w(self.space.ge(w_list0, w_list1), |
| 351 |
self.space.w_False) |
| 352 |
assert self.space.eq_w(self.space.ge(w_list1, w_list0), |
| 353 |
self.space.w_True) |
| 354 |
assert self.space.eq_w(self.space.ge(w_list1, w_list1), |
| 355 |
self.space.w_True) |
| 356 |
assert self.space.eq_w(self.space.ge(w_list1, w_list2), |
| 357 |
self.space.w_True) |
| 358 |
assert self.space.eq_w(self.space.ge(w_list2, w_list3), |
| 359 |
self.space.w_False) |
| 360 |
assert self.space.eq_w(self.space.ge(w_list4, w_list3), |
| 361 |
self.space.w_False) |
| 362 |
|
| 363 |
def test_gt(self): |
| 364 |
w = self.space.wrap |
| 365 |
|
| 366 |
w_list0 = W_ListObject(self.space, []) |
| 367 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 368 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 369 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 370 |
w_list4 = W_ListObject(self.space, [w(5), w(3), w(9), w(-1)]) |
| 371 |
|
| 372 |
assert self.space.eq_w(self.space.gt(w_list0, w_list1), |
| 373 |
self.space.w_False) |
| 374 |
assert self.space.eq_w(self.space.gt(w_list1, w_list0), |
| 375 |
self.space.w_True) |
| 376 |
assert self.space.eq_w(self.space.gt(w_list1, w_list1), |
| 377 |
self.space.w_False) |
| 378 |
assert self.space.eq_w(self.space.gt(w_list1, w_list2), |
| 379 |
self.space.w_False) |
| 380 |
assert self.space.eq_w(self.space.gt(w_list2, w_list3), |
| 381 |
self.space.w_False) |
| 382 |
assert self.space.eq_w(self.space.gt(w_list4, w_list3), |
| 383 |
self.space.w_False) |
| 384 |
|
| 385 |
def test_le(self): |
| 386 |
w = self.space.wrap |
| 387 |
|
| 388 |
w_list0 = W_ListObject(self.space, []) |
| 389 |
w_list1 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 390 |
w_list2 = W_ListObject(self.space, [w(5), w(3), w(99)]) |
| 391 |
w_list3 = W_ListObject(self.space, [w(5), w(3), w(99), w(-1)]) |
| 392 |
w_list4 = W_ListObject(self.space, [w(5), w(3), w(9), w(-1)]) |
| 393 |
|
| 394 |
assert self.space.eq_w(self.space.le(w_list0, w_list1), |
| 395 |
self.space.w_True) |
| 396 |
assert self.space.eq_w(self.space.le(w_list1, w_list0), |
| 397 |
self.space.w_False) |
| 398 |
assert self.space.eq_w(self.space.le(w_list1, w_list1), |
| 399 |
self.space.w_True) |
| 400 |
assert self.space.eq_w(self.space.le(w_list1, w_list2), |
| 401 |
self.space.w_True) |
| 402 |
assert self.space.eq_w(self.space.le(w_list2, w_list3), |
| 403 |
self.space.w_True) |
| 404 |
assert self.space.eq_w(self.space.le(w_list4, w_list3), |
| 405 |
self.space.w_True) |
| 406 |
|
| 407 |
def test_sizehint(self): |
| 408 |
space = self.space |
| 409 |
w_l = space.newlist([], sizehint=10) |
| 410 |
assert isinstance(w_l.strategy, SizeListStrategy) |
| 411 |
space.call_method(w_l, 'append', space.wrap(3)) |
| 412 |
assert isinstance(w_l.strategy, IntegerListStrategy) |
| 413 |
w_l = space.newlist([], sizehint=10) |
| 414 |
space.call_method(w_l, 'append', space.w_None) |
| 415 |
assert isinstance(w_l.strategy, ObjectListStrategy) |
| 416 |
|
| 417 |
def test_newlist_hint(self): |
| 418 |
space = self.space |
| 419 |
w_lst = space.newlist_hint(13) |
| 420 |
assert isinstance(w_lst.strategy, SizeListStrategy) |
| 421 |
assert w_lst.strategy.sizehint == 13 |
| 422 |
|
| 423 |
def test_find_fast_on_intlist(self, monkeypatch): |
| 424 |
monkeypatch.setattr(self.space, "eq_w", None) |
| 425 |
w = self.space.wrap |
| 426 |
intlist = W_ListObject(self.space, [w(1),w(2),w(3),w(4),w(5),w(6),w(7)]) |
| 427 |
res = intlist.find_or_count(w(4), 0, 7) |
| 428 |
assert res == 3 |
| 429 |
res = intlist.find_or_count(w(4), 0, 100) |
| 430 |
assert res == 3 |
| 431 |
with py.test.raises(ValueError): |
| 432 |
intlist.find_or_count(w(4), 4, 7) |
| 433 |
with py.test.raises(ValueError): |
| 434 |
intlist.find_or_count(w(4), 0, 2) |
| 435 |
|
| 436 |
def test_count_fast_on_intlist(self, monkeypatch): |
| 437 |
monkeypatch.setattr(self.space, "eq_w", None) |
| 438 |
w = self.space.wrap |
| 439 |
intlist = W_ListObject(self.space, [w(1),w(2),w(3),w(4),w(5),w(6),w(7), w(4)]) |
| 440 |
res = intlist.find_or_count(w(4), 0, 7, count=True) |
| 441 |
assert res == 1 |
| 442 |
res = intlist.find_or_count(w(4), 0, 100, count=True) |
| 443 |
assert res == 2 |
| 444 |
res = intlist.find_or_count(w(4), 4, 7, count=True) |
| 445 |
assert res == 0 |
| 446 |
res = intlist.find_or_count(w(4), 0, 2, count=True) |
| 447 |
assert res == 0 |
| 448 |
|
| 449 |
def test_intlist_to_object_too_much_wrapping(self): |
| 450 |
w = self.space.wrap |
| 451 |
w_list = W_ListObject(self.space, [w(1000000)] * 100) |
| 452 |
assert isinstance(w_list.strategy, IntegerListStrategy) |
| 453 |
w_list.setitem(0, w(1)) |
| 454 |
w_list.setitem(0, self.space.w_None) |
| 455 |
assert isinstance(w_list.strategy, ObjectListStrategy) |
| 456 |
l = w_list.getitems() |
| 457 |
assert len(set(l)) == 2 |
| 458 |
|
| 459 |
def test_floatlist_to_object_too_much_wrapping(self): |
| 460 |
w = self.space.wrap |
| 461 |
for value in [11233.12, float('NaN')]: |
| 462 |
w_list = W_ListObject(self.space, [w(value)] * 100) |
| 463 |
assert isinstance(w_list.strategy, FloatListStrategy) |
| 464 |
w_list.setitem(0, self.space.w_None) |
| 465 |
assert isinstance(w_list.strategy, ObjectListStrategy) |
| 466 |
l = w_list.getitems() |
| 467 |
for element in l[2:]: |
| 468 |
assert element is l[1] |
| 469 |
|
| 470 |
def test_intorfloatlist_to_object_too_much_wrapping(self): |
| 471 |
w = self.space.wrap |
| 472 |
w_list = W_ListObject(self.space, [w(0)] * 100) |
| 473 |
w_list.setitem(0, w(1.1)) |
| 474 |
assert isinstance(w_list.strategy, IntOrFloatListStrategy) |
| 475 |
w_list.setitem(0, self.space.w_None) |
| 476 |
assert isinstance(w_list.strategy, ObjectListStrategy) |
| 477 |
l = w_list.getitems() |
| 478 |
assert len(set(l)) == 2 |
| 479 |
|
| 480 |
def test_byteslist_to_object_too_much_wrapping(self): |
| 481 |
w = self.space.wrap |
| 482 |
w_list = W_ListObject(self.space, [self.space.newbytes(b"abc")] * 100) |
| 483 |
assert isinstance(w_list.strategy, BytesListStrategy) |
| 484 |
w_list.setitem(0, self.space.w_None) |
| 485 |
assert isinstance(w_list.strategy, ObjectListStrategy) |
| 486 |
l = w_list.getitems() |
| 487 |
for element in l[2:]: |
| 488 |
assert element is l[1] |
| 489 |
|
| 490 |
def test_asciilist_to_object_too_much_wrapping(self): |
| 491 |
w = self.space.wrap |
| 492 |
w_list = W_ListObject(self.space, [self.space.newutf8(b"abc", 3)] * 100) |
| 493 |
assert isinstance(w_list.strategy, AsciiListStrategy) |
| 494 |
w_list.setitem(0, self.space.w_None) |
| 495 |
assert isinstance(w_list.strategy, ObjectListStrategy) |
| 496 |
l = w_list.getitems() |
| 497 |
for element in l[2:]: |
| 498 |
assert element is l[1] |
| 499 |
|
| 500 |
def test_tuple_extend_shortcut(self, space, monkeypatch): |
| 501 |
from pypy.objspace.std import listobject |
| 502 |
w = self.space.wrap |
| 503 |
w_list = W_ListObject(space, [w(5)]) |
| 504 |
w_tup = space.newtuple([w(6), w(7)]) |
| 505 |
monkeypatch.setattr(listobject, "_do_extend_from_iterable", None) |
| 506 |
space.call_method(w_list, "extend", w_tup) |
| 507 |
assert space.unwrap(w_list) == [5, 6, 7] |
| 508 |
|
| 509 |
|
| 510 |
class AppTestListObject(object): |
| 511 |
|
| 512 |
|
| 513 |
def setup_class(cls): |
| 514 |
import platform |
| 515 |
import sys |
| 516 |
on_cpython = (cls.runappdirect and |
| 517 |
not hasattr(sys, 'pypy_translation_info')) |
| 518 |
cls.w_on_cpython = cls.space.wrap(on_cpython) |
| 519 |
cls.w_on_arm = cls.space.wrap(platform.machine().startswith('arm')) |
| 520 |
cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 521 |
|
| 522 |
def test_doc(self): |
| 523 |
assert list.__doc__ == """Built-in mutable sequence. |
| 524 |
|
| 525 |
If no argument is given, the constructor creates a new empty list. |
| 526 |
The argument must be an iterable if specified.""" |
| 527 |
assert list.__new__.__doc__ == "Create and return a new object. See help(type) for accurate signature." |
| 528 |
assert list.__init__.__doc__ == "Initialize self. See help(type(self)) for accurate signature." |
| 529 |
assert list.__text_signature__ == "(iterable=(), /)" |
| 530 |
|
| 531 |
def test_getstrategyfromlist_w(self): |
| 532 |
l0 = ["a", "2", "a", True] |
| 533 |
|
| 534 |
l1 = ["a", "2", True, "a"] |
| 535 |
l2 = [1, "2", "a", "a"] |
| 536 |
assert set(l1) == set(l2) |
| 537 |
|
| 538 |
def test_notequals(self): |
| 539 |
assert [1,2,3,4] != [1,2,5,4] |
| 540 |
|
| 541 |
def test_contains(self): |
| 542 |
l = [] |
| 543 |
assert not l.__contains__(2) |
| 544 |
|
| 545 |
l = [1,2,3] |
| 546 |
assert l.__contains__(2) |
| 547 |
assert not l.__contains__("2") |
| 548 |
assert l.__contains__(1.0) |
| 549 |
|
| 550 |
l = ["1","2","3"] |
| 551 |
assert l.__contains__("2") |
| 552 |
assert not l.__contains__(2) |
| 553 |
|
| 554 |
l = range(4) |
| 555 |
assert l.__contains__(2) |
| 556 |
assert not l.__contains__("2") |
| 557 |
|
| 558 |
l = [1,2,"3"] |
| 559 |
assert l.__contains__(2) |
| 560 |
assert not l.__contains__("2") |
| 561 |
|
| 562 |
l = range(2, 20, 3) |
| 563 |
assert l.__contains__(2) |
| 564 |
assert l.__contains__(5) |
| 565 |
assert l.__contains__(8) |
| 566 |
assert l.__contains__(11) |
| 567 |
assert l.__contains__(14) |
| 568 |
assert l.__contains__(17) |
| 569 |
assert not l.__contains__(3) |
| 570 |
assert not l.__contains__(4) |
| 571 |
assert not l.__contains__(7) |
| 572 |
assert not l.__contains__(13) |
| 573 |
assert not l.__contains__(20) |
| 574 |
|
| 575 |
l = range(2, -20, -3) |
| 576 |
assert l.__contains__(2) |
| 577 |
assert l.__contains__(-4) |
| 578 |
assert l.__contains__(-13) |
| 579 |
assert l.__contains__(-16) |
| 580 |
assert l.__contains__(-19) |
| 581 |
assert not l.__contains__(-17) |
| 582 |
assert not l.__contains__(-3) |
| 583 |
assert not l.__contains__(-20) |
| 584 |
assert not l.__contains__(-21) |
| 585 |
|
| 586 |
logger = [] |
| 587 |
|
| 588 |
class Foo(object): |
| 589 |
|
| 590 |
def __init__(self, value, name=None): |
| 591 |
self.value = value |
| 592 |
self.name = name or value |
| 593 |
|
| 594 |
def __repr__(self): |
| 595 |
return '<Foo %s>' % self.name |
| 596 |
|
| 597 |
def __eq__(self, other): |
| 598 |
logger.append((self, other)) |
| 599 |
return self.value == other.value |
| 600 |
|
| 601 |
foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3) |
| 602 |
foo42 = Foo(42) |
| 603 |
foo_list = [foo1, foo2, foo3] |
| 604 |
foo42 in foo_list |
| 605 |
logger_copy = logger[:] |
| 606 |
assert logger_copy == [(foo1, foo42), (foo2, foo42), (foo3, foo42)] |
| 607 |
|
| 608 |
del logger[:] |
| 609 |
foo2_bis = Foo(2, '2 bis') |
| 610 |
foo2_bis in foo_list |
| 611 |
logger_copy = logger[:] |
| 612 |
assert logger_copy == [(foo1, foo2_bis), (foo2, foo2_bis)] |
| 613 |
|
| 614 |
def test_call_list(self): |
| 615 |
assert list('') == [] |
| 616 |
assert list('abc') == ['a', 'b', 'c'] |
| 617 |
assert list((1, 2)) == [1, 2] |
| 618 |
l = [1] |
| 619 |
assert list(l) is not l |
| 620 |
assert list(l) == l |
| 621 |
|
| 622 |
def test_explicit_new_init(self): |
| 623 |
l = l0 = list.__new__(list) |
| 624 |
l.__init__([1,2]) |
| 625 |
assert l is l0 |
| 626 |
assert l == [1,2] |
| 627 |
list.__init__(l, [1,2,3]) |
| 628 |
assert l is l0 |
| 629 |
assert l == [1,2,3] |
| 630 |
list.__init__(l, ['a', 'b', 'c']) |
| 631 |
assert l is l0 |
| 632 |
assert l == ['a', 'b', 'c'] |
| 633 |
list.__init__(l) |
| 634 |
assert l == [] |
| 635 |
|
| 636 |
def test_explicit_new_init_more_cases(self): |
| 637 |
for assignment in [[], (), [3], ["foo"]]: |
| 638 |
l = [1, 2] |
| 639 |
l.__init__(assignment) |
| 640 |
assert l == list(assignment) |
| 641 |
|
| 642 |
def test_range_init(self): |
| 643 |
x = list(range(5,1)) |
| 644 |
assert x == [] |
| 645 |
|
| 646 |
x = list(range(1,10)) |
| 647 |
x[22:0:-1] == range(1,10) |
| 648 |
|
| 649 |
r = list(range(10, 10)) |
| 650 |
assert len(r) == 0 |
| 651 |
assert list(reversed(r)) == [] |
| 652 |
assert r[:] == [] |
| 653 |
|
| 654 |
def test_extend_list(self): |
| 655 |
l = l0 = [1] |
| 656 |
l.extend([2]) |
| 657 |
assert l is l0 |
| 658 |
assert l == [1,2] |
| 659 |
l = ['a'] |
| 660 |
l.extend('b') |
| 661 |
assert l == ['a', 'b'] |
| 662 |
l = ['a'] |
| 663 |
l.extend([0]) |
| 664 |
assert l == ['a', 0] |
| 665 |
l = list(range(10)) |
| 666 |
l.extend([10]) |
| 667 |
assert l == list(range(11)) |
| 668 |
|
| 669 |
l = [] |
| 670 |
m = [1,2,3] |
| 671 |
l.extend(m) |
| 672 |
m[0] = 5 |
| 673 |
assert m == [5,2,3] |
| 674 |
assert l == [1,2,3] |
| 675 |
|
| 676 |
def test_extend_tuple(self): |
| 677 |
l = l0 = [1] |
| 678 |
l.extend((2,)) |
| 679 |
assert l is l0 |
| 680 |
assert l == [1,2] |
| 681 |
l = ['a'] |
| 682 |
l.extend(('b',)) |
| 683 |
assert l == ['a', 'b'] |
| 684 |
|
| 685 |
def test_extend_iterable(self): |
| 686 |
l = l0 = [1] |
| 687 |
l.extend(iter([1, 2, 3, 4])) |
| 688 |
assert l is l0 |
| 689 |
assert l == [1, 1, 2, 3, 4] |
| 690 |
|
| 691 |
l = l0 = ['a'] |
| 692 |
l.extend(iter(['b', 'c', 'd'])) |
| 693 |
assert l == ['a', 'b', 'c', 'd'] |
| 694 |
assert l is l0 |
| 695 |
|
| 696 |
l = l0 = [1.2] |
| 697 |
l.extend(iter([2.3, 3.4, 4.5])) |
| 698 |
assert l == [1.2, 2.3, 3.4, 4.5] |
| 699 |
assert l is l0 |
| 700 |
|
| 701 |
def test_extend_iterable_length_hint_overflow(self): |
| 702 |
import sys |
| 703 |
class CustomIterable(object): |
| 704 |
def __iter__(self): |
| 705 |
if False: |
| 706 |
yield |
| 707 |
def __length_hint__(self): |
| 708 |
return sys.maxsize |
| 709 |
a = [1, 2, 3, 4] |
| 710 |
a.extend(CustomIterable()) |
| 711 |
assert a == [1, 2, 3, 4] |
| 712 |
|
| 713 |
def test_sort(self): |
| 714 |
l = l0 = [1, 5, 3, 0] |
| 715 |
l.sort() |
| 716 |
assert l is l0 |
| 717 |
assert l == [0, 1, 3, 5] |
| 718 |
l = l0 = [] |
| 719 |
l.sort() |
| 720 |
assert l is l0 |
| 721 |
assert l == [] |
| 722 |
l = l0 = [1] |
| 723 |
l.sort() |
| 724 |
assert l is l0 |
| 725 |
assert l == [1] |
| 726 |
|
| 727 |
l = ["c", "a", "d", "b"] |
| 728 |
l.sort(reverse=True) |
| 729 |
assert l == ["d", "c", "b", "a"] |
| 730 |
|
| 731 |
l = [3.3, 2.2, 4.4, 1.1, 3.1, 5.5] |
| 732 |
l.sort() |
| 733 |
assert l == [1.1, 2.2, 3.1, 3.3, 4.4, 5.5] |
| 734 |
|
| 735 |
def test_sort_key(self): |
| 736 |
def lower(x): return x.lower() |
| 737 |
l = ['a', 'C', 'b'] |
| 738 |
l.sort(key=lower) |
| 739 |
assert l == ['a', 'b', 'C'] |
| 740 |
l = [] |
| 741 |
l.sort(key=lower) |
| 742 |
assert l == [] |
| 743 |
l = ['a'] |
| 744 |
l.sort(key=lower) |
| 745 |
assert l == ['a'] |
| 746 |
|
| 747 |
r = list(range(10)) |
| 748 |
r.sort(key=lambda x: -x) |
| 749 |
assert r == list(range(9, -1, -1)) |
| 750 |
|
| 751 |
def test_sort_reversed(self): |
| 752 |
l = list(range(10)) |
| 753 |
l.sort(reverse=True) |
| 754 |
assert l == list(range(9, -1, -1)) |
| 755 |
l = [] |
| 756 |
l.sort(reverse=True) |
| 757 |
assert l == [] |
| 758 |
l = [1] |
| 759 |
l.sort(reverse=True) |
| 760 |
assert l == [1] |
| 761 |
raises(TypeError, sorted, [], None, lambda x, y: 0) |
| 762 |
|
| 763 |
def test_sort_cmp_key_reverse(self): |
| 764 |
def lower(x): return x.lower() |
| 765 |
l = ['a', 'C', 'b'] |
| 766 |
l.sort(reverse = True, key = lower) |
| 767 |
assert l == ['C', 'b', 'a'] |
| 768 |
|
| 769 |
def test_sort_simple_string(self): |
| 770 |
l = ["a", "d", "c", "b"] |
| 771 |
l.sort() |
| 772 |
assert l == ["a", "b", "c", "d"] |
| 773 |
|
| 774 |
def test_sort_range(self): |
| 775 |
l = list(range(3, 10, 3)) |
| 776 |
l.sort() |
| 777 |
assert l == [3, 6, 9] |
| 778 |
l.sort(reverse=True) |
| 779 |
assert l == [9, 6, 3] |
| 780 |
l.sort(reverse=True) |
| 781 |
assert l == [9, 6, 3] |
| 782 |
l.sort() |
| 783 |
assert l == [3, 6, 9] |
| 784 |
|
| 785 |
def test_sort_mutation_detected_append_pop(self): |
| 786 |
L = [1, 2, 3] |
| 787 |
def mutating_key(x): |
| 788 |
L.append(99) |
| 789 |
L.pop() |
| 790 |
return x |
| 791 |
raises(ValueError, L.sort, key=mutating_key) |
| 792 |
|
| 793 |
L = [1, 2, 3] |
| 794 |
def mutating_key2(x): |
| 795 |
L.append(99) |
| 796 |
del L[:] |
| 797 |
return x |
| 798 |
raises(ValueError, L.sort, key=mutating_key2) |
| 799 |
|
| 800 |
def test_getitem(self): |
| 801 |
l = [1, 2, 3, 4, 5, 6, 9] |
| 802 |
assert l[0] == 1 |
| 803 |
assert l[-1] == 9 |
| 804 |
assert l[-2] == 6 |
| 805 |
raises(IndexError, "l[len(l)]") |
| 806 |
raises(IndexError, "l[-len(l)-1]") |
| 807 |
|
| 808 |
l = ['a', 'b', 'c'] |
| 809 |
assert l[0] == 'a' |
| 810 |
assert l[-1] == 'c' |
| 811 |
assert l[-2] == 'b' |
| 812 |
raises(IndexError, "l[len(l)]") |
| 813 |
|
| 814 |
l = [1.1, 2.2, 3.3] |
| 815 |
assert l[0] == 1.1 |
| 816 |
assert l[-1] == 3.3 |
| 817 |
assert l[-2] == 2.2 |
| 818 |
raises(IndexError, "l[len(l)]") |
| 819 |
|
| 820 |
l = [] |
| 821 |
raises(IndexError, "l[1]") |
| 822 |
|
| 823 |
def test_getitem_range(self): |
| 824 |
l = range(5) |
| 825 |
raises(IndexError, "l[-6]") |
| 826 |
raises(IndexError, "l[5]") |
| 827 |
assert l[0] == 0 |
| 828 |
assert l[-1] == 4 |
| 829 |
assert l[-2] == 3 |
| 830 |
assert l[-5] == 0 |
| 831 |
|
| 832 |
l = range(1, 5) |
| 833 |
raises(IndexError, "l[-5]") |
| 834 |
raises(IndexError, "l[4]") |
| 835 |
assert l[0] == 1 |
| 836 |
assert l[-1] == 4 |
| 837 |
assert l[-2] == 3 |
| 838 |
assert l[-4] == 1 |
| 839 |
|
| 840 |
def test_setitem(self): |
| 841 |
l = [] |
| 842 |
raises(IndexError, "l[1] = 2") |
| 843 |
|
| 844 |
l = [5,3] |
| 845 |
l[0] = 2 |
| 846 |
assert l == [2,3] |
| 847 |
|
| 848 |
l = [5,3] |
| 849 |
l[0] = "2" |
| 850 |
assert l == ["2",3] |
| 851 |
|
| 852 |
l = list(range(3)) |
| 853 |
l[0] = 1 |
| 854 |
assert l == [1,1,2] |
| 855 |
|
| 856 |
def test_delitem(self): |
| 857 |
l = [1, 2, 3, 4, 5, 6, 9] |
| 858 |
del l[0] |
| 859 |
assert l == [2, 3, 4, 5, 6, 9] |
| 860 |
del l[-1] |
| 861 |
assert l == [2, 3, 4, 5, 6] |
| 862 |
del l[-2] |
| 863 |
assert l == [2, 3, 4, 6] |
| 864 |
raises(IndexError, "del l[len(l)]") |
| 865 |
raises(IndexError, "del l[-len(l)-1]") |
| 866 |
|
| 867 |
l = l0 = ['a', 'b', 'c'] |
| 868 |
del l[0] |
| 869 |
assert l == ['b', 'c'] |
| 870 |
del l[-1] |
| 871 |
assert l == ['b'] |
| 872 |
del l[-1] |
| 873 |
assert l == [] |
| 874 |
assert l is l0 |
| 875 |
raises(IndexError, "del l[0]") |
| 876 |
|
| 877 |
l = l0 = [1.1, 2.2, 3.3] |
| 878 |
del l[0] |
| 879 |
assert l == [2.2, 3.3] |
| 880 |
del l[-1] |
| 881 |
assert l == [2.2] |
| 882 |
del l[-1] |
| 883 |
assert l == [] |
| 884 |
assert l is l0 |
| 885 |
raises(IndexError, "del l[0]") |
| 886 |
|
| 887 |
l = list(range(10)) |
| 888 |
del l[5] |
| 889 |
assert l == [0, 1, 2, 3, 4, 6, 7, 8, 9] |
| 890 |
|
| 891 |
def test_getitem_slice(self): |
| 892 |
l = list(range(10)) |
| 893 |
assert l[::] == l |
| 894 |
del l[::2] |
| 895 |
assert l == [1,3,5,7,9] |
| 896 |
l[-2::-1] = l[:-1] |
| 897 |
assert l == [7,5,3,1,9] |
| 898 |
del l[-1:2:-1] |
| 899 |
assert l == [7,5,3] |
| 900 |
del l[:2] |
| 901 |
assert l == [3] |
| 902 |
assert l[1:] == [] |
| 903 |
assert l[1::2] == [] |
| 904 |
assert l[::] == l |
| 905 |
assert l[0::-2] == l |
| 906 |
assert l[-1::-5] == l |
| 907 |
|
| 908 |
l = [''] |
| 909 |
assert l[1:] == [] |
| 910 |
assert l[1::2] == [] |
| 911 |
assert l[::] == l |
| 912 |
assert l[0::-5] == l |
| 913 |
assert l[-1::-5] == l |
| 914 |
l.extend(['a', 'b']) |
| 915 |
assert l[::-1] == ['b', 'a', ''] |
| 916 |
|
| 917 |
l = [1,2,3,4,5] |
| 918 |
assert l[1:0:None] == [] |
| 919 |
assert l[1:0] == [] |
| 920 |
|
| 921 |
def test_getslice_invalid(self): |
| 922 |
x = [1,2,3,4] |
| 923 |
assert x[10:0] == [] |
| 924 |
assert x[10:0:None] == [] |
| 925 |
|
| 926 |
x = list(range(1,5)) |
| 927 |
assert x[10:0] == [] |
| 928 |
assert x[10:0:None] == [] |
| 929 |
|
| 930 |
assert x[0:22] == [1,2,3,4] |
| 931 |
assert x[-1:10] == [4] |
| 932 |
|
| 933 |
assert x[0:22:None] == [1,2,3,4] |
| 934 |
assert x[-1:10:None] == [4] |
| 935 |
|
| 936 |
def test_getslice_range_backwards(self): |
| 937 |
x = list(range(1,10)) |
| 938 |
assert x[22:-10] == [] |
| 939 |
assert x[22:-10:-1] == [9,8,7,6,5,4,3,2,1] |
| 940 |
assert x[10:3:-1] == [9,8,7,6,5] |
| 941 |
assert x[10:3:-2] == [9,7,5] |
| 942 |
assert x[1:5:-1] == [] |
| 943 |
|
| 944 |
def test_delall(self): |
| 945 |
l = l0 = [1,2,3] |
| 946 |
del l[:] |
| 947 |
assert l is l0 |
| 948 |
assert l == [] |
| 949 |
|
| 950 |
l = ['a', 'b'] |
| 951 |
del l[:] |
| 952 |
assert l == [] |
| 953 |
|
| 954 |
l = [1.1, 2.2] |
| 955 |
del l[:] |
| 956 |
assert l == [] |
| 957 |
|
| 958 |
def test_clear(self): |
| 959 |
l = l0 = [1,2,3] |
| 960 |
l.clear() |
| 961 |
assert l is l0 |
| 962 |
assert l == [] |
| 963 |
|
| 964 |
l = ['a', 'b'] |
| 965 |
l.clear() |
| 966 |
assert l == [] |
| 967 |
|
| 968 |
l = [1.1, 2.2] |
| 969 |
l.clear() |
| 970 |
assert l == [] |
| 971 |
|
| 972 |
l = [] |
| 973 |
l.clear() |
| 974 |
assert l == [] |
| 975 |
|
| 976 |
def test_iadd(self): |
| 977 |
l = l0 = [1,2,3] |
| 978 |
l += [4,5] |
| 979 |
assert l is l0 |
| 980 |
assert l == [1,2,3,4,5] |
| 981 |
|
| 982 |
l = l0 = [1.1,2.2,3.3] |
| 983 |
l += [4.4,5.5] |
| 984 |
assert l is l0 |
| 985 |
assert l == [1.1,2.2,3.3,4.4,5.5] |
| 986 |
|
| 987 |
l = l0 = ['a', 'b', 'c'] |
| 988 |
l1 = l[:] |
| 989 |
l += ['d'] |
| 990 |
assert l is l0 |
| 991 |
assert l == ['a', 'b', 'c', 'd'] |
| 992 |
l1 += [0] |
| 993 |
assert l1 == ['a', 'b', 'c', 0] |
| 994 |
|
| 995 |
r1 = r2 = list(range(5)) |
| 996 |
assert r1 is r2 |
| 997 |
r1 += [15] |
| 998 |
assert r1 is r2 |
| 999 |
assert r1 == [0, 1, 2, 3, 4, 15] |
| 1000 |
assert r2 == [0, 1, 2, 3, 4, 15] |
| 1001 |
|
| 1002 |
def test_iadd_iterable(self): |
| 1003 |
l = l0 = [1,2,3] |
| 1004 |
l += iter([4,5]) |
| 1005 |
assert l is l0 |
| 1006 |
assert l == [1,2,3,4,5] |
| 1007 |
|
| 1008 |
def test_iadd_subclass(self): |
| 1009 |
class Bar(object): |
| 1010 |
def __radd__(self, other): |
| 1011 |
return ('radd', self, other) |
| 1012 |
bar = Bar() |
| 1013 |
l1 = [1,2,3] |
| 1014 |
l1 += bar |
| 1015 |
assert l1 == ('radd', bar, [1,2,3]) |
| 1016 |
|
| 1017 |
def test_add_lists(self): |
| 1018 |
l1 = [1,2,3] |
| 1019 |
l2 = [4,5,6] |
| 1020 |
l3 = l1 + l2 |
| 1021 |
assert l3 == [1,2,3,4,5,6] |
| 1022 |
|
| 1023 |
def test_imul(self): |
| 1024 |
l = l0 = [4,3] |
| 1025 |
l *= 2 |
| 1026 |
assert l is l0 |
| 1027 |
assert l == [4,3,4,3] |
| 1028 |
l *= 0 |
| 1029 |
assert l is l0 |
| 1030 |
assert l == [] |
| 1031 |
l = l0 = [4,3] |
| 1032 |
l *= (-1) |
| 1033 |
assert l is l0 |
| 1034 |
assert l == [] |
| 1035 |
|
| 1036 |
l = l0 = ['a', 'b'] |
| 1037 |
l *= 2 |
| 1038 |
assert l is l0 |
| 1039 |
assert l == ['a', 'b', 'a', 'b'] |
| 1040 |
l *= 0 |
| 1041 |
assert l is l0 |
| 1042 |
assert l == [] |
| 1043 |
l = ['a'] |
| 1044 |
l *= -5 |
| 1045 |
assert l == [] |
| 1046 |
|
| 1047 |
l = l0 = [1.1, 2.2] |
| 1048 |
l *= 2 |
| 1049 |
assert l is l0 |
| 1050 |
assert l == [1.1, 2.2, 1.1, 2.2] |
| 1051 |
|
| 1052 |
l = list(range(2)) |
| 1053 |
l *= 2 |
| 1054 |
assert l == [0, 1, 0, 1] |
| 1055 |
|
| 1056 |
r1 = r2 = list(range(3)) |
| 1057 |
assert r1 is r2 |
| 1058 |
r1 *= 2 |
| 1059 |
assert r1 is r2 |
| 1060 |
assert r1 == [0, 1, 2, 0, 1, 2] |
| 1061 |
assert r2 == [0, 1, 2, 0, 1, 2] |
| 1062 |
|
| 1063 |
def test_mul_errors(self): |
| 1064 |
try: |
| 1065 |
[1, 2, 3] * (3,) |
| 1066 |
except TypeError: |
| 1067 |
pass |
| 1068 |
|
| 1069 |
def test_mul___index__(self): |
| 1070 |
class MyInt(object): |
| 1071 |
def __init__(self, x): |
| 1072 |
self.x = x |
| 1073 |
|
| 1074 |
def __int__(self): |
| 1075 |
return self.x |
| 1076 |
|
| 1077 |
class MyIndex(object): |
| 1078 |
def __init__(self, x): |
| 1079 |
self.x = x |
| 1080 |
|
| 1081 |
def __index__(self): |
| 1082 |
return self.x |
| 1083 |
|
| 1084 |
assert [0] * MyIndex(3) == [0, 0, 0] |
| 1085 |
raises(TypeError, "[0]*MyInt(3)") |
| 1086 |
raises(TypeError, "[0]*MyIndex(MyInt(3))") |
| 1087 |
|
| 1088 |
def test_index(self): |
| 1089 |
c = list(range(10)) |
| 1090 |
assert c.index(0) == 0 |
| 1091 |
raises(ValueError, c.index, 10) |
| 1092 |
|
| 1093 |
c = list('hello world') |
| 1094 |
assert c.index('l') == 2 |
| 1095 |
raises(ValueError, c.index, '!') |
| 1096 |
assert c.index('l', 3) == 3 |
| 1097 |
assert c.index('l', 4) == 9 |
| 1098 |
raises(ValueError, c.index, 'l', 10) |
| 1099 |
assert c.index('l', -5) == 9 |
| 1100 |
assert c.index('l', -25) == 2 |
| 1101 |
assert c.index('o', 1, 5) == 4 |
| 1102 |
raises(ValueError, c.index, 'o', 1, 4) |
| 1103 |
assert c.index('o', 1, 5-11) == 4 |
| 1104 |
raises(ValueError, c.index, 'o', 1, 4-11) |
| 1105 |
raises(TypeError, c.index, 'c', 0, 4.3) |
| 1106 |
raises(TypeError, c.index, 'c', 1.0, 5.6) |
| 1107 |
|
| 1108 |
c = [0, 2, 4] |
| 1109 |
assert c.index(0) == 0 |
| 1110 |
raises(ValueError, c.index, 3) |
| 1111 |
|
| 1112 |
c = [0.0, 2.2, 4.4] |
| 1113 |
assert c.index(0) == 0.0 |
| 1114 |
e = raises(ValueError, c.index, 3) |
| 1115 |
import sys |
| 1116 |
if sys.version_info[:2] == (2, 7): |
| 1117 |
assert str(e.value) == '3 is not in list' |
| 1118 |
|
| 1119 |
def test_index_cpython_bug(self): |
| 1120 |
if self.on_cpython: |
| 1121 |
skip("cpython has a bug here") |
| 1122 |
c = list('hello world') |
| 1123 |
assert c.index('l', None, None) == 2 |
| 1124 |
assert c.index('l', 3, None) == 3 |
| 1125 |
assert c.index('l', None, 4) == 2 |
| 1126 |
|
| 1127 |
def test_ass_slice(self): |
| 1128 |
l = list(range(6)) |
| 1129 |
l[1:3] = 'abc' |
| 1130 |
assert l == [0, 'a', 'b', 'c', 3, 4, 5] |
| 1131 |
l = [] |
| 1132 |
l[:-3] = [] |
| 1133 |
assert l == [] |
| 1134 |
l = list(range(6)) |
| 1135 |
l[:] = [] |
| 1136 |
assert l == [] |
| 1137 |
|
| 1138 |
l = l0 = ['a', 'b'] |
| 1139 |
l[1:1] = ['ae'] |
| 1140 |
assert l == ['a', 'ae', 'b'] |
| 1141 |
l[1:100] = ['B'] |
| 1142 |
assert l == ['a', 'B'] |
| 1143 |
l[:] = [] |
| 1144 |
assert l == [] |
| 1145 |
assert l is l0 |
| 1146 |
|
| 1147 |
l = [] |
| 1148 |
l2 = range(3) |
| 1149 |
l.__setitem__(slice(0,3),l2) |
| 1150 |
assert l == [0,1,2] |
| 1151 |
|
| 1152 |
def test_assign_extended_slice(self): |
| 1153 |
l = l0 = ['a', 'b', 'c'] |
| 1154 |
l[::-1] = ['a', 'b', 'c'] |
| 1155 |
assert l == ['c', 'b', 'a'] |
| 1156 |
l[::-2] = [0, 1] |
| 1157 |
assert l == [1, 'b', 0] |
| 1158 |
l[-1:5:2] = [2] |
| 1159 |
assert l == [1, 'b', 2] |
| 1160 |
l[:-1:2] = [0] |
| 1161 |
assert l == [0, 'b', 2] |
| 1162 |
assert l is l0 |
| 1163 |
|
| 1164 |
l = [1,2,3] |
| 1165 |
raises(ValueError, "l[0:2:2] = [1,2,3,4]") |
| 1166 |
raises(ValueError, "l[::2] = []") |
| 1167 |
|
| 1168 |
l = list(range(6)) |
| 1169 |
l[::3] = ('a', 'b') |
| 1170 |
assert l == ['a', 1, 2, 'b', 4, 5] |
| 1171 |
|
| 1172 |
l = [0.0, 1.1, 2.2, 3.3, 4.4, 5.5] |
| 1173 |
l[::3] = ('a', 'b') |
| 1174 |
assert l == ['a', 1.1, 2.2, 'b', 4.4, 5.5] |
| 1175 |
|
| 1176 |
l_int = [5]; l_int.pop() |
| 1177 |
l_empty = [] |
| 1178 |
raises(ValueError, "l_int[::-1] = [42]") |
| 1179 |
raises(ValueError, "l_int[::7] = [42]") |
| 1180 |
raises(ValueError, "l_empty[::-1] = [42]") |
| 1181 |
raises(ValueError, "l_empty[::7] = [42]") |
| 1182 |
l_int[::1] = [42]; assert l_int == [42] |
| 1183 |
l_empty[::1] = [42]; assert l_empty == [42] |
| 1184 |
|
| 1185 |
def test_setslice_with_self(self): |
| 1186 |
l = [1,2,3,4] |
| 1187 |
l[:] = l |
| 1188 |
assert l == [1,2,3,4] |
| 1189 |
|
| 1190 |
l = [1,2,3,4] |
| 1191 |
l[0:2] = l |
| 1192 |
assert l == [1,2,3,4,3,4] |
| 1193 |
|
| 1194 |
l = [1,2,3,4] |
| 1195 |
l[0:2] = l |
| 1196 |
assert l == [1,2,3,4,3,4] |
| 1197 |
|
| 1198 |
l = [1,2,3,4,5,6,7,8,9,10] |
| 1199 |
raises(ValueError, "l[5::-1] = l") |
| 1200 |
|
| 1201 |
l = [1,2,3,4,5,6,7,8,9,10] |
| 1202 |
raises(ValueError, "l[::2] = l") |
| 1203 |
|
| 1204 |
l = [1,2,3,4,5,6,7,8,9,10] |
| 1205 |
l[5:] = l |
| 1206 |
assert l == [1,2,3,4,5,1,2,3,4,5,6,7,8,9,10] |
| 1207 |
|
| 1208 |
l = [1,2,3,4,5,6] |
| 1209 |
l[::-1] = l |
| 1210 |
assert l == [6,5,4,3,2,1] |
| 1211 |
|
| 1212 |
def test_setitem_slice_performance(self): |
| 1213 |
|
| 1214 |
|
| 1215 |
if self.on_arm: |
| 1216 |
skip("consumes too much memory for most ARM machines") |
| 1217 |
if self.runappdirect: |
| 1218 |
count = 16*1024*1024 |
| 1219 |
else: |
| 1220 |
count = 1024 |
| 1221 |
b = [None] * count |
| 1222 |
for i in range(count): |
| 1223 |
b[i:i+1] = ['y'] |
| 1224 |
assert b == ['y'] * count |
| 1225 |
|
| 1226 |
def test_setslice_full(self): |
| 1227 |
l = [1, 2, 3] |
| 1228 |
l[::] = "abc" |
| 1229 |
assert l == ['a', 'b', 'c'] |
| 1230 |
|
| 1231 |
l = [1, 2, 3] |
| 1232 |
l[::] = [] |
| 1233 |
assert l == [] |
| 1234 |
|
| 1235 |
l = [1, 2, 3] |
| 1236 |
l[::] = l |
| 1237 |
assert l == [1, 2, 3] |
| 1238 |
|
| 1239 |
def test_setslice_full_bug(self): |
| 1240 |
l = [1, 2, 3] |
| 1241 |
l[::] = (x + 1 for x in l) |
| 1242 |
assert l == [2, 3, 4] |
| 1243 |
|
| 1244 |
def test_recursive_repr(self): |
| 1245 |
l = [] |
| 1246 |
assert repr(l) == '[]' |
| 1247 |
l.append(l) |
| 1248 |
assert repr(l) == '[[...]]' |
| 1249 |
|
| 1250 |
def test_repr_strategies(self): |
| 1251 |
assert repr([]) == '[]' |
| 1252 |
assert repr([1, 2, 3]) == '[1, 2, 3]' |
| 1253 |
assert repr([1.1, 2.1, 3.1]) == '[1.1, 2.1, 3.1]' |
| 1254 |
assert repr([1.1, 2.1, 3.1, float('nan')]) == '[1.1, 2.1, 3.1, nan]' |
| 1255 |
|
| 1256 |
assert repr([100, 23, -1.1, 2.1, 3.1, float('nan')]) == '[100, 23, -1.1, 2.1, 3.1, nan]' |
| 1257 |
|
| 1258 |
assert repr([b'a', b'b', b'c']) == "[b'a', b'b', b'c']" |
| 1259 |
|
| 1260 |
assert repr([u'a', u'b', u'c']) == "['a', 'b', 'c']" |
| 1261 |
|
| 1262 |
o1 = object() |
| 1263 |
d = {1: 2, None: False} |
| 1264 |
assert repr([o1, d]) == '[%r, %r]' % (o1, d) |
| 1265 |
|
| 1266 |
def test_copy(self): |
| 1267 |
|
| 1268 |
l = [] |
| 1269 |
c = l.copy() |
| 1270 |
assert c == [] |
| 1271 |
|
| 1272 |
|
| 1273 |
l = list(range(3)) |
| 1274 |
c = l.copy() |
| 1275 |
assert l == c |
| 1276 |
|
| 1277 |
|
| 1278 |
l = ['a', 'b'] |
| 1279 |
c = l.copy() |
| 1280 |
c.append('i') |
| 1281 |
assert l == ['a', 'b'] |
| 1282 |
assert c == l + ['i'] |
| 1283 |
|
| 1284 |
|
| 1285 |
l = [1, 2, [3, 4], 5] |
| 1286 |
c = l.copy() |
| 1287 |
assert l == c |
| 1288 |
assert c[3] == l[3] |
| 1289 |
|
| 1290 |
raises(TypeError, l.copy, None) |
| 1291 |
|
| 1292 |
def test_append(self): |
| 1293 |
l = [] |
| 1294 |
l.append('X') |
| 1295 |
assert l == ['X'] |
| 1296 |
l.append('Y') |
| 1297 |
l.append('Z') |
| 1298 |
assert l == ['X', 'Y', 'Z'] |
| 1299 |
|
| 1300 |
l = [] |
| 1301 |
l.append(0) |
| 1302 |
assert l == [0] |
| 1303 |
for x in range(1, 5): |
| 1304 |
l.append(x) |
| 1305 |
assert l == list(range(5)) |
| 1306 |
|
| 1307 |
l = [1,2,3] |
| 1308 |
l.append("a") |
| 1309 |
assert l == [1,2,3,"a"] |
| 1310 |
|
| 1311 |
l = [1.1, 2.2, 3.3] |
| 1312 |
l.append(4.4) |
| 1313 |
assert l == [1.1, 2.2, 3.3, 4.4] |
| 1314 |
|
| 1315 |
l = list(range(4)) |
| 1316 |
l.append(4) |
| 1317 |
assert l == list(range(5)) |
| 1318 |
|
| 1319 |
l = list(range(5)) |
| 1320 |
l.append(26) |
| 1321 |
assert l == [0,1,2,3,4,26] |
| 1322 |
|
| 1323 |
l = list(range(5)) |
| 1324 |
l.append("a") |
| 1325 |
assert l == [0,1,2,3,4,"a"] |
| 1326 |
|
| 1327 |
l = list(range(5)) |
| 1328 |
l.append(5) |
| 1329 |
assert l == [0,1,2,3,4,5] |
| 1330 |
|
| 1331 |
def test_count(self): |
| 1332 |
c = list('hello') |
| 1333 |
assert c.count('l') == 2 |
| 1334 |
assert c.count('h') == 1 |
| 1335 |
assert c.count('w') == 0 |
| 1336 |
|
| 1337 |
c = [0, 1, 2, 2, 3] |
| 1338 |
assert c.count(2) == 2 |
| 1339 |
assert c.count(1) == 1 |
| 1340 |
assert c.count(0) == 1 |
| 1341 |
assert c.count(10) == 0 |
| 1342 |
|
| 1343 |
c = [0., 1., 2., 2., 3., 0.0, -0.0] |
| 1344 |
assert c.count(2) == 2 |
| 1345 |
assert c.count(1) == 1 |
| 1346 |
assert c.count(0) == 3 |
| 1347 |
assert c.count(10) == 0 |
| 1348 |
assert c.count('abc') == 0 |
| 1349 |
|
| 1350 |
c = [0, 1, 2, 2., 3, 0.0, -0.0] |
| 1351 |
assert c.count(2) == 2 |
| 1352 |
assert c.count(1) == 1 |
| 1353 |
assert c.count(0) == 3 |
| 1354 |
assert c.count(10) == 0 |
| 1355 |
assert c.count(2.) == 2 |
| 1356 |
assert c.count(1.) == 1 |
| 1357 |
assert c.count(0.) == 3 |
| 1358 |
assert c.count(-0.) == 3 |
| 1359 |
assert c.count(10.) == 0 |
| 1360 |
assert c.count('abc') == 0 |
| 1361 |
|
| 1362 |
c = [0, "abc", 1, 2, 2., 3, 0.0, -0.0, None] |
| 1363 |
assert c.count(2) == 2 |
| 1364 |
assert c.count(2.) == 2 |
| 1365 |
assert c.count(1) == 1 |
| 1366 |
assert c.count(1.) == 1 |
| 1367 |
assert c.count(0) == 3 |
| 1368 |
assert c.count(0.0) == 3 |
| 1369 |
assert c.count(-0.0) == 3 |
| 1370 |
assert c.count(10) == 0 |
| 1371 |
assert c.count('abc') == 1 |
| 1372 |
|
| 1373 |
def test_insert(self): |
| 1374 |
c = list('hello world') |
| 1375 |
c.insert(0, 'X') |
| 1376 |
assert c[:4] == ['X', 'h', 'e', 'l'] |
| 1377 |
c.insert(2, 'Y') |
| 1378 |
c.insert(-2, 'Z') |
| 1379 |
assert ''.join(c) == 'XhYello worZld' |
| 1380 |
|
| 1381 |
ls = [1, 2, 3, 4, 5, 6, 7] |
| 1382 |
for i in range(5): |
| 1383 |
ls.insert(0, i) |
| 1384 |
assert len(ls) == 12 |
| 1385 |
|
| 1386 |
l = [] |
| 1387 |
l.insert(4,2) |
| 1388 |
assert l == [2] |
| 1389 |
|
| 1390 |
l = [1,2,3] |
| 1391 |
l.insert(0,"a") |
| 1392 |
assert l == ["a", 1, 2, 3] |
| 1393 |
|
| 1394 |
l = list(range(3)) |
| 1395 |
l.insert(1,5) |
| 1396 |
assert l == [0,5,1,2] |
| 1397 |
|
| 1398 |
def test_pop(self): |
| 1399 |
c = list('hello world') |
| 1400 |
s = '' |
| 1401 |
for i in range(11): |
| 1402 |
s += c.pop() |
| 1403 |
assert s == 'dlrow olleh' |
| 1404 |
raises(IndexError, c.pop) |
| 1405 |
assert len(c) == 0 |
| 1406 |
|
| 1407 |
l = list(range(10)) |
| 1408 |
l.pop() |
| 1409 |
assert l == list(range(9)) |
| 1410 |
assert l.pop(0) == 0 |
| 1411 |
|
| 1412 |
l = [1.1, 2.2, 3.3] |
| 1413 |
l.pop() |
| 1414 |
assert l == [1.1, 2.2] |
| 1415 |
|
| 1416 |
l = [] |
| 1417 |
raises(IndexError, l.pop, 0) |
| 1418 |
|
| 1419 |
def test_custom_int(self): |
| 1420 |
class A(object): |
| 1421 |
def __init__(self, x): |
| 1422 |
self.x = x |
| 1423 |
|
| 1424 |
def __index__(self): |
| 1425 |
return self.x |
| 1426 |
|
| 1427 |
class B(object): |
| 1428 |
def __init__(self, x): |
| 1429 |
self.x = x |
| 1430 |
|
| 1431 |
def __int__(self): |
| 1432 |
return self.x |
| 1433 |
|
| 1434 |
l = list(range(10)) |
| 1435 |
with raises(TypeError) as e: |
| 1436 |
x = l.pop(B(-1)) |
| 1437 |
assert "cannot be interpreted as an integer" in str(e.value) |
| 1438 |
with raises(TypeError): |
| 1439 |
x = l.insert(B(0), 12) |
| 1440 |
|
| 1441 |
x = l.pop(A(-1)) |
| 1442 |
assert x == 9 |
| 1443 |
assert l == list(range(9)) |
| 1444 |
raises(TypeError, list(range(10)).pop, 1.0) |
| 1445 |
|
| 1446 |
def test_pop_negative(self): |
| 1447 |
l1 = [1,2,3,4] |
| 1448 |
l2 = ["1", "2", "3", "4"] |
| 1449 |
l3 = list(range(5)) |
| 1450 |
l4 = [1, 2, 3, "4"] |
| 1451 |
l5 = [1.1, 2.2, 3.3, 4.4] |
| 1452 |
|
| 1453 |
raises(IndexError, l1.pop, -5) |
| 1454 |
raises(IndexError, l2.pop, -5) |
| 1455 |
raises(IndexError, l3.pop, -6) |
| 1456 |
raises(IndexError, l4.pop, -5) |
| 1457 |
raises(IndexError, l5.pop, -5) |
| 1458 |
|
| 1459 |
assert l1.pop(-2) == 3 |
| 1460 |
assert l2.pop(-2) == "3" |
| 1461 |
assert l3.pop(-2) == 3 |
| 1462 |
assert l4.pop(-2) == 3 |
| 1463 |
assert l5.pop(-2) == 3.3 |
| 1464 |
|
| 1465 |
def test_remove(self): |
| 1466 |
c = list('hello world') |
| 1467 |
c.remove('l') |
| 1468 |
assert ''.join(c) == 'helo world' |
| 1469 |
c.remove('l') |
| 1470 |
assert ''.join(c) == 'heo world' |
| 1471 |
c.remove('l') |
| 1472 |
assert ''.join(c) == 'heo word' |
| 1473 |
raises(ValueError, c.remove, 'l') |
| 1474 |
assert ''.join(c) == 'heo word' |
| 1475 |
|
| 1476 |
l = list(range(5)) |
| 1477 |
l.remove(2) |
| 1478 |
assert l == [0, 1, 3, 4] |
| 1479 |
l = [0, 3, 5] |
| 1480 |
raises(ValueError, c.remove, 2) |
| 1481 |
|
| 1482 |
l = [0.0, 1.1, 2.2, 3.3, 4.4] |
| 1483 |
l.remove(2.2) |
| 1484 |
assert l == [0.0, 1.1, 3.3, 4.4] |
| 1485 |
l = [0.0, 3.3, 5.5] |
| 1486 |
raises(ValueError, c.remove, 2) |
| 1487 |
e = raises(ValueError, c.remove, 2.2) |
| 1488 |
if not self.on_cpython: |
| 1489 |
assert str(e.value) == 'list.remove(): 2.2 is not in list' |
| 1490 |
|
| 1491 |
def test_reverse(self): |
| 1492 |
c = list('hello world') |
| 1493 |
c.reverse() |
| 1494 |
assert ''.join(c) == 'dlrow olleh' |
| 1495 |
|
| 1496 |
l = list(range(3)) |
| 1497 |
l.reverse() |
| 1498 |
assert l == [2,1,0] |
| 1499 |
|
| 1500 |
r = list(range(3)) |
| 1501 |
r[0] = 1 |
| 1502 |
assert r == [1, 1, 2] |
| 1503 |
r.reverse() |
| 1504 |
assert r == [2, 1, 1] |
| 1505 |
|
| 1506 |
def test_reversed(self): |
| 1507 |
assert list(list('hello').__reversed__()) == ['o', 'l', 'l', 'e', 'h'] |
| 1508 |
assert list(reversed(list('hello'))) == ['o', 'l', 'l', 'e', 'h'] |
| 1509 |
|
| 1510 |
def test_mutate_while_remove(self): |
| 1511 |
class Mean(object): |
| 1512 |
def __init__(self, i): |
| 1513 |
self.i = i |
| 1514 |
def __eq__(self, other): |
| 1515 |
if self.i == 9: |
| 1516 |
del l[self.i - 1] |
| 1517 |
return True |
| 1518 |
else: |
| 1519 |
return False |
| 1520 |
l = [Mean(i) for i in range(10)] |
| 1521 |
|
| 1522 |
l.remove(None) |
| 1523 |
class Mean2(object): |
| 1524 |
def __init__(self, i): |
| 1525 |
self.i = i |
| 1526 |
def __eq__(self, other): |
| 1527 |
l.append(self.i) |
| 1528 |
return False |
| 1529 |
l = [Mean2(i) for i in range(10)] |
| 1530 |
|
| 1531 |
l.remove(5) |
| 1532 |
assert l[10:] == [0, 1, 2, 3, 4, 6, 7, 8, 9] |
| 1533 |
|
| 1534 |
def test_mutate_while_contains(self): |
| 1535 |
class Mean(object): |
| 1536 |
def __init__(self, i): |
| 1537 |
self.i = i |
| 1538 |
def __eq__(self, other): |
| 1539 |
if self.i == 9 == other: |
| 1540 |
del l[0] |
| 1541 |
return True |
| 1542 |
else: |
| 1543 |
return False |
| 1544 |
l = [Mean(i) for i in range(10)] |
| 1545 |
assert l.__contains__(9) |
| 1546 |
assert not l.__contains__(2) |
| 1547 |
|
| 1548 |
def test_mutate_while_extend(self): |
| 1549 |
|
| 1550 |
import sys |
| 1551 |
if self.runappdirect: |
| 1552 |
iterations = 10 |
| 1553 |
else: |
| 1554 |
|
| 1555 |
iterations = 2 |
| 1556 |
if hasattr(sys, 'pypy_translation_info'): |
| 1557 |
if sys.pypy_translation_info['translation.gc'] == 'boehm': |
| 1558 |
skip("not reliable on top of Boehm") |
| 1559 |
class A(object): |
| 1560 |
def __del__(self): |
| 1561 |
|
| 1562 |
del lst[:] |
| 1563 |
for i in range(iterations): |
| 1564 |
keepalive = [] |
| 1565 |
lst = list(str(i)) * 100 |
| 1566 |
A() |
| 1567 |
while lst: |
| 1568 |
keepalive.append(lst[:]) |
| 1569 |
|
| 1570 |
def test_mutate_while_slice(self): |
| 1571 |
class X: |
| 1572 |
def __index__(self): |
| 1573 |
del a[:] |
| 1574 |
return 1 |
| 1575 |
|
| 1576 |
a = [0] |
| 1577 |
assert a[:X():2] == [] |
| 1578 |
assert a == [] |
| 1579 |
|
| 1580 |
a = [0] |
| 1581 |
assert a[X():2] == [] |
| 1582 |
assert a == [] |
| 1583 |
|
| 1584 |
a = [0] |
| 1585 |
assert a[0:2:X()] == [] |
| 1586 |
assert a == [] |
| 1587 |
|
| 1588 |
def test_mutate_while_slice_delete(self): |
| 1589 |
class X(object): |
| 1590 |
def __index__(self): |
| 1591 |
l[:] = [1] |
| 1592 |
print(l) |
| 1593 |
return 10 |
| 1594 |
|
| 1595 |
l = [1] * 100 |
| 1596 |
del l[1:X():2] |
| 1597 |
assert l == [1] |
| 1598 |
|
| 1599 |
def test_mutate_while_repr(self): |
| 1600 |
class X(object): |
| 1601 |
def __repr__(self): |
| 1602 |
l.__init__() |
| 1603 |
return 'ouchie' |
| 1604 |
|
| 1605 |
l = [1] |
| 1606 |
l.append(X()) |
| 1607 |
l.append(191) |
| 1608 |
assert repr(l) == '[1, ouchie]' |
| 1609 |
|
| 1610 |
def test_mutate_while_eq(self): |
| 1611 |
class Mean(object): |
| 1612 |
def __init__(self, i): |
| 1613 |
self.i = i |
| 1614 |
def __eq__(self, other): |
| 1615 |
if self.i == 9: |
| 1616 |
del l1[self.i - 1] |
| 1617 |
return True |
| 1618 |
l1 = [Mean(i) for i in range(10)] |
| 1619 |
l2 = [Mean(i) for i in range(10)] |
| 1620 |
assert l1 != l2 |
| 1621 |
|
| 1622 |
class Mean(object): |
| 1623 |
def __init__(self, i): |
| 1624 |
self.i = i |
| 1625 |
def __eq__(self, other): |
| 1626 |
if self.i == 9: |
| 1627 |
del l1[self.i - 1] |
| 1628 |
del l2[self.i - 1] |
| 1629 |
return True |
| 1630 |
l1 = [Mean(i) for i in range(10)] |
| 1631 |
l2 = [Mean(i) for i in range(10)] |
| 1632 |
assert l1 == l2 |
| 1633 |
|
| 1634 |
def test_unicode(self): |
| 1635 |
s = "\ufffd\ufffd\ufffd" |
| 1636 |
assert s.encode("ascii", "replace") == b"???" |
| 1637 |
assert s.encode("ascii", "ignore") == b"" |
| 1638 |
l1 = [s.encode("ascii", "replace")] |
| 1639 |
assert l1[0] == b"???" |
| 1640 |
|
| 1641 |
l2 = [s.encode("ascii", "ignore")] |
| 1642 |
assert l2[0] == b"" |
| 1643 |
|
| 1644 |
l3 = [s] |
| 1645 |
assert l3[0].encode("ascii", "replace") == b"???" |
| 1646 |
|
| 1647 |
s = u"\u2039" |
| 1648 |
l1 = list(s) |
| 1649 |
assert len(l1) == 1 |
| 1650 |
|
| 1651 |
def test_unicode_bug_in_listview_utf8(self): |
| 1652 |
l1 = list(u'\u1234\u2345') |
| 1653 |
assert l1 == [u'\u1234', u'\u2345'] |
| 1654 |
|
| 1655 |
def test_list_from_set(self): |
| 1656 |
l = ['a'] |
| 1657 |
l.__init__(set('b')) |
| 1658 |
assert l == ['b'] |
| 1659 |
|
| 1660 |
def test_list_from_generator(self): |
| 1661 |
l = ['a'] |
| 1662 |
g = (i*i for i in range(5)) |
| 1663 |
l.__init__(g) |
| 1664 |
assert l == [0, 1, 4, 9, 16] |
| 1665 |
l.__init__(g) |
| 1666 |
assert l == [] |
| 1667 |
assert list(g) == [] |
| 1668 |
|
| 1669 |
def test_list_from_bytes(self): |
| 1670 |
b = list(b'abc') |
| 1671 |
assert b == [97, 98, 99] |
| 1672 |
|
| 1673 |
def test_uses_custom_iterator(self): |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
class X: pass |
| 1679 |
for base, arg in [ |
| 1680 |
(list, []), (list, [5]), (list, ['x']), (list, [X]), (list, ['x']), |
| 1681 |
(set, []), (set, [5]), (set, ['x']), (set, [X]), (set, ['x']), |
| 1682 |
(dict, []), (dict, [(5,6)]), (dict, [('x',7)]), (dict, [(X,8)]), |
| 1683 |
(dict, [('x', 7)]), |
| 1684 |
]: |
| 1685 |
print(base, arg) |
| 1686 |
class SubClass(base): |
| 1687 |
def __iter__(self): |
| 1688 |
return iter("foobar") |
| 1689 |
sub = SubClass(arg) |
| 1690 |
assert list(sub) == ['f', 'o', 'o', 'b', 'a', 'r'] |
| 1691 |
l = [] |
| 1692 |
l.extend(sub) |
| 1693 |
assert l == ['f', 'o', 'o', 'b', 'a', 'r'] |
| 1694 |
|
| 1695 |
l = ['Z'] |
| 1696 |
l.extend(sub) |
| 1697 |
assert l == ['Z', 'f', 'o', 'o', 'b', 'a', 'r'] |
| 1698 |
|
| 1699 |
class Sub2(base): |
| 1700 |
pass |
| 1701 |
assert list(Sub2(arg)) == list(base(arg)) |
| 1702 |
s = set() |
| 1703 |
s.update(Sub2(arg)) |
| 1704 |
assert s == set(base(arg)) |
| 1705 |
|
| 1706 |
def test_comparison(self): |
| 1707 |
assert ([] < []) is False |
| 1708 |
assert ([] <= []) is True |
| 1709 |
assert ([] == []) is True |
| 1710 |
assert ([] != []) is False |
| 1711 |
assert ([] > []) is False |
| 1712 |
assert ([] >= []) is True |
| 1713 |
assert ([5] < []) is False |
| 1714 |
assert ([5] <= []) is False |
| 1715 |
assert ([5] == []) is False |
| 1716 |
assert ([5] != []) is True |
| 1717 |
assert ([5] > []) is True |
| 1718 |
assert ([5] >= []) is True |
| 1719 |
assert ([] < [5]) is True |
| 1720 |
assert ([] <= [5]) is True |
| 1721 |
assert ([] == [5]) is False |
| 1722 |
assert ([] != [5]) is True |
| 1723 |
assert ([] > [5]) is False |
| 1724 |
assert ([] >= [5]) is False |
| 1725 |
assert ([4] < [5]) is True |
| 1726 |
assert ([4] <= [5]) is True |
| 1727 |
assert ([4] == [5]) is False |
| 1728 |
assert ([4] != [5]) is True |
| 1729 |
assert ([4] > [5]) is False |
| 1730 |
assert ([4] >= [5]) is False |
| 1731 |
assert ([5] < [5]) is False |
| 1732 |
assert ([5] <= [5]) is True |
| 1733 |
assert ([5] == [5]) is True |
| 1734 |
assert ([5] != [5]) is False |
| 1735 |
assert ([5] > [5]) is False |
| 1736 |
assert ([5] >= [5]) is True |
| 1737 |
assert ([6] < [5]) is False |
| 1738 |
assert ([6] <= [5]) is False |
| 1739 |
assert ([6] == [5]) is False |
| 1740 |
assert ([6] != [5]) is True |
| 1741 |
assert ([6] > [5]) is True |
| 1742 |
assert ([6] >= [5]) is True |
| 1743 |
N = float('nan') |
| 1744 |
assert ([N] < [5]) is False |
| 1745 |
assert ([N] <= [5]) is False |
| 1746 |
assert ([N] == [5]) is False |
| 1747 |
assert ([N] != [5]) is True |
| 1748 |
assert ([N] > [5]) is False |
| 1749 |
assert ([N] >= [5]) is False |
| 1750 |
assert ([5] < [N]) is False |
| 1751 |
assert ([5] <= [N]) is False |
| 1752 |
assert ([5] == [N]) is False |
| 1753 |
assert ([5] != [N]) is True |
| 1754 |
assert ([5] > [N]) is False |
| 1755 |
assert ([5] >= [N]) is False |
| 1756 |
|
| 1757 |
def test_resizelist_hint(self): |
| 1758 |
if self.on_cpython: |
| 1759 |
skip('pypy-only test') |
| 1760 |
import __pypy__ |
| 1761 |
l2 = [] |
| 1762 |
__pypy__.resizelist_hint(l2, 100) |
| 1763 |
l1 = [1, 2, 3] |
| 1764 |
l1[:] = l2 |
| 1765 |
assert len(l1) == 0 |
| 1766 |
|
| 1767 |
def test_use_method_for_wrong_object(self): |
| 1768 |
if self.on_cpython: |
| 1769 |
skip('pypy-only test') |
| 1770 |
raises(TypeError, list.append, 1, 2) |
| 1771 |
|
| 1772 |
def test_ne_NotImplemented(self): |
| 1773 |
class NonList(object): |
| 1774 |
pass |
| 1775 |
non_list = NonList() |
| 1776 |
assert [] != non_list |
| 1777 |
|
| 1778 |
def test_extend_from_empty_list_with_subclasses(self): |
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
class T(tuple): |
| 1784 |
def __iter__(self): |
| 1785 |
yield "ok" |
| 1786 |
assert list(T([5, 6])) == ["ok"] |
| 1787 |
|
| 1788 |
class L(list): |
| 1789 |
def __iter__(self): |
| 1790 |
yield "ok" |
| 1791 |
assert list(L([5, 6])) == ["ok"] |
| 1792 |
assert list(L([5.2, 6.3])) == ["ok"] |
| 1793 |
|
| 1794 |
class S(bytes): |
| 1795 |
def __iter__(self): |
| 1796 |
yield "ok" |
| 1797 |
assert list(S(b"don't see me")) == ["ok"] |
| 1798 |
|
| 1799 |
class U(str): |
| 1800 |
def __iter__(self): |
| 1801 |
yield "ok" |
| 1802 |
assert list(U("don't see me")) == ["ok"] |
| 1803 |
|
| 1804 |
class S(bytes): |
| 1805 |
def __getitem__(self, index): |
| 1806 |
never_called |
| 1807 |
assert list(S(b"abc")) == list(b"abc") |
| 1808 |
|
| 1809 |
class U(str): |
| 1810 |
def __getitem__(self, index): |
| 1811 |
never_called |
| 1812 |
assert list(U("abc")) == list("abc") |
| 1813 |
|
| 1814 |
def test_extend_from_nonempty_list_with_subclasses(self): |
| 1815 |
l = ["hi!"] |
| 1816 |
class T(tuple): |
| 1817 |
def __iter__(self): |
| 1818 |
yield "okT" |
| 1819 |
l.extend(T([5, 6])) |
| 1820 |
|
| 1821 |
class L(list): |
| 1822 |
def __iter__(self): |
| 1823 |
yield "okL" |
| 1824 |
l.extend(L([5, 6])) |
| 1825 |
l.extend(L([5.2, 6.3])) |
| 1826 |
|
| 1827 |
class S(bytes): |
| 1828 |
def __iter__(self): |
| 1829 |
yield "okS" |
| 1830 |
l.extend(S(b"don't see me")) |
| 1831 |
|
| 1832 |
class U(str): |
| 1833 |
def __iter__(self): |
| 1834 |
yield "okU" |
| 1835 |
l.extend(U("don't see me")) |
| 1836 |
|
| 1837 |
assert l == ["hi!", "okT", "okL", "okL", "okS", "okU"] |
| 1838 |
|
| 1839 |
class S(bytes): |
| 1840 |
def __getitem__(self, index): |
| 1841 |
never_called |
| 1842 |
l = [] |
| 1843 |
l.extend(S(b"abc")) |
| 1844 |
assert l == list(b"abc") |
| 1845 |
|
| 1846 |
class U(str): |
| 1847 |
def __getitem__(self, index): |
| 1848 |
never_called |
| 1849 |
l = [] |
| 1850 |
l.extend(U("abc")) |
| 1851 |
assert l == list("abc") |
| 1852 |
|
| 1853 |
def test_issue1266(self): |
| 1854 |
l = list(range(1)) |
| 1855 |
l.pop() |
| 1856 |
|
| 1857 |
l.append(1) |
| 1858 |
assert l == [1] |
| 1859 |
|
| 1860 |
l = list(range(1)) |
| 1861 |
l.pop() |
| 1862 |
|
| 1863 |
l.reverse() |
| 1864 |
assert l == [] |
| 1865 |
|
| 1866 |
def test_issue1266_ovf(self): |
| 1867 |
import sys |
| 1868 |
|
| 1869 |
l = list(range(0, sys.maxsize, sys.maxsize)) |
| 1870 |
l.append(sys.maxsize) |
| 1871 |
|
| 1872 |
|
| 1873 |
l.append(-2) |
| 1874 |
assert l == [0, sys.maxsize, -2] |
| 1875 |
assert -2 in l |
| 1876 |
|
| 1877 |
l = list(range(-sys.maxsize, sys.maxsize, sys.maxsize // 10)) |
| 1878 |
item11 = l[11] |
| 1879 |
assert l[::11] == [-sys.maxsize, item11] |
| 1880 |
assert item11 in l[::11] |
| 1881 |
|
| 1882 |
def test_bug_list_of_nans(self): |
| 1883 |
N = float('nan') |
| 1884 |
L1 = [N, 'foo'] |
| 1885 |
assert N in L1 |
| 1886 |
assert L1.index(N) == 0 |
| 1887 |
assert L1 == [N, 'foo'] |
| 1888 |
|
| 1889 |
L2 = [N, 0.0] |
| 1890 |
assert N in L2 |
| 1891 |
assert L2.index(N) == 0 |
| 1892 |
assert L2.index(-0.0) == 1 |
| 1893 |
assert L2 == [N, -0.0] |
| 1894 |
|
| 1895 |
L3 = [N, 0.0, -0.0, 0] |
| 1896 |
assert N in L3 |
| 1897 |
assert L3.index(N) == 0 |
| 1898 |
for i in [1, 2, 3]: |
| 1899 |
assert L3[i] == 0 |
| 1900 |
assert L3[i] == 0.0 |
| 1901 |
assert L3[i] == -0.0 |
| 1902 |
assert L3.index(0, i) == i |
| 1903 |
assert L3.index(0.0, i) == i |
| 1904 |
assert L3.index(-0.0, i) == i |
| 1905 |
|
| 1906 |
def test_list_new_pos_only(self): |
| 1907 |
with raises(TypeError): |
| 1908 |
list(sequence=[]) |
| 1909 |
|
| 1910 |
def test_generic_alias(self): |
| 1911 |
ga = list[int] |
| 1912 |
assert ga.__origin__ is list |
| 1913 |
assert ga.__args__ == (int, ) |
| 1914 |
|
| 1915 |
def test_subclass_kwarg(self): |
| 1916 |
class subclass_with_new(list): |
| 1917 |
def __new__(cls, arg, newarg=None): |
| 1918 |
self = super().__new__(cls, arg) |
| 1919 |
self.newarg = newarg |
| 1920 |
return self |
| 1921 |
u = subclass_with_new([1, 2], newarg=3) |
| 1922 |
assert u.newarg == 3 |
| 1923 |
|
| 1924 |
class subclass_with_init(list): |
| 1925 |
def __init__(self, arg, newarg=None): |
| 1926 |
self.newarg = newarg |
| 1927 |
u = subclass_with_init([1, 2], newarg=3) |
| 1928 |
assert u.newarg == 3 |
| 1929 |
|
| 1930 |
with raises(TypeError) as e: |
| 1931 |
u = list([1, 2], newarg=3) |
| 1932 |
assert str(e.value) == "list() takes no keyword arguments" |
| 1933 |
|
| 1934 |
class subclass(list): |
| 1935 |
pass |
| 1936 |
with raises(TypeError) as e: |
| 1937 |
subclass(sequence=()) |
| 1938 |
assert str(e.value) == "list() takes no keyword arguments" |
| 1939 |
|
| 1940 |
def test_iadd_raises(self): |
| 1941 |
with raises(TypeError): |
| 1942 |
[].__iadd__(1) |
| 1943 |
|
| 1944 |
|
| 1945 |
class AppTestWithoutStrategies: |
| 1946 |
spaceconfig = {"objspace.std.withliststrategies": False} |
| 1947 |
|
| 1948 |
def test_no_shared_empty_list(self): |
| 1949 |
l = [] |
| 1950 |
copy = l[:] |
| 1951 |
copy.append({}) |
| 1952 |
assert copy == [{}] |
| 1953 |
|
| 1954 |
notshared = l[:] |
| 1955 |
assert notshared == [] |
| 1956 |
|
| 1957 |
|
| 1958 |
class AppTestListFastSubscr: |
| 1959 |
spaceconfig = {"objspace.std.optimized_list_getitem": True} |
| 1960 |
|
| 1961 |
def test_getitem(self): |
| 1962 |
import operator |
| 1963 |
l = [0, 1, 2, 3, 4] |
| 1964 |
for i in range(5): |
| 1965 |
assert l[i] == i |
| 1966 |
assert l[3:] == [3, 4] |
| 1967 |
raises(TypeError, operator.getitem, l, "str") |