1 | n/a | """Bigmem tests - tests for the 32-bit boundary in containers. |
---|
2 | n/a | |
---|
3 | n/a | These tests try to exercise the 32-bit boundary that is sometimes, if |
---|
4 | n/a | rarely, exceeded in practice, but almost never tested. They are really only |
---|
5 | n/a | meaningful on 64-bit builds on machines with a *lot* of memory, but the |
---|
6 | n/a | tests are always run, usually with very low memory limits to make sure the |
---|
7 | n/a | tests themselves don't suffer from bitrot. To run them for real, pass a |
---|
8 | n/a | high memory limit to regrtest, with the -M option. |
---|
9 | n/a | """ |
---|
10 | n/a | |
---|
11 | n/a | from test import support |
---|
12 | n/a | from test.support import bigmemtest, _1G, _2G, _4G |
---|
13 | n/a | |
---|
14 | n/a | import unittest |
---|
15 | n/a | import operator |
---|
16 | n/a | import sys |
---|
17 | n/a | |
---|
18 | n/a | # These tests all use one of the bigmemtest decorators to indicate how much |
---|
19 | n/a | # memory they use and how much memory they need to be even meaningful. The |
---|
20 | n/a | # decorators take two arguments: a 'memuse' indicator declaring |
---|
21 | n/a | # (approximate) bytes per size-unit the test will use (at peak usage), and a |
---|
22 | n/a | # 'minsize' indicator declaring a minimum *useful* size. A test that |
---|
23 | n/a | # allocates a bytestring to test various operations near the end will have a |
---|
24 | n/a | # minsize of at least 2Gb (or it wouldn't reach the 32-bit limit, so the |
---|
25 | n/a | # test wouldn't be very useful) and a memuse of 1 (one byte per size-unit, |
---|
26 | n/a | # if it allocates only one big string at a time.) |
---|
27 | n/a | # |
---|
28 | n/a | # When run with a memory limit set, both decorators skip tests that need |
---|
29 | n/a | # more memory than available to be meaningful. The precisionbigmemtest will |
---|
30 | n/a | # always pass minsize as size, even if there is much more memory available. |
---|
31 | n/a | # The bigmemtest decorator will scale size upward to fill available memory. |
---|
32 | n/a | # |
---|
33 | n/a | # Bigmem testing houserules: |
---|
34 | n/a | # |
---|
35 | n/a | # - Try not to allocate too many large objects. It's okay to rely on |
---|
36 | n/a | # refcounting semantics, and don't forget that 's = create_largestring()' |
---|
37 | n/a | # doesn't release the old 's' (if it exists) until well after its new |
---|
38 | n/a | # value has been created. Use 'del s' before the create_largestring call. |
---|
39 | n/a | # |
---|
40 | n/a | # - Do *not* compare large objects using assertEqual, assertIn or similar. |
---|
41 | n/a | # It's a lengthy operation and the errormessage will be utterly useless |
---|
42 | n/a | # due to its size. To make sure whether a result has the right contents, |
---|
43 | n/a | # better to use the strip or count methods, or compare meaningful slices. |
---|
44 | n/a | # |
---|
45 | n/a | # - Don't forget to test for large indices, offsets and results and such, |
---|
46 | n/a | # in addition to large sizes. Anything that probes the 32-bit boundary. |
---|
47 | n/a | # |
---|
48 | n/a | # - When repeating an object (say, a substring, or a small list) to create |
---|
49 | n/a | # a large object, make the subobject of a length that is not a power of |
---|
50 | n/a | # 2. That way, int-wrapping problems are more easily detected. |
---|
51 | n/a | # |
---|
52 | n/a | # - Despite the bigmemtest decorator, all tests will actually be called |
---|
53 | n/a | # with a much smaller number too, in the normal test run (5Kb currently.) |
---|
54 | n/a | # This is so the tests themselves get frequent testing. |
---|
55 | n/a | # Consequently, always make all large allocations based on the |
---|
56 | n/a | # passed-in 'size', and don't rely on the size being very large. Also, |
---|
57 | n/a | # memuse-per-size should remain sane (less than a few thousand); if your |
---|
58 | n/a | # test uses more, adjust 'size' upward, instead. |
---|
59 | n/a | |
---|
60 | n/a | # BEWARE: it seems that one failing test can yield other subsequent tests to |
---|
61 | n/a | # fail as well. I do not know whether it is due to memory fragmentation |
---|
62 | n/a | # issues, or other specifics of the platform malloc() routine. |
---|
63 | n/a | |
---|
64 | n/a | ascii_char_size = 1 |
---|
65 | n/a | ucs2_char_size = 2 |
---|
66 | n/a | ucs4_char_size = 4 |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | class BaseStrTest: |
---|
70 | n/a | |
---|
71 | n/a | def _test_capitalize(self, size): |
---|
72 | n/a | _ = self.from_latin1 |
---|
73 | n/a | SUBSTR = self.from_latin1(' abc def ghi') |
---|
74 | n/a | s = _('-') * size + SUBSTR |
---|
75 | n/a | caps = s.capitalize() |
---|
76 | n/a | self.assertEqual(caps[-len(SUBSTR):], |
---|
77 | n/a | SUBSTR.capitalize()) |
---|
78 | n/a | self.assertEqual(caps.lstrip(_('-')), SUBSTR) |
---|
79 | n/a | |
---|
80 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
81 | n/a | def test_center(self, size): |
---|
82 | n/a | SUBSTR = self.from_latin1(' abc def ghi') |
---|
83 | n/a | s = SUBSTR.center(size) |
---|
84 | n/a | self.assertEqual(len(s), size) |
---|
85 | n/a | lpadsize = rpadsize = (len(s) - len(SUBSTR)) // 2 |
---|
86 | n/a | if len(s) % 2: |
---|
87 | n/a | lpadsize += 1 |
---|
88 | n/a | self.assertEqual(s[lpadsize:-rpadsize], SUBSTR) |
---|
89 | n/a | self.assertEqual(s.strip(), SUBSTR.strip()) |
---|
90 | n/a | |
---|
91 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
92 | n/a | def test_count(self, size): |
---|
93 | n/a | _ = self.from_latin1 |
---|
94 | n/a | SUBSTR = _(' abc def ghi') |
---|
95 | n/a | s = _('.') * size + SUBSTR |
---|
96 | n/a | self.assertEqual(s.count(_('.')), size) |
---|
97 | n/a | s += _('.') |
---|
98 | n/a | self.assertEqual(s.count(_('.')), size + 1) |
---|
99 | n/a | self.assertEqual(s.count(_(' ')), 3) |
---|
100 | n/a | self.assertEqual(s.count(_('i')), 1) |
---|
101 | n/a | self.assertEqual(s.count(_('j')), 0) |
---|
102 | n/a | |
---|
103 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
104 | n/a | def test_endswith(self, size): |
---|
105 | n/a | _ = self.from_latin1 |
---|
106 | n/a | SUBSTR = _(' abc def ghi') |
---|
107 | n/a | s = _('-') * size + SUBSTR |
---|
108 | n/a | self.assertTrue(s.endswith(SUBSTR)) |
---|
109 | n/a | self.assertTrue(s.endswith(s)) |
---|
110 | n/a | s2 = _('...') + s |
---|
111 | n/a | self.assertTrue(s2.endswith(s)) |
---|
112 | n/a | self.assertFalse(s.endswith(_('a') + SUBSTR)) |
---|
113 | n/a | self.assertFalse(SUBSTR.endswith(s)) |
---|
114 | n/a | |
---|
115 | n/a | @bigmemtest(size=_2G + 10, memuse=2) |
---|
116 | n/a | def test_expandtabs(self, size): |
---|
117 | n/a | _ = self.from_latin1 |
---|
118 | n/a | s = _('-') * size |
---|
119 | n/a | tabsize = 8 |
---|
120 | n/a | self.assertTrue(s.expandtabs() == s) |
---|
121 | n/a | del s |
---|
122 | n/a | slen, remainder = divmod(size, tabsize) |
---|
123 | n/a | s = _(' \t') * slen |
---|
124 | n/a | s = s.expandtabs(tabsize) |
---|
125 | n/a | self.assertEqual(len(s), size - remainder) |
---|
126 | n/a | self.assertEqual(len(s.strip(_(' '))), 0) |
---|
127 | n/a | |
---|
128 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
129 | n/a | def test_find(self, size): |
---|
130 | n/a | _ = self.from_latin1 |
---|
131 | n/a | SUBSTR = _(' abc def ghi') |
---|
132 | n/a | sublen = len(SUBSTR) |
---|
133 | n/a | s = _('').join([SUBSTR, _('-') * size, SUBSTR]) |
---|
134 | n/a | self.assertEqual(s.find(_(' ')), 0) |
---|
135 | n/a | self.assertEqual(s.find(SUBSTR), 0) |
---|
136 | n/a | self.assertEqual(s.find(_(' '), sublen), sublen + size) |
---|
137 | n/a | self.assertEqual(s.find(SUBSTR, len(SUBSTR)), sublen + size) |
---|
138 | n/a | self.assertEqual(s.find(_('i')), SUBSTR.find(_('i'))) |
---|
139 | n/a | self.assertEqual(s.find(_('i'), sublen), |
---|
140 | n/a | sublen + size + SUBSTR.find(_('i'))) |
---|
141 | n/a | self.assertEqual(s.find(_('i'), size), |
---|
142 | n/a | sublen + size + SUBSTR.find(_('i'))) |
---|
143 | n/a | self.assertEqual(s.find(_('j')), -1) |
---|
144 | n/a | |
---|
145 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
146 | n/a | def test_index(self, size): |
---|
147 | n/a | _ = self.from_latin1 |
---|
148 | n/a | SUBSTR = _(' abc def ghi') |
---|
149 | n/a | sublen = len(SUBSTR) |
---|
150 | n/a | s = _('').join([SUBSTR, _('-') * size, SUBSTR]) |
---|
151 | n/a | self.assertEqual(s.index(_(' ')), 0) |
---|
152 | n/a | self.assertEqual(s.index(SUBSTR), 0) |
---|
153 | n/a | self.assertEqual(s.index(_(' '), sublen), sublen + size) |
---|
154 | n/a | self.assertEqual(s.index(SUBSTR, sublen), sublen + size) |
---|
155 | n/a | self.assertEqual(s.index(_('i')), SUBSTR.index(_('i'))) |
---|
156 | n/a | self.assertEqual(s.index(_('i'), sublen), |
---|
157 | n/a | sublen + size + SUBSTR.index(_('i'))) |
---|
158 | n/a | self.assertEqual(s.index(_('i'), size), |
---|
159 | n/a | sublen + size + SUBSTR.index(_('i'))) |
---|
160 | n/a | self.assertRaises(ValueError, s.index, _('j')) |
---|
161 | n/a | |
---|
162 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
163 | n/a | def test_isalnum(self, size): |
---|
164 | n/a | _ = self.from_latin1 |
---|
165 | n/a | SUBSTR = _('123456') |
---|
166 | n/a | s = _('a') * size + SUBSTR |
---|
167 | n/a | self.assertTrue(s.isalnum()) |
---|
168 | n/a | s += _('.') |
---|
169 | n/a | self.assertFalse(s.isalnum()) |
---|
170 | n/a | |
---|
171 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
172 | n/a | def test_isalpha(self, size): |
---|
173 | n/a | _ = self.from_latin1 |
---|
174 | n/a | SUBSTR = _('zzzzzzz') |
---|
175 | n/a | s = _('a') * size + SUBSTR |
---|
176 | n/a | self.assertTrue(s.isalpha()) |
---|
177 | n/a | s += _('.') |
---|
178 | n/a | self.assertFalse(s.isalpha()) |
---|
179 | n/a | |
---|
180 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
181 | n/a | def test_isdigit(self, size): |
---|
182 | n/a | _ = self.from_latin1 |
---|
183 | n/a | SUBSTR = _('123456') |
---|
184 | n/a | s = _('9') * size + SUBSTR |
---|
185 | n/a | self.assertTrue(s.isdigit()) |
---|
186 | n/a | s += _('z') |
---|
187 | n/a | self.assertFalse(s.isdigit()) |
---|
188 | n/a | |
---|
189 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
190 | n/a | def test_islower(self, size): |
---|
191 | n/a | _ = self.from_latin1 |
---|
192 | n/a | chars = _(''.join( |
---|
193 | n/a | chr(c) for c in range(255) if not chr(c).isupper())) |
---|
194 | n/a | repeats = size // len(chars) + 2 |
---|
195 | n/a | s = chars * repeats |
---|
196 | n/a | self.assertTrue(s.islower()) |
---|
197 | n/a | s += _('A') |
---|
198 | n/a | self.assertFalse(s.islower()) |
---|
199 | n/a | |
---|
200 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
201 | n/a | def test_isspace(self, size): |
---|
202 | n/a | _ = self.from_latin1 |
---|
203 | n/a | whitespace = _(' \f\n\r\t\v') |
---|
204 | n/a | repeats = size // len(whitespace) + 2 |
---|
205 | n/a | s = whitespace * repeats |
---|
206 | n/a | self.assertTrue(s.isspace()) |
---|
207 | n/a | s += _('j') |
---|
208 | n/a | self.assertFalse(s.isspace()) |
---|
209 | n/a | |
---|
210 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
211 | n/a | def test_istitle(self, size): |
---|
212 | n/a | _ = self.from_latin1 |
---|
213 | n/a | SUBSTR = _('123456') |
---|
214 | n/a | s = _('').join([_('A'), _('a') * size, SUBSTR]) |
---|
215 | n/a | self.assertTrue(s.istitle()) |
---|
216 | n/a | s += _('A') |
---|
217 | n/a | self.assertTrue(s.istitle()) |
---|
218 | n/a | s += _('aA') |
---|
219 | n/a | self.assertFalse(s.istitle()) |
---|
220 | n/a | |
---|
221 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
222 | n/a | def test_isupper(self, size): |
---|
223 | n/a | _ = self.from_latin1 |
---|
224 | n/a | chars = _(''.join( |
---|
225 | n/a | chr(c) for c in range(255) if not chr(c).islower())) |
---|
226 | n/a | repeats = size // len(chars) + 2 |
---|
227 | n/a | s = chars * repeats |
---|
228 | n/a | self.assertTrue(s.isupper()) |
---|
229 | n/a | s += _('a') |
---|
230 | n/a | self.assertFalse(s.isupper()) |
---|
231 | n/a | |
---|
232 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
233 | n/a | def test_join(self, size): |
---|
234 | n/a | _ = self.from_latin1 |
---|
235 | n/a | s = _('A') * size |
---|
236 | n/a | x = s.join([_('aaaaa'), _('bbbbb')]) |
---|
237 | n/a | self.assertEqual(x.count(_('a')), 5) |
---|
238 | n/a | self.assertEqual(x.count(_('b')), 5) |
---|
239 | n/a | self.assertTrue(x.startswith(_('aaaaaA'))) |
---|
240 | n/a | self.assertTrue(x.endswith(_('Abbbbb'))) |
---|
241 | n/a | |
---|
242 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
243 | n/a | def test_ljust(self, size): |
---|
244 | n/a | _ = self.from_latin1 |
---|
245 | n/a | SUBSTR = _(' abc def ghi') |
---|
246 | n/a | s = SUBSTR.ljust(size) |
---|
247 | n/a | self.assertTrue(s.startswith(SUBSTR + _(' '))) |
---|
248 | n/a | self.assertEqual(len(s), size) |
---|
249 | n/a | self.assertEqual(s.strip(), SUBSTR.strip()) |
---|
250 | n/a | |
---|
251 | n/a | @bigmemtest(size=_2G + 10, memuse=2) |
---|
252 | n/a | def test_lower(self, size): |
---|
253 | n/a | _ = self.from_latin1 |
---|
254 | n/a | s = _('A') * size |
---|
255 | n/a | s = s.lower() |
---|
256 | n/a | self.assertEqual(len(s), size) |
---|
257 | n/a | self.assertEqual(s.count(_('a')), size) |
---|
258 | n/a | |
---|
259 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
260 | n/a | def test_lstrip(self, size): |
---|
261 | n/a | _ = self.from_latin1 |
---|
262 | n/a | SUBSTR = _('abc def ghi') |
---|
263 | n/a | s = SUBSTR.rjust(size) |
---|
264 | n/a | self.assertEqual(len(s), size) |
---|
265 | n/a | self.assertEqual(s.lstrip(), SUBSTR.lstrip()) |
---|
266 | n/a | del s |
---|
267 | n/a | s = SUBSTR.ljust(size) |
---|
268 | n/a | self.assertEqual(len(s), size) |
---|
269 | n/a | # Type-specific optimization |
---|
270 | n/a | if isinstance(s, (str, bytes)): |
---|
271 | n/a | stripped = s.lstrip() |
---|
272 | n/a | self.assertTrue(stripped is s) |
---|
273 | n/a | |
---|
274 | n/a | @bigmemtest(size=_2G + 10, memuse=2) |
---|
275 | n/a | def test_replace(self, size): |
---|
276 | n/a | _ = self.from_latin1 |
---|
277 | n/a | replacement = _('a') |
---|
278 | n/a | s = _(' ') * size |
---|
279 | n/a | s = s.replace(_(' '), replacement) |
---|
280 | n/a | self.assertEqual(len(s), size) |
---|
281 | n/a | self.assertEqual(s.count(replacement), size) |
---|
282 | n/a | s = s.replace(replacement, _(' '), size - 4) |
---|
283 | n/a | self.assertEqual(len(s), size) |
---|
284 | n/a | self.assertEqual(s.count(replacement), 4) |
---|
285 | n/a | self.assertEqual(s[-10:], _(' aaaa')) |
---|
286 | n/a | |
---|
287 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
288 | n/a | def test_rfind(self, size): |
---|
289 | n/a | _ = self.from_latin1 |
---|
290 | n/a | SUBSTR = _(' abc def ghi') |
---|
291 | n/a | sublen = len(SUBSTR) |
---|
292 | n/a | s = _('').join([SUBSTR, _('-') * size, SUBSTR]) |
---|
293 | n/a | self.assertEqual(s.rfind(_(' ')), sublen + size + SUBSTR.rfind(_(' '))) |
---|
294 | n/a | self.assertEqual(s.rfind(SUBSTR), sublen + size) |
---|
295 | n/a | self.assertEqual(s.rfind(_(' '), 0, size), SUBSTR.rfind(_(' '))) |
---|
296 | n/a | self.assertEqual(s.rfind(SUBSTR, 0, sublen + size), 0) |
---|
297 | n/a | self.assertEqual(s.rfind(_('i')), sublen + size + SUBSTR.rfind(_('i'))) |
---|
298 | n/a | self.assertEqual(s.rfind(_('i'), 0, sublen), SUBSTR.rfind(_('i'))) |
---|
299 | n/a | self.assertEqual(s.rfind(_('i'), 0, sublen + size), |
---|
300 | n/a | SUBSTR.rfind(_('i'))) |
---|
301 | n/a | self.assertEqual(s.rfind(_('j')), -1) |
---|
302 | n/a | |
---|
303 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
304 | n/a | def test_rindex(self, size): |
---|
305 | n/a | _ = self.from_latin1 |
---|
306 | n/a | SUBSTR = _(' abc def ghi') |
---|
307 | n/a | sublen = len(SUBSTR) |
---|
308 | n/a | s = _('').join([SUBSTR, _('-') * size, SUBSTR]) |
---|
309 | n/a | self.assertEqual(s.rindex(_(' ')), |
---|
310 | n/a | sublen + size + SUBSTR.rindex(_(' '))) |
---|
311 | n/a | self.assertEqual(s.rindex(SUBSTR), sublen + size) |
---|
312 | n/a | self.assertEqual(s.rindex(_(' '), 0, sublen + size - 1), |
---|
313 | n/a | SUBSTR.rindex(_(' '))) |
---|
314 | n/a | self.assertEqual(s.rindex(SUBSTR, 0, sublen + size), 0) |
---|
315 | n/a | self.assertEqual(s.rindex(_('i')), |
---|
316 | n/a | sublen + size + SUBSTR.rindex(_('i'))) |
---|
317 | n/a | self.assertEqual(s.rindex(_('i'), 0, sublen), SUBSTR.rindex(_('i'))) |
---|
318 | n/a | self.assertEqual(s.rindex(_('i'), 0, sublen + size), |
---|
319 | n/a | SUBSTR.rindex(_('i'))) |
---|
320 | n/a | self.assertRaises(ValueError, s.rindex, _('j')) |
---|
321 | n/a | |
---|
322 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
323 | n/a | def test_rjust(self, size): |
---|
324 | n/a | _ = self.from_latin1 |
---|
325 | n/a | SUBSTR = _(' abc def ghi') |
---|
326 | n/a | s = SUBSTR.ljust(size) |
---|
327 | n/a | self.assertTrue(s.startswith(SUBSTR + _(' '))) |
---|
328 | n/a | self.assertEqual(len(s), size) |
---|
329 | n/a | self.assertEqual(s.strip(), SUBSTR.strip()) |
---|
330 | n/a | |
---|
331 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
332 | n/a | def test_rstrip(self, size): |
---|
333 | n/a | _ = self.from_latin1 |
---|
334 | n/a | SUBSTR = _(' abc def ghi') |
---|
335 | n/a | s = SUBSTR.ljust(size) |
---|
336 | n/a | self.assertEqual(len(s), size) |
---|
337 | n/a | self.assertEqual(s.rstrip(), SUBSTR.rstrip()) |
---|
338 | n/a | del s |
---|
339 | n/a | s = SUBSTR.rjust(size) |
---|
340 | n/a | self.assertEqual(len(s), size) |
---|
341 | n/a | # Type-specific optimization |
---|
342 | n/a | if isinstance(s, (str, bytes)): |
---|
343 | n/a | stripped = s.rstrip() |
---|
344 | n/a | self.assertTrue(stripped is s) |
---|
345 | n/a | |
---|
346 | n/a | # The test takes about size bytes to build a string, and then about |
---|
347 | n/a | # sqrt(size) substrings of sqrt(size) in size and a list to |
---|
348 | n/a | # hold sqrt(size) items. It's close but just over 2x size. |
---|
349 | n/a | @bigmemtest(size=_2G, memuse=2.1) |
---|
350 | n/a | def test_split_small(self, size): |
---|
351 | n/a | _ = self.from_latin1 |
---|
352 | n/a | # Crudely calculate an estimate so that the result of s.split won't |
---|
353 | n/a | # take up an inordinate amount of memory |
---|
354 | n/a | chunksize = int(size ** 0.5 + 2) |
---|
355 | n/a | SUBSTR = _('a') + _(' ') * chunksize |
---|
356 | n/a | s = SUBSTR * chunksize |
---|
357 | n/a | l = s.split() |
---|
358 | n/a | self.assertEqual(len(l), chunksize) |
---|
359 | n/a | expected = _('a') |
---|
360 | n/a | for item in l: |
---|
361 | n/a | self.assertEqual(item, expected) |
---|
362 | n/a | del l |
---|
363 | n/a | l = s.split(_('a')) |
---|
364 | n/a | self.assertEqual(len(l), chunksize + 1) |
---|
365 | n/a | expected = _(' ') * chunksize |
---|
366 | n/a | for item in filter(None, l): |
---|
367 | n/a | self.assertEqual(item, expected) |
---|
368 | n/a | |
---|
369 | n/a | # Allocates a string of twice size (and briefly two) and a list of |
---|
370 | n/a | # size. Because of internal affairs, the s.split() call produces a |
---|
371 | n/a | # list of size times the same one-character string, so we only |
---|
372 | n/a | # suffer for the list size. (Otherwise, it'd cost another 48 times |
---|
373 | n/a | # size in bytes!) Nevertheless, a list of size takes |
---|
374 | n/a | # 8*size bytes. |
---|
375 | n/a | @bigmemtest(size=_2G + 5, memuse=2 * ascii_char_size + 8) |
---|
376 | n/a | def test_split_large(self, size): |
---|
377 | n/a | _ = self.from_latin1 |
---|
378 | n/a | s = _(' a') * size + _(' ') |
---|
379 | n/a | l = s.split() |
---|
380 | n/a | self.assertEqual(len(l), size) |
---|
381 | n/a | self.assertEqual(set(l), set([_('a')])) |
---|
382 | n/a | del l |
---|
383 | n/a | l = s.split(_('a')) |
---|
384 | n/a | self.assertEqual(len(l), size + 1) |
---|
385 | n/a | self.assertEqual(set(l), set([_(' ')])) |
---|
386 | n/a | |
---|
387 | n/a | @bigmemtest(size=_2G, memuse=2.1) |
---|
388 | n/a | def test_splitlines(self, size): |
---|
389 | n/a | _ = self.from_latin1 |
---|
390 | n/a | # Crudely calculate an estimate so that the result of s.split won't |
---|
391 | n/a | # take up an inordinate amount of memory |
---|
392 | n/a | chunksize = int(size ** 0.5 + 2) // 2 |
---|
393 | n/a | SUBSTR = _(' ') * chunksize + _('\n') + _(' ') * chunksize + _('\r\n') |
---|
394 | n/a | s = SUBSTR * (chunksize * 2) |
---|
395 | n/a | l = s.splitlines() |
---|
396 | n/a | self.assertEqual(len(l), chunksize * 4) |
---|
397 | n/a | expected = _(' ') * chunksize |
---|
398 | n/a | for item in l: |
---|
399 | n/a | self.assertEqual(item, expected) |
---|
400 | n/a | |
---|
401 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
402 | n/a | def test_startswith(self, size): |
---|
403 | n/a | _ = self.from_latin1 |
---|
404 | n/a | SUBSTR = _(' abc def ghi') |
---|
405 | n/a | s = _('-') * size + SUBSTR |
---|
406 | n/a | self.assertTrue(s.startswith(s)) |
---|
407 | n/a | self.assertTrue(s.startswith(_('-') * size)) |
---|
408 | n/a | self.assertFalse(s.startswith(SUBSTR)) |
---|
409 | n/a | |
---|
410 | n/a | @bigmemtest(size=_2G, memuse=1) |
---|
411 | n/a | def test_strip(self, size): |
---|
412 | n/a | _ = self.from_latin1 |
---|
413 | n/a | SUBSTR = _(' abc def ghi ') |
---|
414 | n/a | s = SUBSTR.rjust(size) |
---|
415 | n/a | self.assertEqual(len(s), size) |
---|
416 | n/a | self.assertEqual(s.strip(), SUBSTR.strip()) |
---|
417 | n/a | del s |
---|
418 | n/a | s = SUBSTR.ljust(size) |
---|
419 | n/a | self.assertEqual(len(s), size) |
---|
420 | n/a | self.assertEqual(s.strip(), SUBSTR.strip()) |
---|
421 | n/a | |
---|
422 | n/a | def _test_swapcase(self, size): |
---|
423 | n/a | _ = self.from_latin1 |
---|
424 | n/a | SUBSTR = _("aBcDeFG12.'\xa9\x00") |
---|
425 | n/a | sublen = len(SUBSTR) |
---|
426 | n/a | repeats = size // sublen + 2 |
---|
427 | n/a | s = SUBSTR * repeats |
---|
428 | n/a | s = s.swapcase() |
---|
429 | n/a | self.assertEqual(len(s), sublen * repeats) |
---|
430 | n/a | self.assertEqual(s[:sublen * 3], SUBSTR.swapcase() * 3) |
---|
431 | n/a | self.assertEqual(s[-sublen * 3:], SUBSTR.swapcase() * 3) |
---|
432 | n/a | |
---|
433 | n/a | def _test_title(self, size): |
---|
434 | n/a | _ = self.from_latin1 |
---|
435 | n/a | SUBSTR = _('SpaaHAaaAaham') |
---|
436 | n/a | s = SUBSTR * (size // len(SUBSTR) + 2) |
---|
437 | n/a | s = s.title() |
---|
438 | n/a | self.assertTrue(s.startswith((SUBSTR * 3).title())) |
---|
439 | n/a | self.assertTrue(s.endswith(SUBSTR.lower() * 3)) |
---|
440 | n/a | |
---|
441 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
442 | n/a | def test_translate(self, size): |
---|
443 | n/a | _ = self.from_latin1 |
---|
444 | n/a | SUBSTR = _('aZz.z.Aaz.') |
---|
445 | n/a | trans = bytes.maketrans(b'.aZ', b'-!$') |
---|
446 | n/a | sublen = len(SUBSTR) |
---|
447 | n/a | repeats = size // sublen + 2 |
---|
448 | n/a | s = SUBSTR * repeats |
---|
449 | n/a | s = s.translate(trans) |
---|
450 | n/a | self.assertEqual(len(s), repeats * sublen) |
---|
451 | n/a | self.assertEqual(s[:sublen], SUBSTR.translate(trans)) |
---|
452 | n/a | self.assertEqual(s[-sublen:], SUBSTR.translate(trans)) |
---|
453 | n/a | self.assertEqual(s.count(_('.')), 0) |
---|
454 | n/a | self.assertEqual(s.count(_('!')), repeats * 2) |
---|
455 | n/a | self.assertEqual(s.count(_('z')), repeats * 3) |
---|
456 | n/a | |
---|
457 | n/a | @bigmemtest(size=_2G + 5, memuse=2) |
---|
458 | n/a | def test_upper(self, size): |
---|
459 | n/a | _ = self.from_latin1 |
---|
460 | n/a | s = _('a') * size |
---|
461 | n/a | s = s.upper() |
---|
462 | n/a | self.assertEqual(len(s), size) |
---|
463 | n/a | self.assertEqual(s.count(_('A')), size) |
---|
464 | n/a | |
---|
465 | n/a | @bigmemtest(size=_2G + 20, memuse=1) |
---|
466 | n/a | def test_zfill(self, size): |
---|
467 | n/a | _ = self.from_latin1 |
---|
468 | n/a | SUBSTR = _('-568324723598234') |
---|
469 | n/a | s = SUBSTR.zfill(size) |
---|
470 | n/a | self.assertTrue(s.endswith(_('0') + SUBSTR[1:])) |
---|
471 | n/a | self.assertTrue(s.startswith(_('-0'))) |
---|
472 | n/a | self.assertEqual(len(s), size) |
---|
473 | n/a | self.assertEqual(s.count(_('0')), size - len(SUBSTR)) |
---|
474 | n/a | |
---|
475 | n/a | # This test is meaningful even with size < 2G, as long as the |
---|
476 | n/a | # doubled string is > 2G (but it tests more if both are > 2G :) |
---|
477 | n/a | @bigmemtest(size=_1G + 2, memuse=3) |
---|
478 | n/a | def test_concat(self, size): |
---|
479 | n/a | _ = self.from_latin1 |
---|
480 | n/a | s = _('.') * size |
---|
481 | n/a | self.assertEqual(len(s), size) |
---|
482 | n/a | s = s + s |
---|
483 | n/a | self.assertEqual(len(s), size * 2) |
---|
484 | n/a | self.assertEqual(s.count(_('.')), size * 2) |
---|
485 | n/a | |
---|
486 | n/a | # This test is meaningful even with size < 2G, as long as the |
---|
487 | n/a | # repeated string is > 2G (but it tests more if both are > 2G :) |
---|
488 | n/a | @bigmemtest(size=_1G + 2, memuse=3) |
---|
489 | n/a | def test_repeat(self, size): |
---|
490 | n/a | _ = self.from_latin1 |
---|
491 | n/a | s = _('.') * size |
---|
492 | n/a | self.assertEqual(len(s), size) |
---|
493 | n/a | s = s * 2 |
---|
494 | n/a | self.assertEqual(len(s), size * 2) |
---|
495 | n/a | self.assertEqual(s.count(_('.')), size * 2) |
---|
496 | n/a | |
---|
497 | n/a | @bigmemtest(size=_2G + 20, memuse=2) |
---|
498 | n/a | def test_slice_and_getitem(self, size): |
---|
499 | n/a | _ = self.from_latin1 |
---|
500 | n/a | SUBSTR = _('0123456789') |
---|
501 | n/a | sublen = len(SUBSTR) |
---|
502 | n/a | s = SUBSTR * (size // sublen) |
---|
503 | n/a | stepsize = len(s) // 100 |
---|
504 | n/a | stepsize = stepsize - (stepsize % sublen) |
---|
505 | n/a | for i in range(0, len(s) - stepsize, stepsize): |
---|
506 | n/a | self.assertEqual(s[i], SUBSTR[0]) |
---|
507 | n/a | self.assertEqual(s[i:i + sublen], SUBSTR) |
---|
508 | n/a | self.assertEqual(s[i:i + sublen:2], SUBSTR[::2]) |
---|
509 | n/a | if i > 0: |
---|
510 | n/a | self.assertEqual(s[i + sublen - 1:i - 1:-3], |
---|
511 | n/a | SUBSTR[sublen::-3]) |
---|
512 | n/a | # Make sure we do some slicing and indexing near the end of the |
---|
513 | n/a | # string, too. |
---|
514 | n/a | self.assertEqual(s[len(s) - 1], SUBSTR[-1]) |
---|
515 | n/a | self.assertEqual(s[-1], SUBSTR[-1]) |
---|
516 | n/a | self.assertEqual(s[len(s) - 10], SUBSTR[0]) |
---|
517 | n/a | self.assertEqual(s[-sublen], SUBSTR[0]) |
---|
518 | n/a | self.assertEqual(s[len(s):], _('')) |
---|
519 | n/a | self.assertEqual(s[len(s) - 1:], SUBSTR[-1:]) |
---|
520 | n/a | self.assertEqual(s[-1:], SUBSTR[-1:]) |
---|
521 | n/a | self.assertEqual(s[len(s) - sublen:], SUBSTR) |
---|
522 | n/a | self.assertEqual(s[-sublen:], SUBSTR) |
---|
523 | n/a | self.assertEqual(len(s[:]), len(s)) |
---|
524 | n/a | self.assertEqual(len(s[:len(s) - 5]), len(s) - 5) |
---|
525 | n/a | self.assertEqual(len(s[5:-5]), len(s) - 10) |
---|
526 | n/a | |
---|
527 | n/a | self.assertRaises(IndexError, operator.getitem, s, len(s)) |
---|
528 | n/a | self.assertRaises(IndexError, operator.getitem, s, len(s) + 1) |
---|
529 | n/a | self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31) |
---|
530 | n/a | |
---|
531 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
532 | n/a | def test_contains(self, size): |
---|
533 | n/a | _ = self.from_latin1 |
---|
534 | n/a | SUBSTR = _('0123456789') |
---|
535 | n/a | edge = _('-') * (size // 2) |
---|
536 | n/a | s = _('').join([edge, SUBSTR, edge]) |
---|
537 | n/a | del edge |
---|
538 | n/a | self.assertTrue(SUBSTR in s) |
---|
539 | n/a | self.assertFalse(SUBSTR * 2 in s) |
---|
540 | n/a | self.assertTrue(_('-') in s) |
---|
541 | n/a | self.assertFalse(_('a') in s) |
---|
542 | n/a | s += _('a') |
---|
543 | n/a | self.assertTrue(_('a') in s) |
---|
544 | n/a | |
---|
545 | n/a | @bigmemtest(size=_2G + 10, memuse=2) |
---|
546 | n/a | def test_compare(self, size): |
---|
547 | n/a | _ = self.from_latin1 |
---|
548 | n/a | s1 = _('-') * size |
---|
549 | n/a | s2 = _('-') * size |
---|
550 | n/a | self.assertTrue(s1 == s2) |
---|
551 | n/a | del s2 |
---|
552 | n/a | s2 = s1 + _('a') |
---|
553 | n/a | self.assertFalse(s1 == s2) |
---|
554 | n/a | del s2 |
---|
555 | n/a | s2 = _('.') * size |
---|
556 | n/a | self.assertFalse(s1 == s2) |
---|
557 | n/a | |
---|
558 | n/a | @bigmemtest(size=_2G + 10, memuse=1) |
---|
559 | n/a | def test_hash(self, size): |
---|
560 | n/a | # Not sure if we can do any meaningful tests here... Even if we |
---|
561 | n/a | # start relying on the exact algorithm used, the result will be |
---|
562 | n/a | # different depending on the size of the C 'long int'. Even this |
---|
563 | n/a | # test is dodgy (there's no *guarantee* that the two things should |
---|
564 | n/a | # have a different hash, even if they, in the current |
---|
565 | n/a | # implementation, almost always do.) |
---|
566 | n/a | _ = self.from_latin1 |
---|
567 | n/a | s = _('\x00') * size |
---|
568 | n/a | h1 = hash(s) |
---|
569 | n/a | del s |
---|
570 | n/a | s = _('\x00') * (size + 1) |
---|
571 | n/a | self.assertNotEqual(h1, hash(s)) |
---|
572 | n/a | |
---|
573 | n/a | |
---|
574 | n/a | class StrTest(unittest.TestCase, BaseStrTest): |
---|
575 | n/a | |
---|
576 | n/a | def from_latin1(self, s): |
---|
577 | n/a | return s |
---|
578 | n/a | |
---|
579 | n/a | def basic_encode_test(self, size, enc, c='.', expectedsize=None): |
---|
580 | n/a | if expectedsize is None: |
---|
581 | n/a | expectedsize = size |
---|
582 | n/a | try: |
---|
583 | n/a | s = c * size |
---|
584 | n/a | self.assertEqual(len(s.encode(enc)), expectedsize) |
---|
585 | n/a | finally: |
---|
586 | n/a | s = None |
---|
587 | n/a | |
---|
588 | n/a | def setUp(self): |
---|
589 | n/a | # HACK: adjust memory use of tests inherited from BaseStrTest |
---|
590 | n/a | # according to character size. |
---|
591 | n/a | self._adjusted = {} |
---|
592 | n/a | for name in dir(BaseStrTest): |
---|
593 | n/a | if not name.startswith('test_'): |
---|
594 | n/a | continue |
---|
595 | n/a | meth = getattr(type(self), name) |
---|
596 | n/a | try: |
---|
597 | n/a | memuse = meth.memuse |
---|
598 | n/a | except AttributeError: |
---|
599 | n/a | continue |
---|
600 | n/a | meth.memuse = ascii_char_size * memuse |
---|
601 | n/a | self._adjusted[name] = memuse |
---|
602 | n/a | |
---|
603 | n/a | def tearDown(self): |
---|
604 | n/a | for name, memuse in self._adjusted.items(): |
---|
605 | n/a | getattr(type(self), name).memuse = memuse |
---|
606 | n/a | |
---|
607 | n/a | @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) |
---|
608 | n/a | def test_capitalize(self, size): |
---|
609 | n/a | self._test_capitalize(size) |
---|
610 | n/a | |
---|
611 | n/a | @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) |
---|
612 | n/a | def test_title(self, size): |
---|
613 | n/a | self._test_title(size) |
---|
614 | n/a | |
---|
615 | n/a | @bigmemtest(size=_2G, memuse=ucs4_char_size * 3) |
---|
616 | n/a | def test_swapcase(self, size): |
---|
617 | n/a | self._test_swapcase(size) |
---|
618 | n/a | |
---|
619 | n/a | # Many codecs convert to the legacy representation first, explaining |
---|
620 | n/a | # why we add 'ucs4_char_size' to the 'memuse' below. |
---|
621 | n/a | |
---|
622 | n/a | @bigmemtest(size=_2G + 2, memuse=ascii_char_size + 1) |
---|
623 | n/a | def test_encode(self, size): |
---|
624 | n/a | return self.basic_encode_test(size, 'utf-8') |
---|
625 | n/a | |
---|
626 | n/a | @bigmemtest(size=_4G // 6 + 2, memuse=ascii_char_size + ucs4_char_size + 1) |
---|
627 | n/a | def test_encode_raw_unicode_escape(self, size): |
---|
628 | n/a | try: |
---|
629 | n/a | return self.basic_encode_test(size, 'raw_unicode_escape') |
---|
630 | n/a | except MemoryError: |
---|
631 | n/a | pass # acceptable on 32-bit |
---|
632 | n/a | |
---|
633 | n/a | @bigmemtest(size=_4G // 5 + 70, memuse=ascii_char_size + ucs4_char_size + 1) |
---|
634 | n/a | def test_encode_utf7(self, size): |
---|
635 | n/a | try: |
---|
636 | n/a | return self.basic_encode_test(size, 'utf7') |
---|
637 | n/a | except MemoryError: |
---|
638 | n/a | pass # acceptable on 32-bit |
---|
639 | n/a | |
---|
640 | n/a | @bigmemtest(size=_4G // 4 + 5, memuse=ascii_char_size + ucs4_char_size + 4) |
---|
641 | n/a | def test_encode_utf32(self, size): |
---|
642 | n/a | try: |
---|
643 | n/a | return self.basic_encode_test(size, 'utf32', expectedsize=4 * size + 4) |
---|
644 | n/a | except MemoryError: |
---|
645 | n/a | pass # acceptable on 32-bit |
---|
646 | n/a | |
---|
647 | n/a | @bigmemtest(size=_2G - 1, memuse=ascii_char_size + 1) |
---|
648 | n/a | def test_encode_ascii(self, size): |
---|
649 | n/a | return self.basic_encode_test(size, 'ascii', c='A') |
---|
650 | n/a | |
---|
651 | n/a | # str % (...) uses a Py_UCS4 intermediate representation |
---|
652 | n/a | |
---|
653 | n/a | @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2 + ucs4_char_size) |
---|
654 | n/a | def test_format(self, size): |
---|
655 | n/a | s = '-' * size |
---|
656 | n/a | sf = '%s' % (s,) |
---|
657 | n/a | self.assertTrue(s == sf) |
---|
658 | n/a | del sf |
---|
659 | n/a | sf = '..%s..' % (s,) |
---|
660 | n/a | self.assertEqual(len(sf), len(s) + 4) |
---|
661 | n/a | self.assertTrue(sf.startswith('..-')) |
---|
662 | n/a | self.assertTrue(sf.endswith('-..')) |
---|
663 | n/a | del s, sf |
---|
664 | n/a | |
---|
665 | n/a | size //= 2 |
---|
666 | n/a | edge = '-' * size |
---|
667 | n/a | s = ''.join([edge, '%s', edge]) |
---|
668 | n/a | del edge |
---|
669 | n/a | s = s % '...' |
---|
670 | n/a | self.assertEqual(len(s), size * 2 + 3) |
---|
671 | n/a | self.assertEqual(s.count('.'), 3) |
---|
672 | n/a | self.assertEqual(s.count('-'), size * 2) |
---|
673 | n/a | |
---|
674 | n/a | @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 2) |
---|
675 | n/a | def test_repr_small(self, size): |
---|
676 | n/a | s = '-' * size |
---|
677 | n/a | s = repr(s) |
---|
678 | n/a | self.assertEqual(len(s), size + 2) |
---|
679 | n/a | self.assertEqual(s[0], "'") |
---|
680 | n/a | self.assertEqual(s[-1], "'") |
---|
681 | n/a | self.assertEqual(s.count('-'), size) |
---|
682 | n/a | del s |
---|
683 | n/a | # repr() will create a string four times as large as this 'binary |
---|
684 | n/a | # string', but we don't want to allocate much more than twice |
---|
685 | n/a | # size in total. (We do extra testing in test_repr_large()) |
---|
686 | n/a | size = size // 5 * 2 |
---|
687 | n/a | s = '\x00' * size |
---|
688 | n/a | s = repr(s) |
---|
689 | n/a | self.assertEqual(len(s), size * 4 + 2) |
---|
690 | n/a | self.assertEqual(s[0], "'") |
---|
691 | n/a | self.assertEqual(s[-1], "'") |
---|
692 | n/a | self.assertEqual(s.count('\\'), size) |
---|
693 | n/a | self.assertEqual(s.count('0'), size * 2) |
---|
694 | n/a | |
---|
695 | n/a | @bigmemtest(size=_2G + 10, memuse=ascii_char_size * 5) |
---|
696 | n/a | def test_repr_large(self, size): |
---|
697 | n/a | s = '\x00' * size |
---|
698 | n/a | s = repr(s) |
---|
699 | n/a | self.assertEqual(len(s), size * 4 + 2) |
---|
700 | n/a | self.assertEqual(s[0], "'") |
---|
701 | n/a | self.assertEqual(s[-1], "'") |
---|
702 | n/a | self.assertEqual(s.count('\\'), size) |
---|
703 | n/a | self.assertEqual(s.count('0'), size * 2) |
---|
704 | n/a | |
---|
705 | n/a | # ascii() calls encode('ascii', 'backslashreplace'), which itself |
---|
706 | n/a | # creates a temporary Py_UNICODE representation in addition to the |
---|
707 | n/a | # original (Py_UCS2) one |
---|
708 | n/a | # There's also some overallocation when resizing the ascii() result |
---|
709 | n/a | # that isn't taken into account here. |
---|
710 | n/a | @bigmemtest(size=_2G // 5 + 1, memuse=ucs2_char_size + |
---|
711 | n/a | ucs4_char_size + ascii_char_size * 6) |
---|
712 | n/a | def test_unicode_repr(self, size): |
---|
713 | n/a | # Use an assigned, but not printable code point. |
---|
714 | n/a | # It is in the range of the low surrogates \uDC00-\uDFFF. |
---|
715 | n/a | char = "\uDCBA" |
---|
716 | n/a | s = char * size |
---|
717 | n/a | try: |
---|
718 | n/a | for f in (repr, ascii): |
---|
719 | n/a | r = f(s) |
---|
720 | n/a | self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size) |
---|
721 | n/a | self.assertTrue(r.endswith(r"\udcba'"), r[-10:]) |
---|
722 | n/a | r = None |
---|
723 | n/a | finally: |
---|
724 | n/a | r = s = None |
---|
725 | n/a | |
---|
726 | n/a | @bigmemtest(size=_2G // 5 + 1, memuse=ucs4_char_size * 2 + ascii_char_size * 10) |
---|
727 | n/a | def test_unicode_repr_wide(self, size): |
---|
728 | n/a | char = "\U0001DCBA" |
---|
729 | n/a | s = char * size |
---|
730 | n/a | try: |
---|
731 | n/a | for f in (repr, ascii): |
---|
732 | n/a | r = f(s) |
---|
733 | n/a | self.assertEqual(len(r), 2 + (len(f(char)) - 2) * size) |
---|
734 | n/a | self.assertTrue(r.endswith(r"\U0001dcba'"), r[-12:]) |
---|
735 | n/a | r = None |
---|
736 | n/a | finally: |
---|
737 | n/a | r = s = None |
---|
738 | n/a | |
---|
739 | n/a | # The original test_translate is overridden here, so as to get the |
---|
740 | n/a | # correct size estimate: str.translate() uses an intermediate Py_UCS4 |
---|
741 | n/a | # representation. |
---|
742 | n/a | |
---|
743 | n/a | @bigmemtest(size=_2G, memuse=ascii_char_size * 2 + ucs4_char_size) |
---|
744 | n/a | def test_translate(self, size): |
---|
745 | n/a | _ = self.from_latin1 |
---|
746 | n/a | SUBSTR = _('aZz.z.Aaz.') |
---|
747 | n/a | trans = { |
---|
748 | n/a | ord(_('.')): _('-'), |
---|
749 | n/a | ord(_('a')): _('!'), |
---|
750 | n/a | ord(_('Z')): _('$'), |
---|
751 | n/a | } |
---|
752 | n/a | sublen = len(SUBSTR) |
---|
753 | n/a | repeats = size // sublen + 2 |
---|
754 | n/a | s = SUBSTR * repeats |
---|
755 | n/a | s = s.translate(trans) |
---|
756 | n/a | self.assertEqual(len(s), repeats * sublen) |
---|
757 | n/a | self.assertEqual(s[:sublen], SUBSTR.translate(trans)) |
---|
758 | n/a | self.assertEqual(s[-sublen:], SUBSTR.translate(trans)) |
---|
759 | n/a | self.assertEqual(s.count(_('.')), 0) |
---|
760 | n/a | self.assertEqual(s.count(_('!')), repeats * 2) |
---|
761 | n/a | self.assertEqual(s.count(_('z')), repeats * 3) |
---|
762 | n/a | |
---|
763 | n/a | |
---|
764 | n/a | class BytesTest(unittest.TestCase, BaseStrTest): |
---|
765 | n/a | |
---|
766 | n/a | def from_latin1(self, s): |
---|
767 | n/a | return s.encode("latin-1") |
---|
768 | n/a | |
---|
769 | n/a | @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size) |
---|
770 | n/a | def test_decode(self, size): |
---|
771 | n/a | s = self.from_latin1('.') * size |
---|
772 | n/a | self.assertEqual(len(s.decode('utf-8')), size) |
---|
773 | n/a | |
---|
774 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
775 | n/a | def test_capitalize(self, size): |
---|
776 | n/a | self._test_capitalize(size) |
---|
777 | n/a | |
---|
778 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
779 | n/a | def test_title(self, size): |
---|
780 | n/a | self._test_title(size) |
---|
781 | n/a | |
---|
782 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
783 | n/a | def test_swapcase(self, size): |
---|
784 | n/a | self._test_swapcase(size) |
---|
785 | n/a | |
---|
786 | n/a | |
---|
787 | n/a | class BytearrayTest(unittest.TestCase, BaseStrTest): |
---|
788 | n/a | |
---|
789 | n/a | def from_latin1(self, s): |
---|
790 | n/a | return bytearray(s.encode("latin-1")) |
---|
791 | n/a | |
---|
792 | n/a | @bigmemtest(size=_2G + 2, memuse=1 + ascii_char_size) |
---|
793 | n/a | def test_decode(self, size): |
---|
794 | n/a | s = self.from_latin1('.') * size |
---|
795 | n/a | self.assertEqual(len(s.decode('utf-8')), size) |
---|
796 | n/a | |
---|
797 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
798 | n/a | def test_capitalize(self, size): |
---|
799 | n/a | self._test_capitalize(size) |
---|
800 | n/a | |
---|
801 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
802 | n/a | def test_title(self, size): |
---|
803 | n/a | self._test_title(size) |
---|
804 | n/a | |
---|
805 | n/a | @bigmemtest(size=_2G, memuse=2) |
---|
806 | n/a | def test_swapcase(self, size): |
---|
807 | n/a | self._test_swapcase(size) |
---|
808 | n/a | |
---|
809 | n/a | test_hash = None |
---|
810 | n/a | test_split_large = None |
---|
811 | n/a | |
---|
812 | n/a | class TupleTest(unittest.TestCase): |
---|
813 | n/a | |
---|
814 | n/a | # Tuples have a small, fixed-sized head and an array of pointers to |
---|
815 | n/a | # data. Since we're testing 64-bit addressing, we can assume that the |
---|
816 | n/a | # pointers are 8 bytes, and that thus that the tuples take up 8 bytes |
---|
817 | n/a | # per size. |
---|
818 | n/a | |
---|
819 | n/a | # As a side-effect of testing long tuples, these tests happen to test |
---|
820 | n/a | # having more than 2<<31 references to any given object. Hence the |
---|
821 | n/a | # use of different types of objects as contents in different tests. |
---|
822 | n/a | |
---|
823 | n/a | @bigmemtest(size=_2G + 2, memuse=16) |
---|
824 | n/a | def test_compare(self, size): |
---|
825 | n/a | t1 = ('',) * size |
---|
826 | n/a | t2 = ('',) * size |
---|
827 | n/a | self.assertTrue(t1 == t2) |
---|
828 | n/a | del t2 |
---|
829 | n/a | t2 = ('',) * (size + 1) |
---|
830 | n/a | self.assertFalse(t1 == t2) |
---|
831 | n/a | del t2 |
---|
832 | n/a | t2 = (1,) * size |
---|
833 | n/a | self.assertFalse(t1 == t2) |
---|
834 | n/a | |
---|
835 | n/a | # Test concatenating into a single tuple of more than 2G in length, |
---|
836 | n/a | # and concatenating a tuple of more than 2G in length separately, so |
---|
837 | n/a | # the smaller test still gets run even if there isn't memory for the |
---|
838 | n/a | # larger test (but we still let the tester know the larger test is |
---|
839 | n/a | # skipped, in verbose mode.) |
---|
840 | n/a | def basic_concat_test(self, size): |
---|
841 | n/a | t = ((),) * size |
---|
842 | n/a | self.assertEqual(len(t), size) |
---|
843 | n/a | t = t + t |
---|
844 | n/a | self.assertEqual(len(t), size * 2) |
---|
845 | n/a | |
---|
846 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=24) |
---|
847 | n/a | def test_concat_small(self, size): |
---|
848 | n/a | return self.basic_concat_test(size) |
---|
849 | n/a | |
---|
850 | n/a | @bigmemtest(size=_2G + 2, memuse=24) |
---|
851 | n/a | def test_concat_large(self, size): |
---|
852 | n/a | return self.basic_concat_test(size) |
---|
853 | n/a | |
---|
854 | n/a | @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5) |
---|
855 | n/a | def test_contains(self, size): |
---|
856 | n/a | t = (1, 2, 3, 4, 5) * size |
---|
857 | n/a | self.assertEqual(len(t), size * 5) |
---|
858 | n/a | self.assertTrue(5 in t) |
---|
859 | n/a | self.assertFalse((1, 2, 3, 4, 5) in t) |
---|
860 | n/a | self.assertFalse(0 in t) |
---|
861 | n/a | |
---|
862 | n/a | @bigmemtest(size=_2G + 10, memuse=8) |
---|
863 | n/a | def test_hash(self, size): |
---|
864 | n/a | t1 = (0,) * size |
---|
865 | n/a | h1 = hash(t1) |
---|
866 | n/a | del t1 |
---|
867 | n/a | t2 = (0,) * (size + 1) |
---|
868 | n/a | self.assertFalse(h1 == hash(t2)) |
---|
869 | n/a | |
---|
870 | n/a | @bigmemtest(size=_2G + 10, memuse=8) |
---|
871 | n/a | def test_index_and_slice(self, size): |
---|
872 | n/a | t = (None,) * size |
---|
873 | n/a | self.assertEqual(len(t), size) |
---|
874 | n/a | self.assertEqual(t[-1], None) |
---|
875 | n/a | self.assertEqual(t[5], None) |
---|
876 | n/a | self.assertEqual(t[size - 1], None) |
---|
877 | n/a | self.assertRaises(IndexError, operator.getitem, t, size) |
---|
878 | n/a | self.assertEqual(t[:5], (None,) * 5) |
---|
879 | n/a | self.assertEqual(t[-5:], (None,) * 5) |
---|
880 | n/a | self.assertEqual(t[20:25], (None,) * 5) |
---|
881 | n/a | self.assertEqual(t[-25:-20], (None,) * 5) |
---|
882 | n/a | self.assertEqual(t[size - 5:], (None,) * 5) |
---|
883 | n/a | self.assertEqual(t[size - 5:size], (None,) * 5) |
---|
884 | n/a | self.assertEqual(t[size - 6:size - 2], (None,) * 4) |
---|
885 | n/a | self.assertEqual(t[size:size], ()) |
---|
886 | n/a | self.assertEqual(t[size:size+5], ()) |
---|
887 | n/a | |
---|
888 | n/a | # Like test_concat, split in two. |
---|
889 | n/a | def basic_test_repeat(self, size): |
---|
890 | n/a | t = ('',) * size |
---|
891 | n/a | self.assertEqual(len(t), size) |
---|
892 | n/a | t = t * 2 |
---|
893 | n/a | self.assertEqual(len(t), size * 2) |
---|
894 | n/a | |
---|
895 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=24) |
---|
896 | n/a | def test_repeat_small(self, size): |
---|
897 | n/a | return self.basic_test_repeat(size) |
---|
898 | n/a | |
---|
899 | n/a | @bigmemtest(size=_2G + 2, memuse=24) |
---|
900 | n/a | def test_repeat_large(self, size): |
---|
901 | n/a | return self.basic_test_repeat(size) |
---|
902 | n/a | |
---|
903 | n/a | @bigmemtest(size=_1G - 1, memuse=12) |
---|
904 | n/a | def test_repeat_large_2(self, size): |
---|
905 | n/a | return self.basic_test_repeat(size) |
---|
906 | n/a | |
---|
907 | n/a | @bigmemtest(size=_1G - 1, memuse=9) |
---|
908 | n/a | def test_from_2G_generator(self, size): |
---|
909 | n/a | self.skipTest("test needs much more memory than advertised, see issue5438") |
---|
910 | n/a | try: |
---|
911 | n/a | t = tuple(range(size)) |
---|
912 | n/a | except MemoryError: |
---|
913 | n/a | pass # acceptable on 32-bit |
---|
914 | n/a | else: |
---|
915 | n/a | count = 0 |
---|
916 | n/a | for item in t: |
---|
917 | n/a | self.assertEqual(item, count) |
---|
918 | n/a | count += 1 |
---|
919 | n/a | self.assertEqual(count, size) |
---|
920 | n/a | |
---|
921 | n/a | @bigmemtest(size=_1G - 25, memuse=9) |
---|
922 | n/a | def test_from_almost_2G_generator(self, size): |
---|
923 | n/a | self.skipTest("test needs much more memory than advertised, see issue5438") |
---|
924 | n/a | try: |
---|
925 | n/a | t = tuple(range(size)) |
---|
926 | n/a | count = 0 |
---|
927 | n/a | for item in t: |
---|
928 | n/a | self.assertEqual(item, count) |
---|
929 | n/a | count += 1 |
---|
930 | n/a | self.assertEqual(count, size) |
---|
931 | n/a | except MemoryError: |
---|
932 | n/a | pass # acceptable, expected on 32-bit |
---|
933 | n/a | |
---|
934 | n/a | # Like test_concat, split in two. |
---|
935 | n/a | def basic_test_repr(self, size): |
---|
936 | n/a | t = (0,) * size |
---|
937 | n/a | s = repr(t) |
---|
938 | n/a | # The repr of a tuple of 0's is exactly three times the tuple length. |
---|
939 | n/a | self.assertEqual(len(s), size * 3) |
---|
940 | n/a | self.assertEqual(s[:5], '(0, 0') |
---|
941 | n/a | self.assertEqual(s[-5:], '0, 0)') |
---|
942 | n/a | self.assertEqual(s.count('0'), size) |
---|
943 | n/a | |
---|
944 | n/a | @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size) |
---|
945 | n/a | def test_repr_small(self, size): |
---|
946 | n/a | return self.basic_test_repr(size) |
---|
947 | n/a | |
---|
948 | n/a | @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size) |
---|
949 | n/a | def test_repr_large(self, size): |
---|
950 | n/a | return self.basic_test_repr(size) |
---|
951 | n/a | |
---|
952 | n/a | class ListTest(unittest.TestCase): |
---|
953 | n/a | |
---|
954 | n/a | # Like tuples, lists have a small, fixed-sized head and an array of |
---|
955 | n/a | # pointers to data, so 8 bytes per size. Also like tuples, we make the |
---|
956 | n/a | # lists hold references to various objects to test their refcount |
---|
957 | n/a | # limits. |
---|
958 | n/a | |
---|
959 | n/a | @bigmemtest(size=_2G + 2, memuse=16) |
---|
960 | n/a | def test_compare(self, size): |
---|
961 | n/a | l1 = [''] * size |
---|
962 | n/a | l2 = [''] * size |
---|
963 | n/a | self.assertTrue(l1 == l2) |
---|
964 | n/a | del l2 |
---|
965 | n/a | l2 = [''] * (size + 1) |
---|
966 | n/a | self.assertFalse(l1 == l2) |
---|
967 | n/a | del l2 |
---|
968 | n/a | l2 = [2] * size |
---|
969 | n/a | self.assertFalse(l1 == l2) |
---|
970 | n/a | |
---|
971 | n/a | # Test concatenating into a single list of more than 2G in length, |
---|
972 | n/a | # and concatenating a list of more than 2G in length separately, so |
---|
973 | n/a | # the smaller test still gets run even if there isn't memory for the |
---|
974 | n/a | # larger test (but we still let the tester know the larger test is |
---|
975 | n/a | # skipped, in verbose mode.) |
---|
976 | n/a | def basic_test_concat(self, size): |
---|
977 | n/a | l = [[]] * size |
---|
978 | n/a | self.assertEqual(len(l), size) |
---|
979 | n/a | l = l + l |
---|
980 | n/a | self.assertEqual(len(l), size * 2) |
---|
981 | n/a | |
---|
982 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=24) |
---|
983 | n/a | def test_concat_small(self, size): |
---|
984 | n/a | return self.basic_test_concat(size) |
---|
985 | n/a | |
---|
986 | n/a | @bigmemtest(size=_2G + 2, memuse=24) |
---|
987 | n/a | def test_concat_large(self, size): |
---|
988 | n/a | return self.basic_test_concat(size) |
---|
989 | n/a | |
---|
990 | n/a | def basic_test_inplace_concat(self, size): |
---|
991 | n/a | l = [sys.stdout] * size |
---|
992 | n/a | l += l |
---|
993 | n/a | self.assertEqual(len(l), size * 2) |
---|
994 | n/a | self.assertTrue(l[0] is l[-1]) |
---|
995 | n/a | self.assertTrue(l[size - 1] is l[size + 1]) |
---|
996 | n/a | |
---|
997 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=24) |
---|
998 | n/a | def test_inplace_concat_small(self, size): |
---|
999 | n/a | return self.basic_test_inplace_concat(size) |
---|
1000 | n/a | |
---|
1001 | n/a | @bigmemtest(size=_2G + 2, memuse=24) |
---|
1002 | n/a | def test_inplace_concat_large(self, size): |
---|
1003 | n/a | return self.basic_test_inplace_concat(size) |
---|
1004 | n/a | |
---|
1005 | n/a | @bigmemtest(size=_2G // 5 + 10, memuse=8 * 5) |
---|
1006 | n/a | def test_contains(self, size): |
---|
1007 | n/a | l = [1, 2, 3, 4, 5] * size |
---|
1008 | n/a | self.assertEqual(len(l), size * 5) |
---|
1009 | n/a | self.assertTrue(5 in l) |
---|
1010 | n/a | self.assertFalse([1, 2, 3, 4, 5] in l) |
---|
1011 | n/a | self.assertFalse(0 in l) |
---|
1012 | n/a | |
---|
1013 | n/a | @bigmemtest(size=_2G + 10, memuse=8) |
---|
1014 | n/a | def test_hash(self, size): |
---|
1015 | n/a | l = [0] * size |
---|
1016 | n/a | self.assertRaises(TypeError, hash, l) |
---|
1017 | n/a | |
---|
1018 | n/a | @bigmemtest(size=_2G + 10, memuse=8) |
---|
1019 | n/a | def test_index_and_slice(self, size): |
---|
1020 | n/a | l = [None] * size |
---|
1021 | n/a | self.assertEqual(len(l), size) |
---|
1022 | n/a | self.assertEqual(l[-1], None) |
---|
1023 | n/a | self.assertEqual(l[5], None) |
---|
1024 | n/a | self.assertEqual(l[size - 1], None) |
---|
1025 | n/a | self.assertRaises(IndexError, operator.getitem, l, size) |
---|
1026 | n/a | self.assertEqual(l[:5], [None] * 5) |
---|
1027 | n/a | self.assertEqual(l[-5:], [None] * 5) |
---|
1028 | n/a | self.assertEqual(l[20:25], [None] * 5) |
---|
1029 | n/a | self.assertEqual(l[-25:-20], [None] * 5) |
---|
1030 | n/a | self.assertEqual(l[size - 5:], [None] * 5) |
---|
1031 | n/a | self.assertEqual(l[size - 5:size], [None] * 5) |
---|
1032 | n/a | self.assertEqual(l[size - 6:size - 2], [None] * 4) |
---|
1033 | n/a | self.assertEqual(l[size:size], []) |
---|
1034 | n/a | self.assertEqual(l[size:size+5], []) |
---|
1035 | n/a | |
---|
1036 | n/a | l[size - 2] = 5 |
---|
1037 | n/a | self.assertEqual(len(l), size) |
---|
1038 | n/a | self.assertEqual(l[-3:], [None, 5, None]) |
---|
1039 | n/a | self.assertEqual(l.count(5), 1) |
---|
1040 | n/a | self.assertRaises(IndexError, operator.setitem, l, size, 6) |
---|
1041 | n/a | self.assertEqual(len(l), size) |
---|
1042 | n/a | |
---|
1043 | n/a | l[size - 7:] = [1, 2, 3, 4, 5] |
---|
1044 | n/a | size -= 2 |
---|
1045 | n/a | self.assertEqual(len(l), size) |
---|
1046 | n/a | self.assertEqual(l[-7:], [None, None, 1, 2, 3, 4, 5]) |
---|
1047 | n/a | |
---|
1048 | n/a | l[:7] = [1, 2, 3, 4, 5] |
---|
1049 | n/a | size -= 2 |
---|
1050 | n/a | self.assertEqual(len(l), size) |
---|
1051 | n/a | self.assertEqual(l[:7], [1, 2, 3, 4, 5, None, None]) |
---|
1052 | n/a | |
---|
1053 | n/a | del l[size - 1] |
---|
1054 | n/a | size -= 1 |
---|
1055 | n/a | self.assertEqual(len(l), size) |
---|
1056 | n/a | self.assertEqual(l[-1], 4) |
---|
1057 | n/a | |
---|
1058 | n/a | del l[-2:] |
---|
1059 | n/a | size -= 2 |
---|
1060 | n/a | self.assertEqual(len(l), size) |
---|
1061 | n/a | self.assertEqual(l[-1], 2) |
---|
1062 | n/a | |
---|
1063 | n/a | del l[0] |
---|
1064 | n/a | size -= 1 |
---|
1065 | n/a | self.assertEqual(len(l), size) |
---|
1066 | n/a | self.assertEqual(l[0], 2) |
---|
1067 | n/a | |
---|
1068 | n/a | del l[:2] |
---|
1069 | n/a | size -= 2 |
---|
1070 | n/a | self.assertEqual(len(l), size) |
---|
1071 | n/a | self.assertEqual(l[0], 4) |
---|
1072 | n/a | |
---|
1073 | n/a | # Like test_concat, split in two. |
---|
1074 | n/a | def basic_test_repeat(self, size): |
---|
1075 | n/a | l = [] * size |
---|
1076 | n/a | self.assertFalse(l) |
---|
1077 | n/a | l = [''] * size |
---|
1078 | n/a | self.assertEqual(len(l), size) |
---|
1079 | n/a | l = l * 2 |
---|
1080 | n/a | self.assertEqual(len(l), size * 2) |
---|
1081 | n/a | |
---|
1082 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=24) |
---|
1083 | n/a | def test_repeat_small(self, size): |
---|
1084 | n/a | return self.basic_test_repeat(size) |
---|
1085 | n/a | |
---|
1086 | n/a | @bigmemtest(size=_2G + 2, memuse=24) |
---|
1087 | n/a | def test_repeat_large(self, size): |
---|
1088 | n/a | return self.basic_test_repeat(size) |
---|
1089 | n/a | |
---|
1090 | n/a | def basic_test_inplace_repeat(self, size): |
---|
1091 | n/a | l = [''] |
---|
1092 | n/a | l *= size |
---|
1093 | n/a | self.assertEqual(len(l), size) |
---|
1094 | n/a | self.assertTrue(l[0] is l[-1]) |
---|
1095 | n/a | del l |
---|
1096 | n/a | |
---|
1097 | n/a | l = [''] * size |
---|
1098 | n/a | l *= 2 |
---|
1099 | n/a | self.assertEqual(len(l), size * 2) |
---|
1100 | n/a | self.assertTrue(l[size - 1] is l[-1]) |
---|
1101 | n/a | |
---|
1102 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=16) |
---|
1103 | n/a | def test_inplace_repeat_small(self, size): |
---|
1104 | n/a | return self.basic_test_inplace_repeat(size) |
---|
1105 | n/a | |
---|
1106 | n/a | @bigmemtest(size=_2G + 2, memuse=16) |
---|
1107 | n/a | def test_inplace_repeat_large(self, size): |
---|
1108 | n/a | return self.basic_test_inplace_repeat(size) |
---|
1109 | n/a | |
---|
1110 | n/a | def basic_test_repr(self, size): |
---|
1111 | n/a | l = [0] * size |
---|
1112 | n/a | s = repr(l) |
---|
1113 | n/a | # The repr of a list of 0's is exactly three times the list length. |
---|
1114 | n/a | self.assertEqual(len(s), size * 3) |
---|
1115 | n/a | self.assertEqual(s[:5], '[0, 0') |
---|
1116 | n/a | self.assertEqual(s[-5:], '0, 0]') |
---|
1117 | n/a | self.assertEqual(s.count('0'), size) |
---|
1118 | n/a | |
---|
1119 | n/a | @bigmemtest(size=_2G // 3 + 2, memuse=8 + 3 * ascii_char_size) |
---|
1120 | n/a | def test_repr_small(self, size): |
---|
1121 | n/a | return self.basic_test_repr(size) |
---|
1122 | n/a | |
---|
1123 | n/a | @bigmemtest(size=_2G + 2, memuse=8 + 3 * ascii_char_size) |
---|
1124 | n/a | def test_repr_large(self, size): |
---|
1125 | n/a | return self.basic_test_repr(size) |
---|
1126 | n/a | |
---|
1127 | n/a | # list overallocates ~1/8th of the total size (on first expansion) so |
---|
1128 | n/a | # the single list.append call puts memuse at 9 bytes per size. |
---|
1129 | n/a | @bigmemtest(size=_2G, memuse=9) |
---|
1130 | n/a | def test_append(self, size): |
---|
1131 | n/a | l = [object()] * size |
---|
1132 | n/a | l.append(object()) |
---|
1133 | n/a | self.assertEqual(len(l), size+1) |
---|
1134 | n/a | self.assertTrue(l[-3] is l[-2]) |
---|
1135 | n/a | self.assertFalse(l[-2] is l[-1]) |
---|
1136 | n/a | |
---|
1137 | n/a | @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) |
---|
1138 | n/a | def test_count(self, size): |
---|
1139 | n/a | l = [1, 2, 3, 4, 5] * size |
---|
1140 | n/a | self.assertEqual(l.count(1), size) |
---|
1141 | n/a | self.assertEqual(l.count("1"), 0) |
---|
1142 | n/a | |
---|
1143 | n/a | def basic_test_extend(self, size): |
---|
1144 | n/a | l = [object] * size |
---|
1145 | n/a | l.extend(l) |
---|
1146 | n/a | self.assertEqual(len(l), size * 2) |
---|
1147 | n/a | self.assertTrue(l[0] is l[-1]) |
---|
1148 | n/a | self.assertTrue(l[size - 1] is l[size + 1]) |
---|
1149 | n/a | |
---|
1150 | n/a | @bigmemtest(size=_2G // 2 + 2, memuse=16) |
---|
1151 | n/a | def test_extend_small(self, size): |
---|
1152 | n/a | return self.basic_test_extend(size) |
---|
1153 | n/a | |
---|
1154 | n/a | @bigmemtest(size=_2G + 2, memuse=16) |
---|
1155 | n/a | def test_extend_large(self, size): |
---|
1156 | n/a | return self.basic_test_extend(size) |
---|
1157 | n/a | |
---|
1158 | n/a | @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) |
---|
1159 | n/a | def test_index(self, size): |
---|
1160 | n/a | l = [1, 2, 3, 4, 5] * size |
---|
1161 | n/a | size *= 5 |
---|
1162 | n/a | self.assertEqual(l.index(1), 0) |
---|
1163 | n/a | self.assertEqual(l.index(5, size - 5), size - 1) |
---|
1164 | n/a | self.assertEqual(l.index(5, size - 5, size), size - 1) |
---|
1165 | n/a | self.assertRaises(ValueError, l.index, 1, size - 4, size) |
---|
1166 | n/a | self.assertRaises(ValueError, l.index, 6) |
---|
1167 | n/a | |
---|
1168 | n/a | # This tests suffers from overallocation, just like test_append. |
---|
1169 | n/a | @bigmemtest(size=_2G + 10, memuse=9) |
---|
1170 | n/a | def test_insert(self, size): |
---|
1171 | n/a | l = [1.0] * size |
---|
1172 | n/a | l.insert(size - 1, "A") |
---|
1173 | n/a | size += 1 |
---|
1174 | n/a | self.assertEqual(len(l), size) |
---|
1175 | n/a | self.assertEqual(l[-3:], [1.0, "A", 1.0]) |
---|
1176 | n/a | |
---|
1177 | n/a | l.insert(size + 1, "B") |
---|
1178 | n/a | size += 1 |
---|
1179 | n/a | self.assertEqual(len(l), size) |
---|
1180 | n/a | self.assertEqual(l[-3:], ["A", 1.0, "B"]) |
---|
1181 | n/a | |
---|
1182 | n/a | l.insert(1, "C") |
---|
1183 | n/a | size += 1 |
---|
1184 | n/a | self.assertEqual(len(l), size) |
---|
1185 | n/a | self.assertEqual(l[:3], [1.0, "C", 1.0]) |
---|
1186 | n/a | self.assertEqual(l[size - 3:], ["A", 1.0, "B"]) |
---|
1187 | n/a | |
---|
1188 | n/a | @bigmemtest(size=_2G // 5 + 4, memuse=8 * 5) |
---|
1189 | n/a | def test_pop(self, size): |
---|
1190 | n/a | l = ["a", "b", "c", "d", "e"] * size |
---|
1191 | n/a | size *= 5 |
---|
1192 | n/a | self.assertEqual(len(l), size) |
---|
1193 | n/a | |
---|
1194 | n/a | item = l.pop() |
---|
1195 | n/a | size -= 1 |
---|
1196 | n/a | self.assertEqual(len(l), size) |
---|
1197 | n/a | self.assertEqual(item, "e") |
---|
1198 | n/a | self.assertEqual(l[-2:], ["c", "d"]) |
---|
1199 | n/a | |
---|
1200 | n/a | item = l.pop(0) |
---|
1201 | n/a | size -= 1 |
---|
1202 | n/a | self.assertEqual(len(l), size) |
---|
1203 | n/a | self.assertEqual(item, "a") |
---|
1204 | n/a | self.assertEqual(l[:2], ["b", "c"]) |
---|
1205 | n/a | |
---|
1206 | n/a | item = l.pop(size - 2) |
---|
1207 | n/a | size -= 1 |
---|
1208 | n/a | self.assertEqual(len(l), size) |
---|
1209 | n/a | self.assertEqual(item, "c") |
---|
1210 | n/a | self.assertEqual(l[-2:], ["b", "d"]) |
---|
1211 | n/a | |
---|
1212 | n/a | @bigmemtest(size=_2G + 10, memuse=8) |
---|
1213 | n/a | def test_remove(self, size): |
---|
1214 | n/a | l = [10] * size |
---|
1215 | n/a | self.assertEqual(len(l), size) |
---|
1216 | n/a | |
---|
1217 | n/a | l.remove(10) |
---|
1218 | n/a | size -= 1 |
---|
1219 | n/a | self.assertEqual(len(l), size) |
---|
1220 | n/a | |
---|
1221 | n/a | # Because of the earlier l.remove(), this append doesn't trigger |
---|
1222 | n/a | # a resize. |
---|
1223 | n/a | l.append(5) |
---|
1224 | n/a | size += 1 |
---|
1225 | n/a | self.assertEqual(len(l), size) |
---|
1226 | n/a | self.assertEqual(l[-2:], [10, 5]) |
---|
1227 | n/a | l.remove(5) |
---|
1228 | n/a | size -= 1 |
---|
1229 | n/a | self.assertEqual(len(l), size) |
---|
1230 | n/a | self.assertEqual(l[-2:], [10, 10]) |
---|
1231 | n/a | |
---|
1232 | n/a | @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) |
---|
1233 | n/a | def test_reverse(self, size): |
---|
1234 | n/a | l = [1, 2, 3, 4, 5] * size |
---|
1235 | n/a | l.reverse() |
---|
1236 | n/a | self.assertEqual(len(l), size * 5) |
---|
1237 | n/a | self.assertEqual(l[-5:], [5, 4, 3, 2, 1]) |
---|
1238 | n/a | self.assertEqual(l[:5], [5, 4, 3, 2, 1]) |
---|
1239 | n/a | |
---|
1240 | n/a | @bigmemtest(size=_2G // 5 + 2, memuse=8 * 5) |
---|
1241 | n/a | def test_sort(self, size): |
---|
1242 | n/a | l = [1, 2, 3, 4, 5] * size |
---|
1243 | n/a | l.sort() |
---|
1244 | n/a | self.assertEqual(len(l), size * 5) |
---|
1245 | n/a | self.assertEqual(l.count(1), size) |
---|
1246 | n/a | self.assertEqual(l[:10], [1] * 10) |
---|
1247 | n/a | self.assertEqual(l[-10:], [5] * 10) |
---|
1248 | n/a | |
---|
1249 | n/a | def test_main(): |
---|
1250 | n/a | support.run_unittest(StrTest, BytesTest, BytearrayTest, |
---|
1251 | n/a | TupleTest, ListTest) |
---|
1252 | n/a | |
---|
1253 | n/a | if __name__ == '__main__': |
---|
1254 | n/a | if len(sys.argv) > 1: |
---|
1255 | n/a | support.set_memlimit(sys.argv[1]) |
---|
1256 | n/a | test_main() |
---|