blob: e41c0105520f23088490dff43a3ad46c54126705 [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 Pitrou94190bb2011-10-04 10:22:36 +0200377 @bigmemtest(size=_2G + 5, memuse=10)
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 Pitrou87a484c2011-10-04 15:55:44 +0200650 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651 def test_format(self, size):
652 s = '-' * size
653 sf = '%s' % (s,)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100654 self.assertTrue(s == sf)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 del sf
656 sf = '..%s..' % (s,)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000657 self.assertEqual(len(sf), len(s) + 4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000658 self.assertTrue(sf.startswith('..-'))
659 self.assertTrue(sf.endswith('-..'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 del s, sf
661
662 size //= 2
663 edge = '-' * size
664 s = ''.join([edge, '%s', edge])
665 del edge
666 s = s % '...'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000667 self.assertEqual(len(s), size * 2 + 3)
668 self.assertEqual(s.count('.'), 3)
669 self.assertEqual(s.count('-'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200671 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 def test_repr_small(self, size):
673 s = '-' * size
674 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000675 self.assertEqual(len(s), size + 2)
676 self.assertEqual(s[0], "'")
677 self.assertEqual(s[-1], "'")
678 self.assertEqual(s.count('-'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 del s
680 # repr() will create a string four times as large as this 'binary
681 # string', but we don't want to allocate much more than twice
682 # size in total. (We do extra testing in test_repr_large())
683 size = size // 5 * 2
684 s = '\x00' * size
685 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000686 self.assertEqual(len(s), size * 4 + 2)
687 self.assertEqual(s[0], "'")
688 self.assertEqual(s[-1], "'")
689 self.assertEqual(s.count('\\'), size)
690 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200692 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693 def test_repr_large(self, size):
694 s = '\x00' * size
695 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000696 self.assertEqual(len(s), size * 4 + 2)
697 self.assertEqual(s[0], "'")
698 self.assertEqual(s[-1], "'")
699 self.assertEqual(s.count('\\'), size)
700 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200702 @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size + ascii_char_size * 6)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000703 def test_unicode_repr(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000704 # Use an assigned, but not printable code point.
705 # It is in the range of the low surrogates \uDC00-\uDFFF.
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200706 char = "\uDCBA"
707 s = char * size
708 try:
709 for f in (repr, ascii):
710 r = f(s)
711 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
712 self.assertTrue(r.endswith(r"\udcba'"), r[-10:])
713 r = None
714 finally:
715 r = s = None
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000716
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200717 @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size + ascii_char_size * 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000718 def test_unicode_repr_wide(self, size):
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200719 char = "\U0001DCBA"
720 s = char * size
721 try:
722 for f in (repr, ascii):
723 r = f(s)
724 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
725 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:])
726 r = None
727 finally:
728 r = s = None
729
Thomas Wouters477c8d52006-05-27 19:21:47 +0000730
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000731class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000733 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000734 return s.encode("latin-1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000735
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200736 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000737 def test_decode(self, size):
738 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000739 self.assertEqual(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000742class BytearrayTest(unittest.TestCase, BaseStrTest):
743
744 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000745 return bytearray(s.encode("latin-1"))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000746
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200747 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000748 def test_decode(self, size):
749 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000750 self.assertEqual(len(s.decode('utf-8')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000751
752 test_hash = None
753 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000754
755class TupleTest(unittest.TestCase):
756
757 # Tuples have a small, fixed-sized head and an array of pointers to
758 # data. Since we're testing 64-bit addressing, we can assume that the
759 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
760 # per size.
761
762 # As a side-effect of testing long tuples, these tests happen to test
763 # having more than 2<<31 references to any given object. Hence the
764 # use of different types of objects as contents in different tests.
765
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200766 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000767 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000768 t1 = ('',) * size
769 t2 = ('',) * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100770 self.assertTrue(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000772 t2 = ('',) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000773 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000774 del t2
775 t2 = (1,) * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000776 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777
778 # Test concatenating into a single tuple of more than 2G in length,
779 # and concatenating a tuple of more than 2G in length separately, so
780 # the smaller test still gets run even if there isn't memory for the
781 # larger test (but we still let the tester know the larger test is
782 # skipped, in verbose mode.)
783 def basic_concat_test(self, size):
784 t = ((),) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000785 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 t = t + t
Ezio Melottib3aedd42010-11-20 19:04:17 +0000787 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200789 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000790 def test_concat_small(self, size):
791 return self.basic_concat_test(size)
792
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200793 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000794 def test_concat_large(self, size):
795 return self.basic_concat_test(size)
796
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200797 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000798 def test_contains(self, size):
799 t = (1, 2, 3, 4, 5) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000800 self.assertEqual(len(t), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100801 self.assertTrue(5 in t)
802 self.assertFalse((1, 2, 3, 4, 5) in t)
803 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200805 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000806 def test_hash(self, size):
807 t1 = (0,) * size
808 h1 = hash(t1)
809 del t1
810 t2 = (0,) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000811 self.assertFalse(h1 == hash(t2))
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_index_and_slice(self, size):
815 t = (None,) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000816 self.assertEqual(len(t), size)
817 self.assertEqual(t[-1], None)
818 self.assertEqual(t[5], None)
819 self.assertEqual(t[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820 self.assertRaises(IndexError, operator.getitem, t, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000821 self.assertEqual(t[:5], (None,) * 5)
822 self.assertEqual(t[-5:], (None,) * 5)
823 self.assertEqual(t[20:25], (None,) * 5)
824 self.assertEqual(t[-25:-20], (None,) * 5)
825 self.assertEqual(t[size - 5:], (None,) * 5)
826 self.assertEqual(t[size - 5:size], (None,) * 5)
827 self.assertEqual(t[size - 6:size - 2], (None,) * 4)
828 self.assertEqual(t[size:size], ())
829 self.assertEqual(t[size:size+5], ())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830
831 # Like test_concat, split in two.
832 def basic_test_repeat(self, size):
833 t = ('',) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000834 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000835 t = t * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000836 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200838 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 def test_repeat_small(self, size):
840 return self.basic_test_repeat(size)
841
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200842 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000843 def test_repeat_large(self, size):
844 return self.basic_test_repeat(size)
845
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200846 @bigmemtest(size=_1G - 1, memuse=12)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000847 def test_repeat_large_2(self, size):
848 return self.basic_test_repeat(size)
849
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200850 @bigmemtest(size=_1G - 1, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000851 def test_from_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000852 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000853 try:
854 t = tuple(range(size))
855 except MemoryError:
856 pass # acceptable on 32-bit
857 else:
858 count = 0
859 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000860 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000861 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000862 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000863
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200864 @bigmemtest(size=_1G - 25, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000865 def test_from_almost_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000866 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000867 try:
868 t = tuple(range(size))
869 count = 0
870 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000871 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000872 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000873 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000874 except MemoryError:
875 pass # acceptable, expected on 32-bit
876
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 # Like test_concat, split in two.
878 def basic_test_repr(self, size):
879 t = (0,) * size
880 s = repr(t)
881 # The repr of a tuple of 0's is exactly three times the tuple length.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000882 self.assertEqual(len(s), size * 3)
883 self.assertEqual(s[:5], '(0, 0')
884 self.assertEqual(s[-5:], '0, 0)')
885 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000886
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200887 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000888 def test_repr_small(self, size):
889 return self.basic_test_repr(size)
890
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200891 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892 def test_repr_large(self, size):
893 return self.basic_test_repr(size)
894
895class ListTest(unittest.TestCase):
896
897 # Like tuples, lists have a small, fixed-sized head and an array of
898 # pointers to data, so 8 bytes per size. Also like tuples, we make the
899 # lists hold references to various objects to test their refcount
900 # limits.
901
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200902 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000904 l1 = [''] * size
905 l2 = [''] * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100906 self.assertTrue(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000908 l2 = [''] * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000909 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000910 del l2
911 l2 = [2] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000912 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913
914 # Test concatenating into a single list of more than 2G in length,
915 # and concatenating a list of more than 2G in length separately, so
916 # the smaller test still gets run even if there isn't memory for the
917 # larger test (but we still let the tester know the larger test is
918 # skipped, in verbose mode.)
919 def basic_test_concat(self, size):
920 l = [[]] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000921 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922 l = l + l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000923 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200925 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926 def test_concat_small(self, size):
927 return self.basic_test_concat(size)
928
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200929 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 def test_concat_large(self, size):
931 return self.basic_test_concat(size)
932
933 def basic_test_inplace_concat(self, size):
934 l = [sys.stdout] * size
935 l += l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000936 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000937 self.assertTrue(l[0] is l[-1])
938 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200940 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941 def test_inplace_concat_small(self, size):
942 return self.basic_test_inplace_concat(size)
943
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200944 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945 def test_inplace_concat_large(self, size):
946 return self.basic_test_inplace_concat(size)
947
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200948 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949 def test_contains(self, size):
950 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000951 self.assertEqual(len(l), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100952 self.assertTrue(5 in l)
953 self.assertFalse([1, 2, 3, 4, 5] in l)
954 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200956 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 def test_hash(self, size):
958 l = [0] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000959 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200961 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 def test_index_and_slice(self, size):
963 l = [None] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000964 self.assertEqual(len(l), size)
965 self.assertEqual(l[-1], None)
966 self.assertEqual(l[5], None)
967 self.assertEqual(l[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 self.assertRaises(IndexError, operator.getitem, l, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000969 self.assertEqual(l[:5], [None] * 5)
970 self.assertEqual(l[-5:], [None] * 5)
971 self.assertEqual(l[20:25], [None] * 5)
972 self.assertEqual(l[-25:-20], [None] * 5)
973 self.assertEqual(l[size - 5:], [None] * 5)
974 self.assertEqual(l[size - 5:size], [None] * 5)
975 self.assertEqual(l[size - 6:size - 2], [None] * 4)
976 self.assertEqual(l[size:size], [])
977 self.assertEqual(l[size:size+5], [])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978
979 l[size - 2] = 5
Ezio Melottib3aedd42010-11-20 19:04:17 +0000980 self.assertEqual(len(l), size)
981 self.assertEqual(l[-3:], [None, 5, None])
982 self.assertEqual(l.count(5), 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983 self.assertRaises(IndexError, operator.setitem, l, size, 6)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000984 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985
986 l[size - 7:] = [1, 2, 3, 4, 5]
987 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000988 self.assertEqual(len(l), size)
989 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990
991 l[:7] = [1, 2, 3, 4, 5]
992 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000993 self.assertEqual(len(l), size)
994 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995
996 del l[size - 1]
997 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000998 self.assertEqual(len(l), size)
999 self.assertEqual(l[-1], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000
1001 del l[-2:]
1002 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001003 self.assertEqual(len(l), size)
1004 self.assertEqual(l[-1], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005
1006 del l[0]
1007 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001008 self.assertEqual(len(l), size)
1009 self.assertEqual(l[0], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010
1011 del l[:2]
1012 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001013 self.assertEqual(len(l), size)
1014 self.assertEqual(l[0], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015
1016 # Like test_concat, split in two.
1017 def basic_test_repeat(self, size):
1018 l = [] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001019 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020 l = [''] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001021 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022 l = l * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001023 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001025 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026 def test_repeat_small(self, size):
1027 return self.basic_test_repeat(size)
1028
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001029 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 def test_repeat_large(self, size):
1031 return self.basic_test_repeat(size)
1032
1033 def basic_test_inplace_repeat(self, size):
1034 l = ['']
1035 l *= size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001036 self.assertEqual(len(l), size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001037 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 del l
1039
1040 l = [''] * size
1041 l *= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001042 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001043 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001045 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046 def test_inplace_repeat_small(self, size):
1047 return self.basic_test_inplace_repeat(size)
1048
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001049 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050 def test_inplace_repeat_large(self, size):
1051 return self.basic_test_inplace_repeat(size)
1052
1053 def basic_test_repr(self, size):
1054 l = [0] * size
1055 s = repr(l)
1056 # The repr of a list of 0's is exactly three times the list length.
Ezio Melottib3aedd42010-11-20 19:04:17 +00001057 self.assertEqual(len(s), size * 3)
1058 self.assertEqual(s[:5], '[0, 0')
1059 self.assertEqual(s[-5:], '0, 0]')
1060 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001062 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063 def test_repr_small(self, size):
1064 return self.basic_test_repr(size)
1065
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001066 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 def test_repr_large(self, size):
1068 return self.basic_test_repr(size)
1069
1070 # list overallocates ~1/8th of the total size (on first expansion) so
1071 # the single list.append call puts memuse at 9 bytes per size.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001072 @bigmemtest(size=_2G, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073 def test_append(self, size):
1074 l = [object()] * size
1075 l.append(object())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001076 self.assertEqual(len(l), size+1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001077 self.assertTrue(l[-3] is l[-2])
1078 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001079
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001080 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 def test_count(self, size):
1082 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001083 self.assertEqual(l.count(1), size)
1084 self.assertEqual(l.count("1"), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085
1086 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001087 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001088 l.extend(l)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001089 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001090 self.assertTrue(l[0] is l[-1])
1091 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001093 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001094 def test_extend_small(self, size):
1095 return self.basic_test_extend(size)
1096
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001097 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001098 def test_extend_large(self, size):
1099 return self.basic_test_extend(size)
1100
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001101 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001102 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001103 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001105 self.assertEqual(l.index(1), 0)
1106 self.assertEqual(l.index(5, size - 5), size - 1)
1107 self.assertEqual(l.index(5, size - 5, size), size - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001108 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001109 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110
1111 # This tests suffers from overallocation, just like test_append.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001112 @bigmemtest(size=_2G + 10, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113 def test_insert(self, size):
1114 l = [1.0] * size
1115 l.insert(size - 1, "A")
1116 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001117 self.assertEqual(len(l), size)
1118 self.assertEqual(l[-3:], [1.0, "A", 1.0])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001119
1120 l.insert(size + 1, "B")
1121 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001122 self.assertEqual(len(l), size)
1123 self.assertEqual(l[-3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001124
1125 l.insert(1, "C")
1126 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001127 self.assertEqual(len(l), size)
1128 self.assertEqual(l[:3], [1.0, "C", 1.0])
1129 self.assertEqual(l[size - 3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001130
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001131 @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001132 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001133 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001135 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001136
1137 item = l.pop()
1138 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001139 self.assertEqual(len(l), size)
1140 self.assertEqual(item, "e")
1141 self.assertEqual(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001142
1143 item = l.pop(0)
1144 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001145 self.assertEqual(len(l), size)
1146 self.assertEqual(item, "a")
1147 self.assertEqual(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148
1149 item = l.pop(size - 2)
1150 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001151 self.assertEqual(len(l), size)
1152 self.assertEqual(item, "c")
1153 self.assertEqual(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001155 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001156 def test_remove(self, size):
1157 l = [10] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001158 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159
1160 l.remove(10)
1161 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001162 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001163
1164 # Because of the earlier l.remove(), this append doesn't trigger
1165 # a resize.
1166 l.append(5)
1167 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001168 self.assertEqual(len(l), size)
1169 self.assertEqual(l[-2:], [10, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001170 l.remove(5)
1171 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001172 self.assertEqual(len(l), size)
1173 self.assertEqual(l[-2:], [10, 10])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001175 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176 def test_reverse(self, size):
1177 l = [1, 2, 3, 4, 5] * size
1178 l.reverse()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001179 self.assertEqual(len(l), size * 5)
1180 self.assertEqual(l[-5:], [5, 4, 3, 2, 1])
1181 self.assertEqual(l[:5], [5, 4, 3, 2, 1])
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_sort(self, size):
1185 l = [1, 2, 3, 4, 5] * size
1186 l.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001187 self.assertEqual(len(l), size * 5)
1188 self.assertEqual(l.count(1), size)
1189 self.assertEqual(l[:10], [1] * 10)
1190 self.assertEqual(l[-10:], [5] * 10)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001191
1192def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001193 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1194 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001195
1196if __name__ == '__main__':
1197 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001198 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001199 test_main()