ยปCore Development>Code coverage>Lib/test/test_bigmem.py

Python code coverage for Lib/test/test_bigmem.py

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