blob: 4d7535c45d37fb27fe8334a9f3c557c176f732cc [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.')
Antoine Pitrou399df552011-10-06 22:41:08 +0200449 trans = bytes.maketrans(b'.aZ', b'-!$')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000450 sublen = len(SUBSTR)
451 repeats = size // sublen + 2
452 s = SUBSTR * repeats
453 s = s.translate(trans)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000454 self.assertEqual(len(s), repeats * sublen)
455 self.assertEqual(s[:sublen], SUBSTR.translate(trans))
456 self.assertEqual(s[-sublen:], SUBSTR.translate(trans))
457 self.assertEqual(s.count(_('.')), 0)
458 self.assertEqual(s.count(_('!')), repeats * 2)
459 self.assertEqual(s.count(_('z')), repeats * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000460
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200461 @bigmemtest(size=_2G + 5, memuse=2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000462 def test_upper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000463 _ = self.from_latin1
464 s = _('a') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465 s = s.upper()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000466 self.assertEqual(len(s), size)
467 self.assertEqual(s.count(_('A')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000468
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200469 @bigmemtest(size=_2G + 20, memuse=1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000470 def test_zfill(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000471 _ = self.from_latin1
472 SUBSTR = _('-568324723598234')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000473 s = SUBSTR.zfill(size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000474 self.assertTrue(s.endswith(_('0') + SUBSTR[1:]))
475 self.assertTrue(s.startswith(_('-0')))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000476 self.assertEqual(len(s), size)
477 self.assertEqual(s.count(_('0')), size - len(SUBSTR))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000478
479 # This test is meaningful even with size < 2G, as long as the
480 # doubled string is > 2G (but it tests more if both are > 2G :)
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200481 @bigmemtest(size=_1G + 2, memuse=3)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000482 def test_concat(self, size):
483 _ = self.from_latin1
484 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000485 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000486 s = s + s
Ezio Melottib3aedd42010-11-20 19:04:17 +0000487 self.assertEqual(len(s), size * 2)
488 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000489
490 # This test is meaningful even with size < 2G, as long as the
491 # repeated string is > 2G (but it tests more if both are > 2G :)
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200492 @bigmemtest(size=_1G + 2, memuse=3)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000493 def test_repeat(self, size):
494 _ = self.from_latin1
495 s = _('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000496 self.assertEqual(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000497 s = s * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000498 self.assertEqual(len(s), size * 2)
499 self.assertEqual(s.count(_('.')), size * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000500
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200501 @bigmemtest(size=_2G + 20, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000502 def test_slice_and_getitem(self, size):
503 _ = self.from_latin1
504 SUBSTR = _('0123456789')
505 sublen = len(SUBSTR)
506 s = SUBSTR * (size // sublen)
507 stepsize = len(s) // 100
508 stepsize = stepsize - (stepsize % sublen)
509 for i in range(0, len(s) - stepsize, stepsize):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000510 self.assertEqual(s[i], SUBSTR[0])
511 self.assertEqual(s[i:i + sublen], SUBSTR)
512 self.assertEqual(s[i:i + sublen:2], SUBSTR[::2])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000513 if i > 0:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000514 self.assertEqual(s[i + sublen - 1:i - 1:-3],
515 SUBSTR[sublen::-3])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000516 # Make sure we do some slicing and indexing near the end of the
517 # string, too.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000518 self.assertEqual(s[len(s) - 1], SUBSTR[-1])
519 self.assertEqual(s[-1], SUBSTR[-1])
520 self.assertEqual(s[len(s) - 10], SUBSTR[0])
521 self.assertEqual(s[-sublen], SUBSTR[0])
522 self.assertEqual(s[len(s):], _(''))
523 self.assertEqual(s[len(s) - 1:], SUBSTR[-1:])
524 self.assertEqual(s[-1:], SUBSTR[-1:])
525 self.assertEqual(s[len(s) - sublen:], SUBSTR)
526 self.assertEqual(s[-sublen:], SUBSTR)
527 self.assertEqual(len(s[:]), len(s))
528 self.assertEqual(len(s[:len(s) - 5]), len(s) - 5)
529 self.assertEqual(len(s[5:-5]), len(s) - 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000530
531 self.assertRaises(IndexError, operator.getitem, s, len(s))
532 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1)
533 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31)
534
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200535 @bigmemtest(size=_2G, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000536 def test_contains(self, size):
537 _ = self.from_latin1
538 SUBSTR = _('0123456789')
539 edge = _('-') * (size // 2)
540 s = _('').join([edge, SUBSTR, edge])
541 del edge
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100542 self.assertTrue(SUBSTR in s)
543 self.assertFalse(SUBSTR * 2 in s)
544 self.assertTrue(_('-') in s)
545 self.assertFalse(_('a') in s)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000546 s += _('a')
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100547 self.assertTrue(_('a') in s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000548
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200549 @bigmemtest(size=_2G + 10, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000550 def test_compare(self, size):
551 _ = self.from_latin1
552 s1 = _('-') * size
553 s2 = _('-') * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100554 self.assertTrue(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000555 del s2
556 s2 = s1 + _('a')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000557 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000558 del s2
559 s2 = _('.') * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000560 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000561
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200562 @bigmemtest(size=_2G + 10, memuse=1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000563 def test_hash(self, size):
564 # Not sure if we can do any meaningful tests here... Even if we
565 # start relying on the exact algorithm used, the result will be
566 # different depending on the size of the C 'long int'. Even this
567 # test is dodgy (there's no *guarantee* that the two things should
568 # have a different hash, even if they, in the current
569 # implementation, almost always do.)
570 _ = self.from_latin1
571 s = _('\x00') * size
572 h1 = hash(s)
573 del s
574 s = _('\x00') * (size + 1)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100575 self.assertNotEqual(h1, hash(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000576
577
578class StrTest(unittest.TestCase, BaseStrTest):
579
580 def from_latin1(self, s):
581 return s
582
583 def basic_encode_test(self, size, enc, c='.', expectedsize=None):
584 if expectedsize is None:
585 expectedsize = size
Antoine Pitrou45545f72011-01-12 20:46:37 +0000586 try:
587 s = c * size
588 self.assertEqual(len(s.encode(enc)), expectedsize)
589 finally:
590 s = None
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000591
592 def setUp(self):
593 # HACK: adjust memory use of tests inherited from BaseStrTest
594 # according to character size.
595 self._adjusted = {}
596 for name in dir(BaseStrTest):
597 if not name.startswith('test_'):
598 continue
599 meth = getattr(type(self), name)
600 try:
601 memuse = meth.memuse
602 except AttributeError:
603 continue
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200604 meth.memuse = ascii_char_size * memuse
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000605 self._adjusted[name] = memuse
606
607 def tearDown(self):
608 for name, memuse in self._adjusted.items():
609 getattr(type(self), name).memuse = memuse
610
Antoine Pitroude21f842011-10-06 21:46:23 +0200611 # Many codecs convert to the legacy representation first, explaining
612 # why we add 'ucs4_char_size' to the 'memuse' below.
613
614 @bigmemtest(size=_2G + 2, memuse=ascii_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000615 def test_encode(self, size):
616 return self.basic_encode_test(size, 'utf-8')
617
Antoine Pitroude21f842011-10-06 21:46:23 +0200618 @bigmemtest(size=_4G // 6 + 2, memuse=ascii_char_size + ucs4_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000619 def test_encode_raw_unicode_escape(self, size):
620 try:
621 return self.basic_encode_test(size, 'raw_unicode_escape')
622 except MemoryError:
623 pass # acceptable on 32-bit
624
Antoine Pitroude21f842011-10-06 21:46:23 +0200625 @bigmemtest(size=_4G // 5 + 70, memuse=ascii_char_size + ucs4_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000626 def test_encode_utf7(self, size):
627 try:
628 return self.basic_encode_test(size, 'utf7')
629 except MemoryError:
630 pass # acceptable on 32-bit
631
Antoine Pitroude21f842011-10-06 21:46:23 +0200632 @bigmemtest(size=_4G // 4 + 5, memuse=ascii_char_size + ucs4_char_size + 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000633 def test_encode_utf32(self, size):
634 try:
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200635 return self.basic_encode_test(size, 'utf32', expectedsize=4 * size + 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000636 except MemoryError:
637 pass # acceptable on 32-bit
638
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200639 @bigmemtest(size=_2G - 1, memuse=ascii_char_size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000640 def test_encode_ascii(self, size):
641 return self.basic_encode_test(size, 'ascii', c='A')
642
Antoine Pitrou8ac582f2011-10-06 21:55:51 +0200643 # str % (...) uses a Py_UCS4 intermediate representation
644
645 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2 + ucs4_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000646 def test_format(self, size):
647 s = '-' * size
648 sf = '%s' % (s,)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100649 self.assertTrue(s == sf)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 del sf
651 sf = '..%s..' % (s,)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000652 self.assertEqual(len(sf), len(s) + 4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000653 self.assertTrue(sf.startswith('..-'))
654 self.assertTrue(sf.endswith('-..'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 del s, sf
656
657 size //= 2
658 edge = '-' * size
659 s = ''.join([edge, '%s', edge])
660 del edge
661 s = s % '...'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000662 self.assertEqual(len(s), size * 2 + 3)
663 self.assertEqual(s.count('.'), 3)
664 self.assertEqual(s.count('-'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000665
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200666 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 def test_repr_small(self, size):
668 s = '-' * size
669 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000670 self.assertEqual(len(s), size + 2)
671 self.assertEqual(s[0], "'")
672 self.assertEqual(s[-1], "'")
673 self.assertEqual(s.count('-'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 del s
675 # repr() will create a string four times as large as this 'binary
676 # string', but we don't want to allocate much more than twice
677 # size in total. (We do extra testing in test_repr_large())
678 size = size // 5 * 2
679 s = '\x00' * size
680 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000681 self.assertEqual(len(s), size * 4 + 2)
682 self.assertEqual(s[0], "'")
683 self.assertEqual(s[-1], "'")
684 self.assertEqual(s.count('\\'), size)
685 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200687 @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000688 def test_repr_large(self, size):
689 s = '\x00' * size
690 s = repr(s)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000691 self.assertEqual(len(s), size * 4 + 2)
692 self.assertEqual(s[0], "'")
693 self.assertEqual(s[-1], "'")
694 self.assertEqual(s.count('\\'), size)
695 self.assertEqual(s.count('0'), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000696
Antoine Pitroub6080b62011-10-06 22:32:10 +0200697 # ascii() calls encode('ascii', 'backslashreplace'), which itself
698 # creates a temporary Py_UNICODE representation in addition to the
699 # original (Py_UCS2) one
700 # There's also some overallocation when resizing the ascii() result
701 # that isn't taken into account here.
702 @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size +
703 ucs4_char_size + ascii_char_size * 6)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000704 def test_unicode_repr(self, size):
Florent Xiclunafaa663f2010-03-19 13:37:08 +0000705 # Use an assigned, but not printable code point.
706 # It is in the range of the low surrogates \uDC00-\uDFFF.
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200707 char = "\uDCBA"
708 s = char * size
709 try:
710 for f in (repr, ascii):
711 r = f(s)
712 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
713 self.assertTrue(r.endswith(r"\udcba'"), r[-10:])
714 r = None
715 finally:
716 r = s = None
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000717
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200718 @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size + ascii_char_size * 10)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000719 def test_unicode_repr_wide(self, size):
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200720 char = "\U0001DCBA"
721 s = char * size
722 try:
723 for f in (repr, ascii):
724 r = f(s)
725 self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size)
726 self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:])
727 r = None
728 finally:
729 r = s = None
730
Antoine Pitrou399df552011-10-06 22:41:08 +0200731 # The original test_translate is overriden here, so as to get the
732 # correct size estimate: str.translate() uses an intermediate Py_UCS4
733 # representation.
734
735 @bigmemtest(size=_2G, memuse=ascii_char_size * 2 + ucs4_char_size)
736 def test_translate(self, size):
737 _ = self.from_latin1
738 SUBSTR = _('aZz.z.Aaz.')
739 trans = {
740 ord(_('.')): _('-'),
741 ord(_('a')): _('!'),
742 ord(_('Z')): _('$'),
743 }
744 sublen = len(SUBSTR)
745 repeats = size // sublen + 2
746 s = SUBSTR * repeats
747 s = s.translate(trans)
748 self.assertEqual(len(s), repeats * sublen)
749 self.assertEqual(s[:sublen], SUBSTR.translate(trans))
750 self.assertEqual(s[-sublen:], SUBSTR.translate(trans))
751 self.assertEqual(s.count(_('.')), 0)
752 self.assertEqual(s.count(_('!')), repeats * 2)
753 self.assertEqual(s.count(_('z')), repeats * 3)
754
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000756class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000757
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000758 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000759 return s.encode("latin-1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200761 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000762 def test_decode(self, size):
763 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000764 self.assertEqual(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000765
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000767class BytearrayTest(unittest.TestCase, BaseStrTest):
768
769 def from_latin1(self, s):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000770 return bytearray(s.encode("latin-1"))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000771
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200772 @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000773 def test_decode(self, size):
774 s = self.from_latin1('.') * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000775 self.assertEqual(len(s.decode('utf-8')), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000776
777 test_hash = None
778 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779
780class TupleTest(unittest.TestCase):
781
782 # Tuples have a small, fixed-sized head and an array of pointers to
783 # data. Since we're testing 64-bit addressing, we can assume that the
784 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
785 # per size.
786
787 # As a side-effect of testing long tuples, these tests happen to test
788 # having more than 2<<31 references to any given object. Hence the
789 # use of different types of objects as contents in different tests.
790
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200791 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000793 t1 = ('',) * size
794 t2 = ('',) * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100795 self.assertTrue(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000797 t2 = ('',) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000798 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 del t2
800 t2 = (1,) * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000801 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802
803 # Test concatenating into a single tuple of more than 2G in length,
804 # and concatenating a tuple of more than 2G in length separately, so
805 # the smaller test still gets run even if there isn't memory for the
806 # larger test (but we still let the tester know the larger test is
807 # skipped, in verbose mode.)
808 def basic_concat_test(self, size):
809 t = ((),) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000810 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 t = t + t
Ezio Melottib3aedd42010-11-20 19:04:17 +0000812 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000813
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200814 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815 def test_concat_small(self, size):
816 return self.basic_concat_test(size)
817
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200818 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819 def test_concat_large(self, size):
820 return self.basic_concat_test(size)
821
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200822 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823 def test_contains(self, size):
824 t = (1, 2, 3, 4, 5) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000825 self.assertEqual(len(t), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100826 self.assertTrue(5 in t)
827 self.assertFalse((1, 2, 3, 4, 5) in t)
828 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200830 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 def test_hash(self, size):
832 t1 = (0,) * size
833 h1 = hash(t1)
834 del t1
835 t2 = (0,) * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000836 self.assertFalse(h1 == hash(t2))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200838 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 def test_index_and_slice(self, size):
840 t = (None,) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000841 self.assertEqual(len(t), size)
842 self.assertEqual(t[-1], None)
843 self.assertEqual(t[5], None)
844 self.assertEqual(t[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845 self.assertRaises(IndexError, operator.getitem, t, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000846 self.assertEqual(t[:5], (None,) * 5)
847 self.assertEqual(t[-5:], (None,) * 5)
848 self.assertEqual(t[20:25], (None,) * 5)
849 self.assertEqual(t[-25:-20], (None,) * 5)
850 self.assertEqual(t[size - 5:], (None,) * 5)
851 self.assertEqual(t[size - 5:size], (None,) * 5)
852 self.assertEqual(t[size - 6:size - 2], (None,) * 4)
853 self.assertEqual(t[size:size], ())
854 self.assertEqual(t[size:size+5], ())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855
856 # Like test_concat, split in two.
857 def basic_test_repeat(self, size):
858 t = ('',) * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000859 self.assertEqual(len(t), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000860 t = t * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +0000861 self.assertEqual(len(t), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200863 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864 def test_repeat_small(self, size):
865 return self.basic_test_repeat(size)
866
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200867 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868 def test_repeat_large(self, size):
869 return self.basic_test_repeat(size)
870
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200871 @bigmemtest(size=_1G - 1, memuse=12)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000872 def test_repeat_large_2(self, size):
873 return self.basic_test_repeat(size)
874
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200875 @bigmemtest(size=_1G - 1, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000876 def test_from_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000877 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000878 try:
879 t = tuple(range(size))
880 except MemoryError:
881 pass # acceptable on 32-bit
882 else:
883 count = 0
884 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000885 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000886 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000887 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000888
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200889 @bigmemtest(size=_1G - 25, memuse=9)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000890 def test_from_almost_2G_generator(self, size):
Antoine Pitrouea510eb2010-11-08 21:40:13 +0000891 self.skipTest("test needs much more memory than advertised, see issue5438")
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000892 try:
893 t = tuple(range(size))
894 count = 0
895 for item in t:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000896 self.assertEqual(item, count)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000897 count += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000898 self.assertEqual(count, size)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000899 except MemoryError:
900 pass # acceptable, expected on 32-bit
901
Thomas Wouters477c8d52006-05-27 19:21:47 +0000902 # Like test_concat, split in two.
903 def basic_test_repr(self, size):
904 t = (0,) * size
905 s = repr(t)
906 # The repr of a tuple of 0's is exactly three times the tuple length.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000907 self.assertEqual(len(s), size * 3)
908 self.assertEqual(s[:5], '(0, 0')
909 self.assertEqual(s[-5:], '0, 0)')
910 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200912 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913 def test_repr_small(self, size):
914 return self.basic_test_repr(size)
915
Antoine Pitrou87a484c2011-10-04 15:55:44 +0200916 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917 def test_repr_large(self, size):
918 return self.basic_test_repr(size)
919
920class ListTest(unittest.TestCase):
921
922 # Like tuples, lists have a small, fixed-sized head and an array of
923 # pointers to data, so 8 bytes per size. Also like tuples, we make the
924 # lists hold references to various objects to test their refcount
925 # limits.
926
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200927 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000929 l1 = [''] * size
930 l2 = [''] * size
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100931 self.assertTrue(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000933 l2 = [''] * (size + 1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000934 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935 del l2
936 l2 = [2] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000937 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938
939 # Test concatenating into a single list of more than 2G in length,
940 # and concatenating a list of more than 2G in length separately, so
941 # the smaller test still gets run even if there isn't memory for the
942 # larger test (but we still let the tester know the larger test is
943 # skipped, in verbose mode.)
944 def basic_test_concat(self, size):
945 l = [[]] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000946 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947 l = l + l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000948 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200950 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 def test_concat_small(self, size):
952 return self.basic_test_concat(size)
953
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200954 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955 def test_concat_large(self, size):
956 return self.basic_test_concat(size)
957
958 def basic_test_inplace_concat(self, size):
959 l = [sys.stdout] * size
960 l += l
Ezio Melottib3aedd42010-11-20 19:04:17 +0000961 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000962 self.assertTrue(l[0] is l[-1])
963 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200965 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966 def test_inplace_concat_small(self, size):
967 return self.basic_test_inplace_concat(size)
968
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200969 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970 def test_inplace_concat_large(self, size):
971 return self.basic_test_inplace_concat(size)
972
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200973 @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974 def test_contains(self, size):
975 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000976 self.assertEqual(len(l), size * 5)
Thomas Wouters518b5ae2011-03-25 11:42:37 +0100977 self.assertTrue(5 in l)
978 self.assertFalse([1, 2, 3, 4, 5] in l)
979 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000980
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200981 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000982 def test_hash(self, size):
983 l = [0] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000984 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985
Antoine Pitrou94190bb2011-10-04 10:22:36 +0200986 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987 def test_index_and_slice(self, size):
988 l = [None] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +0000989 self.assertEqual(len(l), size)
990 self.assertEqual(l[-1], None)
991 self.assertEqual(l[5], None)
992 self.assertEqual(l[size - 1], None)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993 self.assertRaises(IndexError, operator.getitem, l, size)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000994 self.assertEqual(l[:5], [None] * 5)
995 self.assertEqual(l[-5:], [None] * 5)
996 self.assertEqual(l[20:25], [None] * 5)
997 self.assertEqual(l[-25:-20], [None] * 5)
998 self.assertEqual(l[size - 5:], [None] * 5)
999 self.assertEqual(l[size - 5:size], [None] * 5)
1000 self.assertEqual(l[size - 6:size - 2], [None] * 4)
1001 self.assertEqual(l[size:size], [])
1002 self.assertEqual(l[size:size+5], [])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003
1004 l[size - 2] = 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001005 self.assertEqual(len(l), size)
1006 self.assertEqual(l[-3:], [None, 5, None])
1007 self.assertEqual(l.count(5), 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 self.assertRaises(IndexError, operator.setitem, l, size, 6)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001009 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010
1011 l[size - 7:] = [1, 2, 3, 4, 5]
1012 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001013 self.assertEqual(len(l), size)
1014 self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015
1016 l[:7] = [1, 2, 3, 4, 5]
1017 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001018 self.assertEqual(len(l), size)
1019 self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020
1021 del l[size - 1]
1022 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001023 self.assertEqual(len(l), size)
1024 self.assertEqual(l[-1], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025
1026 del l[-2:]
1027 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001028 self.assertEqual(len(l), size)
1029 self.assertEqual(l[-1], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030
1031 del l[0]
1032 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001033 self.assertEqual(len(l), size)
1034 self.assertEqual(l[0], 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035
1036 del l[:2]
1037 size -= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001038 self.assertEqual(len(l), size)
1039 self.assertEqual(l[0], 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040
1041 # Like test_concat, split in two.
1042 def basic_test_repeat(self, size):
1043 l = [] * size
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001044 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045 l = [''] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001046 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047 l = l * 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001048 self.assertEqual(len(l), size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001050 @bigmemtest(size=_2G // 2 + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001051 def test_repeat_small(self, size):
1052 return self.basic_test_repeat(size)
1053
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001054 @bigmemtest(size=_2G + 2, memuse=24)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 def test_repeat_large(self, size):
1056 return self.basic_test_repeat(size)
1057
1058 def basic_test_inplace_repeat(self, size):
1059 l = ['']
1060 l *= size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001061 self.assertEqual(len(l), size)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001062 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063 del l
1064
1065 l = [''] * size
1066 l *= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +00001067 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001068 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001070 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 def test_inplace_repeat_small(self, size):
1072 return self.basic_test_inplace_repeat(size)
1073
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001074 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 def test_inplace_repeat_large(self, size):
1076 return self.basic_test_inplace_repeat(size)
1077
1078 def basic_test_repr(self, size):
1079 l = [0] * size
1080 s = repr(l)
1081 # The repr of a list of 0's is exactly three times the list length.
Ezio Melottib3aedd42010-11-20 19:04:17 +00001082 self.assertEqual(len(s), size * 3)
1083 self.assertEqual(s[:5], '[0, 0')
1084 self.assertEqual(s[-5:], '0, 0]')
1085 self.assertEqual(s.count('0'), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001087 @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001088 def test_repr_small(self, size):
1089 return self.basic_test_repr(size)
1090
Antoine Pitrou87a484c2011-10-04 15:55:44 +02001091 @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092 def test_repr_large(self, size):
1093 return self.basic_test_repr(size)
1094
1095 # list overallocates ~1/8th of the total size (on first expansion) so
1096 # the single list.append call puts memuse at 9 bytes per size.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001097 @bigmemtest(size=_2G, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001098 def test_append(self, size):
1099 l = [object()] * size
1100 l.append(object())
Ezio Melottib3aedd42010-11-20 19:04:17 +00001101 self.assertEqual(len(l), size+1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001102 self.assertTrue(l[-3] is l[-2])
1103 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001105 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106 def test_count(self, size):
1107 l = [1, 2, 3, 4, 5] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001108 self.assertEqual(l.count(1), size)
1109 self.assertEqual(l.count("1"), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110
1111 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001112 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113 l.extend(l)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001114 self.assertEqual(len(l), size * 2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001115 self.assertTrue(l[0] is l[-1])
1116 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001117
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001118 @bigmemtest(size=_2G // 2 + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001119 def test_extend_small(self, size):
1120 return self.basic_test_extend(size)
1121
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001122 @bigmemtest(size=_2G + 2, memuse=16)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001123 def test_extend_large(self, size):
1124 return self.basic_test_extend(size)
1125
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001126 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001127 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001128 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001129 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001130 self.assertEqual(l.index(1), 0)
1131 self.assertEqual(l.index(5, size - 5), size - 1)
1132 self.assertEqual(l.index(5, size - 5, size), size - 1)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001134 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001135
1136 # This tests suffers from overallocation, just like test_append.
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001137 @bigmemtest(size=_2G + 10, memuse=9)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138 def test_insert(self, size):
1139 l = [1.0] * size
1140 l.insert(size - 1, "A")
1141 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001142 self.assertEqual(len(l), size)
1143 self.assertEqual(l[-3:], [1.0, "A", 1.0])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144
1145 l.insert(size + 1, "B")
1146 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001147 self.assertEqual(len(l), size)
1148 self.assertEqual(l[-3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149
1150 l.insert(1, "C")
1151 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001152 self.assertEqual(len(l), size)
1153 self.assertEqual(l[:3], [1.0, "C", 1.0])
1154 self.assertEqual(l[size - 3:], ["A", 1.0, "B"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001156 @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001158 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159 size *= 5
Ezio Melottib3aedd42010-11-20 19:04:17 +00001160 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001161
1162 item = l.pop()
1163 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001164 self.assertEqual(len(l), size)
1165 self.assertEqual(item, "e")
1166 self.assertEqual(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167
1168 item = l.pop(0)
1169 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001170 self.assertEqual(len(l), size)
1171 self.assertEqual(item, "a")
1172 self.assertEqual(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001173
1174 item = l.pop(size - 2)
1175 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001176 self.assertEqual(len(l), size)
1177 self.assertEqual(item, "c")
1178 self.assertEqual(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001179
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001180 @bigmemtest(size=_2G + 10, memuse=8)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001181 def test_remove(self, size):
1182 l = [10] * size
Ezio Melottib3aedd42010-11-20 19:04:17 +00001183 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001184
1185 l.remove(10)
1186 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001187 self.assertEqual(len(l), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188
1189 # Because of the earlier l.remove(), this append doesn't trigger
1190 # a resize.
1191 l.append(5)
1192 size += 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001193 self.assertEqual(len(l), size)
1194 self.assertEqual(l[-2:], [10, 5])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001195 l.remove(5)
1196 size -= 1
Ezio Melottib3aedd42010-11-20 19:04:17 +00001197 self.assertEqual(len(l), size)
1198 self.assertEqual(l[-2:], [10, 10])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001199
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001200 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001201 def test_reverse(self, size):
1202 l = [1, 2, 3, 4, 5] * size
1203 l.reverse()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001204 self.assertEqual(len(l), size * 5)
1205 self.assertEqual(l[-5:], [5, 4, 3, 2, 1])
1206 self.assertEqual(l[:5], [5, 4, 3, 2, 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001207
Antoine Pitrou94190bb2011-10-04 10:22:36 +02001208 @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001209 def test_sort(self, size):
1210 l = [1, 2, 3, 4, 5] * size
1211 l.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +00001212 self.assertEqual(len(l), size * 5)
1213 self.assertEqual(l.count(1), size)
1214 self.assertEqual(l[:10], [1] * 10)
1215 self.assertEqual(l[-10:], [5] * 10)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001216
1217def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001218 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1219 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001220
1221if __name__ == '__main__':
1222 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001223 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001224 test_main()