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