blob: 030ada50c68c5ceaffb0a4d2c6e8baf946908be0 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Neal Norwitz3ce5d922008-08-24 07:08:55 +00002from test.support import bigmemtest, _1G, _2G, _4G, precisionbigmemtest
Thomas Wouters477c8d52006-05-27 19:21:47 +00003
4import unittest
5import operator
Thomas Wouters477c8d52006-05-27 19:21:47 +00006import sys
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00007import functools
Thomas Wouters477c8d52006-05-27 19:21:47 +00008
9# Bigmem testing houserules:
10#
11# - Try not to allocate too many large objects. It's okay to rely on
12# refcounting semantics, but don't forget that 's = create_largestring()'
13# doesn't release the old 's' (if it exists) until well after its new
14# value has been created. Use 'del s' before the create_largestring call.
15#
16# - Do *not* compare large objects using assertEquals or similar. It's a
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000017# lengthy operation and the errormessage will be utterly useless due to
Thomas Wouters477c8d52006-05-27 19:21:47 +000018# its size. To make sure whether a result has the right contents, better
19# to use the strip or count methods, or compare meaningful slices.
20#
21# - Don't forget to test for large indices, offsets and results and such,
22# in addition to large sizes.
23#
24# - When repeating an object (say, a substring, or a small list) to create
25# a large object, make the subobject of a length that is not a power of
26# 2. That way, int-wrapping problems are more easily detected.
27#
28# - While the bigmemtest decorator speaks of 'minsize', all tests will
29# actually be called with a much smaller number too, in the normal
30# test run (5Kb currently.) This is so the tests themselves get frequent
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031# testing. Consequently, always make all large allocations based on the
Thomas Wouters477c8d52006-05-27 19:21:47 +000032# passed-in 'size', and don't rely on the size being very large. Also,
33# memuse-per-size should remain sane (less than a few thousand); if your
34# test uses more, adjust 'size' upward, instead.
35
Antoine Pitrouec00e2f2009-03-07 23:52:26 +000036# BEWARE: it seems that one failing test can yield other subsequent tests to
37# fail as well. I do not know whether it is due to memory fragmentation
38# issues, or other specifics of the platform malloc() routine.
39
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000040character_size = 4 if sys.maxunicode > 0xFFFF else 2
41
42
43class BaseStrTest:
44
Thomas Wouters477c8d52006-05-27 19:21:47 +000045 @bigmemtest(minsize=_2G, memuse=2)
46 def test_capitalize(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000047 _ = self.from_latin1
48 SUBSTR = self.from_latin1(' abc def ghi')
49 s = _('-') * size + SUBSTR
Thomas Wouters477c8d52006-05-27 19:21:47 +000050 caps = s.capitalize()
51 self.assertEquals(caps[-len(SUBSTR):],
52 SUBSTR.capitalize())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000053 self.assertEquals(caps.lstrip(_('-')), SUBSTR)
Thomas Wouters477c8d52006-05-27 19:21:47 +000054
55 @bigmemtest(minsize=_2G + 10, memuse=1)
56 def test_center(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000057 SUBSTR = self.from_latin1(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +000058 s = SUBSTR.center(size)
59 self.assertEquals(len(s), size)
60 lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2
61 if len(s) % 2:
62 lpadsize += 1
63 self.assertEquals(s[lpadsize:-rpadsize], SUBSTR)
64 self.assertEquals(s.strip(), SUBSTR.strip())
65
66 @bigmemtest(minsize=_2G, memuse=2)
67 def test_count(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000068 _ = self.from_latin1
69 SUBSTR = _(' abc def ghi')
70 s = _('.') * size + SUBSTR
71 self.assertEquals(s.count(_('.')), size)
72 s += _('.')
73 self.assertEquals(s.count(_('.')), size + 1)
74 self.assertEquals(s.count(_(' ')), 3)
75 self.assertEquals(s.count(_('i')), 1)
76 self.assertEquals(s.count(_('j')), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +000077
78 @bigmemtest(minsize=_2G, memuse=2)
79 def test_endswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000080 _ = self.from_latin1
81 SUBSTR = _(' abc def ghi')
82 s = _('-') * size + SUBSTR
Georg Brandlab91fde2009-08-13 08:51:18 +000083 self.assertTrue(s.endswith(SUBSTR))
84 self.assertTrue(s.endswith(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000085 s2 = _('...') + s
Georg Brandlab91fde2009-08-13 08:51:18 +000086 self.assertTrue(s2.endswith(s))
87 self.assertFalse(s.endswith(_('a') + SUBSTR))
88 self.assertFalse(SUBSTR.endswith(s))
Thomas Wouters477c8d52006-05-27 19:21:47 +000089
90 @bigmemtest(minsize=_2G + 10, memuse=2)
91 def test_expandtabs(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000092 _ = self.from_latin1
93 s = _('-') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 tabsize = 8
95 self.assertEquals(s.expandtabs(), s)
96 del s
97 slen, remainder = divmod(size, tabsize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +000098 s = _(' \t') * slen
Thomas Wouters477c8d52006-05-27 19:21:47 +000099 s = s.expandtabs(tabsize)
100 self.assertEquals(len(s), size - remainder)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000101 self.assertEquals(len(s.strip(_(' '))), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102
103 @bigmemtest(minsize=_2G, memuse=2)
104 def test_find(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000105 _ = self.from_latin1
106 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000108 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
109 self.assertEquals(s.find(_(' ')), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110 self.assertEquals(s.find(SUBSTR), 0)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000111 self.assertEquals(s.find(_(' '), sublen), sublen + size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112 self.assertEquals(s.find(SUBSTR, len(SUBSTR)), sublen + size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000113 self.assertEquals(s.find(_('i')), SUBSTR.find(_('i')))
114 self.assertEquals(s.find(_('i'), sublen),
115 sublen + size + SUBSTR.find(_('i')))
116 self.assertEquals(s.find(_('i'), size),
117 sublen + size + SUBSTR.find(_('i')))
118 self.assertEquals(s.find(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119
120 @bigmemtest(minsize=_2G, memuse=2)
121 def test_index(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000122 _ = self.from_latin1
123 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000125 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
126 self.assertEquals(s.index(_(' ')), 0)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000127 self.assertEquals(s.index(SUBSTR), 0)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000128 self.assertEquals(s.index(_(' '), sublen), sublen + size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129 self.assertEquals(s.index(SUBSTR, sublen), sublen + size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000130 self.assertEquals(s.index(_('i')), SUBSTR.index(_('i')))
131 self.assertEquals(s.index(_('i'), sublen),
132 sublen + size + SUBSTR.index(_('i')))
133 self.assertEquals(s.index(_('i'), size),
134 sublen + size + SUBSTR.index(_('i')))
135 self.assertRaises(ValueError, s.index, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136
137 @bigmemtest(minsize=_2G, memuse=2)
138 def test_isalnum(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000139 _ = self.from_latin1
140 SUBSTR = _('123456')
141 s = _('a') * size + SUBSTR
Georg Brandlab91fde2009-08-13 08:51:18 +0000142 self.assertTrue(s.isalnum())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000143 s += _('.')
Georg Brandlab91fde2009-08-13 08:51:18 +0000144 self.assertFalse(s.isalnum())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145
146 @bigmemtest(minsize=_2G, memuse=2)
147 def test_isalpha(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000148 _ = self.from_latin1
149 SUBSTR = _('zzzzzzz')
150 s = _('a') * size + SUBSTR
Georg Brandlab91fde2009-08-13 08:51:18 +0000151 self.assertTrue(s.isalpha())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000152 s += _('.')
Georg Brandlab91fde2009-08-13 08:51:18 +0000153 self.assertFalse(s.isalpha())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154
155 @bigmemtest(minsize=_2G, memuse=2)
156 def test_isdigit(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000157 _ = self.from_latin1
158 SUBSTR = _('123456')
159 s = _('9') * size + SUBSTR
Georg Brandlab91fde2009-08-13 08:51:18 +0000160 self.assertTrue(s.isdigit())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000161 s += _('z')
Georg Brandlab91fde2009-08-13 08:51:18 +0000162 self.assertFalse(s.isdigit())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000163
164 @bigmemtest(minsize=_2G, memuse=2)
165 def test_islower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000166 _ = self.from_latin1
167 chars = _(''.join(
168 chr(c) for c in range(255) if not chr(c).isupper()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000169 repeats = size // len(chars) + 2
170 s = chars * repeats
Georg Brandlab91fde2009-08-13 08:51:18 +0000171 self.assertTrue(s.islower())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000172 s += _('A')
Georg Brandlab91fde2009-08-13 08:51:18 +0000173 self.assertFalse(s.islower())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000174
175 @bigmemtest(minsize=_2G, memuse=2)
176 def test_isspace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000177 _ = self.from_latin1
178 whitespace = _(' \f\n\r\t\v')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000179 repeats = size // len(whitespace) + 2
180 s = whitespace * repeats
Georg Brandlab91fde2009-08-13 08:51:18 +0000181 self.assertTrue(s.isspace())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000182 s += _('j')
Georg Brandlab91fde2009-08-13 08:51:18 +0000183 self.assertFalse(s.isspace())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000184
185 @bigmemtest(minsize=_2G, memuse=2)
186 def test_istitle(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000187 _ = self.from_latin1
188 SUBSTR = _('123456')
189 s = _('').join([_('A'), _('a') * size, SUBSTR])
Georg Brandlab91fde2009-08-13 08:51:18 +0000190 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000191 s += _('A')
Georg Brandlab91fde2009-08-13 08:51:18 +0000192 self.assertTrue(s.istitle())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000193 s += _('aA')
Georg Brandlab91fde2009-08-13 08:51:18 +0000194 self.assertFalse(s.istitle())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000195
196 @bigmemtest(minsize=_2G, memuse=2)
197 def test_isupper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000198 _ = self.from_latin1
199 chars = _(''.join(
200 chr(c) for c in range(255) if not chr(c).islower()))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201 repeats = size // len(chars) + 2
202 s = chars * repeats
Georg Brandlab91fde2009-08-13 08:51:18 +0000203 self.assertTrue(s.isupper())
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000204 s += _('a')
Georg Brandlab91fde2009-08-13 08:51:18 +0000205 self.assertFalse(s.isupper())
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206
207 @bigmemtest(minsize=_2G, memuse=2)
208 def test_join(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000209 _ = self.from_latin1
210 s = _('A') * size
211 x = s.join([_('aaaaa'), _('bbbbb')])
212 self.assertEquals(x.count(_('a')), 5)
213 self.assertEquals(x.count(_('b')), 5)
Georg Brandlab91fde2009-08-13 08:51:18 +0000214 self.assertTrue(x.startswith(_('aaaaaA')))
215 self.assertTrue(x.endswith(_('Abbbbb')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000216
217 @bigmemtest(minsize=_2G + 10, memuse=1)
218 def test_ljust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000219 _ = self.from_latin1
220 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000221 s = SUBSTR.ljust(size)
Georg Brandlab91fde2009-08-13 08:51:18 +0000222 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000223 self.assertEquals(len(s), size)
224 self.assertEquals(s.strip(), SUBSTR.strip())
225
226 @bigmemtest(minsize=_2G + 10, memuse=2)
227 def test_lower(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000228 _ = self.from_latin1
229 s = _('A') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000230 s = s.lower()
231 self.assertEquals(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000232 self.assertEquals(s.count(_('a')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000233
234 @bigmemtest(minsize=_2G + 10, memuse=1)
235 def test_lstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000236 _ = self.from_latin1
237 SUBSTR = _('abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000238 s = SUBSTR.rjust(size)
239 self.assertEquals(len(s), size)
240 self.assertEquals(s.lstrip(), SUBSTR.lstrip())
241 del s
242 s = SUBSTR.ljust(size)
243 self.assertEquals(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000244 # Type-specific optimization
245 if isinstance(s, (str, bytes)):
246 stripped = s.lstrip()
Georg Brandlab91fde2009-08-13 08:51:18 +0000247 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000248
249 @bigmemtest(minsize=_2G + 10, memuse=2)
250 def test_replace(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000251 _ = self.from_latin1
252 replacement = _('a')
253 s = _(' ') * size
254 s = s.replace(_(' '), replacement)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 self.assertEquals(len(s), size)
256 self.assertEquals(s.count(replacement), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000257 s = s.replace(replacement, _(' '), size - 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 self.assertEquals(len(s), size)
259 self.assertEquals(s.count(replacement), 4)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000260 self.assertEquals(s[-10:], _(' aaaa'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000261
262 @bigmemtest(minsize=_2G, memuse=2)
263 def test_rfind(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000264 _ = self.from_latin1
265 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000266 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000267 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
268 self.assertEquals(s.rfind(_(' ')), sublen + size + SUBSTR.rfind(_(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269 self.assertEquals(s.rfind(SUBSTR), sublen + size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000270 self.assertEquals(s.rfind(_(' '), 0, size), SUBSTR.rfind(_(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000271 self.assertEquals(s.rfind(SUBSTR, 0, sublen + size), 0)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000272 self.assertEquals(s.rfind(_('i')), sublen + size + SUBSTR.rfind(_('i')))
273 self.assertEquals(s.rfind(_('i'), 0, sublen), SUBSTR.rfind(_('i')))
274 self.assertEquals(s.rfind(_('i'), 0, sublen + size),
275 SUBSTR.rfind(_('i')))
276 self.assertEquals(s.rfind(_('j')), -1)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000277
278 @bigmemtest(minsize=_2G, memuse=2)
279 def test_rindex(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000280 _ = self.from_latin1
281 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000282 sublen = len(SUBSTR)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000283 s = _('').join([SUBSTR, _('-') * size, SUBSTR])
284 self.assertEquals(s.rindex(_(' ')),
285 sublen + size + SUBSTR.rindex(_(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000286 self.assertEquals(s.rindex(SUBSTR), sublen + size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000287 self.assertEquals(s.rindex(_(' '), 0, sublen + size - 1),
288 SUBSTR.rindex(_(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289 self.assertEquals(s.rindex(SUBSTR, 0, sublen + size), 0)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000290 self.assertEquals(s.rindex(_('i')),
291 sublen + size + SUBSTR.rindex(_('i')))
292 self.assertEquals(s.rindex(_('i'), 0, sublen), SUBSTR.rindex(_('i')))
293 self.assertEquals(s.rindex(_('i'), 0, sublen + size),
294 SUBSTR.rindex(_('i')))
295 self.assertRaises(ValueError, s.rindex, _('j'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000296
297 @bigmemtest(minsize=_2G + 10, memuse=1)
298 def test_rjust(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000299 _ = self.from_latin1
300 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000301 s = SUBSTR.ljust(size)
Georg Brandlab91fde2009-08-13 08:51:18 +0000302 self.assertTrue(s.startswith(SUBSTR + _(' ')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000303 self.assertEquals(len(s), size)
304 self.assertEquals(s.strip(), SUBSTR.strip())
305
306 @bigmemtest(minsize=_2G + 10, memuse=1)
307 def test_rstrip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000308 _ = self.from_latin1
309 SUBSTR = _(' abc def ghi')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000310 s = SUBSTR.ljust(size)
311 self.assertEquals(len(s), size)
312 self.assertEquals(s.rstrip(), SUBSTR.rstrip())
313 del s
314 s = SUBSTR.rjust(size)
315 self.assertEquals(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000316 # Type-specific optimization
317 if isinstance(s, (str, bytes)):
318 stripped = s.rstrip()
Georg Brandlab91fde2009-08-13 08:51:18 +0000319 self.assertTrue(stripped is s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320
321 # The test takes about size bytes to build a string, and then about
322 # sqrt(size) substrings of sqrt(size) in size and a list to
323 # hold sqrt(size) items. It's close but just over 2x size.
324 @bigmemtest(minsize=_2G, memuse=2.1)
325 def test_split_small(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000326 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000327 # Crudely calculate an estimate so that the result of s.split won't
328 # take up an inordinate amount of memory
329 chunksize = int(size ** 0.5 + 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000330 SUBSTR = _('a') + _(' ') * chunksize
Thomas Wouters477c8d52006-05-27 19:21:47 +0000331 s = SUBSTR * chunksize
332 l = s.split()
333 self.assertEquals(len(l), chunksize)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000334 expected = _('a')
335 for item in l:
336 self.assertEquals(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000337 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000338 l = s.split(_('a'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000339 self.assertEquals(len(l), chunksize + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000340 expected = _(' ') * chunksize
341 for item in filter(None, l):
342 self.assertEquals(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000343
344 # Allocates a string of twice size (and briefly two) and a list of
345 # size. Because of internal affairs, the s.split() call produces a
346 # list of size times the same one-character string, so we only
347 # suffer for the list size. (Otherwise, it'd cost another 48 times
348 # size in bytes!) Nevertheless, a list of size takes
349 # 8*size bytes.
350 @bigmemtest(minsize=_2G + 5, memuse=10)
351 def test_split_large(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000352 _ = self.from_latin1
353 s = _(' a') * size + _(' ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000354 l = s.split()
355 self.assertEquals(len(l), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000356 self.assertEquals(set(l), set([_('a')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000357 del l
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000358 l = s.split(_('a'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000359 self.assertEquals(len(l), size + 1)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000360 self.assertEquals(set(l), set([_(' ')]))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361
362 @bigmemtest(minsize=_2G, memuse=2.1)
363 def test_splitlines(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000364 _ = self.from_latin1
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 # Crudely calculate an estimate so that the result of s.split won't
366 # take up an inordinate amount of memory
367 chunksize = int(size ** 0.5 + 2) // 2
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000368 SUBSTR = _(' ') * chunksize + _('\n') + _(' ') * chunksize + _('\r\n')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000369 s = SUBSTR * chunksize
370 l = s.splitlines()
371 self.assertEquals(len(l), chunksize * 2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000372 expected = _(' ') * chunksize
373 for item in l:
374 self.assertEquals(item, expected)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375
376 @bigmemtest(minsize=_2G, memuse=2)
377 def test_startswith(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000378 _ = self.from_latin1
379 SUBSTR = _(' abc def ghi')
380 s = _('-') * size + SUBSTR
Georg Brandlab91fde2009-08-13 08:51:18 +0000381 self.assertTrue(s.startswith(s))
382 self.assertTrue(s.startswith(_('-') * size))
383 self.assertFalse(s.startswith(SUBSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384
385 @bigmemtest(minsize=_2G, memuse=1)
386 def test_strip(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000387 _ = self.from_latin1
388 SUBSTR = _(' abc def ghi ')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000389 s = SUBSTR.rjust(size)
390 self.assertEquals(len(s), size)
391 self.assertEquals(s.strip(), SUBSTR.strip())
392 del s
393 s = SUBSTR.ljust(size)
394 self.assertEquals(len(s), size)
395 self.assertEquals(s.strip(), SUBSTR.strip())
396
397 @bigmemtest(minsize=_2G, memuse=2)
398 def test_swapcase(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000399 _ = self.from_latin1
400 SUBSTR = _("aBcDeFG12.'\xa9\x00")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000401 sublen = len(SUBSTR)
402 repeats = size // sublen + 2
403 s = SUBSTR * repeats
404 s = s.swapcase()
405 self.assertEquals(len(s), sublen * repeats)
406 self.assertEquals(s[:sublen * 3], SUBSTR.swapcase() * 3)
407 self.assertEquals(s[-sublen * 3:], SUBSTR.swapcase() * 3)
408
409 @bigmemtest(minsize=_2G, memuse=2)
410 def test_title(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000411 _ = self.from_latin1
412 SUBSTR = _('SpaaHAaaAaham')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000413 s = SUBSTR * (size // len(SUBSTR) + 2)
414 s = s.title()
Georg Brandlab91fde2009-08-13 08:51:18 +0000415 self.assertTrue(s.startswith((SUBSTR * 3).title()))
416 self.assertTrue(s.endswith(SUBSTR.lower() * 3))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000417
418 @bigmemtest(minsize=_2G, memuse=2)
419 def test_translate(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000420 _ = self.from_latin1
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000421 SUBSTR = _('aZz.z.Aaz.')
Georg Brandlabc38772009-04-12 15:51:51 +0000422 if isinstance(SUBSTR, str):
423 trans = {
424 ord(_('.')): _('-'),
425 ord(_('a')): _('!'),
426 ord(_('Z')): _('$'),
427 }
428 else:
429 trans = bytes.maketrans(b'.aZ', b'-!$')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000430 sublen = len(SUBSTR)
431 repeats = size // sublen + 2
432 s = SUBSTR * repeats
433 s = s.translate(trans)
434 self.assertEquals(len(s), repeats * sublen)
435 self.assertEquals(s[:sublen], SUBSTR.translate(trans))
436 self.assertEquals(s[-sublen:], SUBSTR.translate(trans))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000437 self.assertEquals(s.count(_('.')), 0)
438 self.assertEquals(s.count(_('!')), repeats * 2)
439 self.assertEquals(s.count(_('z')), repeats * 3)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000440
441 @bigmemtest(minsize=_2G + 5, memuse=2)
442 def test_upper(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000443 _ = self.from_latin1
444 s = _('a') * size
Thomas Wouters477c8d52006-05-27 19:21:47 +0000445 s = s.upper()
446 self.assertEquals(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000447 self.assertEquals(s.count(_('A')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000448
449 @bigmemtest(minsize=_2G + 20, memuse=1)
450 def test_zfill(self, size):
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000451 _ = self.from_latin1
452 SUBSTR = _('-568324723598234')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000453 s = SUBSTR.zfill(size)
Georg Brandlab91fde2009-08-13 08:51:18 +0000454 self.assertTrue(s.endswith(_('0') + SUBSTR[1:]))
455 self.assertTrue(s.startswith(_('-0')))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000456 self.assertEquals(len(s), size)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000457 self.assertEquals(s.count(_('0')), size - len(SUBSTR))
458
459 # This test is meaningful even with size < 2G, as long as the
460 # doubled string is > 2G (but it tests more if both are > 2G :)
461 @bigmemtest(minsize=_1G + 2, memuse=3)
462 def test_concat(self, size):
463 _ = self.from_latin1
464 s = _('.') * size
465 self.assertEquals(len(s), size)
466 s = s + s
467 self.assertEquals(len(s), size * 2)
468 self.assertEquals(s.count(_('.')), size * 2)
469
470 # This test is meaningful even with size < 2G, as long as the
471 # repeated string is > 2G (but it tests more if both are > 2G :)
472 @bigmemtest(minsize=_1G + 2, memuse=3)
473 def test_repeat(self, size):
474 _ = self.from_latin1
475 s = _('.') * size
476 self.assertEquals(len(s), size)
477 s = s * 2
478 self.assertEquals(len(s), size * 2)
479 self.assertEquals(s.count(_('.')), size * 2)
480
481 @bigmemtest(minsize=_2G + 20, memuse=2)
482 def test_slice_and_getitem(self, size):
483 _ = self.from_latin1
484 SUBSTR = _('0123456789')
485 sublen = len(SUBSTR)
486 s = SUBSTR * (size // sublen)
487 stepsize = len(s) // 100
488 stepsize = stepsize - (stepsize % sublen)
489 for i in range(0, len(s) - stepsize, stepsize):
490 self.assertEquals(s[i], SUBSTR[0])
491 self.assertEquals(s[i:i + sublen], SUBSTR)
492 self.assertEquals(s[i:i + sublen:2], SUBSTR[::2])
493 if i > 0:
494 self.assertEquals(s[i + sublen - 1:i - 1:-3],
495 SUBSTR[sublen::-3])
496 # Make sure we do some slicing and indexing near the end of the
497 # string, too.
498 self.assertEquals(s[len(s) - 1], SUBSTR[-1])
499 self.assertEquals(s[-1], SUBSTR[-1])
500 self.assertEquals(s[len(s) - 10], SUBSTR[0])
501 self.assertEquals(s[-sublen], SUBSTR[0])
502 self.assertEquals(s[len(s):], _(''))
503 self.assertEquals(s[len(s) - 1:], SUBSTR[-1:])
504 self.assertEquals(s[-1:], SUBSTR[-1:])
505 self.assertEquals(s[len(s) - sublen:], SUBSTR)
506 self.assertEquals(s[-sublen:], SUBSTR)
507 self.assertEquals(len(s[:]), len(s))
508 self.assertEquals(len(s[:len(s) - 5]), len(s) - 5)
509 self.assertEquals(len(s[5:-5]), len(s) - 10)
510
511 self.assertRaises(IndexError, operator.getitem, s, len(s))
512 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1)
513 self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31)
514
515 @bigmemtest(minsize=_2G, memuse=2)
516 def test_contains(self, size):
517 _ = self.from_latin1
518 SUBSTR = _('0123456789')
519 edge = _('-') * (size // 2)
520 s = _('').join([edge, SUBSTR, edge])
521 del edge
Georg Brandlab91fde2009-08-13 08:51:18 +0000522 self.assertTrue(SUBSTR in s)
523 self.assertFalse(SUBSTR * 2 in s)
524 self.assertTrue(_('-') in s)
525 self.assertFalse(_('a') in s)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000526 s += _('a')
Georg Brandlab91fde2009-08-13 08:51:18 +0000527 self.assertTrue(_('a') in s)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000528
529 @bigmemtest(minsize=_2G + 10, memuse=2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000530 def test_compare(self, size):
531 _ = self.from_latin1
532 s1 = _('-') * size
533 s2 = _('-') * size
534 self.assertEqual(s1, s2)
535 del s2
536 s2 = s1 + _('a')
Georg Brandlab91fde2009-08-13 08:51:18 +0000537 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000538 del s2
539 s2 = _('.') * size
Georg Brandlab91fde2009-08-13 08:51:18 +0000540 self.assertFalse(s1 == s2)
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000541
542 @bigmemtest(minsize=_2G + 10, memuse=1)
543 def test_hash(self, size):
544 # Not sure if we can do any meaningful tests here... Even if we
545 # start relying on the exact algorithm used, the result will be
546 # different depending on the size of the C 'long int'. Even this
547 # test is dodgy (there's no *guarantee* that the two things should
548 # have a different hash, even if they, in the current
549 # implementation, almost always do.)
550 _ = self.from_latin1
551 s = _('\x00') * size
552 h1 = hash(s)
553 del s
554 s = _('\x00') * (size + 1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000555 self.assertFalse(h1 == hash(s))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000556
557
558class StrTest(unittest.TestCase, BaseStrTest):
559
560 def from_latin1(self, s):
561 return s
562
563 def basic_encode_test(self, size, enc, c='.', expectedsize=None):
564 if expectedsize is None:
565 expectedsize = size
566
567 s = c * size
568 self.assertEquals(len(s.encode(enc)), expectedsize)
569
570 def setUp(self):
571 # HACK: adjust memory use of tests inherited from BaseStrTest
572 # according to character size.
573 self._adjusted = {}
574 for name in dir(BaseStrTest):
575 if not name.startswith('test_'):
576 continue
577 meth = getattr(type(self), name)
578 try:
579 memuse = meth.memuse
580 except AttributeError:
581 continue
582 meth.memuse = character_size * memuse
583 self._adjusted[name] = memuse
584
585 def tearDown(self):
586 for name, memuse in self._adjusted.items():
587 getattr(type(self), name).memuse = memuse
588
589 @bigmemtest(minsize=_2G + 2, memuse=character_size + 1)
590 def test_encode(self, size):
591 return self.basic_encode_test(size, 'utf-8')
592
593 @precisionbigmemtest(size=_4G // 6 + 2, memuse=character_size + 1)
594 def test_encode_raw_unicode_escape(self, size):
595 try:
596 return self.basic_encode_test(size, 'raw_unicode_escape')
597 except MemoryError:
598 pass # acceptable on 32-bit
599
600 @precisionbigmemtest(size=_4G // 5 + 70, memuse=character_size + 1)
601 def test_encode_utf7(self, size):
602 try:
603 return self.basic_encode_test(size, 'utf7')
604 except MemoryError:
605 pass # acceptable on 32-bit
606
607 @precisionbigmemtest(size=_4G // 4 + 5, memuse=character_size + 4)
608 def test_encode_utf32(self, size):
609 try:
610 return self.basic_encode_test(size, 'utf32', expectedsize=4*size+4)
611 except MemoryError:
612 pass # acceptable on 32-bit
613
614 @precisionbigmemtest(size=_2G - 1, memuse=character_size + 1)
615 def test_encode_ascii(self, size):
616 return self.basic_encode_test(size, 'ascii', c='A')
617
618 @precisionbigmemtest(size=_4G // 5, memuse=character_size * (6 + 1))
619 def test_unicode_repr_overflow(self, size):
620 try:
621 s = "\uAAAA"*size
622 r = repr(s)
623 except MemoryError:
624 pass # acceptable on 32-bit
625 else:
Georg Brandlab91fde2009-08-13 08:51:18 +0000626 self.assertTrue(s == eval(r))
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000627
628 @bigmemtest(minsize=_2G + 10, memuse=character_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629 def test_format(self, size):
630 s = '-' * size
631 sf = '%s' % (s,)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000632 self.assertEqual(s, sf)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000633 del sf
634 sf = '..%s..' % (s,)
635 self.assertEquals(len(sf), len(s) + 4)
Georg Brandlab91fde2009-08-13 08:51:18 +0000636 self.assertTrue(sf.startswith('..-'))
637 self.assertTrue(sf.endswith('-..'))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000638 del s, sf
639
640 size //= 2
641 edge = '-' * size
642 s = ''.join([edge, '%s', edge])
643 del edge
644 s = s % '...'
645 self.assertEquals(len(s), size * 2 + 3)
646 self.assertEquals(s.count('.'), 3)
647 self.assertEquals(s.count('-'), size * 2)
648
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000649 @bigmemtest(minsize=_2G + 10, memuse=character_size * 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650 def test_repr_small(self, size):
651 s = '-' * size
652 s = repr(s)
653 self.assertEquals(len(s), size + 2)
654 self.assertEquals(s[0], "'")
655 self.assertEquals(s[-1], "'")
656 self.assertEquals(s.count('-'), size)
657 del s
658 # repr() will create a string four times as large as this 'binary
659 # string', but we don't want to allocate much more than twice
660 # size in total. (We do extra testing in test_repr_large())
661 size = size // 5 * 2
662 s = '\x00' * size
663 s = repr(s)
664 self.assertEquals(len(s), size * 4 + 2)
665 self.assertEquals(s[0], "'")
666 self.assertEquals(s[-1], "'")
667 self.assertEquals(s.count('\\'), size)
668 self.assertEquals(s.count('0'), size * 2)
669
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000670 @bigmemtest(minsize=_2G + 10, memuse=character_size * 5)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 def test_repr_large(self, size):
672 s = '\x00' * size
673 s = repr(s)
674 self.assertEquals(len(s), size * 4 + 2)
675 self.assertEquals(s[0], "'")
676 self.assertEquals(s[-1], "'")
677 self.assertEquals(s.count('\\'), size)
678 self.assertEquals(s.count('0'), size * 2)
679
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000680 @bigmemtest(minsize=2**32 / 5, memuse=character_size * 7)
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000681 def test_unicode_repr(self, size):
682 s = "\uAAAA" * size
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000683 for f in (repr, ascii):
684 r = f(s)
Georg Brandlab91fde2009-08-13 08:51:18 +0000685 self.assertTrue(len(r) > size)
686 self.assertTrue(r.endswith(r"\uaaaa'"), r[-10:])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000687 del r
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000688
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000689 # The character takes 4 bytes even in UCS-2 builds because it will
690 # be decomposed into surrogates.
691 @bigmemtest(minsize=2**32 / 5, memuse=4 + character_size * 9)
692 def test_unicode_repr_wide(self, size):
693 s = "\U0001AAAA" * size
694 for f in (repr, ascii):
695 r = f(s)
Georg Brandlab91fde2009-08-13 08:51:18 +0000696 self.assertTrue(len(r) > size)
697 self.assertTrue(r.endswith(r"\U0001aaaa'"), r[-12:])
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000698 del r
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000701class BytesTest(unittest.TestCase, BaseStrTest):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000702
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000703 def from_latin1(self, s):
704 return s.encode("latin1")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000706 @bigmemtest(minsize=_2G + 2, memuse=1 + character_size)
707 def test_decode(self, size):
708 s = self.from_latin1('.') * size
709 self.assertEquals(len(s.decode('utf-8')), size)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710
Thomas Wouters477c8d52006-05-27 19:21:47 +0000711
Antoine Pitrou7cdb4952009-03-07 23:40:49 +0000712class BytearrayTest(unittest.TestCase, BaseStrTest):
713
714 def from_latin1(self, s):
715 return bytearray(s.encode("latin1"))
716
717 @bigmemtest(minsize=_2G + 2, memuse=1 + character_size)
718 def test_decode(self, size):
719 s = self.from_latin1('.') * size
720 self.assertEquals(len(s.decode('utf-8')), size)
721
722 test_hash = None
723 test_split_large = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724
725class TupleTest(unittest.TestCase):
726
727 # Tuples have a small, fixed-sized head and an array of pointers to
728 # data. Since we're testing 64-bit addressing, we can assume that the
729 # pointers are 8 bytes, and that thus that the tuples take up 8 bytes
730 # per size.
731
732 # As a side-effect of testing long tuples, these tests happen to test
733 # having more than 2<<31 references to any given object. Hence the
734 # use of different types of objects as contents in different tests.
735
736 @bigmemtest(minsize=_2G + 2, memuse=16)
737 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000738 t1 = ('',) * size
739 t2 = ('',) * size
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000740 self.assertEqual(t1, t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000741 del t2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000742 t2 = ('',) * (size + 1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000743 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000744 del t2
745 t2 = (1,) * size
Georg Brandlab91fde2009-08-13 08:51:18 +0000746 self.assertFalse(t1 == t2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000747
748 # Test concatenating into a single tuple of more than 2G in length,
749 # and concatenating a tuple of more than 2G in length separately, so
750 # the smaller test still gets run even if there isn't memory for the
751 # larger test (but we still let the tester know the larger test is
752 # skipped, in verbose mode.)
753 def basic_concat_test(self, size):
754 t = ((),) * size
755 self.assertEquals(len(t), size)
756 t = t + t
757 self.assertEquals(len(t), size * 2)
758
759 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
760 def test_concat_small(self, size):
761 return self.basic_concat_test(size)
762
763 @bigmemtest(minsize=_2G + 2, memuse=24)
764 def test_concat_large(self, size):
765 return self.basic_concat_test(size)
766
767 @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5)
768 def test_contains(self, size):
769 t = (1, 2, 3, 4, 5) * size
770 self.assertEquals(len(t), size * 5)
Georg Brandlab91fde2009-08-13 08:51:18 +0000771 self.assertTrue(5 in t)
772 self.assertFalse((1, 2, 3, 4, 5) in t)
773 self.assertFalse(0 in t)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000774
775 @bigmemtest(minsize=_2G + 10, memuse=8)
776 def test_hash(self, size):
777 t1 = (0,) * size
778 h1 = hash(t1)
779 del t1
780 t2 = (0,) * (size + 1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000781 self.assertFalse(h1 == hash(t2))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000782
783 @bigmemtest(minsize=_2G + 10, memuse=8)
784 def test_index_and_slice(self, size):
785 t = (None,) * size
786 self.assertEquals(len(t), size)
787 self.assertEquals(t[-1], None)
788 self.assertEquals(t[5], None)
789 self.assertEquals(t[size - 1], None)
790 self.assertRaises(IndexError, operator.getitem, t, size)
791 self.assertEquals(t[:5], (None,) * 5)
792 self.assertEquals(t[-5:], (None,) * 5)
793 self.assertEquals(t[20:25], (None,) * 5)
794 self.assertEquals(t[-25:-20], (None,) * 5)
795 self.assertEquals(t[size - 5:], (None,) * 5)
796 self.assertEquals(t[size - 5:size], (None,) * 5)
797 self.assertEquals(t[size - 6:size - 2], (None,) * 4)
798 self.assertEquals(t[size:size], ())
799 self.assertEquals(t[size:size+5], ())
800
801 # Like test_concat, split in two.
802 def basic_test_repeat(self, size):
803 t = ('',) * size
804 self.assertEquals(len(t), size)
805 t = t * 2
806 self.assertEquals(len(t), size * 2)
807
808 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
809 def test_repeat_small(self, size):
810 return self.basic_test_repeat(size)
811
812 @bigmemtest(minsize=_2G + 2, memuse=24)
813 def test_repeat_large(self, size):
814 return self.basic_test_repeat(size)
815
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000816 @bigmemtest(minsize=_1G - 1, memuse=12)
817 def test_repeat_large_2(self, size):
818 return self.basic_test_repeat(size)
819
820 @precisionbigmemtest(size=_1G - 1, memuse=9)
821 def test_from_2G_generator(self, size):
822 try:
823 t = tuple(range(size))
824 except MemoryError:
825 pass # acceptable on 32-bit
826 else:
827 count = 0
828 for item in t:
829 self.assertEquals(item, count)
830 count += 1
831 self.assertEquals(count, size)
832
833 @precisionbigmemtest(size=_1G - 25, memuse=9)
834 def test_from_almost_2G_generator(self, size):
835 try:
836 t = tuple(range(size))
837 count = 0
838 for item in t:
839 self.assertEquals(item, count)
840 count += 1
841 self.assertEquals(count, size)
842 except MemoryError:
843 pass # acceptable, expected on 32-bit
844
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845 # Like test_concat, split in two.
846 def basic_test_repr(self, size):
847 t = (0,) * size
848 s = repr(t)
849 # The repr of a tuple of 0's is exactly three times the tuple length.
850 self.assertEquals(len(s), size * 3)
851 self.assertEquals(s[:5], '(0, 0')
852 self.assertEquals(s[-5:], '0, 0)')
853 self.assertEquals(s.count('0'), size)
854
855 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3)
856 def test_repr_small(self, size):
857 return self.basic_test_repr(size)
858
859 @bigmemtest(minsize=_2G + 2, memuse=8 + 3)
860 def test_repr_large(self, size):
861 return self.basic_test_repr(size)
862
863class ListTest(unittest.TestCase):
864
865 # Like tuples, lists have a small, fixed-sized head and an array of
866 # pointers to data, so 8 bytes per size. Also like tuples, we make the
867 # lists hold references to various objects to test their refcount
868 # limits.
869
870 @bigmemtest(minsize=_2G + 2, memuse=16)
871 def test_compare(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000872 l1 = [''] * size
873 l2 = [''] * size
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000874 self.assertEqual(l1, l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875 del l2
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000876 l2 = [''] * (size + 1)
Georg Brandlab91fde2009-08-13 08:51:18 +0000877 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 del l2
879 l2 = [2] * size
Georg Brandlab91fde2009-08-13 08:51:18 +0000880 self.assertFalse(l1 == l2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000881
882 # Test concatenating into a single list of more than 2G in length,
883 # and concatenating a list of more than 2G in length separately, so
884 # the smaller test still gets run even if there isn't memory for the
885 # larger test (but we still let the tester know the larger test is
886 # skipped, in verbose mode.)
887 def basic_test_concat(self, size):
888 l = [[]] * size
889 self.assertEquals(len(l), size)
890 l = l + l
891 self.assertEquals(len(l), size * 2)
892
893 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
894 def test_concat_small(self, size):
895 return self.basic_test_concat(size)
896
897 @bigmemtest(minsize=_2G + 2, memuse=24)
898 def test_concat_large(self, size):
899 return self.basic_test_concat(size)
900
901 def basic_test_inplace_concat(self, size):
902 l = [sys.stdout] * size
903 l += l
904 self.assertEquals(len(l), size * 2)
Georg Brandlab91fde2009-08-13 08:51:18 +0000905 self.assertTrue(l[0] is l[-1])
906 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907
908 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
909 def test_inplace_concat_small(self, size):
910 return self.basic_test_inplace_concat(size)
911
912 @bigmemtest(minsize=_2G + 2, memuse=24)
913 def test_inplace_concat_large(self, size):
914 return self.basic_test_inplace_concat(size)
915
916 @bigmemtest(minsize=_2G // 5 + 10, memuse=8 * 5)
917 def test_contains(self, size):
918 l = [1, 2, 3, 4, 5] * size
919 self.assertEquals(len(l), size * 5)
Georg Brandlab91fde2009-08-13 08:51:18 +0000920 self.assertTrue(5 in l)
921 self.assertFalse([1, 2, 3, 4, 5] in l)
922 self.assertFalse(0 in l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923
924 @bigmemtest(minsize=_2G + 10, memuse=8)
925 def test_hash(self, size):
926 l = [0] * size
Georg Brandlab91fde2009-08-13 08:51:18 +0000927 self.assertRaises(TypeError, hash, l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928
929 @bigmemtest(minsize=_2G + 10, memuse=8)
930 def test_index_and_slice(self, size):
931 l = [None] * size
932 self.assertEquals(len(l), size)
933 self.assertEquals(l[-1], None)
934 self.assertEquals(l[5], None)
935 self.assertEquals(l[size - 1], None)
936 self.assertRaises(IndexError, operator.getitem, l, size)
937 self.assertEquals(l[:5], [None] * 5)
938 self.assertEquals(l[-5:], [None] * 5)
939 self.assertEquals(l[20:25], [None] * 5)
940 self.assertEquals(l[-25:-20], [None] * 5)
941 self.assertEquals(l[size - 5:], [None] * 5)
942 self.assertEquals(l[size - 5:size], [None] * 5)
943 self.assertEquals(l[size - 6:size - 2], [None] * 4)
944 self.assertEquals(l[size:size], [])
945 self.assertEquals(l[size:size+5], [])
946
947 l[size - 2] = 5
948 self.assertEquals(len(l), size)
949 self.assertEquals(l[-3:], [None, 5, None])
950 self.assertEquals(l.count(5), 1)
951 self.assertRaises(IndexError, operator.setitem, l, size, 6)
952 self.assertEquals(len(l), size)
953
954 l[size - 7:] = [1, 2, 3, 4, 5]
955 size -= 2
956 self.assertEquals(len(l), size)
957 self.assertEquals(l[-7:], [None, None, 1, 2, 3, 4, 5])
958
959 l[:7] = [1, 2, 3, 4, 5]
960 size -= 2
961 self.assertEquals(len(l), size)
962 self.assertEquals(l[:7], [1, 2, 3, 4, 5, None, None])
963
964 del l[size - 1]
965 size -= 1
966 self.assertEquals(len(l), size)
967 self.assertEquals(l[-1], 4)
968
969 del l[-2:]
970 size -= 2
971 self.assertEquals(len(l), size)
972 self.assertEquals(l[-1], 2)
973
974 del l[0]
975 size -= 1
976 self.assertEquals(len(l), size)
977 self.assertEquals(l[0], 2)
978
979 del l[:2]
980 size -= 2
981 self.assertEquals(len(l), size)
982 self.assertEquals(l[0], 4)
983
984 # Like test_concat, split in two.
985 def basic_test_repeat(self, size):
986 l = [] * size
Georg Brandlab91fde2009-08-13 08:51:18 +0000987 self.assertFalse(l)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988 l = [''] * size
989 self.assertEquals(len(l), size)
990 l = l * 2
991 self.assertEquals(len(l), size * 2)
992
993 @bigmemtest(minsize=_2G // 2 + 2, memuse=24)
994 def test_repeat_small(self, size):
995 return self.basic_test_repeat(size)
996
997 @bigmemtest(minsize=_2G + 2, memuse=24)
998 def test_repeat_large(self, size):
999 return self.basic_test_repeat(size)
1000
1001 def basic_test_inplace_repeat(self, size):
1002 l = ['']
1003 l *= size
1004 self.assertEquals(len(l), size)
Georg Brandlab91fde2009-08-13 08:51:18 +00001005 self.assertTrue(l[0] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006 del l
1007
1008 l = [''] * size
1009 l *= 2
1010 self.assertEquals(len(l), size * 2)
Georg Brandlab91fde2009-08-13 08:51:18 +00001011 self.assertTrue(l[size - 1] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012
1013 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
1014 def test_inplace_repeat_small(self, size):
1015 return self.basic_test_inplace_repeat(size)
1016
1017 @bigmemtest(minsize=_2G + 2, memuse=16)
1018 def test_inplace_repeat_large(self, size):
1019 return self.basic_test_inplace_repeat(size)
1020
1021 def basic_test_repr(self, size):
1022 l = [0] * size
1023 s = repr(l)
1024 # The repr of a list of 0's is exactly three times the list length.
1025 self.assertEquals(len(s), size * 3)
1026 self.assertEquals(s[:5], '[0, 0')
1027 self.assertEquals(s[-5:], '0, 0]')
1028 self.assertEquals(s.count('0'), size)
1029
1030 @bigmemtest(minsize=_2G // 3 + 2, memuse=8 + 3)
1031 def test_repr_small(self, size):
1032 return self.basic_test_repr(size)
1033
1034 @bigmemtest(minsize=_2G + 2, memuse=8 + 3)
1035 def test_repr_large(self, size):
1036 return self.basic_test_repr(size)
1037
1038 # list overallocates ~1/8th of the total size (on first expansion) so
1039 # the single list.append call puts memuse at 9 bytes per size.
1040 @bigmemtest(minsize=_2G, memuse=9)
1041 def test_append(self, size):
1042 l = [object()] * size
1043 l.append(object())
1044 self.assertEquals(len(l), size+1)
Georg Brandlab91fde2009-08-13 08:51:18 +00001045 self.assertTrue(l[-3] is l[-2])
1046 self.assertFalse(l[-2] is l[-1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047
1048 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1049 def test_count(self, size):
1050 l = [1, 2, 3, 4, 5] * size
1051 self.assertEquals(l.count(1), size)
1052 self.assertEquals(l.count("1"), 0)
1053
1054 def basic_test_extend(self, size):
Guido van Rossuma5d0c262007-07-12 08:11:23 +00001055 l = [object] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056 l.extend(l)
1057 self.assertEquals(len(l), size * 2)
Georg Brandlab91fde2009-08-13 08:51:18 +00001058 self.assertTrue(l[0] is l[-1])
1059 self.assertTrue(l[size - 1] is l[size + 1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060
1061 @bigmemtest(minsize=_2G // 2 + 2, memuse=16)
1062 def test_extend_small(self, size):
1063 return self.basic_test_extend(size)
1064
1065 @bigmemtest(minsize=_2G + 2, memuse=16)
1066 def test_extend_large(self, size):
1067 return self.basic_test_extend(size)
1068
1069 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1070 def test_index(self, size):
Guido van Rossume2a383d2007-01-15 16:59:06 +00001071 l = [1, 2, 3, 4, 5] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072 size *= 5
1073 self.assertEquals(l.index(1), 0)
1074 self.assertEquals(l.index(5, size - 5), size - 1)
1075 self.assertEquals(l.index(5, size - 5, size), size - 1)
1076 self.assertRaises(ValueError, l.index, 1, size - 4, size)
Guido van Rossume2a383d2007-01-15 16:59:06 +00001077 self.assertRaises(ValueError, l.index, 6)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078
1079 # This tests suffers from overallocation, just like test_append.
1080 @bigmemtest(minsize=_2G + 10, memuse=9)
1081 def test_insert(self, size):
1082 l = [1.0] * size
1083 l.insert(size - 1, "A")
1084 size += 1
1085 self.assertEquals(len(l), size)
1086 self.assertEquals(l[-3:], [1.0, "A", 1.0])
1087
1088 l.insert(size + 1, "B")
1089 size += 1
1090 self.assertEquals(len(l), size)
1091 self.assertEquals(l[-3:], ["A", 1.0, "B"])
1092
1093 l.insert(1, "C")
1094 size += 1
1095 self.assertEquals(len(l), size)
1096 self.assertEquals(l[:3], [1.0, "C", 1.0])
1097 self.assertEquals(l[size - 3:], ["A", 1.0, "B"])
1098
1099 @bigmemtest(minsize=_2G // 5 + 4, memuse=8 * 5)
1100 def test_pop(self, size):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001101 l = ["a", "b", "c", "d", "e"] * size
Thomas Wouters477c8d52006-05-27 19:21:47 +00001102 size *= 5
1103 self.assertEquals(len(l), size)
1104
1105 item = l.pop()
1106 size -= 1
1107 self.assertEquals(len(l), size)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001108 self.assertEquals(item, "e")
1109 self.assertEquals(l[-2:], ["c", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110
1111 item = l.pop(0)
1112 size -= 1
1113 self.assertEquals(len(l), size)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001114 self.assertEquals(item, "a")
1115 self.assertEquals(l[:2], ["b", "c"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001116
1117 item = l.pop(size - 2)
1118 size -= 1
1119 self.assertEquals(len(l), size)
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001120 self.assertEquals(item, "c")
1121 self.assertEquals(l[-2:], ["b", "d"])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001122
1123 @bigmemtest(minsize=_2G + 10, memuse=8)
1124 def test_remove(self, size):
1125 l = [10] * size
1126 self.assertEquals(len(l), size)
1127
1128 l.remove(10)
1129 size -= 1
1130 self.assertEquals(len(l), size)
1131
1132 # Because of the earlier l.remove(), this append doesn't trigger
1133 # a resize.
1134 l.append(5)
1135 size += 1
1136 self.assertEquals(len(l), size)
1137 self.assertEquals(l[-2:], [10, 5])
1138 l.remove(5)
1139 size -= 1
1140 self.assertEquals(len(l), size)
1141 self.assertEquals(l[-2:], [10, 10])
1142
1143 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1144 def test_reverse(self, size):
1145 l = [1, 2, 3, 4, 5] * size
1146 l.reverse()
1147 self.assertEquals(len(l), size * 5)
1148 self.assertEquals(l[-5:], [5, 4, 3, 2, 1])
1149 self.assertEquals(l[:5], [5, 4, 3, 2, 1])
1150
1151 @bigmemtest(minsize=_2G // 5 + 2, memuse=8 * 5)
1152 def test_sort(self, size):
1153 l = [1, 2, 3, 4, 5] * size
1154 l.sort()
1155 self.assertEquals(len(l), size * 5)
1156 self.assertEquals(l.count(1), size)
1157 self.assertEquals(l[:10], [1] * 10)
1158 self.assertEquals(l[-10:], [5] * 10)
1159
1160def test_main():
Antoine Pitrou7cdb4952009-03-07 23:40:49 +00001161 support.run_unittest(StrTest, BytesTest, BytearrayTest,
1162 TupleTest, ListTest)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001163
1164if __name__ == '__main__':
1165 if len(sys.argv) > 1:
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001166 support.set_memlimit(sys.argv[1])
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167 test_main()