blob: 91c62af9e3f339a59d53d98d38c243f0e6632c86 [file] [log] [blame]
Thomas Wouters518b5ae2011-03-25 11:42:37 +01001"""Bigmem tests - tests for the 32-bit boundary in containers.
2
3These tests try to exercise the 32-bit boundary that is sometimes, if
4rarely, exceeded in practice, but almost never tested. They are really only
5meaningful on 64-bit builds on machines with a *lot* of memory, but the
6tests are always run, usually with very low memory limits to make sure the
7tests themselves don't suffer from bitrot. To run them for real, pass a
8high memory limit to regrtest, with the -M option.
9"""
10
Benjamin Petersonee8712c2008-05-20 21:35:26 +000011from test import support
Neal Norwitz3ce5d922008-08-24 07:08:55 +000012from test.support import bigmemtest, _1G, _2G, _4G, precisionbigmemtest
Thomas Wouters477c8d52006-05-27 19:21:47 +000013
14import unittest
15import operator
Thomas Wouters477c8d52006-05-27 19:21:47 +000016import sys
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000017import functools
Thomas Wouters477c8d52006-05-27 19:21:47 +000018
Thomas Wouters518b5ae2011-03-25 11:42:37 +010019# 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#
Thomas Wouters477c8d52006-05-27 19:21:47 +000034# Bigmem testing houserules:
35#
36# - Try not to allocate too many large objects. It's okay to rely on
Thomas Wouters518b5ae2011-03-25 11:42:37 +010037# refcounting semantics, and don't forget that 's = create_largestring()'
Thomas Wouters477c8d52006-05-27 19:21:47 +000038# 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#
Thomas Wouters518b5ae2011-03-25 11:42:37 +010041# - 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.
Thomas Wouters477c8d52006-05-27 19:21:47 +000045#
46# - Don't forget to test for large indices, offsets and results and such,
Thomas Wouters518b5ae2011-03-25 11:42:37 +010047# in addition to large sizes. Anything that probes the 32-bit boundary.
Thomas Wouters477c8d52006-05-27 19:21:47 +000048#
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#
Thomas Wouters518b5ae2011-03-25 11:42:37 +010053# - While the bigmem decorators speak of 'minsize', all tests will actually
54# be called with a much smaller number too, in the normal test run (5Kb
55# currently.) This is so the tests themselves get frequent testing.
56# Consequently, always make all large allocations based on the passed-in
57# 'size', and don't rely on the size being very large. Also,
Thomas Wouters477c8d52006-05-27 19:21:47 +000058# memuse-per-size should remain sane (less than a few thousand); if your
59# test uses more, adjust 'size' upward, instead.
60
Antoine Pitrouec00e2f2009-03-07 23:52:26 +000061# 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
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000065character_size = 4 if sys.maxunicode > 0xFFFF else 2
66
67
68class BaseStrTest:
69
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 @bigmemtest(minsize=_2G, memuse=2)
71 def test_capitalize(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000072 _ = self.from_latin1
73 SUBSTR = self.from_latin1(' abc def ghi')
74 s = _('-') * size + SUBSTR
Thomas Wouters477c8d52006-05-27 19:21:47 +000075 caps = s.capitalize()
Ezio Melottib3aedd42010-11-20 19:04:17 +000076 self.assertEqual(caps[-len(SUBSTR):],
Thomas Wouters477c8d52006-05-27 19:21:47 +000077 SUBSTR.capitalize())
Ezio Melottib3aedd42010-11-20 19:04:17 +000078 self.assertEqual(caps.lstrip(_('-')), SUBSTR)
Thomas Wouters477c8d52006-05-27 19:21:47 +000079
80 @bigmemtest(minsize=_2G + 10, memuse=1)
81 def test_center(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000082 SUBSTR = self.from_latin1(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +000083 s = SUBSTR.center(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +000084 self.assertEqual(len(s), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +000085 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
86 if len(s) % 2:
87 lpadsize += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +000088 self.assertEqual(s[lpadsize:-rpadsize], SUBSTR)
89 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +000090
91 @bigmemtest(minsize=_2G, memuse=2)
92 def test_count(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000093 _ = self.from_latin1
94 SUBSTR = _(' abc def ghi')
95 s = _('.') * size + SUBSTR
Ezio Melottib3aedd42010-11-20 19:04:17 +000096 self.assertEqual(s.count(_('.')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000097 s += _('.')
Ezio Melottib3aedd42010-11-20 19:04:17 +000098 self.assertEqual(s.count(_('.')), size + 1)
99 self.assertEqual(s.count(_(' ')), 3)
100 self.assertEqual(s.count(_('i')), 1)
101 self.assertEqual(s.count(_('j')), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102
103 @bigmemtest(minsize=_2G, memuse=2)
104 def test_endswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000105 _ = self.from_latin1
106 SUBSTR = _(' abc def ghi')
107 s = _('-') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000108 self.assertTrue(s.endswith(SUBSTR))
109 self.assertTrue(s.endswith(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000110 s2 = _('...') + s
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000111 self.assertTrue(s2.endswith(s))
112 self.assertFalse(s.endswith(_('a') + SUBSTR))
113 self.assertFalse(SUBSTR.endswith(s))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114
115 @bigmemtest(minsize=_2G + 10, memuse=2)
116 def test_expandtabs(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000117 _ = self.from_latin1
118 s = _('-') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119 tabsize = 8
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100120 self.assertTrue(s.expandtabs() == s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121 del s
122 slen, remainder = divmod(size, tabsize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000123 s = _(' \t') * slen
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 s = s.expandtabs(tabsize)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000125 self.assertEqual(len(s), size - remainder)
126 self.assertEqual(len(s.strip(_(' '))), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127
128 @bigmemtest(minsize=_2G, memuse=2)
129 def test_find(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000130 _ = self.from_latin1
131 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000133 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000134 self.assertEqual(s.find(_(' ')), 0)
135 self.assertEqual(s.find(SUBSTR), 0)
136 self.assertEqual(s.find(_(' '), sublen), sublen + size)
137 self.assertEqual(s.find(SUBSTR, len(SUBSTR)), sublen + size)
138 self.assertEqual(s.find(_('i')), SUBSTR.find(_('i')))
139 self.assertEqual(s.find(_('i'), sublen),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000140 sublen + size + SUBSTR.find(_('i')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000141 self.assertEqual(s.find(_('i'), size),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000142 sublen + size + SUBSTR.find(_('i')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000143 self.assertEqual(s.find(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000144
145 @bigmemtest(minsize=_2G, memuse=2)
146 def test_index(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000147 _ = self.from_latin1
148 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000150 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000151 self.assertEqual(s.index(_(' ')), 0)
152 self.assertEqual(s.index(SUBSTR), 0)
153 self.assertEqual(s.index(_(' '), sublen), sublen + size)
154 self.assertEqual(s.index(SUBSTR, sublen), sublen + size)
155 self.assertEqual(s.index(_('i')), SUBSTR.index(_('i')))
156 self.assertEqual(s.index(_('i'), sublen),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000157 sublen + size + SUBSTR.index(_('i')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000158 self.assertEqual(s.index(_('i'), size),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000159 sublen + size + SUBSTR.index(_('i')))
160 self.assertRaises(ValueError, s.index, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161
162 @bigmemtest(minsize=_2G, memuse=2)
163 def test_isalnum(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000164 _ = self.from_latin1
165 SUBSTR = _('123456')
166 s = _('a') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000167 self.assertTrue(s.isalnum())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000168 s += _('.')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertFalse(s.isalnum())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000170
171 @bigmemtest(minsize=_2G, memuse=2)
172 def test_isalpha(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000173 _ = self.from_latin1
174 SUBSTR = _('zzzzzzz')
175 s = _('a') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000176 self.assertTrue(s.isalpha())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000177 s += _('.')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000178 self.assertFalse(s.isalpha())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000179
180 @bigmemtest(minsize=_2G, memuse=2)
181 def test_isdigit(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000182 _ = self.from_latin1
183 SUBSTR = _('123456')
184 s = _('9') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000185 self.assertTrue(s.isdigit())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000186 s += _('z')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000187 self.assertFalse(s.isdigit())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000188
189 @bigmemtest(minsize=_2G, memuse=2)
190 def test_islower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000191 _ = self.from_latin1
192 chars = _(''.join(
193 chr(c) for c in range(255) if not chr(c).isupper()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000194 repeats = size // len(chars) + 2
195 s = chars * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000196 self.assertTrue(s.islower())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000197 s += _('A')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000198 self.assertFalse(s.islower())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199
200 @bigmemtest(minsize=_2G, memuse=2)
201 def test_isspace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000202 _ = self.from_latin1
203 whitespace = _(' \f\n\r\t\v')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000204 repeats = size // len(whitespace) + 2
205 s = whitespace * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000206 self.assertTrue(s.isspace())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000207 s += _('j')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000208 self.assertFalse(s.isspace())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209
210 @bigmemtest(minsize=_2G, memuse=2)
211 def test_istitle(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000212 _ = self.from_latin1
213 SUBSTR = _('123456')
214 s = _('').join([_('A'), _('a') * size, SUBSTR])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000215 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000216 s += _('A')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000217 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000218 s += _('aA')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000219 self.assertFalse(s.istitle())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000220
221 @bigmemtest(minsize=_2G, memuse=2)
222 def test_isupper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000223 _ = self.from_latin1
224 chars = _(''.join(
225 chr(c) for c in range(255) if not chr(c).islower()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000226 repeats = size // len(chars) + 2
227 s = chars * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000228 self.assertTrue(s.isupper())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000229 s += _('a')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000230 self.assertFalse(s.isupper())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000231
232 @bigmemtest(minsize=_2G, memuse=2)
233 def test_join(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000234 _ = self.from_latin1
235 s = _('A') * size
236 x = s.join([_('aaaaa'), _('bbbbb')])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000237 self.assertEqual(x.count(_('a')), 5)
238 self.assertEqual(x.count(_('b')), 5)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000239 self.assertTrue(x.startswith(_('aaaaaA')))
240 self.assertTrue(x.endswith(_('Abbbbb')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000241
242 @bigmemtest(minsize=_2G + 10, memuse=1)
243 def test_ljust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000244 _ = self.from_latin1
245 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 s = SUBSTR.ljust(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000247 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000248 self.assertEqual(len(s), size)
249 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000250
251 @bigmemtest(minsize=_2G + 10, memuse=2)
252 def test_lower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000253 _ = self.from_latin1
254 s = _('A') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 s = s.lower()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000256 self.assertEqual(len(s), size)
257 self.assertEqual(s.count(_('a')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258
259 @bigmemtest(minsize=_2G + 10, memuse=1)
260 def test_lstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000261 _ = self.from_latin1
262 SUBSTR = _('abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000263 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000264 self.assertEqual(len(s), size)
265 self.assertEqual(s.lstrip(), SUBSTR.lstrip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000266 del s
267 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000268 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000269 # Type-specific optimization
270 if isinstance(s, (str, bytes)):
271 stripped = s.lstrip()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000272 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000273
274 @bigmemtest(minsize=_2G + 10, memuse=2)
275 def test_replace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000276 _ = self.from_latin1
277 replacement = _('a')
278 s = _(' ') * size
279 s = s.replace(_(' '), replacement)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000280 self.assertEqual(len(s), size)
281 self.assertEqual(s.count(replacement), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000282 s = s.replace(replacement, _(' '), size - 4)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000283 self.assertEqual(len(s), size)
284 self.assertEqual(s.count(replacement), 4)
285 self.assertEqual(s[-10:], _(' aaaa'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000286
287 @bigmemtest(minsize=_2G, memuse=2)
288 def test_rfind(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000289 _ = self.from_latin1
290 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000292 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000293 self.assertEqual(s.rfind(_(' ')), sublen + size + SUBSTR.rfind(_(' ')))
294 self.assertEqual(s.rfind(SUBSTR), sublen + size)
295 self.assertEqual(s.rfind(_(' '), 0, size), SUBSTR.rfind(_(' ')))
296 self.assertEqual(s.rfind(SUBSTR, 0, sublen + size), 0)
297 self.assertEqual(s.rfind(_('i')), sublen + size + SUBSTR.rfind(_('i')))
298 self.assertEqual(s.rfind(_('i'), 0, sublen), SUBSTR.rfind(_('i')))
299 self.assertEqual(s.rfind(_('i'), 0, sublen + size),
300 SUBSTR.rfind(_('i')))
301 self.assertEqual(s.rfind(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000302
303 @bigmemtest(minsize=_2G, memuse=2)
304 def test_rindex(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000305 _ = self.from_latin1
306 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000308 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000309 self.assertEqual(s.rindex(_(' ')),
310 sublen + size + SUBSTR.rindex(_(' ')))
311 self.assertEqual(s.rindex(SUBSTR), sublen + size)
312 self.assertEqual(s.rindex(_(' '), 0, sublen + size - 1),
313 SUBSTR.rindex(_(' ')))
314 self.assertEqual(s.rindex(SUBSTR, 0, sublen + size), 0)
315 self.assertEqual(s.rindex(_('i')),
316 sublen + size + SUBSTR.rindex(_('i')))
317 self.assertEqual(s.rindex(_('i'), 0, sublen), SUBSTR.rindex(_('i')))
318 self.assertEqual(s.rindex(_('i'), 0, sublen + size),
319 SUBSTR.rindex(_('i')))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000320 self.assertRaises(ValueError, s.rindex, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321
322 @bigmemtest(minsize=_2G + 10, memuse=1)
323 def test_rjust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000324 _ = self.from_latin1
325 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000326 s = SUBSTR.ljust(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000327 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000328 self.assertEqual(len(s), size)
329 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000330
331 @bigmemtest(minsize=_2G + 10, memuse=1)
332 def test_rstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000333 _ = self.from_latin1
334 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000336 self.assertEqual(len(s), size)
337 self.assertEqual(s.rstrip(), SUBSTR.rstrip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000338 del s
339 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000340 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000341 # Type-specific optimization
342 if isinstance(s, (str, bytes)):
343 stripped = s.rstrip()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000344 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345
346 # The test takes about size bytes to build a string, and then about
347 # sqrt(size) substrings of sqrt(size) in size and a list to
348 # hold sqrt(size) items. It's close but just over 2x size.
349 @bigmemtest(minsize=_2G, memuse=2.1)
350 def test_split_small(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000351 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352 # Crudely calculate an estimate so that the result of s.split won't
353 # take up an inordinate amount of memory
354 chunksize = int(size ** 0.5 + 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000355 SUBSTR = _('a') + _(' ') * chunksize
Thomas Wouters477c8d52006-05-27 19:21:47 +0000356 s = SUBSTR * chunksize
357 l = s.split()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000358 self.assertEqual(len(l), chunksize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000359 expected = _('a')
360 for item in l:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000361 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000363 l = s.split(_('a'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000364 self.assertEqual(len(l), chunksize + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000365 expected = _(' ') * chunksize
366 for item in filter(None, l):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000367 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000368
369 # Allocates a string of twice size (and briefly two) and a list of
370 # size. Because of internal affairs, the s.split() call produces a
371 # list of size times the same one-character string, so we only
372 # suffer for the list size. (Otherwise, it'd cost another 48 times
373 # size in bytes!) Nevertheless, a list of size takes
374 # 8*size bytes.
375 @bigmemtest(minsize=_2G + 5, memuse=10)
376 def test_split_large(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000377 _ = self.from_latin1
378 s = _(' a') * size + _(' ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000379 l = s.split()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000380 self.assertEqual(len(l), size)
381 self.assertEqual(set(l), set([_('a')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000383 l = s.split(_('a'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000384 self.assertEqual(len(l), size + 1)
385 self.assertEqual(set(l), set([_(' ')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386
387 @bigmemtest(minsize=_2G, memuse=2.1)
388 def test_splitlines(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000389 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000390 # Crudely calculate an estimate so that the result of s.split won't
391 # take up an inordinate amount of memory
392 chunksize = int(size ** 0.5 + 2) // 2
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000393 SUBSTR = _(' ') * chunksize + _('\n') + _(' ') * chunksize + _('\r\n')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000394 s = SUBSTR * chunksize
395 l = s.splitlines()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000396 self.assertEqual(len(l), chunksize * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000397 expected = _(' ') * chunksize
398 for item in l:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000399 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000400
401 @bigmemtest(minsize=_2G, memuse=2)
402 def test_startswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000403 _ = self.from_latin1
404 SUBSTR = _(' abc def ghi')
405 s = _('-') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000406 self.assertTrue(s.startswith(s))
407 self.assertTrue(s.startswith(_('-') * size))
408 self.assertFalse(s.startswith(SUBSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000409
410 @bigmemtest(minsize=_2G, memuse=1)
411 def test_strip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000412 _ = self.from_latin1
413 SUBSTR = _(' abc def ghi ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000414 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000415 self.assertEqual(len(s), size)
416 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000417 del s
418 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000419 self.assertEqual(len(s), size)
420 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000421
422 @bigmemtest(minsize=_2G, memuse=2)
423 def test_swapcase(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000424 _ = self.from_latin1
425 SUBSTR = _("aBcDeFG12.'\xa9\x00")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000426 sublen = len(SUBSTR)
427 repeats = size // sublen + 2
428 s = SUBSTR * repeats
429 s = s.swapcase()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000430 self.assertEqual(len(s), sublen * repeats)
431 self.assertEqual(s[:sublen * 3], SUBSTR.swapcase() * 3)
432 self.assertEqual(s[-sublen * 3:], SUBSTR.swapcase() * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000433
434 @bigmemtest(minsize=_2G, memuse=2)
435 def test_title(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000436 _ = self.from_latin1
437 SUBSTR = _('SpaaHAaaAaham')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000438 s = SUBSTR * (size // len(SUBSTR) + 2)
439 s = s.title()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000440 self.assertTrue(s.startswith((SUBSTR * 3).title()))
441 self.assertTrue(s.endswith(SUBSTR.lower() * 3))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000442
443 @bigmemtest(minsize=_2G, memuse=2)
444 def test_translate(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000445 _ = self.from_latin1
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000446 SUBSTR = _('aZz.z.Aaz.')
Georg Brandlabc38772009-04-12 15:51:51 +0000447 if isinstance(SUBSTR, str):
448 trans = {
449 ord(_('.')): _('-'),
450 ord(_('a')): _('!'),
451 ord(_('Z')): _('$'),
452 }
453 else:
454 trans = bytes.maketrans(b'.aZ', b'-!$')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000455 sublen = len(SUBSTR)
456 repeats = size // sublen + 2
457 s = SUBSTR * repeats
458 s = s.translate(trans)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000459 self.assertEqual(len(s), repeats * sublen)
460 self.assertEqual(s[:sublen], SUBSTR.translate(trans))
461 self.assertEqual(s[-sublen:], SUBSTR.translate(trans))
462 self.assertEqual(s.count(_('.')), 0)
463 self.assertEqual(s.count(_('!')), repeats * 2)
464 self.assertEqual(s.count(_('z')), repeats * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465
466 @bigmemtest(minsize=_2G + 5, memuse=2)
467 def test_upper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000468 _ = self.from_latin1
469 s = _('a') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000470 s = s.upper()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000471 self.assertEqual(len(s), size)
472 self.assertEqual(s.count(_('A')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000473
474 @bigmemtest(minsize=_2G + 20, memuse=1)
475 def test_zfill(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000476 _ = self.from_latin1
477 SUBSTR = _('-568324723598234')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000478 s = SUBSTR.zfill(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000479 self.assertTrue(s.endswith(_('0') + SUBSTR[1:]))
480 self.assertTrue(s.startswith(_('-0')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000481 self.assertEqual(len(s), size)
482 self.assertEqual(s.count(_('0')), size - len(SUBSTR))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000483
484 # This test is meaningful even with size < 2G, as long as the
485 # doubled string is > 2G (but it tests more if both are > 2G :)
486 @bigmemtest(minsize=_1G + 2, memuse=3)
487 def test_concat(self, size):
488 _ = self.from_latin1
489 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000490 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000491 s = s + s
Ezio Melottib3aedd42010-11-20 19:04:17 +0000492 self.assertEqual(len(s), size * 2)
493 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000494
495 # This test is meaningful even with size < 2G, as long as the
496 # repeated string is > 2G (but it tests more if both are > 2G :)
497 @bigmemtest(minsize=_1G + 2, memuse=3)
498 def test_repeat(self, size):
499 _ = self.from_latin1
500 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000501 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000502 s = s * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000503 self.assertEqual(len(s), size * 2)
504 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000505
506 @bigmemtest(minsize=_2G + 20, memuse=2)
507 def test_slice_and_getitem(self, size):
508 _ = self.from_latin1
509 SUBSTR = _('0123456789')
510 sublen = len(SUBSTR)
511 s = SUBSTR * (size // sublen)
512 stepsize = len(s) // 100
513 stepsize = stepsize - (stepsize % sublen)
514 for i in range(0, len(s) - stepsize, stepsize):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000515 self.assertEqual(s[i], SUBSTR[0])
516 self.assertEqual(s[i:i + sublen], SUBSTR)
517 self.assertEqual(s[i:i + sublen:2], SUBSTR[::2])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000518 if i > 0:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000519 self.assertEqual(s[i + sublen - 1:i - 1:-3],
520 SUBSTR[sublen::-3])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000521 # Make sure we do some slicing and indexing near the end of the
522 # string, too.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000523 self.assertEqual(s[len(s) - 1], SUBSTR[-1])
524 self.assertEqual(s[-1], SUBSTR[-1])
525 self.assertEqual(s[len(s) - 10], SUBSTR[0])
526 self.assertEqual(s[-sublen], SUBSTR[0])
527 self.assertEqual(s[len(s):], _(''))
528 self.assertEqual(s[len(s) - 1:], SUBSTR[-1:])
529 self.assertEqual(s[-1:], SUBSTR[-1:])
530 self.assertEqual(s[len(s) - sublen:], SUBSTR)
531 self.assertEqual(s[-sublen:], SUBSTR)
532 self.assertEqual(len(s[:]), len(s))
533 self.assertEqual(len(s[:len(s) - 5]), len(s) - 5)
534 self.assertEqual(len(s[5:-5]), len(s) - 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000535
536 self.assertRaises(IndexError, operator.getitem, s, len(s))
537 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1)
538 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31)
539
540 @bigmemtest(minsize=_2G, memuse=2)
541 def test_contains(self, size):
542 _ = self.from_latin1
543 SUBSTR = _('0123456789')
544 edge = _('-') * (size // 2)
545 s = _('').join([edge, SUBSTR, edge])
546 del edge
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100547 self.assertTrue(SUBSTR in s)
548 self.assertFalse(SUBSTR * 2 in s)
549 self.assertTrue(_('-') in s)
550 self.assertFalse(_('a') in s)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000551 s += _('a')
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100552 self.assertTrue(_('a') in s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000553
554 @bigmemtest(minsize=_2G + 10, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000555 def test_compare(self, size):
556 _ = self.from_latin1
557 s1 = _('-') * size
558 s2 = _('-') * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100559 self.assertTrue(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000560 del s2
561 s2 = s1 + _('a')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000562 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000563 del s2
564 s2 = _('.') * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000565 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000566
567 @bigmemtest(minsize=_2G + 10, memuse=1)
568 def test_hash(self, size):
569 # Not sure if we can do any meaningful tests here... Even if we
570 # start relying on the exact algorithm used, the result will be
571 # different depending on the size of the C 'long int'. Even this
572 # test is dodgy (there's no *guarantee* that the two things should
573 # have a different hash, even if they, in the current
574 # implementation, almost always do.)
575 _ = self.from_latin1
576 s = _('\x00') * size
577 h1 = hash(s)
578 del s
579 s = _('\x00') * (size + 1)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100580 self.assertNotEqual(h1, hash(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000581
582
583class StrTest(unittest.TestCase, BaseStrTest):
584
585 def from_latin1(self, s):
586 return s
587
588 def basic_encode_test(self, size, enc, c='.', expectedsize=None):
589 if expectedsize is None:
590 expectedsize = size
591
Antoine Pitrou45545f72011-01-12 20:46:37 +0000592 try:
593 s = c * size
594 self.assertEqual(len(s.encode(enc)), expectedsize)
595 finally:
596 s = None
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000597
598 def setUp(self):
599 # HACK: adjust memory use of tests inherited from BaseStrTest
600 # according to character size.
601 self._adjusted = {}
602 for name in dir(BaseStrTest):
603 if not name.startswith('test_'):
604 continue
605 meth = getattr(type(self), name)
606 try:
607 memuse = meth.memuse
608 except AttributeError:
609 continue
610 meth.memuse = character_size * memuse
611 self._adjusted[name] = memuse
612
613 def tearDown(self):
614 for name, memuse in self._adjusted.items():
615 getattr(type(self), name).memuse = memuse
616
Antoine Pitrou45545f72011-01-12 20:46:37 +0000617 # the utf8 encoder preallocates big time (4x the number of characters)
618 @bigmemtest(minsize=_2G + 2, memuse=character_size + 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000619 def test_encode(self, size):
620 return self.basic_encode_test(size, 'utf-8')
621
622 @precisionbigmemtest(size=_4G // 6 + 2, memuse=character_size + 1)
623 def test_encode_raw_unicode_escape(self, size):
624 try:
625 return self.basic_encode_test(size, 'raw_unicode_escape')
626 except MemoryError:
627 pass # acceptable on 32-bit
628
629 @precisionbigmemtest(size=_4G // 5 + 70, memuse=character_size + 1)
630 def test_encode_utf7(self, size):
631 try:
632 return self.basic_encode_test(size, 'utf7')
633 except MemoryError:
634 pass # acceptable on 32-bit
635
636 @precisionbigmemtest(size=_4G // 4 + 5, memuse=character_size + 4)
637 def test_encode_utf32(self, size):
638 try:
639 return self.basic_encode_test(size, 'utf32', expectedsize=4*size+4)
640 except MemoryError:
641 pass # acceptable on 32-bit
642
643 @precisionbigmemtest(size=_2G - 1, memuse=character_size + 1)
644 def test_encode_ascii(self, size):
645 return self.basic_encode_test(size, 'ascii', c='A')
646
647 @precisionbigmemtest(size=_4G // 5, memuse=character_size * (6 + 1))
648 def test_unicode_repr_overflow(self, size):
649 try:
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000650 s = "\uDCBA"*size
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000651 r = repr(s)
652 except MemoryError:
653 pass # acceptable on 32-bit
654 else:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000655 self.assertTrue(s == eval(r))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000656
657 @bigmemtest(minsize=_2G + 10, memuse=character_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 def test_format(self, size):
659 s = '-' * size
660 sf = '%s' % (s,)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100661 self.assertTrue(s == sf)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 del sf
663 sf = '..%s..' % (s,)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000664 self.assertEqual(len(sf), len(s) + 4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000665 self.assertTrue(sf.startswith('..-'))
666 self.assertTrue(sf.endswith('-..'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 del s, sf
668
669 size //= 2
670 edge = '-' * size
671 s = ''.join([edge, '%s', edge])
672 del edge
673 s = s % '...'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000674 self.assertEqual(len(s), size * 2 + 3)
675 self.assertEqual(s.count('.'), 3)
676 self.assertEqual(s.count('-'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000678 @bigmemtest(minsize=_2G + 10, memuse=character_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 def test_repr_small(self, size):
680 s = '-' * size
681 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000682 self.assertEqual(len(s), size + 2)
683 self.assertEqual(s[0], "'")
684 self.assertEqual(s[-1], "'")
685 self.assertEqual(s.count('-'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686 del s
687 # repr() will create a string four times as large as this 'binary
688 # string', but we don't want to allocate much more than twice
689 # size in total. (We do extra testing in test_repr_large())
690 size = size // 5 * 2
691 s = '\x00' * size
692 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000693 self.assertEqual(len(s), size * 4 + 2)
694 self.assertEqual(s[0], "'")
695 self.assertEqual(s[-1], "'")
696 self.assertEqual(s.count('\\'), size)
697 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000699 @bigmemtest(minsize=_2G + 10, memuse=character_size * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700 def test_repr_large(self, size):
701 s = '\x00' * size
702 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000703 self.assertEqual(len(s), size * 4 + 2)
704 self.assertEqual(s[0], "'")
705 self.assertEqual(s[-1], "'")
706 self.assertEqual(s.count('\\'), size)
707 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000709 @bigmemtest(minsize=2**32 / 5, memuse=character_size * 7)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000710 def test_unicode_repr(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000711 # Use an assigned, but not printable code point.
712 # It is in the range of the low surrogates \uDC00-\uDFFF.
713 s = "\uDCBA" * size
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000714 for f in (repr, ascii):
715 r = f(s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000716 self.assertTrue(len(r) > size)
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000717 self.assertTrue(r.endswith(r"\udcba'"), r[-10:])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000718 del r
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000719
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000720 # The character takes 4 bytes even in UCS-2 builds because it will
721 # be decomposed into surrogates.
722 @bigmemtest(minsize=2**32 / 5, memuse=4 + character_size * 9)
723 def test_unicode_repr_wide(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000724 s = "\U0001DCBA" * size
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000725 for f in (repr, ascii):
726 r = f(s)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000727 self.assertTrue(len(r) > size)
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000728 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000729 del r
Thomas Wouters477c8d52006-05-27 19:21:47 +0000730
Thomas Wouters477c8d52006-05-27 19:21:47 +0000731
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000732class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000734 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000735 return s.encode("latin-1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000737 @bigmemtest(minsize=_2G + 2, memuse=1 + character_size)
738 def test_decode(self, size):
739 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000740 self.assertEqual(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741
Thomas Wouters477c8d52006-05-27 19:21:47 +0000742
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000743class BytearrayTest(unittest.TestCase, BaseStrTest):
744
745 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000746 return bytearray(s.encode("latin-1"))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000747
748 @bigmemtest(minsize=_2G + 2, memuse=1 + character_size)
749 def test_decode(self, size):
750 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000751 self.assertEqual(len(s.decode('utf-8')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000752
753 test_hash = None
754 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755
756class TupleTest(unittest.TestCase):
757
758 # Tuples have a small, fixed-sized head and an array of pointers to
759 # data. Since we're testing 64-bit addressing, we can assume that the
760 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
761 # per size.
762
763 # As a side-effect of testing long tuples, these tests happen to test
764 # having more than 2<<31 references to any given object. Hence the
765 # use of different types of objects as contents in different tests.
766
767 @bigmemtest(minsize=_2G + 2, memuse=16)
768 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000769 t1 = ('',) * size
770 t2 = ('',) * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100771 self.assertTrue(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000773 t2 = ('',) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000774 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775 del t2
776 t2 = (1,) * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000777 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778
779 # Test concatenating into a single tuple of more than 2G in length,
780 # and concatenating a tuple of more than 2G in length separately, so
781 # the smaller test still gets run even if there isn't memory for the
782 # larger test (but we still let the tester know the larger test is
783 # skipped, in verbose mode.)
784 def basic_concat_test(self, size):
785 t = ((),) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000786 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 t = t + t
Ezio Melottib3aedd42010-11-20 19:04:17 +0000788 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789
790 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
791 def test_concat_small(self, size):
792 return self.basic_concat_test(size)
793
794 @bigmemtest(minsize=_2G + 2, memuse=24)
795 def test_concat_large(self, size):
796 return self.basic_concat_test(size)
797
798 @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5)
799 def test_contains(self, size):
800 t = (1, 2, 3, 4, 5) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000801 self.assertEqual(len(t), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100802 self.assertTrue(5 in t)
803 self.assertFalse((1, 2, 3, 4, 5) in t)
804 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000805
806 @bigmemtest(minsize=_2G + 10, memuse=8)
807 def test_hash(self, size):
808 t1 = (0,) * size
809 h1 = hash(t1)
810 del t1
811 t2 = (0,) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000812 self.assertFalse(h1 == hash(t2))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000813
814 @bigmemtest(minsize=_2G + 10, memuse=8)
815 def test_index_and_slice(self, size):
816 t = (None,) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000817 self.assertEqual(len(t), size)
818 self.assertEqual(t[-1], None)
819 self.assertEqual(t[5], None)
820 self.assertEqual(t[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821 self.assertRaises(IndexError, operator.getitem, t, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000822 self.assertEqual(t[:5], (None,) * 5)
823 self.assertEqual(t[-5:], (None,) * 5)
824 self.assertEqual(t[20:25], (None,) * 5)
825 self.assertEqual(t[-25:-20], (None,) * 5)
826 self.assertEqual(t[size - 5:], (None,) * 5)
827 self.assertEqual(t[size - 5:size], (None,) * 5)
828 self.assertEqual(t[size - 6:size - 2], (None,) * 4)
829 self.assertEqual(t[size:size], ())
830 self.assertEqual(t[size:size+5], ())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831
832 # Like test_concat, split in two.
833 def basic_test_repeat(self, size):
834 t = ('',) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000835 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000836 t = t * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000837 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838
839 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
840 def test_repeat_small(self, size):
841 return self.basic_test_repeat(size)
842
843 @bigmemtest(minsize=_2G + 2, memuse=24)
844 def test_repeat_large(self, size):
845 return self.basic_test_repeat(size)
846
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000847 @bigmemtest(minsize=_1G - 1, memuse=12)
848 def test_repeat_large_2(self, size):
849 return self.basic_test_repeat(size)
850
851 @precisionbigmemtest(size=_1G - 1, memuse=9)
852 def test_from_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000853 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000854 try:
855 t = tuple(range(size))
856 except MemoryError:
857 pass # acceptable on 32-bit
858 else:
859 count = 0
860 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000861 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000862 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000863 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000864
865 @precisionbigmemtest(size=_1G - 25, memuse=9)
866 def test_from_almost_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000867 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000868 try:
869 t = tuple(range(size))
870 count = 0
871 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000872 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000873 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000874 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000875 except MemoryError:
876 pass # acceptable, expected on 32-bit
877
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 # Like test_concat, split in two.
879 def basic_test_repr(self, size):
880 t = (0,) * size
881 s = repr(t)
882 # The repr of a tuple of 0's is exactly three times the tuple length.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000883 self.assertEqual(len(s), size * 3)
884 self.assertEqual(s[:5], '(0, 0')
885 self.assertEqual(s[-5:], '0, 0)')
886 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000887
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000888 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3 * character_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000889 def test_repr_small(self, size):
890 return self.basic_test_repr(size)
891
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000892 @bigmemtest(minsize=_2G + 2, memuse=8 + 3 * character_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893 def test_repr_large(self, size):
894 return self.basic_test_repr(size)
895
896class ListTest(unittest.TestCase):
897
898 # Like tuples, lists have a small, fixed-sized head and an array of
899 # pointers to data, so 8 bytes per size. Also like tuples, we make the
900 # lists hold references to various objects to test their refcount
901 # limits.
902
903 @bigmemtest(minsize=_2G + 2, memuse=16)
904 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000905 l1 = [''] * size
906 l2 = [''] * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100907 self.assertTrue(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000909 l2 = [''] * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000910 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911 del l2
912 l2 = [2] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000913 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914
915 # Test concatenating into a single list of more than 2G in length,
916 # and concatenating a list of more than 2G in length separately, so
917 # the smaller test still gets run even if there isn't memory for the
918 # larger test (but we still let the tester know the larger test is
919 # skipped, in verbose mode.)
920 def basic_test_concat(self, size):
921 l = [[]] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000922 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923 l = l + l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000924 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925
926 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
927 def test_concat_small(self, size):
928 return self.basic_test_concat(size)
929
930 @bigmemtest(minsize=_2G + 2, memuse=24)
931 def test_concat_large(self, size):
932 return self.basic_test_concat(size)
933
934 def basic_test_inplace_concat(self, size):
935 l = [sys.stdout] * size
936 l += l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000937 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000938 self.assertTrue(l[0] is l[-1])
939 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940
941 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
942 def test_inplace_concat_small(self, size):
943 return self.basic_test_inplace_concat(size)
944
945 @bigmemtest(minsize=_2G + 2, memuse=24)
946 def test_inplace_concat_large(self, size):
947 return self.basic_test_inplace_concat(size)
948
949 @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5)
950 def test_contains(self, size):
951 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000952 self.assertEqual(len(l), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100953 self.assertTrue(5 in l)
954 self.assertFalse([1, 2, 3, 4, 5] in l)
955 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956
957 @bigmemtest(minsize=_2G + 10, memuse=8)
958 def test_hash(self, size):
959 l = [0] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000960 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961
962 @bigmemtest(minsize=_2G + 10, memuse=8)
963 def test_index_and_slice(self, size):
964 l = [None] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000965 self.assertEqual(len(l), size)
966 self.assertEqual(l[-1], None)
967 self.assertEqual(l[5], None)
968 self.assertEqual(l[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000969 self.assertRaises(IndexError, operator.getitem, l, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000970 self.assertEqual(l[:5], [None] * 5)
971 self.assertEqual(l[-5:], [None] * 5)
972 self.assertEqual(l[20:25], [None] * 5)
973 self.assertEqual(l[-25:-20], [None] * 5)
974 self.assertEqual(l[size - 5:], [None] * 5)
975 self.assertEqual(l[size - 5:size], [None] * 5)
976 self.assertEqual(l[size - 6:size - 2], [None] * 4)
977 self.assertEqual(l[size:size], [])
978 self.assertEqual(l[size:size+5], [])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979
980 l[size - 2] = 5
Ezio Melottib3aedd42010-11-20 19:04:17 +0000981 self.assertEqual(len(l), size)
982 self.assertEqual(l[-3:], [None, 5, None])
983 self.assertEqual(l.count(5), 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000984 self.assertRaises(IndexError, operator.setitem, l, size, 6)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000985 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000986
987 l[size - 7:] = [1, 2, 3, 4, 5]
988 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000989 self.assertEqual(len(l), size)
990 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991
992 l[:7] = [1, 2, 3, 4, 5]
993 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000994 self.assertEqual(len(l), size)
995 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996
997 del l[size - 1]
998 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000999 self.assertEqual(len(l), size)
1000 self.assertEqual(l[-1], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001
1002 del l[-2:]
1003 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001004 self.assertEqual(len(l), size)
1005 self.assertEqual(l[-1], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006
1007 del l[0]
1008 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001009 self.assertEqual(len(l), size)
1010 self.assertEqual(l[0], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001011
1012 del l[:2]
1013 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001014 self.assertEqual(len(l), size)
1015 self.assertEqual(l[0], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001016
1017 # Like test_concat, split in two.
1018 def basic_test_repeat(self, size):
1019 l = [] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001020 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021 l = [''] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001022 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001023 l = l * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001024 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025
1026 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
1027 def test_repeat_small(self, size):
1028 return self.basic_test_repeat(size)
1029
1030 @bigmemtest(minsize=_2G + 2, memuse=24)
1031 def test_repeat_large(self, size):
1032 return self.basic_test_repeat(size)
1033
1034 def basic_test_inplace_repeat(self, size):
1035 l = ['']
1036 l *= size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001037 self.assertEqual(len(l), size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001038 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039 del l
1040
1041 l = [''] * size
1042 l *= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001043 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001044 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045
1046 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
1047 def test_inplace_repeat_small(self, size):
1048 return self.basic_test_inplace_repeat(size)
1049
1050 @bigmemtest(minsize=_2G + 2, memuse=16)
1051 def test_inplace_repeat_large(self, size):
1052 return self.basic_test_inplace_repeat(size)
1053
1054 def basic_test_repr(self, size):
1055 l = [0] * size
1056 s = repr(l)
1057 # The repr of a list of 0's is exactly three times the list length.
Ezio Melottib3aedd42010-11-20 19:04:17 +00001058 self.assertEqual(len(s), size * 3)
1059 self.assertEqual(s[:5], '[0, 0')
1060 self.assertEqual(s[-5:], '0, 0]')
1061 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001062
Antoine Pitrouea510eb2010-11-08 21:40:13 +00001063 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3 * character_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064 def test_repr_small(self, size):
1065 return self.basic_test_repr(size)
1066
Antoine Pitrouea510eb2010-11-08 21:40:13 +00001067 @bigmemtest(minsize=_2G + 2, memuse=8 + 3 * character_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068 def test_repr_large(self, size):
1069 return self.basic_test_repr(size)
1070
1071 # list overallocates ~1/8th of the total size (on first expansion) so
1072 # the single list.append call puts memuse at 9 bytes per size.
1073 @bigmemtest(minsize=_2G, memuse=9)
1074 def test_append(self, size):
1075 l = [object()] * size
1076 l.append(object())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001077 self.assertEqual(len(l), size+1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001078 self.assertTrue(l[-3] is l[-2])
1079 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001080
1081 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1082 def test_count(self, size):
1083 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001084 self.assertEqual(l.count(1), size)
1085 self.assertEqual(l.count("1"), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086
1087 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001088 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089 l.extend(l)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001090 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001091 self.assertTrue(l[0] is l[-1])
1092 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001093
1094 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
1095 def test_extend_small(self, size):
1096 return self.basic_test_extend(size)
1097
1098 @bigmemtest(minsize=_2G + 2, memuse=16)
1099 def test_extend_large(self, size):
1100 return self.basic_test_extend(size)
1101
1102 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1103 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001104 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001105 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001106 self.assertEqual(l.index(1), 0)
1107 self.assertEqual(l.index(5, size - 5), size - 1)
1108 self.assertEqual(l.index(5, size - 5, size), size - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001109 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001110 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001111
1112 # This tests suffers from overallocation, just like test_append.
1113 @bigmemtest(minsize=_2G + 10, memuse=9)
1114 def test_insert(self, size):
1115 l = [1.0] * size
1116 l.insert(size - 1, "A")
1117 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001118 self.assertEqual(len(l), size)
1119 self.assertEqual(l[-3:], [1.0, "A", 1.0])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120
1121 l.insert(size + 1, "B")
1122 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001123 self.assertEqual(len(l), size)
1124 self.assertEqual(l[-3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001125
1126 l.insert(1, "C")
1127 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001128 self.assertEqual(len(l), size)
1129 self.assertEqual(l[:3], [1.0, "C", 1.0])
1130 self.assertEqual(l[size - 3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001131
1132 @bigmemtest(minsize=_2G // 5 + 4, memuse=8 * 5)
1133 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001134 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001135 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001136 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001137
1138 item = l.pop()
1139 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001140 self.assertEqual(len(l), size)
1141 self.assertEqual(item, "e")
1142 self.assertEqual(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001143
1144 item = l.pop(0)
1145 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001146 self.assertEqual(len(l), size)
1147 self.assertEqual(item, "a")
1148 self.assertEqual(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149
1150 item = l.pop(size - 2)
1151 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001152 self.assertEqual(len(l), size)
1153 self.assertEqual(item, "c")
1154 self.assertEqual(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155
1156 @bigmemtest(minsize=_2G + 10, memuse=8)
1157 def test_remove(self, size):
1158 l = [10] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001159 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001160
1161 l.remove(10)
1162 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001163 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164
1165 # Because of the earlier l.remove(), this append doesn't trigger
1166 # a resize.
1167 l.append(5)
1168 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001169 self.assertEqual(len(l), size)
1170 self.assertEqual(l[-2:], [10, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001171 l.remove(5)
1172 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001173 self.assertEqual(len(l), size)
1174 self.assertEqual(l[-2:], [10, 10])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175
1176 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1177 def test_reverse(self, size):
1178 l = [1, 2, 3, 4, 5] * size
1179 l.reverse()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001180 self.assertEqual(len(l), size * 5)
1181 self.assertEqual(l[-5:], [5, 4, 3, 2, 1])
1182 self.assertEqual(l[:5], [5, 4, 3, 2, 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001183
1184 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1185 def test_sort(self, size):
1186 l = [1, 2, 3, 4, 5] * size
1187 l.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001188 self.assertEqual(len(l), size * 5)
1189 self.assertEqual(l.count(1), size)
1190 self.assertEqual(l[:10], [1] * 10)
1191 self.assertEqual(l[-10:], [5] * 10)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001192
1193def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001194 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1195 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001196
1197if __name__ == '__main__':
1198 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001199 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001200 test_main()