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