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