Debian Codegraph @

Lib/test/test_bigmem.py

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