1 """Bigmem tests - tests for the 32-bit boundary in containers. 2 3 These tests try to exercise the 32-bit boundary that is sometimes, if 4 rarely, exceeded in practice, but almost never tested. They are really only 5 meaningful on 64-bit builds on machines with a *lot* of memory, but the 6 tests are always run, usually with very low memory limits to make sure the 7 tests themselves don't suffer from bitrot. To run them for real, pass a 8 high memory limit to regrtest, with the -M option. 9 """ 10 11 from test import support 12 from test.support import bigmemtest, _1G, _2G, _4G 13 14 import unittest 15 import operator 16 import sys 17 18 # These tests all use one of the bigmemtest decorators to indicate how much 19 # memory they use and how much memory they need to be even meaningful. The 20 # decorators take two arguments: a 'memuse' indicator declaring 21 # (approximate) bytes per size-unit the test will use (at peak usage), and a 22 # 'minsize' indicator declaring a minimum *useful* size. A test that 23 # allocates a bytestring to test various operations near the end will have a 24 # minsize of at least 2Gb (or it wouldn't reach the 32-bit limit, so the 25 # test wouldn't be very useful) and a memuse of 1 (one byte per size-unit, 26 # if it allocates only one big string at a time.) 27 # 28 # When run with a memory limit set, both decorators skip tests that need 29 # more memory than available to be meaningful. The precisionbigmemtest will 30 # always pass minsize as size, even if there is much more memory available. 31 # The bigmemtest decorator will scale size upward to fill available memory. 32 # 33 # Bigmem testing houserules: 34 # 35 # - Try not to allocate too many large objects. It's okay to rely on 36 # refcounting semantics, and don't forget that 's = create_largestring()' 37 # doesn't release the old 's' (if it exists) until well after its new 38 # value has been created. Use 'del s' before the create_largestring call. 39 # 40 # - Do *not* compare large objects using assertEqual, assertIn or similar. 41 # It's a lengthy operation and the errormessage will be utterly useless 42 # due to its size. To make sure whether a result has the right contents, 43 # better to use the strip or count methods, or compare meaningful slices. 44 # 45 # - Don't forget to test for large indices, offsets and results and such, 46 # in addition to large sizes. Anything that probes the 32-bit boundary. 47 # 48 # - When repeating an object (say, a substring, or a small list) to create 49 # a large object, make the subobject of a length that is not a power of 50 # 2. That way, int-wrapping problems are more easily detected. 51 # 52 # - Despite the bigmemtest decorator, all tests will actually be called 53 # with a much smaller number too, in the normal test run (5Kb currently.) 54 # This is so the tests themselves get frequent testing. 55 # Consequently, always make all large allocations based on the 56 # passed-in 'size', and don't rely on the size being very large. Also, 57 # memuse-per-size should remain sane (less than a few thousand); if your 58 # test uses more, adjust 'size' upward, instead. 59 60 # BEWARE: it seems that one failing test can yield other subsequent tests to 61 # fail as well. I do not know whether it is due to memory fragmentation 62 # issues, or other specifics of the platform malloc() routine. 63 64 ascii_char_size = 1 65 ucs2_char_size = 2 66 ucs4_char_size = 4 67 68 69 class BaseStrTest: 70 71 def _test_capitalize(self, size): 72 _ = self.from_latin1 73 SUBSTR = self.from_latin1(' abc def ghi') 74 s = _('-') * size + SUBSTR 75 caps = s.capitalize() 76 self.assertEqual(caps[-len(SUBSTR):], 77 SUBSTR.capitalize()) 78 self.assertEqual(caps.lstrip(_('-')), SUBSTR) 79 80 @bigmemtest(size=_2G + 10, memuse=1) 81 def test_center(self, size): 82 SUBSTR = self.from_latin1(' abc def ghi') 83 s = SUBSTR.center(size) 84 self.assertEqual(len(s), size) 85 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2 86 if len(s) % 2: 87 lpadsize += 1 88 self.assertEqual(s[lpadsize:-rpadsize], SUBSTR) 89 self.assertEqual(s.strip(), SUBSTR.strip()) 90 91 @bigmemtest(size=_2G, memuse=2) 92 def test_count(self, size): 93 _ = self.from_latin1 94 SUBSTR = _(' abc def ghi') 95 s = _('.') * size + SUBSTR 96 self.assertEqual(s.count(_('.')), size) 97 s += _('.') 98 self.assertEqual(s.count(_('.')), size + 1) 99 self.assertEqual(s.count(_(' ')), 3) 100 self.assertEqual(s.count(_('i')), 1) 101 self.assertEqual(s.count(_('j')), 0) 102 103 @bigmemtest(size=_2G, memuse=2) 104 def test_endswith(self, size): 105 _ = self.from_latin1 106 SUBSTR = _(' abc def ghi') 107 s = _('-') * size + SUBSTR 108 self.assertTrue(s.endswith(SUBSTR)) 109 self.assertTrue(s.endswith(s)) 110 s2 = _('...') + s 111 self.assertTrue(s2.endswith(s)) 112 self.assertFalse(s.endswith(_('a') + SUBSTR)) 113 self.assertFalse(SUBSTR.endswith(s)) 114 115 @bigmemtest(size=_2G + 10, memuse=2) 116 def test_expandtabs(self, size): 117 _ = self.from_latin1 118 s = _('-') * size 119 tabsize = 8 120 self.assertTrue(s.expandtabs() == s) 121 del s 122 slen, remainder = divmod(size, tabsize) 123 s = _(' \t') * slen 124 s = s.expandtabs(tabsize) 125 self.assertEqual(len(s), size - remainder) 126 self.assertEqual(len(s.strip(_(' '))), 0) 127 128 @bigmemtest(size=_2G, memuse=2) 129 def test_find(self, size): 130 _ = self.from_latin1 131 SUBSTR = _(' abc def ghi') 132 sublen = len(SUBSTR) 133 s = _('').join([SUBSTR, _('-') * size, SUBSTR]) 134 self.assertEqual(s.find(_(' ')), 0) 135 self.assertEqual(s.find(SUBSTR), 0) 136 self.assertEqual(s.find(_(' '), sublen), sublen + size) 137 self.assertEqual(s.find(SUBSTR, len(SUBSTR)), sublen + size) 138 self.assertEqual(s.find(_('i')), SUBSTR.find(_('i'))) 139 self.assertEqual(s.find(_('i'), sublen), 140 sublen + size + SUBSTR.find(_('i'))) 141 self.assertEqual(s.find(_('i'), size), 142 sublen + size + SUBSTR.find(_('i'))) 143 self.assertEqual(s.find(_('j')), -1) 144 145 @bigmemtest(size=_2G, memuse=2) 146 def test_index(self, size): 147 _ = self.from_latin1 148 SUBSTR = _(' abc def ghi') 149 sublen = len(SUBSTR) 150 s = _('').join([SUBSTR, _('-') * size, SUBSTR]) 151 self.assertEqual(s.index(_(' ')), 0) 152 self.assertEqual(s.index(SUBSTR), 0) 153 self.assertEqual(s.index(_(' '), sublen), sublen + size) 154 self.assertEqual(s.index(SUBSTR, sublen), sublen + size) 155 self.assertEqual(s.index(_('i')), SUBSTR.index(_('i'))) 156 self.assertEqual(s.index(_('i'), sublen), 157 sublen + size + SUBSTR.index(_('i'))) 158 self.assertEqual(s.index(_('i'), size), 159 sublen + size + SUBSTR.index(_('i'))) 160 self.assertRaises(ValueError, s.index, _('j')) 161 162 @bigmemtest(size=_2G, memuse=2) 163 def test_isalnum(self, size): 164 _ = self.from_latin1 165 SUBSTR = _('123456') 166 s = _('a') * size + SUBSTR 167 self.assertTrue(s.isalnum()) 168 s += _('.') 169 self.assertFalse(s.isalnum()) 170 171 @bigmemtest(size=_2G, memuse=2) 172 def test_isalpha(self, size): 173 _ = self.from_latin1 174 SUBSTR = _('zzzzzzz') 175 s = _('a') * size + SUBSTR 176 self.assertTrue(s.isalpha()) 177 s += _('.') 178 self.assertFalse(s.isalpha()) 179 180 @bigmemtest(size=_2G, memuse=2) 181 def test_isdigit(self, size): 182 _ = self.from_latin1 183 SUBSTR = _('123456') 184 s = _('9') * size + SUBSTR 185 self.assertTrue(s.isdigit()) 186 s += _('z') 187 self.assertFalse(s.isdigit()) 188 189 @bigmemtest(size=_2G, memuse=2) 190 def test_islower(self, size): 191 _ = self.from_latin1 192 chars = _(''.join( 193 chr(c) for c in range(255) if not chr(c).isupper())) 194 repeats = size // len(chars) + 2 195 s = chars * repeats 196 self.assertTrue(s.islower()) 197 s += _('A') 198 self.assertFalse(s.islower()) 199 200 @bigmemtest(size=_2G, memuse=2) 201 def test_isspace(self, size): 202 _ = self.from_latin1 203 whitespace = _(' \f\n\r\t\v') 204 repeats = size // len(whitespace) + 2 205 s = whitespace * repeats 206 self.assertTrue(s.isspace()) 207 s += _('j') 208 self.assertFalse(s.isspace()) 209 210 @bigmemtest(size=_2G, memuse=2) 211 def test_istitle(self, size): 212 _ = self.from_latin1 213 SUBSTR = _('123456') 214 s = _('').join([_('A'), _('a') * size, SUBSTR]) 215 self.assertTrue(s.istitle()) 216 s += _('A') 217 self.assertTrue(s.istitle()) 218 s += _('aA') 219 self.assertFalse(s.istitle()) 220 221 @bigmemtest(size=_2G, memuse=2) 222 def test_isupper(self, size): 223 _ = self.from_latin1 224 chars = _(''.join( 225 chr(c) for c in range(255) if not chr(c).islower())) 226 repeats = size // len(chars) + 2 227 s = chars * repeats 228 self.assertTrue(s.isupper()) 229 s += _('a') 230 self.assertFalse(s.isupper()) 231 232 @bigmemtest(size=_2G, memuse=2) 233 def test_join(self, size): 234 _ = self.from_latin1 235 s = _('A') * size 236 x = s.join([_('aaaaa'), _('bbbbb')]) 237 self.assertEqual(x.count(_('a')), 5) 238 self.assertEqual(x.count(_('b')), 5) 239 self.assertTrue(x.startswith(_('aaaaaA'))) 240 self.assertTrue(x.endswith(_('Abbbbb'))) 241 242 @bigmemtest(size=_2G + 10, memuse=1) 243 def test_ljust(self, size): 244 _ = self.from_latin1 245 SUBSTR = _(' abc def ghi') 246 s = SUBSTR.ljust(size) 247 self.assertTrue(s.startswith(SUBSTR + _(' '))) 248 self.assertEqual(len(s), size) 249 self.assertEqual(s.strip(), SUBSTR.strip()) 250 251 @bigmemtest(size=_2G + 10, memuse=2) 252 def test_lower(self, size): 253 _ = self.from_latin1 254 s = _('A') * size 255 s = s.lower() 256 self.assertEqual(len(s), size) 257 self.assertEqual(s.count(_('a')), size) 258 259 @bigmemtest(size=_2G + 10, memuse=1) 260 def test_lstrip(self, size): 261 _ = self.from_latin1 262 SUBSTR = _('abc def ghi') 263 s = SUBSTR.rjust(size) 264 self.assertEqual(len(s), size) 265 self.assertEqual(s.lstrip(), SUBSTR.lstrip()) 266 del s 267 s = SUBSTR.ljust(size) 268 self.assertEqual(len(s), size) 269 # Type-specific optimization 270 if isinstance(s, (str, bytes)): 271 stripped = s.lstrip() 272 self.assertTrue(stripped is s) 273 274 @bigmemtest(size=_2G + 10, memuse=2) 275 def test_replace(self, size): 276 _ = self.from_latin1 277 replacement = _('a') 278 s = _(' ') * size 279 s = s.replace(_(' '), replacement) 280 self.assertEqual(len(s), size) 281 self.assertEqual(s.count(replacement), size) 282 s = s.replace(replacement, _(' '), size - 4) 283 self.assertEqual(len(s), size) 284 self.assertEqual(s.count(replacement), 4) 285 self.assertEqual(s[-10:], _(' aaaa')) 286 287 @bigmemtest(size=_2G, memuse=2) 288 def test_rfind(self, size): 289 _ = self.from_latin1 290 SUBSTR = _(' abc def ghi') 291 sublen = len(SUBSTR) 292 s = _('').join([SUBSTR, _('-') * size, SUBSTR]) 293 self.assertEqual(s.rfind(_(' ')), sublen + size + SUBSTR.rfind(_(' '))) 294 self.assertEqual(s.rfind(SUBSTR), sublen + size) 295 self.assertEqual(s.rfind(_(' '), 0, size), SUBSTR.rfind(_(' '))) 296 self.assertEqual(s.rfind(SUBSTR, 0, sublen + size), 0) 297 self.assertEqual(s.rfind(_('i')), sublen + size + SUBSTR.rfind(_('i'))) 298 self.assertEqual(s.rfind(_('i'), 0, sublen), SUBSTR.rfind(_('i'))) 299 self.assertEqual(s.rfind(_('i'), 0, sublen + size), 300 SUBSTR.rfind(_('i'))) 301 self.assertEqual(s.rfind(_('j')), -1) 302 303 @bigmemtest(size=_2G, memuse=2) 304 def test_rindex(self, size): 305 _ = self.from_latin1 306 SUBSTR = _(' abc def ghi') 307 sublen = len(SUBSTR) 308 s = _('').join([SUBSTR, _('-') * size, SUBSTR]) 309 self.assertEqual(s.rindex(_(' ')), 310 sublen + size + SUBSTR.rindex(_(' '))) 311 self.assertEqual(s.rindex(SUBSTR), sublen + size) 312 self.assertEqual(s.rindex(_(' '), 0, sublen + size - 1), 313 SUBSTR.rindex(_(' '))) 314 self.assertEqual(s.rindex(SUBSTR, 0, sublen + size), 0) 315 self.assertEqual(s.rindex(_('i')), 316 sublen + size + SUBSTR.rindex(_('i'))) 317 self.assertEqual(s.rindex(_('i'), 0, sublen), SUBSTR.rindex(_('i'))) 318 self.assertEqual(s.rindex(_('i'), 0, sublen + size), 319 SUBSTR.rindex(_('i'))) 320 self.assertRaises(ValueError, s.rindex, _('j')) 321 322 @bigmemtest(size=_2G + 10, memuse=1) 323 def test_rjust(self, size): 324 _ = self.from_latin1 325 SUBSTR = _(' abc def ghi') 326 s = SUBSTR.ljust(size) 327 self.assertTrue(s.startswith(SUBSTR + _(' '))) 328 self.assertEqual(len(s), size) 329 self.assertEqual(s.strip(), SUBSTR.strip()) 330 331 @bigmemtest(size=_2G + 10, memuse=1) 332 def test_rstrip(self, size): 333 _ = self.from_latin1 334 SUBSTR = _(' abc def ghi') 335 s = SUBSTR.ljust(size) 336 self.assertEqual(len(s), size) 337 self.assertEqual(s.rstrip(), SUBSTR.rstrip()) 338 del s 339 s = SUBSTR.rjust(size) 340 self.assertEqual(len(s), size) 341 # Type-specific optimization 342 if isinstance(s, (str, bytes)): 343 stripped = s.rstrip() 344 self.assertTrue(stripped is s) 345 346 # The test takes about size bytes to build a string, and then about 347 # sqrt(size) substrings of sqrt(size) in size and a list to 348 # hold sqrt(size) items. It's close but just over 2x size. 349 @bigmemtest(size=_2G, memuse=2.1) 350 def test_split_small(self, size): 351 _ = self.from_latin1 352 # Crudely calculate an estimate so that the result of s.split won't 353 # take up an inordinate amount of memory 354 chunksize = int(size ** 0.5 + 2) 355 SUBSTR = _('a') + _(' ') * chunksize 356 s = SUBSTR * chunksize 357 l = s.split() 358 self.assertEqual(len(l), chunksize) 359 expected = _('a') 360 for item in l: 361 self.assertEqual(item, expected) 362 del l 363 l = s.split(_('a')) 364 self.assertEqual(len(l), chunksize + 1) 365 expected = _(' ') * chunksize 366 for item in filter(None, l): 367 self.assertEqual(item, expected) 368 369 # Allocates a string of twice size (and briefly two) and a list of 370 # size. Because of internal affairs, the s.split() call produces a 371 # list of size times the same one-character string, so we only 372 # suffer for the list size. (Otherwise, it'd cost another 48 times 373 # size in bytes!) Nevertheless, a list of size takes 374 # 8*size bytes. 375 @bigmemtest(size=_2G + 5, memuse=2 * ascii_char_size + 8) 376 def test_split_large(self, size): 377 _ = self.from_latin1 378 s = _(' a') * size + _(' ') 379 l = s.split() 380 self.assertEqual(len(l), size) 381 self.assertEqual(set(l), set([_('a')])) 382 del l 383 l = s.split(_('a')) 384 self.assertEqual(len(l), size + 1) 385 self.assertEqual(set(l), set([_(' ')])) 386 387 @bigmemtest(size=_2G, memuse=2.1) 388 def test_splitlines(self, size): 389 _ = self.from_latin1 390 # Crudely calculate an estimate so that the result of s.split won't 391 # take up an inordinate amount of memory 392 chunksize = int(size ** 0.5 + 2) // 2 393 SUBSTR = _(' ') * chunksize + _('\n') + _(' ') * chunksize + _('\r\n') 394 s = SUBSTR * (chunksize * 2) 395 l = s.splitlines() 396 self.assertEqual(len(l), chunksize * 4) 397 expected = _(' ') * chunksize 398 for item in l: 399 self.assertEqual(item, expected) 400 401 @bigmemtest(size=_2G, memuse=2) 402 def test_startswith(self, size): 403 _ = self.from_latin1 404 SUBSTR = _(' abc def ghi') 405 s = _('-') * size + SUBSTR 406 self.assertTrue(s.startswith(s)) 407 self.assertTrue(s.startswith(_('-') * size)) 408 self.assertFalse(s.startswith(SUBSTR)) 409 410 @bigmemtest(size=_2G, memuse=1) 411 def test_strip(self, size): 412 _ = self.from_latin1 413 SUBSTR = _(' abc def ghi ') 414 s = SUBSTR.rjust(size) 415 self.assertEqual(len(s), size) 416 self.assertEqual(s.strip(), SUBSTR.strip()) 417 del s 418 s = SUBSTR.ljust(size) 419 self.assertEqual(len(s), size) 420 self.assertEqual(s.strip(), SUBSTR.strip()) 421 422 def _test_swapcase(self, size): 423 _ = self.from_latin1 424 SUBSTR = _("aBcDeFG12.'\xa9\x00") 425 sublen = len(SUBSTR) 426 repeats = size // sublen + 2 427 s = SUBSTR * repeats 428 s = s.swapcase() 429 self.assertEqual(len(s), sublen * repeats) 430 self.assertEqual(s[:sublen * 3], SUBSTR.swapcase() * 3) 431 self.assertEqual(s[-sublen * 3:], SUBSTR.swapcase() * 3) 432 433 def _test_title(self, size): 434 _ = self.from_latin1 435 SUBSTR = _('SpaaHAaaAaham') 436 s = SUBSTR * (size // len(SUBSTR) + 2) 437 s = s.title() 438 self.assertTrue(s.startswith((SUBSTR * 3).title())) 439 self.assertTrue(s.endswith(SUBSTR.lower() * 3)) 440 441 @bigmemtest(size=_2G, memuse=2) 442 def test_translate(self, size): 443 _ = self.from_latin1 444 SUBSTR = _('aZz.z.Aaz.') 445 trans = bytes.maketrans(b'.aZ', b'-!$') 446 sublen = len(SUBSTR) 447 repeats = size // sublen + 2 448 s = SUBSTR * repeats 449 s = s.translate(trans) 450 self.assertEqual(len(s), repeats * sublen) 451 self.assertEqual(s[:sublen], SUBSTR.translate(trans)) 452 self.assertEqual(s[-sublen:], SUBSTR.translate(trans)) 453 self.assertEqual(s.count(_('.')), 0) 454 self.assertEqual(s.count(_('!')), repeats * 2) 455 self.assertEqual(s.count(_('z')), repeats * 3) 456 457 @bigmemtest(size=_2G + 5, memuse=2) 458 def test_upper(self, size): 459 _ = self.from_latin1 460 s = _('a') * size 461 s = s.upper() 462 self.assertEqual(len(s), size) 463 self.assertEqual(s.count(_('A')), size) 464 465 @bigmemtest(size=_2G + 20, memuse=1) 466 def test_zfill(self, size): 467 _ = self.from_latin1 468 SUBSTR = _('-568324723598234') 469 s = SUBSTR.zfill(size) 470 self.assertTrue(s.endswith(_('0') + SUBSTR[1:])) 471 self.assertTrue(s.startswith(_('-0'))) 472 self.assertEqual(len(s), size) 473 self.assertEqual(s.count(_('0')), size - len(SUBSTR)) 474 475 # This test is meaningful even with size < 2G, as long as the 476 # doubled string is > 2G (but it tests more if both are > 2G :) 477 @bigmemtest(size=_1G + 2, memuse=3) 478 def test_concat(self, size): 479 _ = self.from_latin1 480 s = _('.') * size 481 self.assertEqual(len(s), size) 482 s = s + s 483 self.assertEqual(len(s), size * 2) 484 self.assertEqual(s.count(_('.')), size * 2) 485 486 # This test is meaningful even with size < 2G, as long as the 487 # repeated string is > 2G (but it tests more if both are > 2G :) 488 @bigmemtest(size=_1G + 2, memuse=3) 489 def test_repeat(self, size): 490 _ = self.from_latin1 491 s = _('.') * size 492 self.assertEqual(len(s), size) 493 s = s * 2 494 self.assertEqual(len(s), size * 2) 495 self.assertEqual(s.count(_('.')), size * 2) 496 497 @bigmemtest(size=_2G + 20, memuse=2) 498 def test_slice_and_getitem(self, size): 499 _ = self.from_latin1 500 SUBSTR = _('0123456789') 501 sublen = len(SUBSTR) 502 s = SUBSTR * (size // sublen) 503 stepsize = len(s) // 100 504 stepsize = stepsize - (stepsize % sublen) 505 for i in range(0, len(s) - stepsize, stepsize): 506 self.assertEqual(s[i], SUBSTR[0]) 507 self.assertEqual(s[i:i + sublen], SUBSTR) 508 self.assertEqual(s[i:i + sublen:2], SUBSTR[::2]) 509 if i > 0: 510 self.assertEqual(s[i + sublen - 1:i - 1:-3], 511 SUBSTR[sublen::-3]) 512 # Make sure we do some slicing and indexing near the end of the 513 # string, too. 514 self.assertEqual(s[len(s) - 1], SUBSTR[-1]) 515 self.assertEqual(s[-1], SUBSTR[-1]) 516 self.assertEqual(s[len(s) - 10], SUBSTR[0]) 517 self.assertEqual(s[-sublen], SUBSTR[0]) 518 self.assertEqual(s[len(s):], _('')) 519 self.assertEqual(s[len(s) - 1:], SUBSTR[-1:]) 520 self.assertEqual(s[-1:], SUBSTR[-1:]) 521 self.assertEqual(s[len(s) - sublen:], SUBSTR) 522 self.assertEqual(s[-sublen:], SUBSTR) 523 self.assertEqual(len(s[:]), len(s)) 524 self.assertEqual(len(s[:len(s) - 5]), len(s) - 5) 525 self.assertEqual(len(s[5:-5]), len(s) - 10) 526 527 self.assertRaises(IndexError, operator.getitem, s, len(s)) 528 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1) 529 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31) 530 531 @bigmemtest(size=_2G, memuse=2) 532 def test_contains(self, size): 533 _ = self.from_latin1 534 SUBSTR = _('0123456789') 535 edge = _('-') * (size // 2) 536 s = _('').join([edge, SUBSTR, edge]) 537 del edge 538 self.assertTrue(SUBSTR in s) 539 self.assertFalse(SUBSTR * 2 in s) 540 self.assertTrue(_('-') in s) 541 self.assertFalse(_('a') in s) 542 s += _('a') 543 self.assertTrue(_('a') in s) 544 545 @bigmemtest(size=_2G + 10, memuse=2) 546 def test_compare(self, size): 547 _ = self.from_latin1 548 s1 = _('-') * size 549 s2 = _('-') * size 550 self.assertTrue(s1 == s2) 551 del s2 552 s2 = s1 + _('a') 553 self.assertFalse(s1 == s2) 554 del s2 555 s2 = _('.') * size 556 self.assertFalse(s1 == s2) 557 558 @bigmemtest(size=_2G + 10, memuse=1) 559 def test_hash(self, size): 560 # Not sure if we can do any meaningful tests here... Even if we 561 # start relying on the exact algorithm used, the result will be 562 # different depending on the size of the C 'long int'. Even this 563 # test is dodgy (there's no *guarantee* that the two things should 564 # have a different hash, even if they, in the current 565 # implementation, almost always do.) 566 _ = self.from_latin1 567 s = _('\x00') * size 568 h1 = hash(s) 569 del s 570 s = _('\x00') * (size + 1) 571 self.assertNotEqual(h1, hash(s)) 572 573 574 class StrTest(unittest.TestCase, BaseStrTest): 575 576 def from_latin1(self, s): 577 return s 578 579 def basic_encode_test(self, size, enc, c='.', expectedsize=None): 580 if expectedsize is None: 581 expectedsize = size 582 try: 583 s = c * size 584 self.assertEqual(len(s.encode(enc)), expectedsize) 585 finally: 586 s = None 587 588 def setUp(self): 589 # HACK: adjust memory use of tests inherited from BaseStrTest 590 # according to character size. 591 self._adjusted = {} 592 for name in dir(BaseStrTest): 593 if not name.startswith('test_'): 594 continue 595 meth = getattr(type(self), name) 596 try: 597 memuse = meth.memuse 598 except AttributeError: 599 continue 600 meth.memuse = ascii_char_size * memuse 601 self._adjusted[name] = memuse 602 603 def tearDown(self): 604 for name, memuse in self._adjusted.items(): 605 getattr(type(self), name).memuse = memuse 606 607 @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) 608 def test_capitalize(self, size): 609 self._test_capitalize(size) 610 611 @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) 612 def test_title(self, size): 613 self._test_title(size) 614 615 @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) 616 def test_swapcase(self, size): 617 self._test_swapcase(size) 618 619 # Many codecs convert to the legacy representation first, explaining 620 # why we add 'ucs4_char_size' to the 'memuse' below. 621 622 @bigmemtest(size=_2G + 2, memuse=ascii_char_size + 1) 623 def test_encode(self, size): 624 return self.basic_encode_test(size, 'utf-8') 625 626 @bigmemtest(size=_4G // 6 + 2, memuse=ascii_char_size + ucs4_char_size + 1) 627 def test_encode_raw_unicode_escape(self, size): 628 try: 629 return self.basic_encode_test(size, 'raw_unicode_escape') 630 except MemoryError: 631 pass # acceptable on 32-bit 632 633 @bigmemtest(size=_4G // 5 + 70, memuse=ascii_char_size + ucs4_char_size + 1) 634 def test_encode_utf7(self, size): 635 try: 636 return self.basic_encode_test(size, 'utf7') 637 except MemoryError: 638 pass # acceptable on 32-bit 639 640 @bigmemtest(size=_4G // 4 + 5, memuse=ascii_char_size + ucs4_char_size + 4) 641 def test_encode_utf32(self, size): 642 try: 643 return self.basic_encode_test(size, 'utf32', expectedsize=4 * size + 4) 644 except MemoryError: 645 pass # acceptable on 32-bit 646 647 @bigmemtest(size=_2G - 1, memuse=ascii_char_size + 1) 648 def test_encode_ascii(self, size): 649 return self.basic_encode_test(size, 'ascii', c='A') 650 651 # str % (...) uses a Py_UCS4 intermediate representation 652 653 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2 + ucs4_char_size) 654 def test_format(self, size): 655 s = '-' * size 656 sf = '%s' % (s,) 657 self.assertTrue(s == sf) 658 del sf 659 sf = '..%s..' % (s,) 660 self.assertEqual(len(sf), len(s) + 4) 661 self.assertTrue(sf.startswith('..-')) 662 self.assertTrue(sf.endswith('-..')) 663 del s, sf 664 665 size //= 2 666 edge = '-' * size 667 s = ''.join([edge, '%s', edge]) 668 del edge 669 s = s % '...' 670 self.assertEqual(len(s), size * 2 + 3) 671 self.assertEqual(s.count('.'), 3) 672 self.assertEqual(s.count('-'), size * 2) 673 674 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2) 675 def test_repr_small(self, size): 676 s = '-' * size 677 s = repr(s) 678 self.assertEqual(len(s), size + 2) 679 self.assertEqual(s[0], "'") 680 self.assertEqual(s[-1], "'") 681 self.assertEqual(s.count('-'), size) 682 del s 683 # repr() will create a string four times as large as this 'binary 684 # string', but we don't want to allocate much more than twice 685 # size in total. (We do extra testing in test_repr_large()) 686 size = size // 5 * 2 687 s = '\x00' * size 688 s = repr(s) 689 self.assertEqual(len(s), size * 4 + 2) 690 self.assertEqual(s[0], "'") 691 self.assertEqual(s[-1], "'") 692 self.assertEqual(s.count('\\'), size) 693 self.assertEqual(s.count('0'), size * 2) 694 695 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 5) 696 def test_repr_large(self, size): 697 s = '\x00' * size 698 s = repr(s) 699 self.assertEqual(len(s), size * 4 + 2) 700 self.assertEqual(s[0], "'") 701 self.assertEqual(s[-1], "'") 702 self.assertEqual(s.count('\\'), size) 703 self.assertEqual(s.count('0'), size * 2) 704 705 # ascii() calls encode('ascii', 'backslashreplace'), which itself 706 # creates a temporary Py_UNICODE representation in addition to the 707 # original (Py_UCS2) one 708 # There's also some overallocation when resizing the ascii() result 709 # that isn't taken into account here. 710 @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size + 711 ucs4_char_size + ascii_char_size * 6) 712 def test_unicode_repr(self, size): 713 # Use an assigned, but not printable code point. 714 # It is in the range of the low surrogates \uDC00-\uDFFF. 715 char = "\uDCBA" 716 s = char * size 717 try: 718 for f in (repr, ascii): 719 r = f(s) 720 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size) 721 self.assertTrue(r.endswith(r"\udcba'"), r[-10:]) 722 r = None 723 finally: 724 r = s = None 725 726 @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size * 2 + ascii_char_size * 10) 727 def test_unicode_repr_wide(self, size): 728 char = "\U0001DCBA" 729 s = char * size 730 try: 731 for f in (repr, ascii): 732 r = f(s) 733 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size) 734 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:]) 735 r = None 736 finally: 737 r = s = None 738 739 # The original test_translate is overridden here, so as to get the 740 # correct size estimate: str.translate() uses an intermediate Py_UCS4 741 # representation. 742 743 @bigmemtest(size=_2G, memuse=ascii_char_size * 2 + ucs4_char_size) 744 def test_translate(self, size): 745 _ = self.from_latin1 746 SUBSTR = _('aZz.z.Aaz.') 747 trans = { 748 ord(_('.')): _('-'), 749 ord(_('a')): _('!'), 750 ord(_('Z')): _('$'), 751 } 752 sublen = len(SUBSTR) 753 repeats = size // sublen + 2 754 s = SUBSTR * repeats 755 s = s.translate(trans) 756 self.assertEqual(len(s), repeats * sublen) 757 self.assertEqual(s[:sublen], SUBSTR.translate(trans)) 758 self.assertEqual(s[-sublen:], SUBSTR.translate(trans)) 759 self.assertEqual(s.count(_('.')), 0) 760 self.assertEqual(s.count(_('!')), repeats * 2) 761 self.assertEqual(s.count(_('z')), repeats * 3) 762 763 764 class BytesTest(unittest.TestCase, BaseStrTest): 765 766 def from_latin1(self, s): 767 return s.encode("latin-1") 768 769 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size) 770 def test_decode(self, size): 771 s = self.from_latin1('.') * size 772 self.assertEqual(len(s.decode('utf-8')), size) 773 774 @bigmemtest(size=_2G, memuse=2) 775 def test_capitalize(self, size): 776 self._test_capitalize(size) 777 778 @bigmemtest(size=_2G, memuse=2) 779 def test_title(self, size): 780 self._test_title(size) 781 782 @bigmemtest(size=_2G, memuse=2) 783 def test_swapcase(self, size): 784 self._test_swapcase(size) 785 786 787 class BytearrayTest(unittest.TestCase, BaseStrTest): 788 789 def from_latin1(self, s): 790 return bytearray(s.encode("latin-1")) 791 792 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size) 793 def test_decode(self, size): 794 s = self.from_latin1('.') * size 795 self.assertEqual(len(s.decode('utf-8')), size) 796 797 @bigmemtest(size=_2G, memuse=2) 798 def test_capitalize(self, size): 799 self._test_capitalize(size) 800 801 @bigmemtest(size=_2G, memuse=2) 802 def test_title(self, size): 803 self._test_title(size) 804 805 @bigmemtest(size=_2G, memuse=2) 806 def test_swapcase(self, size): 807 self._test_swapcase(size) 808 809 test_hash = None 810 test_split_large = None 811 812 class TupleTest(unittest.TestCase): 813 814 # Tuples have a small, fixed-sized head and an array of pointers to 815 # data. Since we're testing 64-bit addressing, we can assume that the 816 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes 817 # per size. 818 819 # As a side-effect of testing long tuples, these tests happen to test 820 # having more than 2<<31 references to any given object. Hence the 821 # use of different types of objects as contents in different tests. 822 823 @bigmemtest(size=_2G + 2, memuse=16) 824 def test_compare(self, size): 825 t1 = ('',) * size 826 t2 = ('',) * size 827 self.assertTrue(t1 == t2) 828 del t2 829 t2 = ('',) * (size + 1) 830 self.assertFalse(t1 == t2) 831 del t2 832 t2 = (1,) * size 833 self.assertFalse(t1 == t2) 834 835 # Test concatenating into a single tuple of more than 2G in length, 836 # and concatenating a tuple of more than 2G in length separately, so 837 # the smaller test still gets run even if there isn't memory for the 838 # larger test (but we still let the tester know the larger test is 839 # skipped, in verbose mode.) 840 def basic_concat_test(self, size): 841 t = ((),) * size 842 self.assertEqual(len(t), size) 843 t = t + t 844 self.assertEqual(len(t), size * 2) 845 846 @bigmemtest(size=_2G // 2 + 2, memuse=24) 847 def test_concat_small(self, size): 848 return self.basic_concat_test(size) 849 850 @bigmemtest(size=_2G + 2, memuse=24) 851 def test_concat_large(self, size): 852 return self.basic_concat_test(size) 853 854 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5) 855 def test_contains(self, size): 856 t = (1, 2, 3, 4, 5) * size 857 self.assertEqual(len(t), size * 5) 858 self.assertTrue(5 in t) 859 self.assertFalse((1, 2, 3, 4, 5) in t) 860 self.assertFalse(0 in t) 861 862 @bigmemtest(size=_2G + 10, memuse=8) 863 def test_hash(self, size): 864 t1 = (0,) * size 865 h1 = hash(t1) 866 del t1 867 t2 = (0,) * (size + 1) 868 self.assertFalse(h1 == hash(t2)) 869 870 @bigmemtest(size=_2G + 10, memuse=8) 871 def test_index_and_slice(self, size): 872 t = (None,) * size 873 self.assertEqual(len(t), size) 874 self.assertEqual(t[-1], None) 875 self.assertEqual(t[5], None) 876 self.assertEqual(t[size - 1], None) 877 self.assertRaises(IndexError, operator.getitem, t, size) 878 self.assertEqual(t[:5], (None,) * 5) 879 self.assertEqual(t[-5:], (None,) * 5) 880 self.assertEqual(t[20:25], (None,) * 5) 881 self.assertEqual(t[-25:-20], (None,) * 5) 882 self.assertEqual(t[size - 5:], (None,) * 5) 883 self.assertEqual(t[size - 5:size], (None,) * 5) 884 self.assertEqual(t[size - 6:size - 2], (None,) * 4) 885 self.assertEqual(t[size:size], ()) 886 self.assertEqual(t[size:size+5], ()) 887 888 # Like test_concat, split in two. 889 def basic_test_repeat(self, size): 890 t = ('',) * size 891 self.assertEqual(len(t), size) 892 t = t * 2 893 self.assertEqual(len(t), size * 2) 894 895 @bigmemtest(size=_2G // 2 + 2, memuse=24) 896 def test_repeat_small(self, size): 897 return self.basic_test_repeat(size) 898 899 @bigmemtest(size=_2G + 2, memuse=24) 900 def test_repeat_large(self, size): 901 return self.basic_test_repeat(size) 902 903 @bigmemtest(size=_1G - 1, memuse=12) 904 def test_repeat_large_2(self, size): 905 return self.basic_test_repeat(size) 906 907 @bigmemtest(size=_1G - 1, memuse=9) 908 def test_from_2G_generator(self, size): 909 self.skipTest("test needs much more memory than advertised, see issue5438") 910 try: 911 t = tuple(range(size)) 912 except MemoryError: 913 pass # acceptable on 32-bit 914 else: 915 count = 0 916 for item in t: 917 self.assertEqual(item, count) 918 count += 1 919 self.assertEqual(count, size) 920 921 @bigmemtest(size=_1G - 25, memuse=9) 922 def test_from_almost_2G_generator(self, size): 923 self.skipTest("test needs much more memory than advertised, see issue5438") 924 try: 925 t = tuple(range(size)) 926 count = 0 927 for item in t: 928 self.assertEqual(item, count) 929 count += 1 930 self.assertEqual(count, size) 931 except MemoryError: 932 pass # acceptable, expected on 32-bit 933 934 # Like test_concat, split in two. 935 def basic_test_repr(self, size): 936 t = (0,) * size 937 s = repr(t) 938 # The repr of a tuple of 0's is exactly three times the tuple length. 939 self.assertEqual(len(s), size * 3) 940 self.assertEqual(s[:5], '(0, 0') 941 self.assertEqual(s[-5:], '0, 0)') 942 self.assertEqual(s.count('0'), size) 943 944 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size) 945 def test_repr_small(self, size): 946 return self.basic_test_repr(size) 947 948 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size) 949 def test_repr_large(self, size): 950 return self.basic_test_repr(size) 951 952 class ListTest(unittest.TestCase): 953 954 # Like tuples, lists have a small, fixed-sized head and an array of 955 # pointers to data, so 8 bytes per size. Also like tuples, we make the 956 # lists hold references to various objects to test their refcount 957 # limits. 958 959 @bigmemtest(size=_2G + 2, memuse=16) 960 def test_compare(self, size): 961 l1 = [''] * size 962 l2 = [''] * size 963 self.assertTrue(l1 == l2) 964 del l2 965 l2 = [''] * (size + 1) 966 self.assertFalse(l1 == l2) 967 del l2 968 l2 = [2] * size 969 self.assertFalse(l1 == l2) 970 971 # Test concatenating into a single list of more than 2G in length, 972 # and concatenating a list of more than 2G in length separately, so 973 # the smaller test still gets run even if there isn't memory for the 974 # larger test (but we still let the tester know the larger test is 975 # skipped, in verbose mode.) 976 def basic_test_concat(self, size): 977 l = [[]] * size 978 self.assertEqual(len(l), size) 979 l = l + l 980 self.assertEqual(len(l), size * 2) 981 982 @bigmemtest(size=_2G // 2 + 2, memuse=24) 983 def test_concat_small(self, size): 984 return self.basic_test_concat(size) 985 986 @bigmemtest(size=_2G + 2, memuse=24) 987 def test_concat_large(self, size): 988 return self.basic_test_concat(size) 989 990 def basic_test_inplace_concat(self, size): 991 l = [sys.stdout] * size 992 l += l 993 self.assertEqual(len(l), size * 2) 994 self.assertTrue(l[0] is l[-1]) 995 self.assertTrue(l[size - 1] is l[size + 1]) 996 997 @bigmemtest(size=_2G // 2 + 2, memuse=24) 998 def test_inplace_concat_small(self, size): 999 return self.basic_test_inplace_concat(size) 1000 1001 @bigmemtest(size=_2G + 2, memuse=24) 1002 def test_inplace_concat_large(self, size): 1003 return self.basic_test_inplace_concat(size) 1004 1005 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5) 1006 def test_contains(self, size): 1007 l = [1, 2, 3, 4, 5] * size 1008 self.assertEqual(len(l), size * 5) 1009 self.assertTrue(5 in l) 1010 self.assertFalse([1, 2, 3, 4, 5] in l) 1011 self.assertFalse(0 in l) 1012 1013 @bigmemtest(size=_2G + 10, memuse=8) 1014 def test_hash(self, size): 1015 l = [0] * size 1016 self.assertRaises(TypeError, hash, l) 1017 1018 @bigmemtest(size=_2G + 10, memuse=8) 1019 def test_index_and_slice(self, size): 1020 l = [None] * size 1021 self.assertEqual(len(l), size) 1022 self.assertEqual(l[-1], None) 1023 self.assertEqual(l[5], None) 1024 self.assertEqual(l[size - 1], None) 1025 self.assertRaises(IndexError, operator.getitem, l, size) 1026 self.assertEqual(l[:5], [None] * 5) 1027 self.assertEqual(l[-5:], [None] * 5) 1028 self.assertEqual(l[20:25], [None] * 5) 1029 self.assertEqual(l[-25:-20], [None] * 5) 1030 self.assertEqual(l[size - 5:], [None] * 5) 1031 self.assertEqual(l[size - 5:size], [None] * 5) 1032 self.assertEqual(l[size - 6:size - 2], [None] * 4) 1033 self.assertEqual(l[size:size], []) 1034 self.assertEqual(l[size:size+5], []) 1035 1036 l[size - 2] = 5 1037 self.assertEqual(len(l), size) 1038 self.assertEqual(l[-3:], [None, 5, None]) 1039 self.assertEqual(l.count(5), 1) 1040 self.assertRaises(IndexError, operator.setitem, l, size, 6) 1041 self.assertEqual(len(l), size) 1042 1043 l[size - 7:] = [1, 2, 3, 4, 5] 1044 size -= 2 1045 self.assertEqual(len(l), size) 1046 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5]) 1047 1048 l[:7] = [1, 2, 3, 4, 5] 1049 size -= 2 1050 self.assertEqual(len(l), size) 1051 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None]) 1052 1053 del l[size - 1] 1054 size -= 1 1055 self.assertEqual(len(l), size) 1056 self.assertEqual(l[-1], 4) 1057 1058 del l[-2:] 1059 size -= 2 1060 self.assertEqual(len(l), size) 1061 self.assertEqual(l[-1], 2) 1062 1063 del l[0] 1064 size -= 1 1065 self.assertEqual(len(l), size) 1066 self.assertEqual(l[0], 2) 1067 1068 del l[:2] 1069 size -= 2 1070 self.assertEqual(len(l), size) 1071 self.assertEqual(l[0], 4) 1072 1073 # Like test_concat, split in two. 1074 def basic_test_repeat(self, size): 1075 l = [] * size 1076 self.assertFalse(l) 1077 l = [''] * size 1078 self.assertEqual(len(l), size) 1079 l = l * 2 1080 self.assertEqual(len(l), size * 2) 1081 1082 @bigmemtest(size=_2G // 2 + 2, memuse=24) 1083 def test_repeat_small(self, size): 1084 return self.basic_test_repeat(size) 1085 1086 @bigmemtest(size=_2G + 2, memuse=24) 1087 def test_repeat_large(self, size): 1088 return self.basic_test_repeat(size) 1089 1090 def basic_test_inplace_repeat(self, size): 1091 l = [''] 1092 l *= size 1093 self.assertEqual(len(l), size) 1094 self.assertTrue(l[0] is l[-1]) 1095 del l 1096 1097 l = [''] * size 1098 l *= 2 1099 self.assertEqual(len(l), size * 2) 1100 self.assertTrue(l[size - 1] is l[-1]) 1101 1102 @bigmemtest(size=_2G // 2 + 2, memuse=16) 1103 def test_inplace_repeat_small(self, size): 1104 return self.basic_test_inplace_repeat(size) 1105 1106 @bigmemtest(size=_2G + 2, memuse=16) 1107 def test_inplace_repeat_large(self, size): 1108 return self.basic_test_inplace_repeat(size) 1109 1110 def basic_test_repr(self, size): 1111 l = [0] * size 1112 s = repr(l) 1113 # The repr of a list of 0's is exactly three times the list length. 1114 self.assertEqual(len(s), size * 3) 1115 self.assertEqual(s[:5], '[0, 0') 1116 self.assertEqual(s[-5:], '0, 0]') 1117 self.assertEqual(s.count('0'), size) 1118 1119 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size) 1120 def test_repr_small(self, size): 1121 return self.basic_test_repr(size) 1122 1123 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size) 1124 def test_repr_large(self, size): 1125 return self.basic_test_repr(size) 1126 1127 # list overallocates ~1/8th of the total size (on first expansion) so 1128 # the single list.append call puts memuse at 9 bytes per size. 1129 @bigmemtest(size=_2G, memuse=9) 1130 def test_append(self, size): 1131 l = [object()] * size 1132 l.append(object()) 1133 self.assertEqual(len(l), size+1) 1134 self.assertTrue(l[-3] is l[-2]) 1135 self.assertFalse(l[-2] is l[-1]) 1136 1137 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) 1138 def test_count(self, size): 1139 l = [1, 2, 3, 4, 5] * size 1140 self.assertEqual(l.count(1), size) 1141 self.assertEqual(l.count("1"), 0) 1142 1143 def basic_test_extend(self, size): 1144 l = [object] * size 1145 l.extend(l) 1146 self.assertEqual(len(l), size * 2) 1147 self.assertTrue(l[0] is l[-1]) 1148 self.assertTrue(l[size - 1] is l[size + 1]) 1149 1150 @bigmemtest(size=_2G // 2 + 2, memuse=16) 1151 def test_extend_small(self, size): 1152 return self.basic_test_extend(size) 1153 1154 @bigmemtest(size=_2G + 2, memuse=16) 1155 def test_extend_large(self, size): 1156 return self.basic_test_extend(size) 1157 1158 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) 1159 def test_index(self, size): 1160 l = [1, 2, 3, 4, 5] * size 1161 size *= 5 1162 self.assertEqual(l.index(1), 0) 1163 self.assertEqual(l.index(5, size - 5), size - 1) 1164 self.assertEqual(l.index(5, size - 5, size), size - 1) 1165 self.assertRaises(ValueError, l.index, 1, size - 4, size) 1166 self.assertRaises(ValueError, l.index, 6) 1167 1168 # This tests suffers from overallocation, just like test_append. 1169 @bigmemtest(size=_2G + 10, memuse=9) 1170 def test_insert(self, size): 1171 l = [1.0] * size 1172 l.insert(size - 1, "A") 1173 size += 1 1174 self.assertEqual(len(l), size) 1175 self.assertEqual(l[-3:], [1.0, "A", 1.0]) 1176 1177 l.insert(size + 1, "B") 1178 size += 1 1179 self.assertEqual(len(l), size) 1180 self.assertEqual(l[-3:], ["A", 1.0, "B"]) 1181 1182 l.insert(1, "C") 1183 size += 1 1184 self.assertEqual(len(l), size) 1185 self.assertEqual(l[:3], [1.0, "C", 1.0]) 1186 self.assertEqual(l[size - 3:], ["A", 1.0, "B"]) 1187 1188 @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5) 1189 def test_pop(self, size): 1190 l = ["a", "b", "c", "d", "e"] * size 1191 size *= 5 1192 self.assertEqual(len(l), size) 1193 1194 item = l.pop() 1195 size -= 1 1196 self.assertEqual(len(l), size) 1197 self.assertEqual(item, "e") 1198 self.assertEqual(l[-2:], ["c", "d"]) 1199 1200 item = l.pop(0) 1201 size -= 1 1202 self.assertEqual(len(l), size) 1203 self.assertEqual(item, "a") 1204 self.assertEqual(l[:2], ["b", "c"]) 1205 1206 item = l.pop(size - 2) 1207 size -= 1 1208 self.assertEqual(len(l), size) 1209 self.assertEqual(item, "c") 1210 self.assertEqual(l[-2:], ["b", "d"]) 1211 1212 @bigmemtest(size=_2G + 10, memuse=8) 1213 def test_remove(self, size): 1214 l = [10] * size 1215 self.assertEqual(len(l), size) 1216 1217 l.remove(10) 1218 size -= 1 1219 self.assertEqual(len(l), size) 1220 1221 # Because of the earlier l.remove(), this append doesn't trigger 1222 # a resize. 1223 l.append(5) 1224 size += 1 1225 self.assertEqual(len(l), size) 1226 self.assertEqual(l[-2:], [10, 5]) 1227 l.remove(5) 1228 size -= 1 1229 self.assertEqual(len(l), size) 1230 self.assertEqual(l[-2:], [10, 10]) 1231 1232 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) 1233 def test_reverse(self, size): 1234 l = [1, 2, 3, 4, 5] * size 1235 l.reverse() 1236 self.assertEqual(len(l), size * 5) 1237 self.assertEqual(l[-5:], [5, 4, 3, 2, 1]) 1238 self.assertEqual(l[:5], [5, 4, 3, 2, 1]) 1239 1240 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) 1241 def test_sort(self, size): 1242 l = [1, 2, 3, 4, 5] * size 1243 l.sort() 1244 self.assertEqual(len(l), size * 5) 1245 self.assertEqual(l.count(1), size) 1246 self.assertEqual(l[:10], [1] * 10) 1247 self.assertEqual(l[-10:], [5] * 10) 1248 1249 def test_main(): 1250 support.run_unittest(StrTest, BytesTest, BytearrayTest, 1251 TupleTest, ListTest) 1252 1253 if __name__ == '__main__': 1254 if len(sys.argv) > 1: 1255 support.set_memlimit(sys.argv[1]) 1256 test_main() 1257