1 from test
import test_support
2 from test
.test_support
import bigmemtest
, _1G
, _2G
, _4G
, precisionbigmemtest
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
42 self
.assertEquals(caps
[-len(SUBSTR
):],
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
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'
61 s
= SUBSTR
.center(size
)
63 pass # acceptable on 32-bit
65 self
.assertEquals(len(s
), size
)
66 lpadsize
= rpadsize
= (len(s
) - len(SUBSTR
)) // 2
69 self
.assertEquals(s
[lpadsize
:-rpadsize
], SUBSTR
)
70 self
.assertEquals(s
.strip(), SUBSTR
.strip())
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
)
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
):
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:
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
):
103 return self
.basic_encode_test(size
, 'raw_unicode_escape')
105 pass # acceptable on 32-bit
107 @precisionbigmemtest(size
=_4G
// 5 + 70, memuse
=3)
108 def test_encode_utf7(self
, size
):
110 return self
.basic_encode_test(size
, 'utf7')
112 pass # acceptable on 32-bit
114 @precisionbigmemtest(size
=_4G
// 4 + 5, memuse
=6)
115 def test_encode_utf32(self
, size
):
117 return self
.basic_encode_test(size
, 'utf32', expectedsize
=4*size
+4)
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
):
131 pass # acceptable on 32-bit
133 self
.assertTrue(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
.assertTrue(s
.endswith(SUBSTR
))
140 self
.assertTrue(s
.endswith(s
))
142 self
.assertTrue(s2
.endswith(s
))
143 self
.assertFalse(s
.endswith('a' + SUBSTR
))
144 self
.assertFalse(SUBSTR
.endswith(s
))
146 @bigmemtest(minsize
=_2G
+ 10, memuse
=2)
147 def test_expandtabs(self
, size
):
150 self
.assertEquals(s
.expandtabs(), s
)
152 slen
, remainder
= divmod(size
, tabsize
)
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'
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'
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
):
193 s
= 'a' * size
+ SUBSTR
194 self
.assertTrue(s
.isalnum())
196 self
.assertFalse(s
.isalnum())
198 @bigmemtest(minsize
=_2G
, memuse
=2)
199 def test_isalpha(self
, size
):
201 s
= 'a' * size
+ SUBSTR
202 self
.assertTrue(s
.isalpha())
204 self
.assertFalse(s
.isalpha())
206 @bigmemtest(minsize
=_2G
, memuse
=2)
207 def test_isdigit(self
, size
):
209 s
= '9' * size
+ SUBSTR
210 self
.assertTrue(s
.isdigit())
212 self
.assertFalse(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
219 self
.assertTrue(s
.islower())
221 self
.assertFalse(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
.assertTrue(s
.isspace())
230 self
.assertFalse(s
.isspace())
232 @bigmemtest(minsize
=_2G
, memuse
=2)
233 def test_istitle(self
, size
):
235 s
= ''.join(['A', 'a' * size
, SUBSTR
])
236 self
.assertTrue(s
.istitle())
238 self
.assertTrue(s
.istitle())
240 self
.assertFalse(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
247 self
.assertTrue(s
.isupper())
249 self
.assertFalse(s
.isupper())
251 @bigmemtest(minsize
=_2G
, memuse
=2)
252 def test_join(self
, size
):
254 x
= s
.join(['aaaaa', 'bbbbb'])
255 self
.assertEquals(x
.count('a'), 5)
256 self
.assertEquals(x
.count('b'), 5)
257 self
.assertTrue(x
.startswith('aaaaaA'))
258 self
.assertTrue(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
.assertTrue(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
):
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())
282 s
= SUBSTR
.ljust(size
)
283 self
.assertEquals(len(s
), size
)
284 stripped
= s
.lstrip()
285 self
.assertTrue(stripped
is s
)
287 @bigmemtest(minsize
=_2G
+ 10, memuse
=2)
288 def test_replace(self
, 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'
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
),
312 self
.assertEquals(s
.rfind('j'), -1)
314 @bigmemtest(minsize
=_2G
, memuse
=2)
315 def test_rindex(self
, size
):
316 SUBSTR
= ' abc def ghi'
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),
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
),
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
.assertTrue(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())
347 s
= SUBSTR
.rjust(size
)
348 self
.assertEquals(len(s
), size
)
349 stripped
= s
.rstrip()
350 self
.assertTrue(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
363 self
.assertEquals(len(l
), chunksize
)
364 self
.assertEquals(set(l
), set(['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
376 @bigmemtest(minsize
=_2G
+ 5, memuse
=10)
377 def test_split_large(self
, size
):
378 s
= ' a' * size
+ ' '
380 self
.assertEquals(len(l
), size
)
381 self
.assertEquals(set(l
), set(['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
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
.assertTrue(s
.startswith(s
))
403 self
.assertTrue(s
.startswith('-' * size
))
404 self
.assertFalse(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())
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"
421 repeats
= size
// sublen
+ 2
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)
433 self
.assertTrue(s
.startswith((SUBSTR
* 3).title()))
434 self
.assertTrue(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.'
441 repeats
= size
// sublen
+ 2
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
):
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
.assertTrue(s
.endswith('0' + SUBSTR
[1:]))
463 self
.assertTrue(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
):
471 self
.assertTrue(s
== sf
)
474 self
.assertEquals(len(sf
), len(s
) + 4)
475 self
.assertTrue(sf
.startswith('..-'))
476 self
.assertTrue(sf
.endswith('-..'))
481 s
= ''.join([edge
, '%s', edge
])
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
):
492 self
.assertEquals(len(s
), size
+ 2)
493 self
.assertEquals(s
[0], "'")
494 self
.assertEquals(s
[-1], "'")
495 self
.assertEquals(s
.count('-'), size
)
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())
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
):
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
):
522 self
.assertTrue(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
):
529 self
.assertEquals(len(s
), size
)
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
):
539 self
.assertEquals(len(s
), size
)
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'
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])
556 self
.assertEquals(s
[i
+ sublen
- 1:i
- 1:-3],
558 # Make sure we do some slicing and indexing near the end of the
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
])
583 self
.assertIn(SUBSTR
, s
)
584 self
.assertNotIn(SUBSTR
* 2, s
)
585 self
.assertIn('-', s
)
586 self
.assertNotIn('a', s
)
588 self
.assertIn('a', s
)
590 @bigmemtest(minsize
=_2G
+ 10, memuse
=2)
591 def test_compare(self
, size
):
594 self
.assertTrue(s1
== s2
)
597 self
.assertFalse(s1
== s2
)
600 self
.assertFalse(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.)
613 s
= '\x00' * (size
+ 1)
614 self
.assertFalse(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
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
):
631 self
.assertTrue(t1
== t2
)
633 t2
= (u
'',) * (size
+ 1)
634 self
.assertFalse(t1
== t2
)
637 self
.assertFalse(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
):
646 self
.assertEquals(len(t
), size
)
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)
663 self
.assertNotIn((1, 2, 3, 4, 5), t
)
664 self
.assertNotIn(0, t
)
666 @bigmemtest(minsize
=_2G
+ 10, memuse
=8)
667 def test_hash(self
, size
):
671 t2
= (0,) * (size
+ 1)
672 self
.assertFalse(h1
== hash(t2
))
674 @bigmemtest(minsize
=_2G
+ 10, memuse
=8)
675 def test_index_and_slice(self
, 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
):
695 self
.assertEquals(len(t
), size
)
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
):
714 t
= tuple(xrange(size
))
716 pass # acceptable on 32-bit
720 self
.assertEquals(item
, count
)
722 self
.assertEquals(count
, size
)
724 @precisionbigmemtest(size
=_1G
- 25, memuse
=9)
725 def test_from_almost_2G_generator(self
, size
):
727 t
= tuple(xrange(size
))
730 self
.assertEquals(item
, count
)
732 self
.assertEquals(count
, size
)
734 pass # acceptable, expected on 32-bit
736 # Like test_concat, split in two.
737 def basic_test_repr(self
, size
):
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
761 @bigmemtest(minsize
=_2G
+ 2, memuse
=16)
762 def test_compare(self
, size
):
765 self
.assertTrue(l1
== l2
)
767 l2
= [u
''] * (size
+ 1)
768 self
.assertFalse(l1
== l2
)
771 self
.assertFalse(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
):
780 self
.assertEquals(len(l
), size
)
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
795 self
.assertEquals(len(l
), size
* 2)
796 self
.assertTrue(l
[0] is l
[-1])
797 self
.assertTrue(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)
812 self
.assertNotIn([1, 2, 3, 4, 5], l
)
813 self
.assertNotIn(0, l
)
815 @bigmemtest(minsize
=_2G
+ 10, memuse
=8)
816 def test_hash(self
, size
):
818 self
.assertRaises(TypeError, hash, l
)
820 @bigmemtest(minsize
=_2G
+ 10, memuse
=8)
821 def test_index_and_slice(self
, 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], [])
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]
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]
852 self
.assertEquals(len(l
), size
)
853 self
.assertEquals(l
[:7], [1, 2, 3, 4, 5, None, None])
857 self
.assertEquals(len(l
), size
)
858 self
.assertEquals(l
[-1], 4)
862 self
.assertEquals(len(l
), size
)
863 self
.assertEquals(l
[-1], 2)
867 self
.assertEquals(len(l
), size
)
868 self
.assertEquals(l
[0], 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
):
880 self
.assertEquals(len(l
), size
)
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
):
895 self
.assertEquals(len(l
), size
)
896 self
.assertTrue(l
[0] is l
[-1])
901 self
.assertEquals(len(l
), size
* 2)
902 self
.assertTrue(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
):
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
935 self
.assertEquals(len(l
), size
+1)
936 self
.assertTrue(l
[-3] is l
[-2])
937 self
.assertFalse(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
):
948 self
.assertEquals(len(l
), size
* 2)
949 self
.assertTrue(l
[0] is l
[-1])
950 self
.assertTrue(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
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
):
974 l
.insert(size
- 1, "A")
976 self
.assertEquals(len(l
), size
)
977 self
.assertEquals(l
[-3:], [1.0, "A", 1.0])
979 l
.insert(size
+ 1, "B")
981 self
.assertEquals(len(l
), size
)
982 self
.assertEquals(l
[-3:], ["A", 1.0, "B"])
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
994 self
.assertEquals(len(l
), size
)
998 self
.assertEquals(len(l
), size
)
999 self
.assertEquals(item
, u
"e")
1000 self
.assertEquals(l
[-2:], [u
"c", u
"d"])
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)
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
):
1017 self
.assertEquals(len(l
), size
)
1021 self
.assertEquals(len(l
), size
)
1023 # Because of the earlier l.remove(), this append doesn't trigger
1027 self
.assertEquals(len(l
), size
)
1028 self
.assertEquals(l
[-2:], [10, 5])
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
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
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
):
1056 with test_support
.check_py3k_warnings():
1057 b
= buffer("AAAA")*size
1059 pass # acceptable on 32-bit
1063 self
.assertEquals(c
, 'A')
1065 self
.assertEquals(count
, size
*4)
1068 test_support
.run_unittest(StrTest
, TupleTest
, ListTest
, BufferTest
)
1070 if __name__
== '__main__':
1071 if len(sys
.argv
) > 1:
1072 test_support
.set_memlimit(sys
.argv
[1])