blob: 2f50344f831270232f440a90bae49f0e15bc20eb [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
Antoine Pitrou94190bb2011-10-04 10:22:36 +020012from test.support import bigmemtest, _1G, _2G, _4G
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#
Antoine Pitrou94190bb2011-10-04 10:22:36 +020053# - Despite the bigmemtest decorator, all tests will actually be called
54# with a much smaller number too, in the normal test run (5Kb currently.)
55# This is so the tests themselves get frequent testing.
56# Consequently, always make all large allocations based on the
Thomas Wouters477c8d52006-05-27 19:21:47 +000057# passed-in 'size', and don't rely on the size being very large. Also,
58# memuse-per-size should remain sane (less than a few thousand); if your
59# test uses more, adjust 'size' upward, instead.
60
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 Pitrou87a484c2011-10-04 15:55:44 +020065ascii_char_size = 1
66ucs2_char_size = 2
Antoine Pitrou382e8b52011-10-04 16:07:27 +020067ucs4_char_size = 4
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000068
69
70class BaseStrTest:
71
Antoine Pitrou94190bb2011-10-04 10:22:36 +020072 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +000073 def test_capitalize(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000074 _ = self.from_latin1
75 SUBSTR = self.from_latin1(' abc def ghi')
76 s = _('-') * size + SUBSTR
Thomas Wouters477c8d52006-05-27 19:21:47 +000077 caps = s.capitalize()
Ezio Melottib3aedd42010-11-20 19:04:17 +000078 self.assertEqual(caps[-len(SUBSTR):],
Thomas Wouters477c8d52006-05-27 19:21:47 +000079 SUBSTR.capitalize())
Ezio Melottib3aedd42010-11-20 19:04:17 +000080 self.assertEqual(caps.lstrip(_('-')), SUBSTR)
Thomas Wouters477c8d52006-05-27 19:21:47 +000081
Antoine Pitrou94190bb2011-10-04 10:22:36 +020082 @bigmemtest(size=_2G + 10, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +000083 def test_center(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000084 SUBSTR = self.from_latin1(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +000085 s = SUBSTR.center(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +000086 self.assertEqual(len(s), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +000087 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
88 if len(s) % 2:
89 lpadsize += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +000090 self.assertEqual(s[lpadsize:-rpadsize], SUBSTR)
91 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +000092
Antoine Pitrou94190bb2011-10-04 10:22:36 +020093 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 def test_count(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000095 _ = self.from_latin1
96 SUBSTR = _(' abc def ghi')
97 s = _('.') * size + SUBSTR
Ezio Melottib3aedd42010-11-20 19:04:17 +000098 self.assertEqual(s.count(_('.')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000099 s += _('.')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000100 self.assertEqual(s.count(_('.')), size + 1)
101 self.assertEqual(s.count(_(' ')), 3)
102 self.assertEqual(s.count(_('i')), 1)
103 self.assertEqual(s.count(_('j')), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200105 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106 def test_endswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000107 _ = self.from_latin1
108 SUBSTR = _(' abc def ghi')
109 s = _('-') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000110 self.assertTrue(s.endswith(SUBSTR))
111 self.assertTrue(s.endswith(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000112 s2 = _('...') + s
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000113 self.assertTrue(s2.endswith(s))
114 self.assertFalse(s.endswith(_('a') + SUBSTR))
115 self.assertFalse(SUBSTR.endswith(s))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200117 @bigmemtest(size=_2G + 10, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118 def test_expandtabs(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000119 _ = self.from_latin1
120 s = _('-') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121 tabsize = 8
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100122 self.assertTrue(s.expandtabs() == s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 del s
124 slen, remainder = divmod(size, tabsize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000125 s = _(' \t') * slen
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126 s = s.expandtabs(tabsize)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000127 self.assertEqual(len(s), size - remainder)
128 self.assertEqual(len(s.strip(_(' '))), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200130 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000131 def test_find(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000132 _ = self.from_latin1
133 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000135 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000136 self.assertEqual(s.find(_(' ')), 0)
137 self.assertEqual(s.find(SUBSTR), 0)
138 self.assertEqual(s.find(_(' '), sublen), sublen + size)
139 self.assertEqual(s.find(SUBSTR, len(SUBSTR)), sublen + size)
140 self.assertEqual(s.find(_('i')), SUBSTR.find(_('i')))
141 self.assertEqual(s.find(_('i'), sublen),
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(_('i'), size),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000144 sublen + size + SUBSTR.find(_('i')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000145 self.assertEqual(s.find(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000146
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200147 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000148 def test_index(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000149 _ = self.from_latin1
150 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000152 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000153 self.assertEqual(s.index(_(' ')), 0)
154 self.assertEqual(s.index(SUBSTR), 0)
155 self.assertEqual(s.index(_(' '), sublen), sublen + size)
156 self.assertEqual(s.index(SUBSTR, sublen), sublen + size)
157 self.assertEqual(s.index(_('i')), SUBSTR.index(_('i')))
158 self.assertEqual(s.index(_('i'), sublen),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000159 sublen + size + SUBSTR.index(_('i')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000160 self.assertEqual(s.index(_('i'), size),
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000161 sublen + size + SUBSTR.index(_('i')))
162 self.assertRaises(ValueError, s.index, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200164 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165 def test_isalnum(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000166 _ = self.from_latin1
167 SUBSTR = _('123456')
168 s = _('a') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertTrue(s.isalnum())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000170 s += _('.')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000171 self.assertFalse(s.isalnum())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000172
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200173 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174 def test_isalpha(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000175 _ = self.from_latin1
176 SUBSTR = _('zzzzzzz')
177 s = _('a') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000178 self.assertTrue(s.isalpha())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000179 s += _('.')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000180 self.assertFalse(s.isalpha())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200182 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000183 def test_isdigit(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000184 _ = self.from_latin1
185 SUBSTR = _('123456')
186 s = _('9') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000187 self.assertTrue(s.isdigit())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000188 s += _('z')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000189 self.assertFalse(s.isdigit())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000190
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200191 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000192 def test_islower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000193 _ = self.from_latin1
194 chars = _(''.join(
195 chr(c) for c in range(255) if not chr(c).isupper()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000196 repeats = size // len(chars) + 2
197 s = chars * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000198 self.assertTrue(s.islower())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000199 s += _('A')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000200 self.assertFalse(s.islower())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200202 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 def test_isspace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000204 _ = self.from_latin1
205 whitespace = _(' \f\n\r\t\v')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206 repeats = size // len(whitespace) + 2
207 s = whitespace * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000208 self.assertTrue(s.isspace())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000209 s += _('j')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000210 self.assertFalse(s.isspace())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000211
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200212 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000213 def test_istitle(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000214 _ = self.from_latin1
215 SUBSTR = _('123456')
216 s = _('').join([_('A'), _('a') * size, SUBSTR])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000217 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000218 s += _('A')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000219 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000220 s += _('aA')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000221 self.assertFalse(s.istitle())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000222
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200223 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000224 def test_isupper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000225 _ = self.from_latin1
226 chars = _(''.join(
227 chr(c) for c in range(255) if not chr(c).islower()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000228 repeats = size // len(chars) + 2
229 s = chars * repeats
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000230 self.assertTrue(s.isupper())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000231 s += _('a')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000232 self.assertFalse(s.isupper())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000233
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200234 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000235 def test_join(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000236 _ = self.from_latin1
237 s = _('A') * size
238 x = s.join([_('aaaaa'), _('bbbbb')])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000239 self.assertEqual(x.count(_('a')), 5)
240 self.assertEqual(x.count(_('b')), 5)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000241 self.assertTrue(x.startswith(_('aaaaaA')))
242 self.assertTrue(x.endswith(_('Abbbbb')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200244 @bigmemtest(size=_2G + 10, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000245 def test_ljust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000246 _ = self.from_latin1
247 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000248 s = SUBSTR.ljust(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000249 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000250 self.assertEqual(len(s), size)
251 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000252
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200253 @bigmemtest(size=_2G + 10, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000254 def test_lower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000255 _ = self.from_latin1
256 s = _('A') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000257 s = s.lower()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000258 self.assertEqual(len(s), size)
259 self.assertEqual(s.count(_('a')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000260
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200261 @bigmemtest(size=_2G + 10, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000262 def test_lstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000263 _ = self.from_latin1
264 SUBSTR = _('abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000265 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000266 self.assertEqual(len(s), size)
267 self.assertEqual(s.lstrip(), SUBSTR.lstrip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000268 del s
269 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000270 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000271 # Type-specific optimization
272 if isinstance(s, (str, bytes)):
273 stripped = s.lstrip()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000274 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200276 @bigmemtest(size=_2G + 10, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277 def test_replace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000278 _ = self.from_latin1
279 replacement = _('a')
280 s = _(' ') * size
281 s = s.replace(_(' '), replacement)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000282 self.assertEqual(len(s), size)
283 self.assertEqual(s.count(replacement), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000284 s = s.replace(replacement, _(' '), size - 4)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000285 self.assertEqual(len(s), size)
286 self.assertEqual(s.count(replacement), 4)
287 self.assertEqual(s[-10:], _(' aaaa'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000288
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200289 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000290 def test_rfind(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000291 _ = self.from_latin1
292 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000293 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000294 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000295 self.assertEqual(s.rfind(_(' ')), sublen + size + SUBSTR.rfind(_(' ')))
296 self.assertEqual(s.rfind(SUBSTR), sublen + size)
297 self.assertEqual(s.rfind(_(' '), 0, size), SUBSTR.rfind(_(' ')))
298 self.assertEqual(s.rfind(SUBSTR, 0, sublen + size), 0)
299 self.assertEqual(s.rfind(_('i')), sublen + size + SUBSTR.rfind(_('i')))
300 self.assertEqual(s.rfind(_('i'), 0, sublen), SUBSTR.rfind(_('i')))
301 self.assertEqual(s.rfind(_('i'), 0, sublen + size),
302 SUBSTR.rfind(_('i')))
303 self.assertEqual(s.rfind(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000304
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200305 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000306 def test_rindex(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000307 _ = self.from_latin1
308 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000310 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000311 self.assertEqual(s.rindex(_(' ')),
312 sublen + size + SUBSTR.rindex(_(' ')))
313 self.assertEqual(s.rindex(SUBSTR), sublen + size)
314 self.assertEqual(s.rindex(_(' '), 0, sublen + size - 1),
315 SUBSTR.rindex(_(' ')))
316 self.assertEqual(s.rindex(SUBSTR, 0, sublen + size), 0)
317 self.assertEqual(s.rindex(_('i')),
318 sublen + size + SUBSTR.rindex(_('i')))
319 self.assertEqual(s.rindex(_('i'), 0, sublen), SUBSTR.rindex(_('i')))
320 self.assertEqual(s.rindex(_('i'), 0, sublen + size),
321 SUBSTR.rindex(_('i')))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000322 self.assertRaises(ValueError, s.rindex, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000323
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200324 @bigmemtest(size=_2G + 10, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325 def test_rjust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000326 _ = self.from_latin1
327 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328 s = SUBSTR.ljust(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000329 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000330 self.assertEqual(len(s), size)
331 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200333 @bigmemtest(size=_2G + 10, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000334 def test_rstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000335 _ = self.from_latin1
336 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000338 self.assertEqual(len(s), size)
339 self.assertEqual(s.rstrip(), SUBSTR.rstrip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340 del s
341 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000342 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000343 # Type-specific optimization
344 if isinstance(s, (str, bytes)):
345 stripped = s.rstrip()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000346 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000347
348 # The test takes about size bytes to build a string, and then about
349 # sqrt(size) substrings of sqrt(size) in size and a list to
350 # hold sqrt(size) items. It's close but just over 2x size.
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200351 @bigmemtest(size=_2G, memuse=2.1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352 def test_split_small(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000353 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000354 # Crudely calculate an estimate so that the result of s.split won't
355 # take up an inordinate amount of memory
356 chunksize = int(size ** 0.5 + 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000357 SUBSTR = _('a') + _(' ') * chunksize
Thomas Wouters477c8d52006-05-27 19:21:47 +0000358 s = SUBSTR * chunksize
359 l = s.split()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000360 self.assertEqual(len(l), chunksize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000361 expected = _('a')
362 for item in l:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000363 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000364 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000365 l = s.split(_('a'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000366 self.assertEqual(len(l), chunksize + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000367 expected = _(' ') * chunksize
368 for item in filter(None, l):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000369 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370
371 # Allocates a string of twice size (and briefly two) and a list of
372 # size. Because of internal affairs, the s.split() call produces a
373 # list of size times the same one-character string, so we only
374 # suffer for the list size. (Otherwise, it'd cost another 48 times
375 # size in bytes!) Nevertheless, a list of size takes
376 # 8*size bytes.
Antoine Pitrouf81ad6f2011-10-06 22:09:18 +0200377 @bigmemtest(size=_2G + 5, memuse=2 * ascii_char_size + 8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000378 def test_split_large(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000379 _ = self.from_latin1
380 s = _(' a') * size + _(' ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381 l = s.split()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000382 self.assertEqual(len(l), size)
383 self.assertEqual(set(l), set([_('a')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000385 l = s.split(_('a'))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000386 self.assertEqual(len(l), size + 1)
387 self.assertEqual(set(l), set([_(' ')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200389 @bigmemtest(size=_2G, memuse=2.1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000390 def test_splitlines(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000391 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 # Crudely calculate an estimate so that the result of s.split won't
393 # take up an inordinate amount of memory
394 chunksize = int(size ** 0.5 + 2) // 2
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000395 SUBSTR = _(' ') * chunksize + _('\n') + _(' ') * chunksize + _('\r\n')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000396 s = SUBSTR * chunksize
397 l = s.splitlines()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000398 self.assertEqual(len(l), chunksize * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000399 expected = _(' ') * chunksize
400 for item in l:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000401 self.assertEqual(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200403 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000404 def test_startswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000405 _ = self.from_latin1
406 SUBSTR = _(' abc def ghi')
407 s = _('-') * size + SUBSTR
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000408 self.assertTrue(s.startswith(s))
409 self.assertTrue(s.startswith(_('-') * size))
410 self.assertFalse(s.startswith(SUBSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000411
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200412 @bigmemtest(size=_2G, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000413 def test_strip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000414 _ = self.from_latin1
415 SUBSTR = _(' abc def ghi ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000416 s = SUBSTR.rjust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000417 self.assertEqual(len(s), size)
418 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000419 del s
420 s = SUBSTR.ljust(size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000421 self.assertEqual(len(s), size)
422 self.assertEqual(s.strip(), SUBSTR.strip())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000423
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200424 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000425 def test_swapcase(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000426 _ = self.from_latin1
427 SUBSTR = _("aBcDeFG12.'\xa9\x00")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 sublen = len(SUBSTR)
429 repeats = size // sublen + 2
430 s = SUBSTR * repeats
431 s = s.swapcase()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000432 self.assertEqual(len(s), sublen * repeats)
433 self.assertEqual(s[:sublen * 3], SUBSTR.swapcase() * 3)
434 self.assertEqual(s[-sublen * 3:], SUBSTR.swapcase() * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000435
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200436 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437 def test_title(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000438 _ = self.from_latin1
439 SUBSTR = _('SpaaHAaaAaham')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000440 s = SUBSTR * (size // len(SUBSTR) + 2)
441 s = s.title()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000442 self.assertTrue(s.startswith((SUBSTR * 3).title()))
443 self.assertTrue(s.endswith(SUBSTR.lower() * 3))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000444
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200445 @bigmemtest(size=_2G, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000446 def test_translate(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000447 _ = self.from_latin1
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000448 SUBSTR = _('aZz.z.Aaz.')
Georg Brandlabc38772009-04-12 15:51:51 +0000449 if isinstance(SUBSTR, str):
450 trans = {
451 ord(_('.')): _('-'),
452 ord(_('a')): _('!'),
453 ord(_('Z')): _('$'),
454 }
455 else:
456 trans = bytes.maketrans(b'.aZ', b'-!$')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000457 sublen = len(SUBSTR)
458 repeats = size // sublen + 2
459 s = SUBSTR * repeats
460 s = s.translate(trans)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000461 self.assertEqual(len(s), repeats * sublen)
462 self.assertEqual(s[:sublen], SUBSTR.translate(trans))
463 self.assertEqual(s[-sublen:], SUBSTR.translate(trans))
464 self.assertEqual(s.count(_('.')), 0)
465 self.assertEqual(s.count(_('!')), repeats * 2)
466 self.assertEqual(s.count(_('z')), repeats * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200468 @bigmemtest(size=_2G + 5, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469 def test_upper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000470 _ = self.from_latin1
471 s = _('a') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000472 s = s.upper()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000473 self.assertEqual(len(s), size)
474 self.assertEqual(s.count(_('A')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000475
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200476 @bigmemtest(size=_2G + 20, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000477 def test_zfill(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000478 _ = self.from_latin1
479 SUBSTR = _('-568324723598234')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480 s = SUBSTR.zfill(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000481 self.assertTrue(s.endswith(_('0') + SUBSTR[1:]))
482 self.assertTrue(s.startswith(_('-0')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000483 self.assertEqual(len(s), size)
484 self.assertEqual(s.count(_('0')), size - len(SUBSTR))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000485
486 # This test is meaningful even with size < 2G, as long as the
487 # doubled string is > 2G (but it tests more if both are > 2G :)
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200488 @bigmemtest(size=_1G + 2, memuse=3)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000489 def test_concat(self, size):
490 _ = self.from_latin1
491 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000492 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000493 s = s + s
Ezio Melottib3aedd42010-11-20 19:04:17 +0000494 self.assertEqual(len(s), size * 2)
495 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000496
497 # This test is meaningful even with size < 2G, as long as the
498 # repeated string is > 2G (but it tests more if both are > 2G :)
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200499 @bigmemtest(size=_1G + 2, memuse=3)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000500 def test_repeat(self, size):
501 _ = self.from_latin1
502 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000503 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000504 s = s * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000505 self.assertEqual(len(s), size * 2)
506 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000507
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200508 @bigmemtest(size=_2G + 20, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000509 def test_slice_and_getitem(self, size):
510 _ = self.from_latin1
511 SUBSTR = _('0123456789')
512 sublen = len(SUBSTR)
513 s = SUBSTR * (size // sublen)
514 stepsize = len(s) // 100
515 stepsize = stepsize - (stepsize % sublen)
516 for i in range(0, len(s) - stepsize, stepsize):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000517 self.assertEqual(s[i], SUBSTR[0])
518 self.assertEqual(s[i:i + sublen], SUBSTR)
519 self.assertEqual(s[i:i + sublen:2], SUBSTR[::2])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000520 if i > 0:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000521 self.assertEqual(s[i + sublen - 1:i - 1:-3],
522 SUBSTR[sublen::-3])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000523 # Make sure we do some slicing and indexing near the end of the
524 # string, too.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000525 self.assertEqual(s[len(s) - 1], SUBSTR[-1])
526 self.assertEqual(s[-1], SUBSTR[-1])
527 self.assertEqual(s[len(s) - 10], SUBSTR[0])
528 self.assertEqual(s[-sublen], SUBSTR[0])
529 self.assertEqual(s[len(s):], _(''))
530 self.assertEqual(s[len(s) - 1:], SUBSTR[-1:])
531 self.assertEqual(s[-1:], SUBSTR[-1:])
532 self.assertEqual(s[len(s) - sublen:], SUBSTR)
533 self.assertEqual(s[-sublen:], SUBSTR)
534 self.assertEqual(len(s[:]), len(s))
535 self.assertEqual(len(s[:len(s) - 5]), len(s) - 5)
536 self.assertEqual(len(s[5:-5]), len(s) - 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000537
538 self.assertRaises(IndexError, operator.getitem, s, len(s))
539 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1)
540 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31)
541
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200542 @bigmemtest(size=_2G, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000543 def test_contains(self, size):
544 _ = self.from_latin1
545 SUBSTR = _('0123456789')
546 edge = _('-') * (size // 2)
547 s = _('').join([edge, SUBSTR, edge])
548 del edge
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100549 self.assertTrue(SUBSTR in s)
550 self.assertFalse(SUBSTR * 2 in s)
551 self.assertTrue(_('-') in s)
552 self.assertFalse(_('a') in s)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000553 s += _('a')
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100554 self.assertTrue(_('a') in s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000555
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200556 @bigmemtest(size=_2G + 10, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000557 def test_compare(self, size):
558 _ = self.from_latin1
559 s1 = _('-') * size
560 s2 = _('-') * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100561 self.assertTrue(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000562 del s2
563 s2 = s1 + _('a')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000564 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000565 del s2
566 s2 = _('.') * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000567 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000568
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200569 @bigmemtest(size=_2G + 10, memuse=1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000570 def test_hash(self, size):
571 # Not sure if we can do any meaningful tests here... Even if we
572 # start relying on the exact algorithm used, the result will be
573 # different depending on the size of the C 'long int'. Even this
574 # test is dodgy (there's no *guarantee* that the two things should
575 # have a different hash, even if they, in the current
576 # implementation, almost always do.)
577 _ = self.from_latin1
578 s = _('\x00') * size
579 h1 = hash(s)
580 del s
581 s = _('\x00') * (size + 1)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100582 self.assertNotEqual(h1, hash(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000583
584
585class StrTest(unittest.TestCase, BaseStrTest):
586
587 def from_latin1(self, s):
588 return s
589
590 def basic_encode_test(self, size, enc, c='.', expectedsize=None):
591 if expectedsize is None:
592 expectedsize = size
Antoine Pitrou45545f72011-01-12 20:46:37 +0000593 try:
594 s = c * size
595 self.assertEqual(len(s.encode(enc)), expectedsize)
596 finally:
597 s = None
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000598
599 def setUp(self):
600 # HACK: adjust memory use of tests inherited from BaseStrTest
601 # according to character size.
602 self._adjusted = {}
603 for name in dir(BaseStrTest):
604 if not name.startswith('test_'):
605 continue
606 meth = getattr(type(self), name)
607 try:
608 memuse = meth.memuse
609 except AttributeError:
610 continue
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200611 meth.memuse = ascii_char_size * memuse
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000612 self._adjusted[name] = memuse
613
614 def tearDown(self):
615 for name, memuse in self._adjusted.items():
616 getattr(type(self), name).memuse = memuse
617
Antoine Pitroude21f842011-10-06 21:46:23 +0200618 # Many codecs convert to the legacy representation first, explaining
619 # why we add 'ucs4_char_size' to the 'memuse' below.
620
621 @bigmemtest(size=_2G + 2, memuse=ascii_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000622 def test_encode(self, size):
623 return self.basic_encode_test(size, 'utf-8')
624
Antoine Pitroude21f842011-10-06 21:46:23 +0200625 @bigmemtest(size=_4G // 6 + 2, memuse=ascii_char_size + ucs4_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000626 def test_encode_raw_unicode_escape(self, size):
627 try:
628 return self.basic_encode_test(size, 'raw_unicode_escape')
629 except MemoryError:
630 pass # acceptable on 32-bit
631
Antoine Pitroude21f842011-10-06 21:46:23 +0200632 @bigmemtest(size=_4G // 5 + 70, memuse=ascii_char_size + ucs4_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000633 def test_encode_utf7(self, size):
634 try:
635 return self.basic_encode_test(size, 'utf7')
636 except MemoryError:
637 pass # acceptable on 32-bit
638
Antoine Pitroude21f842011-10-06 21:46:23 +0200639 @bigmemtest(size=_4G // 4 + 5, memuse=ascii_char_size + ucs4_char_size + 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000640 def test_encode_utf32(self, size):
641 try:
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200642 return self.basic_encode_test(size, 'utf32', expectedsize=4 * size + 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000643 except MemoryError:
644 pass # acceptable on 32-bit
645
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200646 @bigmemtest(size=_2G - 1, memuse=ascii_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000647 def test_encode_ascii(self, size):
648 return self.basic_encode_test(size, 'ascii', c='A')
649
Antoine Pitrou8ac582f2011-10-06 21:55:51 +0200650 # str % (...) uses a Py_UCS4 intermediate representation
651
652 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2 + ucs4_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 def test_format(self, size):
654 s = '-' * size
655 sf = '%s' % (s,)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100656 self.assertTrue(s == sf)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 del sf
658 sf = '..%s..' % (s,)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000659 self.assertEqual(len(sf), len(s) + 4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000660 self.assertTrue(sf.startswith('..-'))
661 self.assertTrue(sf.endswith('-..'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000662 del s, sf
663
664 size //= 2
665 edge = '-' * size
666 s = ''.join([edge, '%s', edge])
667 del edge
668 s = s % '...'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000669 self.assertEqual(len(s), size * 2 + 3)
670 self.assertEqual(s.count('.'), 3)
671 self.assertEqual(s.count('-'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200673 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 def test_repr_small(self, size):
675 s = '-' * size
676 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000677 self.assertEqual(len(s), size + 2)
678 self.assertEqual(s[0], "'")
679 self.assertEqual(s[-1], "'")
680 self.assertEqual(s.count('-'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000681 del s
682 # repr() will create a string four times as large as this 'binary
683 # string', but we don't want to allocate much more than twice
684 # size in total. (We do extra testing in test_repr_large())
685 size = size // 5 * 2
686 s = '\x00' * size
687 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000688 self.assertEqual(len(s), size * 4 + 2)
689 self.assertEqual(s[0], "'")
690 self.assertEqual(s[-1], "'")
691 self.assertEqual(s.count('\\'), size)
692 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200694 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 def test_repr_large(self, size):
696 s = '\x00' * size
697 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000698 self.assertEqual(len(s), size * 4 + 2)
699 self.assertEqual(s[0], "'")
700 self.assertEqual(s[-1], "'")
701 self.assertEqual(s.count('\\'), size)
702 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200704 @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size + ascii_char_size * 6)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000705 def test_unicode_repr(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000706 # Use an assigned, but not printable code point.
707 # It is in the range of the low surrogates \uDC00-\uDFFF.
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200708 char = "\uDCBA"
709 s = char * size
710 try:
711 for f in (repr, ascii):
712 r = f(s)
713 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
714 self.assertTrue(r.endswith(r"\udcba'"), r[-10:])
715 r = None
716 finally:
717 r = s = None
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000718
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200719 @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size + ascii_char_size * 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000720 def test_unicode_repr_wide(self, size):
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200721 char = "\U0001DCBA"
722 s = char * size
723 try:
724 for f in (repr, ascii):
725 r = f(s)
726 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
727 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:])
728 r = None
729 finally:
730 r = s = None
731
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000733class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000734
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000735 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000736 return s.encode("latin-1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000737
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200738 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000739 def test_decode(self, size):
740 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000741 self.assertEqual(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000742
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000744class BytearrayTest(unittest.TestCase, BaseStrTest):
745
746 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000747 return bytearray(s.encode("latin-1"))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000748
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200749 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000750 def test_decode(self, size):
751 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000752 self.assertEqual(len(s.decode('utf-8')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000753
754 test_hash = None
755 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756
757class TupleTest(unittest.TestCase):
758
759 # Tuples have a small, fixed-sized head and an array of pointers to
760 # data. Since we're testing 64-bit addressing, we can assume that the
761 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
762 # per size.
763
764 # As a side-effect of testing long tuples, these tests happen to test
765 # having more than 2<<31 references to any given object. Hence the
766 # use of different types of objects as contents in different tests.
767
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200768 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000769 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000770 t1 = ('',) * size
771 t2 = ('',) * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100772 self.assertTrue(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000774 t2 = ('',) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000775 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000776 del t2
777 t2 = (1,) * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000778 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779
780 # Test concatenating into a single tuple of more than 2G in length,
781 # and concatenating a tuple of more than 2G in length separately, so
782 # the smaller test still gets run even if there isn't memory for the
783 # larger test (but we still let the tester know the larger test is
784 # skipped, in verbose mode.)
785 def basic_concat_test(self, size):
786 t = ((),) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000787 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788 t = t + t
Ezio Melottib3aedd42010-11-20 19:04:17 +0000789 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000790
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200791 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 def test_concat_small(self, size):
793 return self.basic_concat_test(size)
794
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200795 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 def test_concat_large(self, size):
797 return self.basic_concat_test(size)
798
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200799 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000800 def test_contains(self, size):
801 t = (1, 2, 3, 4, 5) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000802 self.assertEqual(len(t), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100803 self.assertTrue(5 in t)
804 self.assertFalse((1, 2, 3, 4, 5) in t)
805 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200807 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 def test_hash(self, size):
809 t1 = (0,) * size
810 h1 = hash(t1)
811 del t1
812 t2 = (0,) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000813 self.assertFalse(h1 == hash(t2))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200815 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816 def test_index_and_slice(self, size):
817 t = (None,) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000818 self.assertEqual(len(t), size)
819 self.assertEqual(t[-1], None)
820 self.assertEqual(t[5], None)
821 self.assertEqual(t[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822 self.assertRaises(IndexError, operator.getitem, t, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000823 self.assertEqual(t[:5], (None,) * 5)
824 self.assertEqual(t[-5:], (None,) * 5)
825 self.assertEqual(t[20:25], (None,) * 5)
826 self.assertEqual(t[-25:-20], (None,) * 5)
827 self.assertEqual(t[size - 5:], (None,) * 5)
828 self.assertEqual(t[size - 5:size], (None,) * 5)
829 self.assertEqual(t[size - 6:size - 2], (None,) * 4)
830 self.assertEqual(t[size:size], ())
831 self.assertEqual(t[size:size+5], ())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
833 # Like test_concat, split in two.
834 def basic_test_repeat(self, size):
835 t = ('',) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000836 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837 t = t * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000838 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200840 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 def test_repeat_small(self, size):
842 return self.basic_test_repeat(size)
843
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200844 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845 def test_repeat_large(self, size):
846 return self.basic_test_repeat(size)
847
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200848 @bigmemtest(size=_1G - 1, memuse=12)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000849 def test_repeat_large_2(self, size):
850 return self.basic_test_repeat(size)
851
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200852 @bigmemtest(size=_1G - 1, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000853 def test_from_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000854 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000855 try:
856 t = tuple(range(size))
857 except MemoryError:
858 pass # acceptable on 32-bit
859 else:
860 count = 0
861 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000862 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000863 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000864 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000865
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200866 @bigmemtest(size=_1G - 25, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000867 def test_from_almost_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000868 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000869 try:
870 t = tuple(range(size))
871 count = 0
872 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000873 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000874 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000875 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000876 except MemoryError:
877 pass # acceptable, expected on 32-bit
878
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879 # Like test_concat, split in two.
880 def basic_test_repr(self, size):
881 t = (0,) * size
882 s = repr(t)
883 # The repr of a tuple of 0's is exactly three times the tuple length.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000884 self.assertEqual(len(s), size * 3)
885 self.assertEqual(s[:5], '(0, 0')
886 self.assertEqual(s[-5:], '0, 0)')
887 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000888
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200889 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 def test_repr_small(self, size):
891 return self.basic_test_repr(size)
892
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200893 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894 def test_repr_large(self, size):
895 return self.basic_test_repr(size)
896
897class ListTest(unittest.TestCase):
898
899 # Like tuples, lists have a small, fixed-sized head and an array of
900 # pointers to data, so 8 bytes per size. Also like tuples, we make the
901 # lists hold references to various objects to test their refcount
902 # limits.
903
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200904 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000906 l1 = [''] * size
907 l2 = [''] * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100908 self.assertTrue(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000910 l2 = [''] * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000911 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912 del l2
913 l2 = [2] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000914 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915
916 # Test concatenating into a single list of more than 2G in length,
917 # and concatenating a list of more than 2G in length separately, so
918 # the smaller test still gets run even if there isn't memory for the
919 # larger test (but we still let the tester know the larger test is
920 # skipped, in verbose mode.)
921 def basic_test_concat(self, size):
922 l = [[]] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000923 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924 l = l + l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000925 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200927 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928 def test_concat_small(self, size):
929 return self.basic_test_concat(size)
930
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200931 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932 def test_concat_large(self, size):
933 return self.basic_test_concat(size)
934
935 def basic_test_inplace_concat(self, size):
936 l = [sys.stdout] * size
937 l += l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000938 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000939 self.assertTrue(l[0] is l[-1])
940 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200942 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 def test_inplace_concat_small(self, size):
944 return self.basic_test_inplace_concat(size)
945
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200946 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947 def test_inplace_concat_large(self, size):
948 return self.basic_test_inplace_concat(size)
949
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200950 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 def test_contains(self, size):
952 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000953 self.assertEqual(len(l), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100954 self.assertTrue(5 in l)
955 self.assertFalse([1, 2, 3, 4, 5] in l)
956 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200958 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959 def test_hash(self, size):
960 l = [0] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000961 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200963 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 def test_index_and_slice(self, size):
965 l = [None] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000966 self.assertEqual(len(l), size)
967 self.assertEqual(l[-1], None)
968 self.assertEqual(l[5], None)
969 self.assertEqual(l[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970 self.assertRaises(IndexError, operator.getitem, l, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000971 self.assertEqual(l[:5], [None] * 5)
972 self.assertEqual(l[-5:], [None] * 5)
973 self.assertEqual(l[20:25], [None] * 5)
974 self.assertEqual(l[-25:-20], [None] * 5)
975 self.assertEqual(l[size - 5:], [None] * 5)
976 self.assertEqual(l[size - 5:size], [None] * 5)
977 self.assertEqual(l[size - 6:size - 2], [None] * 4)
978 self.assertEqual(l[size:size], [])
979 self.assertEqual(l[size:size+5], [])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000980
981 l[size - 2] = 5
Ezio Melottib3aedd42010-11-20 19:04:17 +0000982 self.assertEqual(len(l), size)
983 self.assertEqual(l[-3:], [None, 5, None])
984 self.assertEqual(l.count(5), 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985 self.assertRaises(IndexError, operator.setitem, l, size, 6)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000986 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987
988 l[size - 7:] = [1, 2, 3, 4, 5]
989 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000990 self.assertEqual(len(l), size)
991 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000992
993 l[:7] = [1, 2, 3, 4, 5]
994 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000995 self.assertEqual(len(l), size)
996 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997
998 del l[size - 1]
999 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001000 self.assertEqual(len(l), size)
1001 self.assertEqual(l[-1], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002
1003 del l[-2:]
1004 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001005 self.assertEqual(len(l), size)
1006 self.assertEqual(l[-1], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007
1008 del l[0]
1009 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001010 self.assertEqual(len(l), size)
1011 self.assertEqual(l[0], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012
1013 del l[:2]
1014 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001015 self.assertEqual(len(l), size)
1016 self.assertEqual(l[0], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001017
1018 # Like test_concat, split in two.
1019 def basic_test_repeat(self, size):
1020 l = [] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001021 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022 l = [''] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001023 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 l = l * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001025 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001027 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028 def test_repeat_small(self, size):
1029 return self.basic_test_repeat(size)
1030
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001031 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 def test_repeat_large(self, size):
1033 return self.basic_test_repeat(size)
1034
1035 def basic_test_inplace_repeat(self, size):
1036 l = ['']
1037 l *= size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001038 self.assertEqual(len(l), size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001039 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 del l
1041
1042 l = [''] * size
1043 l *= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001044 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001045 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001047 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048 def test_inplace_repeat_small(self, size):
1049 return self.basic_test_inplace_repeat(size)
1050
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001051 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001052 def test_inplace_repeat_large(self, size):
1053 return self.basic_test_inplace_repeat(size)
1054
1055 def basic_test_repr(self, size):
1056 l = [0] * size
1057 s = repr(l)
1058 # The repr of a list of 0's is exactly three times the list length.
Ezio Melottib3aedd42010-11-20 19:04:17 +00001059 self.assertEqual(len(s), size * 3)
1060 self.assertEqual(s[:5], '[0, 0')
1061 self.assertEqual(s[-5:], '0, 0]')
1062 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001064 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065 def test_repr_small(self, size):
1066 return self.basic_test_repr(size)
1067
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001068 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069 def test_repr_large(self, size):
1070 return self.basic_test_repr(size)
1071
1072 # list overallocates ~1/8th of the total size (on first expansion) so
1073 # the single list.append call puts memuse at 9 bytes per size.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001074 @bigmemtest(size=_2G, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 def test_append(self, size):
1076 l = [object()] * size
1077 l.append(object())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001078 self.assertEqual(len(l), size+1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001079 self.assertTrue(l[-3] is l[-2])
1080 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001082 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 def test_count(self, size):
1084 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001085 self.assertEqual(l.count(1), size)
1086 self.assertEqual(l.count("1"), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087
1088 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001089 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001090 l.extend(l)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001091 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001092 self.assertTrue(l[0] is l[-1])
1093 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001094
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001095 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096 def test_extend_small(self, size):
1097 return self.basic_test_extend(size)
1098
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001099 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100 def test_extend_large(self, size):
1101 return self.basic_test_extend(size)
1102
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001103 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001105 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001107 self.assertEqual(l.index(1), 0)
1108 self.assertEqual(l.index(5, size - 5), size - 1)
1109 self.assertEqual(l.index(5, size - 5, size), size - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001111 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112
1113 # This tests suffers from overallocation, just like test_append.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001114 @bigmemtest(size=_2G + 10, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001115 def test_insert(self, size):
1116 l = [1.0] * size
1117 l.insert(size - 1, "A")
1118 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001119 self.assertEqual(len(l), size)
1120 self.assertEqual(l[-3:], [1.0, "A", 1.0])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121
1122 l.insert(size + 1, "B")
1123 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001124 self.assertEqual(len(l), size)
1125 self.assertEqual(l[-3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001126
1127 l.insert(1, "C")
1128 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001129 self.assertEqual(len(l), size)
1130 self.assertEqual(l[:3], [1.0, "C", 1.0])
1131 self.assertEqual(l[size - 3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001132
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001133 @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001135 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001136 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001137 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138
1139 item = l.pop()
1140 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001141 self.assertEqual(len(l), size)
1142 self.assertEqual(item, "e")
1143 self.assertEqual(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144
1145 item = l.pop(0)
1146 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001147 self.assertEqual(len(l), size)
1148 self.assertEqual(item, "a")
1149 self.assertEqual(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001150
1151 item = l.pop(size - 2)
1152 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001153 self.assertEqual(len(l), size)
1154 self.assertEqual(item, "c")
1155 self.assertEqual(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001156
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001157 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001158 def test_remove(self, size):
1159 l = [10] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001160 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001161
1162 l.remove(10)
1163 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001164 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001165
1166 # Because of the earlier l.remove(), this append doesn't trigger
1167 # a resize.
1168 l.append(5)
1169 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001170 self.assertEqual(len(l), size)
1171 self.assertEqual(l[-2:], [10, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001172 l.remove(5)
1173 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001174 self.assertEqual(len(l), size)
1175 self.assertEqual(l[-2:], [10, 10])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001177 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001178 def test_reverse(self, size):
1179 l = [1, 2, 3, 4, 5] * size
1180 l.reverse()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001181 self.assertEqual(len(l), size * 5)
1182 self.assertEqual(l[-5:], [5, 4, 3, 2, 1])
1183 self.assertEqual(l[:5], [5, 4, 3, 2, 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001184
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001185 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186 def test_sort(self, size):
1187 l = [1, 2, 3, 4, 5] * size
1188 l.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001189 self.assertEqual(len(l), size * 5)
1190 self.assertEqual(l.count(1), size)
1191 self.assertEqual(l[:10], [1] * 10)
1192 self.assertEqual(l[-10:], [5] * 10)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001193
1194def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001195 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1196 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001197
1198if __name__ == '__main__':
1199 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001200 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001201 test_main()