blob: 43536022a5071264c91549184b32cef1098fe723 [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')
Antoine Pitroua5d99172011-10-06 22:19:07 +0200396 s = SUBSTR * (chunksize * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397 l = s.splitlines()
Antoine Pitroua5d99172011-10-06 22:19:07 +0200398 self.assertEqual(len(l), chunksize * 4)
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 Pitroub6080b62011-10-06 22:32:10 +0200704 # ascii() calls encode('ascii', 'backslashreplace'), which itself
705 # creates a temporary Py_UNICODE representation in addition to the
706 # original (Py_UCS2) one
707 # There's also some overallocation when resizing the ascii() result
708 # that isn't taken into account here.
709 @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size +
710 ucs4_char_size + ascii_char_size * 6)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000711 def test_unicode_repr(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000712 # Use an assigned, but not printable code point.
713 # It is in the range of the low surrogates \uDC00-\uDFFF.
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200714 char = "\uDCBA"
715 s = char * size
716 try:
717 for f in (repr, ascii):
718 r = f(s)
719 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
720 self.assertTrue(r.endswith(r"\udcba'"), r[-10:])
721 r = None
722 finally:
723 r = s = None
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000724
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200725 @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size + ascii_char_size * 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000726 def test_unicode_repr_wide(self, size):
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200727 char = "\U0001DCBA"
728 s = char * size
729 try:
730 for f in (repr, ascii):
731 r = f(s)
732 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
733 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:])
734 r = None
735 finally:
736 r = s = None
737
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000739class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000741 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000742 return s.encode("latin-1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000743
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200744 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000745 def test_decode(self, size):
746 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000747 self.assertEqual(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000748
Thomas Wouters477c8d52006-05-27 19:21:47 +0000749
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000750class BytearrayTest(unittest.TestCase, BaseStrTest):
751
752 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000753 return bytearray(s.encode("latin-1"))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000754
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200755 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000756 def test_decode(self, size):
757 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000758 self.assertEqual(len(s.decode('utf-8')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000759
760 test_hash = None
761 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000762
763class TupleTest(unittest.TestCase):
764
765 # Tuples have a small, fixed-sized head and an array of pointers to
766 # data. Since we're testing 64-bit addressing, we can assume that the
767 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
768 # per size.
769
770 # As a side-effect of testing long tuples, these tests happen to test
771 # having more than 2<<31 references to any given object. Hence the
772 # use of different types of objects as contents in different tests.
773
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200774 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000775 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000776 t1 = ('',) * size
777 t2 = ('',) * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100778 self.assertTrue(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000780 t2 = ('',) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000781 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000782 del t2
783 t2 = (1,) * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000784 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785
786 # Test concatenating into a single tuple of more than 2G in length,
787 # and concatenating a tuple of more than 2G in length separately, so
788 # the smaller test still gets run even if there isn't memory for the
789 # larger test (but we still let the tester know the larger test is
790 # skipped, in verbose mode.)
791 def basic_concat_test(self, size):
792 t = ((),) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000793 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 t = t + t
Ezio Melottib3aedd42010-11-20 19:04:17 +0000795 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200797 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798 def test_concat_small(self, size):
799 return self.basic_concat_test(size)
800
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200801 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802 def test_concat_large(self, size):
803 return self.basic_concat_test(size)
804
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200805 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806 def test_contains(self, size):
807 t = (1, 2, 3, 4, 5) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000808 self.assertEqual(len(t), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100809 self.assertTrue(5 in t)
810 self.assertFalse((1, 2, 3, 4, 5) in t)
811 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200813 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000814 def test_hash(self, size):
815 t1 = (0,) * size
816 h1 = hash(t1)
817 del t1
818 t2 = (0,) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000819 self.assertFalse(h1 == hash(t2))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200821 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822 def test_index_and_slice(self, size):
823 t = (None,) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000824 self.assertEqual(len(t), size)
825 self.assertEqual(t[-1], None)
826 self.assertEqual(t[5], None)
827 self.assertEqual(t[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828 self.assertRaises(IndexError, operator.getitem, t, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000829 self.assertEqual(t[:5], (None,) * 5)
830 self.assertEqual(t[-5:], (None,) * 5)
831 self.assertEqual(t[20:25], (None,) * 5)
832 self.assertEqual(t[-25:-20], (None,) * 5)
833 self.assertEqual(t[size - 5:], (None,) * 5)
834 self.assertEqual(t[size - 5:size], (None,) * 5)
835 self.assertEqual(t[size - 6:size - 2], (None,) * 4)
836 self.assertEqual(t[size:size], ())
837 self.assertEqual(t[size:size+5], ())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838
839 # Like test_concat, split in two.
840 def basic_test_repeat(self, size):
841 t = ('',) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000842 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000843 t = t * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000844 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200846 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 def test_repeat_small(self, size):
848 return self.basic_test_repeat(size)
849
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200850 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000851 def test_repeat_large(self, size):
852 return self.basic_test_repeat(size)
853
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200854 @bigmemtest(size=_1G - 1, memuse=12)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000855 def test_repeat_large_2(self, size):
856 return self.basic_test_repeat(size)
857
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200858 @bigmemtest(size=_1G - 1, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000859 def test_from_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000860 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000861 try:
862 t = tuple(range(size))
863 except MemoryError:
864 pass # acceptable on 32-bit
865 else:
866 count = 0
867 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000868 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000869 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000870 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000871
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200872 @bigmemtest(size=_1G - 25, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000873 def test_from_almost_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000874 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000875 try:
876 t = tuple(range(size))
877 count = 0
878 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000879 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000880 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000881 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000882 except MemoryError:
883 pass # acceptable, expected on 32-bit
884
Thomas Wouters477c8d52006-05-27 19:21:47 +0000885 # Like test_concat, split in two.
886 def basic_test_repr(self, size):
887 t = (0,) * size
888 s = repr(t)
889 # The repr of a tuple of 0's is exactly three times the tuple length.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000890 self.assertEqual(len(s), size * 3)
891 self.assertEqual(s[:5], '(0, 0')
892 self.assertEqual(s[-5:], '0, 0)')
893 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200895 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896 def test_repr_small(self, size):
897 return self.basic_test_repr(size)
898
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200899 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900 def test_repr_large(self, size):
901 return self.basic_test_repr(size)
902
903class ListTest(unittest.TestCase):
904
905 # Like tuples, lists have a small, fixed-sized head and an array of
906 # pointers to data, so 8 bytes per size. Also like tuples, we make the
907 # lists hold references to various objects to test their refcount
908 # limits.
909
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200910 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000912 l1 = [''] * size
913 l2 = [''] * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100914 self.assertTrue(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000916 l2 = [''] * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000917 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000918 del l2
919 l2 = [2] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000920 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921
922 # Test concatenating into a single list of more than 2G in length,
923 # and concatenating a list of more than 2G in length separately, so
924 # the smaller test still gets run even if there isn't memory for the
925 # larger test (but we still let the tester know the larger test is
926 # skipped, in verbose mode.)
927 def basic_test_concat(self, size):
928 l = [[]] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000929 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 l = l + l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000931 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200933 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934 def test_concat_small(self, size):
935 return self.basic_test_concat(size)
936
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200937 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938 def test_concat_large(self, size):
939 return self.basic_test_concat(size)
940
941 def basic_test_inplace_concat(self, size):
942 l = [sys.stdout] * size
943 l += l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000944 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000945 self.assertTrue(l[0] is l[-1])
946 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200948 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949 def test_inplace_concat_small(self, size):
950 return self.basic_test_inplace_concat(size)
951
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200952 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 def test_inplace_concat_large(self, size):
954 return self.basic_test_inplace_concat(size)
955
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200956 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 def test_contains(self, size):
958 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000959 self.assertEqual(len(l), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100960 self.assertTrue(5 in l)
961 self.assertFalse([1, 2, 3, 4, 5] in l)
962 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200964 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965 def test_hash(self, size):
966 l = [0] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000967 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200969 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970 def test_index_and_slice(self, size):
971 l = [None] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000972 self.assertEqual(len(l), size)
973 self.assertEqual(l[-1], None)
974 self.assertEqual(l[5], None)
975 self.assertEqual(l[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 self.assertRaises(IndexError, operator.getitem, l, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000977 self.assertEqual(l[:5], [None] * 5)
978 self.assertEqual(l[-5:], [None] * 5)
979 self.assertEqual(l[20:25], [None] * 5)
980 self.assertEqual(l[-25:-20], [None] * 5)
981 self.assertEqual(l[size - 5:], [None] * 5)
982 self.assertEqual(l[size - 5:size], [None] * 5)
983 self.assertEqual(l[size - 6:size - 2], [None] * 4)
984 self.assertEqual(l[size:size], [])
985 self.assertEqual(l[size:size+5], [])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000986
987 l[size - 2] = 5
Ezio Melottib3aedd42010-11-20 19:04:17 +0000988 self.assertEqual(len(l), size)
989 self.assertEqual(l[-3:], [None, 5, None])
990 self.assertEqual(l.count(5), 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991 self.assertRaises(IndexError, operator.setitem, l, size, 6)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000992 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993
994 l[size - 7:] = [1, 2, 3, 4, 5]
995 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000996 self.assertEqual(len(l), size)
997 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998
999 l[:7] = [1, 2, 3, 4, 5]
1000 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001001 self.assertEqual(len(l), size)
1002 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003
1004 del l[size - 1]
1005 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001006 self.assertEqual(len(l), size)
1007 self.assertEqual(l[-1], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008
1009 del l[-2:]
1010 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001011 self.assertEqual(len(l), size)
1012 self.assertEqual(l[-1], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013
1014 del l[0]
1015 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001016 self.assertEqual(len(l), size)
1017 self.assertEqual(l[0], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018
1019 del l[:2]
1020 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001021 self.assertEqual(len(l), size)
1022 self.assertEqual(l[0], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001023
1024 # Like test_concat, split in two.
1025 def basic_test_repeat(self, size):
1026 l = [] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001027 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028 l = [''] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001029 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 l = l * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001031 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001033 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034 def test_repeat_small(self, size):
1035 return self.basic_test_repeat(size)
1036
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001037 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 def test_repeat_large(self, size):
1039 return self.basic_test_repeat(size)
1040
1041 def basic_test_inplace_repeat(self, size):
1042 l = ['']
1043 l *= size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001044 self.assertEqual(len(l), size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001045 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046 del l
1047
1048 l = [''] * size
1049 l *= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001050 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001051 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001052
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001053 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 def test_inplace_repeat_small(self, size):
1055 return self.basic_test_inplace_repeat(size)
1056
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001057 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058 def test_inplace_repeat_large(self, size):
1059 return self.basic_test_inplace_repeat(size)
1060
1061 def basic_test_repr(self, size):
1062 l = [0] * size
1063 s = repr(l)
1064 # The repr of a list of 0's is exactly three times the list length.
Ezio Melottib3aedd42010-11-20 19:04:17 +00001065 self.assertEqual(len(s), size * 3)
1066 self.assertEqual(s[:5], '[0, 0')
1067 self.assertEqual(s[-5:], '0, 0]')
1068 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001070 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 def test_repr_small(self, size):
1072 return self.basic_test_repr(size)
1073
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001074 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 def test_repr_large(self, size):
1076 return self.basic_test_repr(size)
1077
1078 # list overallocates ~1/8th of the total size (on first expansion) so
1079 # the single list.append call puts memuse at 9 bytes per size.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001080 @bigmemtest(size=_2G, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 def test_append(self, size):
1082 l = [object()] * size
1083 l.append(object())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001084 self.assertEqual(len(l), size+1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001085 self.assertTrue(l[-3] is l[-2])
1086 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001088 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089 def test_count(self, size):
1090 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001091 self.assertEqual(l.count(1), size)
1092 self.assertEqual(l.count("1"), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001093
1094 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001095 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096 l.extend(l)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001097 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001098 self.assertTrue(l[0] is l[-1])
1099 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001101 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001102 def test_extend_small(self, size):
1103 return self.basic_test_extend(size)
1104
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001105 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106 def test_extend_large(self, size):
1107 return self.basic_test_extend(size)
1108
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001109 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001111 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001113 self.assertEqual(l.index(1), 0)
1114 self.assertEqual(l.index(5, size - 5), size - 1)
1115 self.assertEqual(l.index(5, size - 5, size), size - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001116 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001117 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001118
1119 # This tests suffers from overallocation, just like test_append.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001120 @bigmemtest(size=_2G + 10, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121 def test_insert(self, size):
1122 l = [1.0] * size
1123 l.insert(size - 1, "A")
1124 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001125 self.assertEqual(len(l), size)
1126 self.assertEqual(l[-3:], [1.0, "A", 1.0])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001127
1128 l.insert(size + 1, "B")
1129 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001130 self.assertEqual(len(l), size)
1131 self.assertEqual(l[-3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001132
1133 l.insert(1, "C")
1134 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001135 self.assertEqual(len(l), size)
1136 self.assertEqual(l[:3], [1.0, "C", 1.0])
1137 self.assertEqual(l[size - 3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001139 @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001141 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001142 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001143 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144
1145 item = l.pop()
1146 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001147 self.assertEqual(len(l), size)
1148 self.assertEqual(item, "e")
1149 self.assertEqual(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001150
1151 item = l.pop(0)
1152 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001153 self.assertEqual(len(l), size)
1154 self.assertEqual(item, "a")
1155 self.assertEqual(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001156
1157 item = l.pop(size - 2)
1158 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001159 self.assertEqual(len(l), size)
1160 self.assertEqual(item, "c")
1161 self.assertEqual(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001162
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001163 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164 def test_remove(self, size):
1165 l = [10] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001166 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167
1168 l.remove(10)
1169 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001170 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001171
1172 # Because of the earlier l.remove(), this append doesn't trigger
1173 # a resize.
1174 l.append(5)
1175 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001176 self.assertEqual(len(l), size)
1177 self.assertEqual(l[-2:], [10, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001178 l.remove(5)
1179 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001180 self.assertEqual(len(l), size)
1181 self.assertEqual(l[-2:], [10, 10])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001182
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001183 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001184 def test_reverse(self, size):
1185 l = [1, 2, 3, 4, 5] * size
1186 l.reverse()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001187 self.assertEqual(len(l), size * 5)
1188 self.assertEqual(l[-5:], [5, 4, 3, 2, 1])
1189 self.assertEqual(l[:5], [5, 4, 3, 2, 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001190
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001191 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001192 def test_sort(self, size):
1193 l = [1, 2, 3, 4, 5] * size
1194 l.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001195 self.assertEqual(len(l), size * 5)
1196 self.assertEqual(l.count(1), size)
1197 self.assertEqual(l[:10], [1] * 10)
1198 self.assertEqual(l[-10:], [5] * 10)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001199
1200def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001201 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1202 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203
1204if __name__ == '__main__':
1205 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001206 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001207 test_main()