Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / test / test_bigmem.py
blob360bcb28612a72e95205f2232568ccdf519c65ea
1 from test import test_support
2 from test.test_support import bigmemtest, _1G, _2G, _4G, precisionbigmemtest
4 import unittest
5 import operator
6 import string
7 import sys
9 # Bigmem testing houserules:
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.
16 # - Do *not* compare large objects using assertEquals 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.
21 # - Don't forget to test for large indices, offsets and results and such,
22 # in addition to large sizes.
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.
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.
36 class 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.assertEquals(caps[-len(SUBSTR):],
43 SUBSTR.capitalize())
44 self.assertEquals(caps.lstrip('-'), SUBSTR)
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.assertEquals(len(s), size)
51 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
52 if len(s) % 2:
53 lpadsize += 1
54 self.assertEquals(s[lpadsize:-rpadsize], SUBSTR)
55 self.assertEquals(s.strip(), SUBSTR.strip())
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.assertEquals(len(s), size)
66 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
67 if len(s) % 2:
68 lpadsize += 1
69 self.assertEquals(s[lpadsize:-rpadsize], SUBSTR)
70 self.assertEquals(s.strip(), SUBSTR.strip())
71 del s
73 @bigmemtest(minsize=_2G, memuse=2)
74 def test_count(self, size):
75 SUBSTR = ' abc def ghi'
76 s = '.' * size + SUBSTR
77 self.assertEquals(s.count('.'), size)
78 s += '.'
79 self.assertEquals(s.count('.'), size + 1)
80 self.assertEquals(s.count(' '), 3)
81 self.assertEquals(s.count('i'), 1)
82 self.assertEquals(s.count('j'), 0)
84 @bigmemtest(minsize=_2G + 2, memuse=3)
85 def test_decode(self, size):
86 s = '.' * size
87 self.assertEquals(len(s.decode('utf-8')), size)
89 def basic_encode_test(self, size, enc, c=u'.', expectedsize=None):
90 if expectedsize is None:
91 expectedsize = size
93 s = c * size
94 self.assertEquals(len(s.encode(enc)), expectedsize)
96 @bigmemtest(minsize=_2G + 2, memuse=3)
97 def test_encode(self, size):
98 return self.basic_encode_test(size, 'utf-8')
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
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
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
121 @precisionbigmemtest(size=_2G-1, memuse=2)
122 def test_decodeascii(self, size):
123 return self.basic_encode_test(size, 'ascii', c='A')
125 @precisionbigmemtest(size=_4G / 5, memuse=6+2)
126 def test_unicode_repr_oflw(self, size):
127 try:
128 s = u"\uAAAA"*size
129 r = repr(s)
130 except MemoryError:
131 pass # acceptable on 32-bit
132 else:
133 self.failUnless(s == eval(r))
135 @bigmemtest(minsize=_2G, memuse=2)
136 def test_endswith(self, size):
137 SUBSTR = ' abc def ghi'
138 s = '-' * size + SUBSTR
139 self.failUnless(s.endswith(SUBSTR))
140 self.failUnless(s.endswith(s))
141 s2 = '...' + s
142 self.failUnless(s2.endswith(s))
143 self.failIf(s.endswith('a' + SUBSTR))
144 self.failIf(SUBSTR.endswith(s))
146 @bigmemtest(minsize=_2G + 10, memuse=2)
147 def test_expandtabs(self, size):
148 s = '-' * size
149 tabsize = 8
150 self.assertEquals(s.expandtabs(), s)
151 del s
152 slen, remainder = divmod(size, tabsize)
153 s = ' \t' * slen
154 s = s.expandtabs(tabsize)
155 self.assertEquals(len(s), size - remainder)
156 self.assertEquals(len(s.strip(' ')), 0)
158 @bigmemtest(minsize=_2G, memuse=2)
159 def test_find(self, size):
160 SUBSTR = ' abc def ghi'
161 sublen = len(SUBSTR)
162 s = ''.join([SUBSTR, '-' * size, SUBSTR])
163 self.assertEquals(s.find(' '), 0)
164 self.assertEquals(s.find(SUBSTR), 0)
165 self.assertEquals(s.find(' ', sublen), sublen + size)
166 self.assertEquals(s.find(SUBSTR, len(SUBSTR)), sublen + size)
167 self.assertEquals(s.find('i'), SUBSTR.find('i'))
168 self.assertEquals(s.find('i', sublen),
169 sublen + size + SUBSTR.find('i'))
170 self.assertEquals(s.find('i', size),
171 sublen + size + SUBSTR.find('i'))
172 self.assertEquals(s.find('j'), -1)
174 @bigmemtest(minsize=_2G, memuse=2)
175 def test_index(self, size):
176 SUBSTR = ' abc def ghi'
177 sublen = len(SUBSTR)
178 s = ''.join([SUBSTR, '-' * size, SUBSTR])
179 self.assertEquals(s.index(' '), 0)
180 self.assertEquals(s.index(SUBSTR), 0)
181 self.assertEquals(s.index(' ', sublen), sublen + size)
182 self.assertEquals(s.index(SUBSTR, sublen), sublen + size)
183 self.assertEquals(s.index('i'), SUBSTR.index('i'))
184 self.assertEquals(s.index('i', sublen),
185 sublen + size + SUBSTR.index('i'))
186 self.assertEquals(s.index('i', size),
187 sublen + size + SUBSTR.index('i'))
188 self.assertRaises(ValueError, s.index, 'j')
190 @bigmemtest(minsize=_2G, memuse=2)
191 def test_isalnum(self, size):
192 SUBSTR = '123456'
193 s = 'a' * size + SUBSTR
194 self.failUnless(s.isalnum())
195 s += '.'
196 self.failIf(s.isalnum())
198 @bigmemtest(minsize=_2G, memuse=2)
199 def test_isalpha(self, size):
200 SUBSTR = 'zzzzzzz'
201 s = 'a' * size + SUBSTR
202 self.failUnless(s.isalpha())
203 s += '.'
204 self.failIf(s.isalpha())
206 @bigmemtest(minsize=_2G, memuse=2)
207 def test_isdigit(self, size):
208 SUBSTR = '123456'
209 s = '9' * size + SUBSTR
210 self.failUnless(s.isdigit())
211 s += 'z'
212 self.failIf(s.isdigit())
214 @bigmemtest(minsize=_2G, memuse=2)
215 def test_islower(self, size):
216 chars = ''.join([ chr(c) for c in range(255) if not chr(c).isupper() ])
217 repeats = size // len(chars) + 2
218 s = chars * repeats
219 self.failUnless(s.islower())
220 s += 'A'
221 self.failIf(s.islower())
223 @bigmemtest(minsize=_2G, memuse=2)
224 def test_isspace(self, size):
225 whitespace = ' \f\n\r\t\v'
226 repeats = size // len(whitespace) + 2
227 s = whitespace * repeats
228 self.failUnless(s.isspace())
229 s += 'j'
230 self.failIf(s.isspace())
232 @bigmemtest(minsize=_2G, memuse=2)
233 def test_istitle(self, size):
234 SUBSTR = '123456'
235 s = ''.join(['A', 'a' * size, SUBSTR])
236 self.failUnless(s.istitle())
237 s += 'A'
238 self.failUnless(s.istitle())
239 s += 'aA'
240 self.failIf(s.istitle())
242 @bigmemtest(minsize=_2G, memuse=2)
243 def test_isupper(self, size):
244 chars = ''.join([ chr(c) for c in range(255) if not chr(c).islower() ])
245 repeats = size // len(chars) + 2
246 s = chars * repeats
247 self.failUnless(s.isupper())
248 s += 'a'
249 self.failIf(s.isupper())
251 @bigmemtest(minsize=_2G, memuse=2)
252 def test_join(self, size):
253 s = 'A' * size
254 x = s.join(['aaaaa', 'bbbbb'])
255 self.assertEquals(x.count('a'), 5)
256 self.assertEquals(x.count('b'), 5)
257 self.failUnless(x.startswith('aaaaaA'))
258 self.failUnless(x.endswith('Abbbbb'))
260 @bigmemtest(minsize=_2G + 10, memuse=1)
261 def test_ljust(self, size):
262 SUBSTR = ' abc def ghi'
263 s = SUBSTR.ljust(size)
264 self.failUnless(s.startswith(SUBSTR + ' '))
265 self.assertEquals(len(s), size)
266 self.assertEquals(s.strip(), SUBSTR.strip())
268 @bigmemtest(minsize=_2G + 10, memuse=2)
269 def test_lower(self, size):
270 s = 'A' * size
271 s = s.lower()
272 self.assertEquals(len(s), size)
273 self.assertEquals(s.count('a'), size)
275 @bigmemtest(minsize=_2G + 10, memuse=1)
276 def test_lstrip(self, size):
277 SUBSTR = 'abc def ghi'
278 s = SUBSTR.rjust(size)
279 self.assertEquals(len(s), size)
280 self.assertEquals(s.lstrip(), SUBSTR.lstrip())
281 del s
282 s = SUBSTR.ljust(size)
283 self.assertEquals(len(s), size)
284 stripped = s.lstrip()
285 self.failUnless(stripped is s)
287 @bigmemtest(minsize=_2G + 10, memuse=2)
288 def test_replace(self, size):
289 replacement = 'a'
290 s = ' ' * size
291 s = s.replace(' ', replacement)
292 self.assertEquals(len(s), size)
293 self.assertEquals(s.count(replacement), size)
294 s = s.replace(replacement, ' ', size - 4)
295 self.assertEquals(len(s), size)
296 self.assertEquals(s.count(replacement), 4)
297 self.assertEquals(s[-10:], ' aaaa')
299 @bigmemtest(minsize=_2G, memuse=2)
300 def test_rfind(self, size):
301 SUBSTR = ' abc def ghi'
302 sublen = len(SUBSTR)
303 s = ''.join([SUBSTR, '-' * size, SUBSTR])
304 self.assertEquals(s.rfind(' '), sublen + size + SUBSTR.rfind(' '))
305 self.assertEquals(s.rfind(SUBSTR), sublen + size)
306 self.assertEquals(s.rfind(' ', 0, size), SUBSTR.rfind(' '))
307 self.assertEquals(s.rfind(SUBSTR, 0, sublen + size), 0)
308 self.assertEquals(s.rfind('i'), sublen + size + SUBSTR.rfind('i'))
309 self.assertEquals(s.rfind('i', 0, sublen), SUBSTR.rfind('i'))
310 self.assertEquals(s.rfind('i', 0, sublen + size),
311 SUBSTR.rfind('i'))
312 self.assertEquals(s.rfind('j'), -1)
314 @bigmemtest(minsize=_2G, memuse=2)
315 def test_rindex(self, size):
316 SUBSTR = ' abc def ghi'
317 sublen = len(SUBSTR)
318 s = ''.join([SUBSTR, '-' * size, SUBSTR])
319 self.assertEquals(s.rindex(' '),
320 sublen + size + SUBSTR.rindex(' '))
321 self.assertEquals(s.rindex(SUBSTR), sublen + size)
322 self.assertEquals(s.rindex(' ', 0, sublen + size - 1),
323 SUBSTR.rindex(' '))
324 self.assertEquals(s.rindex(SUBSTR, 0, sublen + size), 0)
325 self.assertEquals(s.rindex('i'),
326 sublen + size + SUBSTR.rindex('i'))
327 self.assertEquals(s.rindex('i', 0, sublen), SUBSTR.rindex('i'))
328 self.assertEquals(s.rindex('i', 0, sublen + size),
329 SUBSTR.rindex('i'))
330 self.assertRaises(ValueError, s.rindex, 'j')
332 @bigmemtest(minsize=_2G + 10, memuse=1)
333 def test_rjust(self, size):
334 SUBSTR = ' abc def ghi'
335 s = SUBSTR.ljust(size)
336 self.failUnless(s.startswith(SUBSTR + ' '))
337 self.assertEquals(len(s), size)
338 self.assertEquals(s.strip(), SUBSTR.strip())
340 @bigmemtest(minsize=_2G + 10, memuse=1)
341 def test_rstrip(self, size):
342 SUBSTR = ' abc def ghi'
343 s = SUBSTR.ljust(size)
344 self.assertEquals(len(s), size)
345 self.assertEquals(s.rstrip(), SUBSTR.rstrip())
346 del s
347 s = SUBSTR.rjust(size)
348 self.assertEquals(len(s), size)
349 stripped = s.rstrip()
350 self.failUnless(stripped is s)
352 # The test takes about size bytes to build a string, and then about
353 # sqrt(size) substrings of sqrt(size) in size and a list to
354 # hold sqrt(size) items. It's close but just over 2x size.
355 @bigmemtest(minsize=_2G, memuse=2.1)
356 def test_split_small(self, size):
357 # Crudely calculate an estimate so that the result of s.split won't
358 # take up an inordinate amount of memory
359 chunksize = int(size ** 0.5 + 2)
360 SUBSTR = 'a' + ' ' * chunksize
361 s = SUBSTR * chunksize
362 l = s.split()
363 self.assertEquals(len(l), chunksize)
364 self.assertEquals(set(l), set(['a']))
365 del l
366 l = s.split('a')
367 self.assertEquals(len(l), chunksize + 1)
368 self.assertEquals(set(l), set(['', ' ' * chunksize]))
370 # Allocates a string of twice size (and briefly two) and a list of
371 # size. Because of internal affairs, the s.split() call produces a
372 # list of size times the same one-character string, so we only
373 # suffer for the list size. (Otherwise, it'd cost another 48 times
374 # size in bytes!) Nevertheless, a list of size takes
375 # 8*size bytes.
376 @bigmemtest(minsize=_2G + 5, memuse=10)
377 def test_split_large(self, size):
378 s = ' a' * size + ' '
379 l = s.split()
380 self.assertEquals(len(l), size)
381 self.assertEquals(set(l), set(['a']))
382 del l
383 l = s.split('a')
384 self.assertEquals(len(l), size + 1)
385 self.assertEquals(set(l), set([' ']))
387 @bigmemtest(minsize=_2G, memuse=2.1)
388 def test_splitlines(self, size):
389 # Crudely calculate an estimate so that the result of s.split won't
390 # take up an inordinate amount of memory
391 chunksize = int(size ** 0.5 + 2) // 2
392 SUBSTR = ' ' * chunksize + '\n' + ' ' * chunksize + '\r\n'
393 s = SUBSTR * chunksize
394 l = s.splitlines()
395 self.assertEquals(len(l), chunksize * 2)
396 self.assertEquals(set(l), set([' ' * chunksize]))
398 @bigmemtest(minsize=_2G, memuse=2)
399 def test_startswith(self, size):
400 SUBSTR = ' abc def ghi'
401 s = '-' * size + SUBSTR
402 self.failUnless(s.startswith(s))
403 self.failUnless(s.startswith('-' * size))
404 self.failIf(s.startswith(SUBSTR))
406 @bigmemtest(minsize=_2G, memuse=1)
407 def test_strip(self, size):
408 SUBSTR = ' abc def ghi '
409 s = SUBSTR.rjust(size)
410 self.assertEquals(len(s), size)
411 self.assertEquals(s.strip(), SUBSTR.strip())
412 del s
413 s = SUBSTR.ljust(size)
414 self.assertEquals(len(s), size)
415 self.assertEquals(s.strip(), SUBSTR.strip())
417 @bigmemtest(minsize=_2G, memuse=2)
418 def test_swapcase(self, size):
419 SUBSTR = "aBcDeFG12.'\xa9\x00"
420 sublen = len(SUBSTR)
421 repeats = size // sublen + 2
422 s = SUBSTR * repeats
423 s = s.swapcase()
424 self.assertEquals(len(s), sublen * repeats)
425 self.assertEquals(s[:sublen * 3], SUBSTR.swapcase() * 3)
426 self.assertEquals(s[-sublen * 3:], SUBSTR.swapcase() * 3)
428 @bigmemtest(minsize=_2G, memuse=2)
429 def test_title(self, size):
430 SUBSTR = 'SpaaHAaaAaham'
431 s = SUBSTR * (size // len(SUBSTR) + 2)
432 s = s.title()
433 self.failUnless(s.startswith((SUBSTR * 3).title()))
434 self.failUnless(s.endswith(SUBSTR.lower() * 3))
436 @bigmemtest(minsize=_2G, memuse=2)
437 def test_translate(self, size):
438 trans = string.maketrans('.aZ', '-!$')
439 SUBSTR = 'aZz.z.Aaz.'
440 sublen = len(SUBSTR)
441 repeats = size // sublen + 2
442 s = SUBSTR * repeats
443 s = s.translate(trans)
444 self.assertEquals(len(s), repeats * sublen)
445 self.assertEquals(s[:sublen], SUBSTR.translate(trans))
446 self.assertEquals(s[-sublen:], SUBSTR.translate(trans))
447 self.assertEquals(s.count('.'), 0)
448 self.assertEquals(s.count('!'), repeats * 2)
449 self.assertEquals(s.count('z'), repeats * 3)
451 @bigmemtest(minsize=_2G + 5, memuse=2)
452 def test_upper(self, size):
453 s = 'a' * size
454 s = s.upper()
455 self.assertEquals(len(s), size)
456 self.assertEquals(s.count('A'), size)
458 @bigmemtest(minsize=_2G + 20, memuse=1)
459 def test_zfill(self, size):
460 SUBSTR = '-568324723598234'
461 s = SUBSTR.zfill(size)
462 self.failUnless(s.endswith('0' + SUBSTR[1:]))
463 self.failUnless(s.startswith('-0'))
464 self.assertEquals(len(s), size)
465 self.assertEquals(s.count('0'), size - len(SUBSTR))
467 @bigmemtest(minsize=_2G + 10, memuse=2)
468 def test_format(self, size):
469 s = '-' * size
470 sf = '%s' % (s,)
471 self.failUnless(s == sf)
472 del sf
473 sf = '..%s..' % (s,)
474 self.assertEquals(len(sf), len(s) + 4)
475 self.failUnless(sf.startswith('..-'))
476 self.failUnless(sf.endswith('-..'))
477 del s, sf
479 size //= 2
480 edge = '-' * size
481 s = ''.join([edge, '%s', edge])
482 del edge
483 s = s % '...'
484 self.assertEquals(len(s), size * 2 + 3)
485 self.assertEquals(s.count('.'), 3)
486 self.assertEquals(s.count('-'), size * 2)
488 @bigmemtest(minsize=_2G + 10, memuse=2)
489 def test_repr_small(self, size):
490 s = '-' * size
491 s = repr(s)
492 self.assertEquals(len(s), size + 2)
493 self.assertEquals(s[0], "'")
494 self.assertEquals(s[-1], "'")
495 self.assertEquals(s.count('-'), size)
496 del s
497 # repr() will create a string four times as large as this 'binary
498 # string', but we don't want to allocate much more than twice
499 # size in total. (We do extra testing in test_repr_large())
500 size = size // 5 * 2
501 s = '\x00' * size
502 s = repr(s)
503 self.assertEquals(len(s), size * 4 + 2)
504 self.assertEquals(s[0], "'")
505 self.assertEquals(s[-1], "'")
506 self.assertEquals(s.count('\\'), size)
507 self.assertEquals(s.count('0'), size * 2)
509 @bigmemtest(minsize=_2G + 10, memuse=5)
510 def test_repr_large(self, size):
511 s = '\x00' * size
512 s = repr(s)
513 self.assertEquals(len(s), size * 4 + 2)
514 self.assertEquals(s[0], "'")
515 self.assertEquals(s[-1], "'")
516 self.assertEquals(s.count('\\'), size)
517 self.assertEquals(s.count('0'), size * 2)
519 @bigmemtest(minsize=2**32 / 5, memuse=6+2)
520 def test_unicode_repr(self, size):
521 s = u"\uAAAA" * size
522 self.failUnless(len(repr(s)) > size)
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.assertEquals(len(s), size)
530 s = s + s
531 self.assertEquals(len(s), size * 2)
532 self.assertEquals(s.count('.'), size * 2)
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.assertEquals(len(s), size)
540 s = s * 2
541 self.assertEquals(len(s), size * 2)
542 self.assertEquals(s.count('.'), size * 2)
544 @bigmemtest(minsize=_2G + 20, memuse=1)
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.assertEquals(s[i], SUBSTR[0])
553 self.assertEquals(s[i:i + sublen], SUBSTR)
554 self.assertEquals(s[i:i + sublen:2], SUBSTR[::2])
555 if i > 0:
556 self.assertEquals(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.assertEquals(s[len(s) - 1], SUBSTR[-1])
561 self.assertEquals(s[-1], SUBSTR[-1])
562 self.assertEquals(s[len(s) - 10], SUBSTR[0])
563 self.assertEquals(s[-sublen], SUBSTR[0])
564 self.assertEquals(s[len(s):], '')
565 self.assertEquals(s[len(s) - 1:], SUBSTR[-1])
566 self.assertEquals(s[-1:], SUBSTR[-1])
567 self.assertEquals(s[len(s) - sublen:], SUBSTR)
568 self.assertEquals(s[-sublen:], SUBSTR)
569 self.assertEquals(len(s[:]), len(s))
570 self.assertEquals(len(s[:len(s) - 5]), len(s) - 5)
571 self.assertEquals(len(s[5:-5]), len(s) - 10)
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)
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.failUnless(SUBSTR in s)
584 self.failIf(SUBSTR * 2 in s)
585 self.failUnless('-' in s)
586 self.failIf('a' in s)
587 s += 'a'
588 self.failUnless('a' in s)
590 @bigmemtest(minsize=_2G + 10, memuse=2)
591 def test_compare(self, size):
592 s1 = '-' * size
593 s2 = '-' * size
594 self.failUnless(s1 == s2)
595 del s2
596 s2 = s1 + 'a'
597 self.failIf(s1 == s2)
598 del s2
599 s2 = '.' * size
600 self.failIf(s1 == s2)
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.failIf(h1 == hash(s))
616 class TupleTest(unittest.TestCase):
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.
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.
627 @bigmemtest(minsize=_2G + 2, memuse=16)
628 def test_compare(self, size):
629 t1 = (u'',) * size
630 t2 = (u'',) * size
631 self.failUnless(t1 == t2)
632 del t2
633 t2 = (u'',) * (size + 1)
634 self.failIf(t1 == t2)
635 del t2
636 t2 = (1,) * size
637 self.failIf(t1 == t2)
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.assertEquals(len(t), size)
647 t = t + t
648 self.assertEquals(len(t), size * 2)
650 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
651 def test_concat_small(self, size):
652 return self.basic_concat_test(size)
654 @bigmemtest(minsize=_2G + 2, memuse=24)
655 def test_concat_large(self, size):
656 return self.basic_concat_test(size)
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.assertEquals(len(t), size * 5)
662 self.failUnless(5 in t)
663 self.failIf((1, 2, 3, 4, 5) in t)
664 self.failIf(0 in t)
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.failIf(h1 == hash(t2))
674 @bigmemtest(minsize=_2G + 10, memuse=8)
675 def test_index_and_slice(self, size):
676 t = (None,) * size
677 self.assertEquals(len(t), size)
678 self.assertEquals(t[-1], None)
679 self.assertEquals(t[5], None)
680 self.assertEquals(t[size - 1], None)
681 self.assertRaises(IndexError, operator.getitem, t, size)
682 self.assertEquals(t[:5], (None,) * 5)
683 self.assertEquals(t[-5:], (None,) * 5)
684 self.assertEquals(t[20:25], (None,) * 5)
685 self.assertEquals(t[-25:-20], (None,) * 5)
686 self.assertEquals(t[size - 5:], (None,) * 5)
687 self.assertEquals(t[size - 5:size], (None,) * 5)
688 self.assertEquals(t[size - 6:size - 2], (None,) * 4)
689 self.assertEquals(t[size:size], ())
690 self.assertEquals(t[size:size+5], ())
692 # Like test_concat, split in two.
693 def basic_test_repeat(self, size):
694 t = ('',) * size
695 self.assertEquals(len(t), size)
696 t = t * 2
697 self.assertEquals(len(t), size * 2)
699 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
700 def test_repeat_small(self, size):
701 return self.basic_test_repeat(size)
703 @bigmemtest(minsize=_2G + 2, memuse=24)
704 def test_repeat_large(self, size):
705 return self.basic_test_repeat(size)
707 @bigmemtest(minsize=_1G - 1, memuse=12)
708 def test_repeat_large_2(self, size):
709 return self.basic_test_repeat(size)
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.assertEquals(item, count)
721 count += 1
722 self.assertEquals(count, size)
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.assertEquals(item, count)
731 count += 1
732 self.assertEquals(count, size)
733 except MemoryError:
734 pass # acceptable, expected on 32-bit
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.assertEquals(len(s), size * 3)
742 self.assertEquals(s[:5], '(0, 0')
743 self.assertEquals(s[-5:], '0, 0)')
744 self.assertEquals(s.count('0'), size)
746 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3)
747 def test_repr_small(self, size):
748 return self.basic_test_repr(size)
750 @bigmemtest(minsize=_2G + 2, memuse=8 + 3)
751 def test_repr_large(self, size):
752 return self.basic_test_repr(size)
754 class ListTest(unittest.TestCase):
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.
761 @bigmemtest(minsize=_2G + 2, memuse=16)
762 def test_compare(self, size):
763 l1 = [u''] * size
764 l2 = [u''] * size
765 self.failUnless(l1 == l2)
766 del l2
767 l2 = [u''] * (size + 1)
768 self.failIf(l1 == l2)
769 del l2
770 l2 = [2] * size
771 self.failIf(l1 == l2)
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.assertEquals(len(l), size)
781 l = l + l
782 self.assertEquals(len(l), size * 2)
784 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
785 def test_concat_small(self, size):
786 return self.basic_test_concat(size)
788 @bigmemtest(minsize=_2G + 2, memuse=24)
789 def test_concat_large(self, size):
790 return self.basic_test_concat(size)
792 def basic_test_inplace_concat(self, size):
793 l = [sys.stdout] * size
794 l += l
795 self.assertEquals(len(l), size * 2)
796 self.failUnless(l[0] is l[-1])
797 self.failUnless(l[size - 1] is l[size + 1])
799 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
800 def test_inplace_concat_small(self, size):
801 return self.basic_test_inplace_concat(size)
803 @bigmemtest(minsize=_2G + 2, memuse=24)
804 def test_inplace_concat_large(self, size):
805 return self.basic_test_inplace_concat(size)
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.assertEquals(len(l), size * 5)
811 self.failUnless(5 in l)
812 self.failIf([1, 2, 3, 4, 5] in l)
813 self.failIf(0 in l)
815 @bigmemtest(minsize=_2G + 10, memuse=8)
816 def test_hash(self, size):
817 l = [0] * size
818 self.failUnlessRaises(TypeError, hash, l)
820 @bigmemtest(minsize=_2G + 10, memuse=8)
821 def test_index_and_slice(self, size):
822 l = [None] * size
823 self.assertEquals(len(l), size)
824 self.assertEquals(l[-1], None)
825 self.assertEquals(l[5], None)
826 self.assertEquals(l[size - 1], None)
827 self.assertRaises(IndexError, operator.getitem, l, size)
828 self.assertEquals(l[:5], [None] * 5)
829 self.assertEquals(l[-5:], [None] * 5)
830 self.assertEquals(l[20:25], [None] * 5)
831 self.assertEquals(l[-25:-20], [None] * 5)
832 self.assertEquals(l[size - 5:], [None] * 5)
833 self.assertEquals(l[size - 5:size], [None] * 5)
834 self.assertEquals(l[size - 6:size - 2], [None] * 4)
835 self.assertEquals(l[size:size], [])
836 self.assertEquals(l[size:size+5], [])
838 l[size - 2] = 5
839 self.assertEquals(len(l), size)
840 self.assertEquals(l[-3:], [None, 5, None])
841 self.assertEquals(l.count(5), 1)
842 self.assertRaises(IndexError, operator.setitem, l, size, 6)
843 self.assertEquals(len(l), size)
845 l[size - 7:] = [1, 2, 3, 4, 5]
846 size -= 2
847 self.assertEquals(len(l), size)
848 self.assertEquals(l[-7:], [None, None, 1, 2, 3, 4, 5])
850 l[:7] = [1, 2, 3, 4, 5]
851 size -= 2
852 self.assertEquals(len(l), size)
853 self.assertEquals(l[:7], [1, 2, 3, 4, 5, None, None])
855 del l[size - 1]
856 size -= 1
857 self.assertEquals(len(l), size)
858 self.assertEquals(l[-1], 4)
860 del l[-2:]
861 size -= 2
862 self.assertEquals(len(l), size)
863 self.assertEquals(l[-1], 2)
865 del l[0]
866 size -= 1
867 self.assertEquals(len(l), size)
868 self.assertEquals(l[0], 2)
870 del l[:2]
871 size -= 2
872 self.assertEquals(len(l), size)
873 self.assertEquals(l[0], 4)
875 # Like test_concat, split in two.
876 def basic_test_repeat(self, size):
877 l = [] * size
878 self.failIf(l)
879 l = [''] * size
880 self.assertEquals(len(l), size)
881 l = l * 2
882 self.assertEquals(len(l), size * 2)
884 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
885 def test_repeat_small(self, size):
886 return self.basic_test_repeat(size)
888 @bigmemtest(minsize=_2G + 2, memuse=24)
889 def test_repeat_large(self, size):
890 return self.basic_test_repeat(size)
892 def basic_test_inplace_repeat(self, size):
893 l = ['']
894 l *= size
895 self.assertEquals(len(l), size)
896 self.failUnless(l[0] is l[-1])
897 del l
899 l = [''] * size
900 l *= 2
901 self.assertEquals(len(l), size * 2)
902 self.failUnless(l[size - 1] is l[-1])
904 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
905 def test_inplace_repeat_small(self, size):
906 return self.basic_test_inplace_repeat(size)
908 @bigmemtest(minsize=_2G + 2, memuse=16)
909 def test_inplace_repeat_large(self, size):
910 return self.basic_test_inplace_repeat(size)
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.assertEquals(len(s), size * 3)
917 self.assertEquals(s[:5], '[0, 0')
918 self.assertEquals(s[-5:], '0, 0]')
919 self.assertEquals(s.count('0'), size)
921 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3)
922 def test_repr_small(self, size):
923 return self.basic_test_repr(size)
925 @bigmemtest(minsize=_2G + 2, memuse=8 + 3)
926 def test_repr_large(self, size):
927 return self.basic_test_repr(size)
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.assertEquals(len(l), size+1)
936 self.failUnless(l[-3] is l[-2])
937 self.failIf(l[-2] is l[-1])
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.assertEquals(l.count(1), size)
943 self.assertEquals(l.count("1"), 0)
945 def basic_test_extend(self, size):
946 l = [file] * size
947 l.extend(l)
948 self.assertEquals(len(l), size * 2)
949 self.failUnless(l[0] is l[-1])
950 self.failUnless(l[size - 1] is l[size + 1])
952 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
953 def test_extend_small(self, size):
954 return self.basic_test_extend(size)
956 @bigmemtest(minsize=_2G + 2, memuse=16)
957 def test_extend_large(self, size):
958 return self.basic_test_extend(size)
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.assertEquals(l.index(1), 0)
965 self.assertEquals(l.index(5, size - 5), size - 1)
966 self.assertEquals(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)
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.assertEquals(len(l), size)
977 self.assertEquals(l[-3:], [1.0, "A", 1.0])
979 l.insert(size + 1, "B")
980 size += 1
981 self.assertEquals(len(l), size)
982 self.assertEquals(l[-3:], ["A", 1.0, "B"])
984 l.insert(1, "C")
985 size += 1
986 self.assertEquals(len(l), size)
987 self.assertEquals(l[:3], [1.0, "C", 1.0])
988 self.assertEquals(l[size - 3:], ["A", 1.0, "B"])
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.assertEquals(len(l), size)
996 item = l.pop()
997 size -= 1
998 self.assertEquals(len(l), size)
999 self.assertEquals(item, u"e")
1000 self.assertEquals(l[-2:], [u"c", u"d"])
1002 item = l.pop(0)
1003 size -= 1
1004 self.assertEquals(len(l), size)
1005 self.assertEquals(item, u"a")
1006 self.assertEquals(l[:2], [u"b", u"c"])
1008 item = l.pop(size - 2)
1009 size -= 1
1010 self.assertEquals(len(l), size)
1011 self.assertEquals(item, u"c")
1012 self.assertEquals(l[-2:], [u"b", u"d"])
1014 @bigmemtest(minsize=_2G + 10, memuse=8)
1015 def test_remove(self, size):
1016 l = [10] * size
1017 self.assertEquals(len(l), size)
1019 l.remove(10)
1020 size -= 1
1021 self.assertEquals(len(l), size)
1023 # Because of the earlier l.remove(), this append doesn't trigger
1024 # a resize.
1025 l.append(5)
1026 size += 1
1027 self.assertEquals(len(l), size)
1028 self.assertEquals(l[-2:], [10, 5])
1029 l.remove(5)
1030 size -= 1
1031 self.assertEquals(len(l), size)
1032 self.assertEquals(l[-2:], [10, 10])
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.assertEquals(len(l), size * 5)
1039 self.assertEquals(l[-5:], [5, 4, 3, 2, 1])
1040 self.assertEquals(l[:5], [5, 4, 3, 2, 1])
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.assertEquals(len(l), size * 5)
1047 self.assertEquals(l.count(1), size)
1048 self.assertEquals(l[:10], [1] * 10)
1049 self.assertEquals(l[-10:], [5] * 10)
1051 class BufferTest(unittest.TestCase):
1053 @precisionbigmemtest(size=_1G, memuse=4)
1054 def test_repeat(self, size):
1055 try:
1056 b = buffer("AAAA")*size
1057 except MemoryError:
1058 pass # acceptable on 32-bit
1059 else:
1060 count = 0
1061 for c in b:
1062 self.assertEquals(c, 'A')
1063 count += 1
1064 self.assertEquals(count, size*4)
1066 def test_main():
1067 test_support.run_unittest(StrTest, TupleTest, ListTest, BufferTest)
1069 if __name__ == '__main__':
1070 if len(sys.argv) > 1:
1071 test_support.set_memlimit(sys.argv[1])
1072 test_main()