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

Python code coverage for Lib/test/string_tests.py

#countcontent
1n/a"""
2n/aCommon tests shared by test_unicode, test_userstring and test_bytes.
3n/a"""
4n/a
5n/aimport unittest, string, sys, struct
6n/afrom test import support
7n/afrom collections import UserList
8n/a
9n/aclass Sequence:
10n/a def __init__(self, seq='wxyz'): self.seq = seq
11n/a def __len__(self): return len(self.seq)
12n/a def __getitem__(self, i): return self.seq[i]
13n/a
14n/aclass BadSeq1(Sequence):
15n/a def __init__(self): self.seq = [7, 'hello', 123]
16n/a def __str__(self): return '{0} {1} {2}'.format(*self.seq)
17n/a
18n/aclass BadSeq2(Sequence):
19n/a def __init__(self): self.seq = ['a', 'b', 'c']
20n/a def __len__(self): return 8
21n/a
22n/aclass BaseTest:
23n/a # These tests are for buffers of values (bytes) and not
24n/a # specific to character interpretation, used for bytes objects
25n/a # and various string implementations
26n/a
27n/a # The type to be tested
28n/a # Change in subclasses to change the behaviour of fixtesttype()
29n/a type2test = None
30n/a
31n/a # Whether the "contained items" of the container are integers in
32n/a # range(0, 256) (i.e. bytes, bytearray) or strings of length 1
33n/a # (str)
34n/a contains_bytes = False
35n/a
36n/a # All tests pass their arguments to the testing methods
37n/a # as str objects. fixtesttype() can be used to propagate
38n/a # these arguments to the appropriate type
39n/a def fixtype(self, obj):
40n/a if isinstance(obj, str):
41n/a return self.__class__.type2test(obj)
42n/a elif isinstance(obj, list):
43n/a return [self.fixtype(x) for x in obj]
44n/a elif isinstance(obj, tuple):
45n/a return tuple([self.fixtype(x) for x in obj])
46n/a elif isinstance(obj, dict):
47n/a return dict([
48n/a (self.fixtype(key), self.fixtype(value))
49n/a for (key, value) in obj.items()
50n/a ])
51n/a else:
52n/a return obj
53n/a
54n/a def test_fixtype(self):
55n/a self.assertIs(type(self.fixtype("123")), self.type2test)
56n/a
57n/a # check that obj.method(*args) returns result
58n/a def checkequal(self, result, obj, methodname, *args, **kwargs):
59n/a result = self.fixtype(result)
60n/a obj = self.fixtype(obj)
61n/a args = self.fixtype(args)
62n/a kwargs = {k: self.fixtype(v) for k,v in kwargs.items()}
63n/a realresult = getattr(obj, methodname)(*args, **kwargs)
64n/a self.assertEqual(
65n/a result,
66n/a realresult
67n/a )
68n/a # if the original is returned make sure that
69n/a # this doesn't happen with subclasses
70n/a if obj is realresult:
71n/a try:
72n/a class subtype(self.__class__.type2test):
73n/a pass
74n/a except TypeError:
75n/a pass # Skip this if we can't subclass
76n/a else:
77n/a obj = subtype(obj)
78n/a realresult = getattr(obj, methodname)(*args)
79n/a self.assertIsNot(obj, realresult)
80n/a
81n/a # check that obj.method(*args) raises exc
82n/a def checkraises(self, exc, obj, methodname, *args):
83n/a obj = self.fixtype(obj)
84n/a args = self.fixtype(args)
85n/a with self.assertRaises(exc) as cm:
86n/a getattr(obj, methodname)(*args)
87n/a self.assertNotEqual(str(cm.exception), '')
88n/a
89n/a # call obj.method(*args) without any checks
90n/a def checkcall(self, obj, methodname, *args):
91n/a obj = self.fixtype(obj)
92n/a args = self.fixtype(args)
93n/a getattr(obj, methodname)(*args)
94n/a
95n/a def test_count(self):
96n/a self.checkequal(3, 'aaa', 'count', 'a')
97n/a self.checkequal(0, 'aaa', 'count', 'b')
98n/a self.checkequal(3, 'aaa', 'count', 'a')
99n/a self.checkequal(0, 'aaa', 'count', 'b')
100n/a self.checkequal(3, 'aaa', 'count', 'a')
101n/a self.checkequal(0, 'aaa', 'count', 'b')
102n/a self.checkequal(0, 'aaa', 'count', 'b')
103n/a self.checkequal(2, 'aaa', 'count', 'a', 1)
104n/a self.checkequal(0, 'aaa', 'count', 'a', 10)
105n/a self.checkequal(1, 'aaa', 'count', 'a', -1)
106n/a self.checkequal(3, 'aaa', 'count', 'a', -10)
107n/a self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
108n/a self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
109n/a self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
110n/a self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
111n/a self.checkequal(3, 'aaa', 'count', '', 1)
112n/a self.checkequal(1, 'aaa', 'count', '', 3)
113n/a self.checkequal(0, 'aaa', 'count', '', 10)
114n/a self.checkequal(2, 'aaa', 'count', '', -1)
115n/a self.checkequal(4, 'aaa', 'count', '', -10)
116n/a
117n/a self.checkequal(1, '', 'count', '')
118n/a self.checkequal(0, '', 'count', '', 1, 1)
119n/a self.checkequal(0, '', 'count', '', sys.maxsize, 0)
120n/a
121n/a self.checkequal(0, '', 'count', 'xx')
122n/a self.checkequal(0, '', 'count', 'xx', 1, 1)
123n/a self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
124n/a
125n/a self.checkraises(TypeError, 'hello', 'count')
126n/a
127n/a if self.contains_bytes:
128n/a self.checkequal(0, 'hello', 'count', 42)
129n/a else:
130n/a self.checkraises(TypeError, 'hello', 'count', 42)
131n/a
132n/a # For a variety of combinations,
133n/a # verify that str.count() matches an equivalent function
134n/a # replacing all occurrences and then differencing the string lengths
135n/a charset = ['', 'a', 'b']
136n/a digits = 7
137n/a base = len(charset)
138n/a teststrings = set()
139n/a for i in range(base ** digits):
140n/a entry = []
141n/a for j in range(digits):
142n/a i, m = divmod(i, base)
143n/a entry.append(charset[m])
144n/a teststrings.add(''.join(entry))
145n/a teststrings = [self.fixtype(ts) for ts in teststrings]
146n/a for i in teststrings:
147n/a n = len(i)
148n/a for j in teststrings:
149n/a r1 = i.count(j)
150n/a if j:
151n/a r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
152n/a len(j))
153n/a else:
154n/a r2, rem = len(i)+1, 0
155n/a if rem or r1 != r2:
156n/a self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
157n/a self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
158n/a
159n/a def test_find(self):
160n/a self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
161n/a self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
162n/a self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
163n/a
164n/a self.checkequal(0, 'abc', 'find', '', 0)
165n/a self.checkequal(3, 'abc', 'find', '', 3)
166n/a self.checkequal(-1, 'abc', 'find', '', 4)
167n/a
168n/a # to check the ability to pass None as defaults
169n/a self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
170n/a self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
171n/a self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
172n/a self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
173n/a self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
174n/a
175n/a self.checkraises(TypeError, 'hello', 'find')
176n/a
177n/a if self.contains_bytes:
178n/a self.checkequal(-1, 'hello', 'find', 42)
179n/a else:
180n/a self.checkraises(TypeError, 'hello', 'find', 42)
181n/a
182n/a self.checkequal(0, '', 'find', '')
183n/a self.checkequal(-1, '', 'find', '', 1, 1)
184n/a self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
185n/a
186n/a self.checkequal(-1, '', 'find', 'xx')
187n/a self.checkequal(-1, '', 'find', 'xx', 1, 1)
188n/a self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
189n/a
190n/a # issue 7458
191n/a self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
192n/a
193n/a # For a variety of combinations,
194n/a # verify that str.find() matches __contains__
195n/a # and that the found substring is really at that location
196n/a charset = ['', 'a', 'b', 'c']
197n/a digits = 5
198n/a base = len(charset)
199n/a teststrings = set()
200n/a for i in range(base ** digits):
201n/a entry = []
202n/a for j in range(digits):
203n/a i, m = divmod(i, base)
204n/a entry.append(charset[m])
205n/a teststrings.add(''.join(entry))
206n/a teststrings = [self.fixtype(ts) for ts in teststrings]
207n/a for i in teststrings:
208n/a for j in teststrings:
209n/a loc = i.find(j)
210n/a r1 = (loc != -1)
211n/a r2 = j in i
212n/a self.assertEqual(r1, r2)
213n/a if loc != -1:
214n/a self.assertEqual(i[loc:loc+len(j)], j)
215n/a
216n/a def test_rfind(self):
217n/a self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
218n/a self.checkequal(12, 'abcdefghiabc', 'rfind', '')
219n/a self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
220n/a self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
221n/a
222n/a self.checkequal(3, 'abc', 'rfind', '', 0)
223n/a self.checkequal(3, 'abc', 'rfind', '', 3)
224n/a self.checkequal(-1, 'abc', 'rfind', '', 4)
225n/a
226n/a # to check the ability to pass None as defaults
227n/a self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
228n/a self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
229n/a self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
230n/a self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
231n/a self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
232n/a
233n/a self.checkraises(TypeError, 'hello', 'rfind')
234n/a
235n/a if self.contains_bytes:
236n/a self.checkequal(-1, 'hello', 'rfind', 42)
237n/a else:
238n/a self.checkraises(TypeError, 'hello', 'rfind', 42)
239n/a
240n/a # For a variety of combinations,
241n/a # verify that str.rfind() matches __contains__
242n/a # and that the found substring is really at that location
243n/a charset = ['', 'a', 'b', 'c']
244n/a digits = 5
245n/a base = len(charset)
246n/a teststrings = set()
247n/a for i in range(base ** digits):
248n/a entry = []
249n/a for j in range(digits):
250n/a i, m = divmod(i, base)
251n/a entry.append(charset[m])
252n/a teststrings.add(''.join(entry))
253n/a teststrings = [self.fixtype(ts) for ts in teststrings]
254n/a for i in teststrings:
255n/a for j in teststrings:
256n/a loc = i.rfind(j)
257n/a r1 = (loc != -1)
258n/a r2 = j in i
259n/a self.assertEqual(r1, r2)
260n/a if loc != -1:
261n/a self.assertEqual(i[loc:loc+len(j)], j)
262n/a
263n/a # issue 7458
264n/a self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
265n/a
266n/a # issue #15534
267n/a self.checkequal(0, '<......\u043c...', "rfind", "<")
268n/a
269n/a def test_index(self):
270n/a self.checkequal(0, 'abcdefghiabc', 'index', '')
271n/a self.checkequal(3, 'abcdefghiabc', 'index', 'def')
272n/a self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
273n/a self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
274n/a
275n/a self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
276n/a self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
277n/a self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
278n/a self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
279n/a
280n/a # to check the ability to pass None as defaults
281n/a self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
282n/a self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
283n/a self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
284n/a self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
285n/a self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
286n/a
287n/a self.checkraises(TypeError, 'hello', 'index')
288n/a
289n/a if self.contains_bytes:
290n/a self.checkraises(ValueError, 'hello', 'index', 42)
291n/a else:
292n/a self.checkraises(TypeError, 'hello', 'index', 42)
293n/a
294n/a def test_rindex(self):
295n/a self.checkequal(12, 'abcdefghiabc', 'rindex', '')
296n/a self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
297n/a self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
298n/a self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
299n/a
300n/a self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
301n/a self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
302n/a self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
303n/a self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
304n/a self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
305n/a
306n/a # to check the ability to pass None as defaults
307n/a self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
308n/a self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
309n/a self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
310n/a self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
311n/a self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
312n/a
313n/a self.checkraises(TypeError, 'hello', 'rindex')
314n/a
315n/a if self.contains_bytes:
316n/a self.checkraises(ValueError, 'hello', 'rindex', 42)
317n/a else:
318n/a self.checkraises(TypeError, 'hello', 'rindex', 42)
319n/a
320n/a def test_lower(self):
321n/a self.checkequal('hello', 'HeLLo', 'lower')
322n/a self.checkequal('hello', 'hello', 'lower')
323n/a self.checkraises(TypeError, 'hello', 'lower', 42)
324n/a
325n/a def test_upper(self):
326n/a self.checkequal('HELLO', 'HeLLo', 'upper')
327n/a self.checkequal('HELLO', 'HELLO', 'upper')
328n/a self.checkraises(TypeError, 'hello', 'upper', 42)
329n/a
330n/a def test_expandtabs(self):
331n/a self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
332n/a 'expandtabs')
333n/a self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
334n/a 'expandtabs', 8)
335n/a self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
336n/a 'expandtabs', 4)
337n/a self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
338n/a 'expandtabs')
339n/a self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
340n/a 'expandtabs', 8)
341n/a self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi',
342n/a 'expandtabs', 4)
343n/a self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi',
344n/a 'expandtabs', 4)
345n/a # check keyword args
346n/a self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
347n/a 'expandtabs', tabsize=8)
348n/a self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi',
349n/a 'expandtabs', tabsize=4)
350n/a
351n/a self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
352n/a
353n/a self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
354n/a # This test is only valid when sizeof(int) == sizeof(void*) == 4.
355n/a if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4:
356n/a self.checkraises(OverflowError,
357n/a '\ta\n\tb', 'expandtabs', sys.maxsize)
358n/a
359n/a def test_split(self):
360n/a # by a char
361n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
362n/a self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
363n/a self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
364n/a self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
365n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
366n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
367n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
368n/a sys.maxsize-2)
369n/a self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
370n/a self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
371n/a self.checkequal(['abcd'], 'abcd', 'split', '|')
372n/a self.checkequal([''], '', 'split', '|')
373n/a self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
374n/a self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
375n/a self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
376n/a self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
377n/a
378n/a self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
379n/a self.checkequal(['a']*15 +['a|a|a|a|a'],
380n/a ('a|'*20)[:-1], 'split', '|', 15)
381n/a
382n/a # by string
383n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
384n/a self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
385n/a self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
386n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
387n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
388n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
389n/a sys.maxsize-10)
390n/a self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
391n/a self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
392n/a self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
393n/a self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
394n/a self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
395n/a 'split', 'test')
396n/a self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
397n/a self.checkequal(['', ''], 'aaa', 'split', 'aaa')
398n/a self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
399n/a self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
400n/a self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
401n/a self.checkequal([''], '', 'split', 'aaa')
402n/a self.checkequal(['aa'], 'aa', 'split', 'aaa')
403n/a self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
404n/a self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
405n/a
406n/a self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
407n/a self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
408n/a self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
409n/a 'split', 'BLAH', 18)
410n/a
411n/a # with keyword args
412n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', sep='|')
413n/a self.checkequal(['a', 'b|c|d'],
414n/a 'a|b|c|d', 'split', '|', maxsplit=1)
415n/a self.checkequal(['a', 'b|c|d'],
416n/a 'a|b|c|d', 'split', sep='|', maxsplit=1)
417n/a self.checkequal(['a', 'b|c|d'],
418n/a 'a|b|c|d', 'split', maxsplit=1, sep='|')
419n/a self.checkequal(['a', 'b c d'],
420n/a 'a b c d', 'split', maxsplit=1)
421n/a
422n/a # argument type
423n/a self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
424n/a
425n/a # null case
426n/a self.checkraises(ValueError, 'hello', 'split', '')
427n/a self.checkraises(ValueError, 'hello', 'split', '', 0)
428n/a
429n/a def test_rsplit(self):
430n/a # by a char
431n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
432n/a self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
433n/a self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
434n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
435n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
436n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
437n/a sys.maxsize-100)
438n/a self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
439n/a self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
440n/a self.checkequal(['abcd'], 'abcd', 'rsplit', '|')
441n/a self.checkequal([''], '', 'rsplit', '|')
442n/a self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
443n/a self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
444n/a self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
445n/a
446n/a self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
447n/a
448n/a self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
449n/a self.checkequal(['a|a|a|a|a']+['a']*15,
450n/a ('a|'*20)[:-1], 'rsplit', '|', 15)
451n/a
452n/a # by string
453n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
454n/a self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
455n/a self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
456n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
457n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
458n/a self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
459n/a sys.maxsize-5)
460n/a self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
461n/a self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
462n/a self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
463n/a self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
464n/a self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
465n/a 'rsplit', 'test')
466n/a self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
467n/a self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
468n/a self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
469n/a self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
470n/a self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
471n/a self.checkequal([''], '', 'rsplit', 'aaa')
472n/a self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
473n/a self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
474n/a self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
475n/a
476n/a self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
477n/a self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
478n/a self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
479n/a 'rsplit', 'BLAH', 18)
480n/a
481n/a # with keyword args
482n/a self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', sep='|')
483n/a self.checkequal(['a|b|c', 'd'],
484n/a 'a|b|c|d', 'rsplit', '|', maxsplit=1)
485n/a self.checkequal(['a|b|c', 'd'],
486n/a 'a|b|c|d', 'rsplit', sep='|', maxsplit=1)
487n/a self.checkequal(['a|b|c', 'd'],
488n/a 'a|b|c|d', 'rsplit', maxsplit=1, sep='|')
489n/a self.checkequal(['a b c', 'd'],
490n/a 'a b c d', 'rsplit', maxsplit=1)
491n/a
492n/a # argument type
493n/a self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
494n/a
495n/a # null case
496n/a self.checkraises(ValueError, 'hello', 'rsplit', '')
497n/a self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
498n/a
499n/a def test_replace(self):
500n/a EQ = self.checkequal
501n/a
502n/a # Operations on the empty string
503n/a EQ("", "", "replace", "", "")
504n/a EQ("A", "", "replace", "", "A")
505n/a EQ("", "", "replace", "A", "")
506n/a EQ("", "", "replace", "A", "A")
507n/a EQ("", "", "replace", "", "", 100)
508n/a EQ("", "", "replace", "", "", sys.maxsize)
509n/a
510n/a # interleave (from=="", 'to' gets inserted everywhere)
511n/a EQ("A", "A", "replace", "", "")
512n/a EQ("*A*", "A", "replace", "", "*")
513n/a EQ("*1A*1", "A", "replace", "", "*1")
514n/a EQ("*-#A*-#", "A", "replace", "", "*-#")
515n/a EQ("*-A*-A*-", "AA", "replace", "", "*-")
516n/a EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
517n/a EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxsize)
518n/a EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
519n/a EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
520n/a EQ("*-A*-A", "AA", "replace", "", "*-", 2)
521n/a EQ("*-AA", "AA", "replace", "", "*-", 1)
522n/a EQ("AA", "AA", "replace", "", "*-", 0)
523n/a
524n/a # single character deletion (from=="A", to=="")
525n/a EQ("", "A", "replace", "A", "")
526n/a EQ("", "AAA", "replace", "A", "")
527n/a EQ("", "AAA", "replace", "A", "", -1)
528n/a EQ("", "AAA", "replace", "A", "", sys.maxsize)
529n/a EQ("", "AAA", "replace", "A", "", 4)
530n/a EQ("", "AAA", "replace", "A", "", 3)
531n/a EQ("A", "AAA", "replace", "A", "", 2)
532n/a EQ("AA", "AAA", "replace", "A", "", 1)
533n/a EQ("AAA", "AAA", "replace", "A", "", 0)
534n/a EQ("", "AAAAAAAAAA", "replace", "A", "")
535n/a EQ("BCD", "ABACADA", "replace", "A", "")
536n/a EQ("BCD", "ABACADA", "replace", "A", "", -1)
537n/a EQ("BCD", "ABACADA", "replace", "A", "", sys.maxsize)
538n/a EQ("BCD", "ABACADA", "replace", "A", "", 5)
539n/a EQ("BCD", "ABACADA", "replace", "A", "", 4)
540n/a EQ("BCDA", "ABACADA", "replace", "A", "", 3)
541n/a EQ("BCADA", "ABACADA", "replace", "A", "", 2)
542n/a EQ("BACADA", "ABACADA", "replace", "A", "", 1)
543n/a EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
544n/a EQ("BCD", "ABCAD", "replace", "A", "")
545n/a EQ("BCD", "ABCADAA", "replace", "A", "")
546n/a EQ("BCD", "BCD", "replace", "A", "")
547n/a EQ("*************", "*************", "replace", "A", "")
548n/a EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
549n/a
550n/a # substring deletion (from=="the", to=="")
551n/a EQ("", "the", "replace", "the", "")
552n/a EQ("ater", "theater", "replace", "the", "")
553n/a EQ("", "thethe", "replace", "the", "")
554n/a EQ("", "thethethethe", "replace", "the", "")
555n/a EQ("aaaa", "theatheatheathea", "replace", "the", "")
556n/a EQ("that", "that", "replace", "the", "")
557n/a EQ("thaet", "thaet", "replace", "the", "")
558n/a EQ("here and re", "here and there", "replace", "the", "")
559n/a EQ("here and re and re", "here and there and there",
560n/a "replace", "the", "", sys.maxsize)
561n/a EQ("here and re and re", "here and there and there",
562n/a "replace", "the", "", -1)
563n/a EQ("here and re and re", "here and there and there",
564n/a "replace", "the", "", 3)
565n/a EQ("here and re and re", "here and there and there",
566n/a "replace", "the", "", 2)
567n/a EQ("here and re and there", "here and there and there",
568n/a "replace", "the", "", 1)
569n/a EQ("here and there and there", "here and there and there",
570n/a "replace", "the", "", 0)
571n/a EQ("here and re and re", "here and there and there", "replace", "the", "")
572n/a
573n/a EQ("abc", "abc", "replace", "the", "")
574n/a EQ("abcdefg", "abcdefg", "replace", "the", "")
575n/a
576n/a # substring deletion (from=="bob", to=="")
577n/a EQ("bob", "bbobob", "replace", "bob", "")
578n/a EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
579n/a EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
580n/a EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
581n/a
582n/a # single character replace in place (len(from)==len(to)==1)
583n/a EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
584n/a EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
585n/a EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxsize)
586n/a EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
587n/a EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
588n/a EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
589n/a EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
590n/a EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
591n/a
592n/a EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
593n/a EQ("who goes there?", "Who goes there?", "replace", "W", "w")
594n/a EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
595n/a EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
596n/a EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
597n/a
598n/a EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
599n/a
600n/a # substring replace in place (len(from)==len(to) > 1)
601n/a EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
602n/a EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxsize)
603n/a EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
604n/a EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
605n/a EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
606n/a EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
607n/a EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
608n/a EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
609n/a EQ("cobob", "bobob", "replace", "bob", "cob")
610n/a EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
611n/a EQ("bobob", "bobob", "replace", "bot", "bot")
612n/a
613n/a # replace single character (len(from)==1, len(to)>1)
614n/a EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
615n/a EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
616n/a EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxsize)
617n/a EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
618n/a EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
619n/a EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
620n/a EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
621n/a # issue #15534
622n/a EQ('...\u043c......&lt;', '...\u043c......<', "replace", "<", "&lt;")
623n/a
624n/a EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
625n/a
626n/a # replace substring (len(from)>1, len(to)!=len(from))
627n/a EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
628n/a "replace", "spam", "ham")
629n/a EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
630n/a "replace", "spam", "ham", sys.maxsize)
631n/a EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
632n/a "replace", "spam", "ham", -1)
633n/a EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
634n/a "replace", "spam", "ham", 4)
635n/a EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
636n/a "replace", "spam", "ham", 3)
637n/a EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
638n/a "replace", "spam", "ham", 2)
639n/a EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
640n/a "replace", "spam", "ham", 1)
641n/a EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
642n/a "replace", "spam", "ham", 0)
643n/a
644n/a EQ("bobob", "bobobob", "replace", "bobob", "bob")
645n/a EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
646n/a EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
647n/a
648n/a self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
649n/a self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
650n/a self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
651n/a self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
652n/a self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
653n/a self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
654n/a self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
655n/a self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
656n/a self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
657n/a self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
658n/a self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
659n/a self.checkequal('abc', 'abc', 'replace', '', '-', 0)
660n/a self.checkequal('', '', 'replace', '', '')
661n/a self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
662n/a self.checkequal('abc', 'abc', 'replace', 'xy', '--')
663n/a # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
664n/a # MemoryError due to empty result (platform malloc issue when requesting
665n/a # 0 bytes).
666n/a self.checkequal('', '123', 'replace', '123', '')
667n/a self.checkequal('', '123123', 'replace', '123', '')
668n/a self.checkequal('x', '123x123', 'replace', '123', '')
669n/a
670n/a self.checkraises(TypeError, 'hello', 'replace')
671n/a self.checkraises(TypeError, 'hello', 'replace', 42)
672n/a self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
673n/a self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
674n/a
675n/a @unittest.skipIf(sys.maxsize > (1 << 32) or struct.calcsize('P') != 4,
676n/a 'only applies to 32-bit platforms')
677n/a def test_replace_overflow(self):
678n/a # Check for overflow checking on 32 bit machines
679n/a A2_16 = "A" * (2**16)
680n/a self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
681n/a self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
682n/a self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
683n/a
684n/a def test_capitalize(self):
685n/a self.checkequal(' hello ', ' hello ', 'capitalize')
686n/a self.checkequal('Hello ', 'Hello ','capitalize')
687n/a self.checkequal('Hello ', 'hello ','capitalize')
688n/a self.checkequal('Aaaa', 'aaaa', 'capitalize')
689n/a self.checkequal('Aaaa', 'AaAa', 'capitalize')
690n/a
691n/a self.checkraises(TypeError, 'hello', 'capitalize', 42)
692n/a
693n/a def test_additional_split(self):
694n/a self.checkequal(['this', 'is', 'the', 'split', 'function'],
695n/a 'this is the split function', 'split')
696n/a
697n/a # by whitespace
698n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
699n/a self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
700n/a self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
701n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
702n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
703n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
704n/a sys.maxsize-1)
705n/a self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
706n/a self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
707n/a self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
708n/a
709n/a self.checkequal([], ' ', 'split')
710n/a self.checkequal(['a'], ' a ', 'split')
711n/a self.checkequal(['a', 'b'], ' a b ', 'split')
712n/a self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
713n/a self.checkequal(['a b c '], ' a b c ', 'split', None, 0)
714n/a self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
715n/a self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
716n/a self.checkequal(['a', 'b', 'c'], ' a b c ', 'split', None, 3)
717n/a self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
718n/a aaa = ' a '*20
719n/a self.checkequal(['a']*20, aaa, 'split')
720n/a self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
721n/a self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
722n/a
723n/a for b in ('arf\tbarf', 'arf\nbarf', 'arf\rbarf',
724n/a 'arf\fbarf', 'arf\vbarf'):
725n/a self.checkequal(['arf', 'barf'], b, 'split')
726n/a self.checkequal(['arf', 'barf'], b, 'split', None)
727n/a self.checkequal(['arf', 'barf'], b, 'split', None, 2)
728n/a
729n/a def test_additional_rsplit(self):
730n/a self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
731n/a 'this is the rsplit function', 'rsplit')
732n/a
733n/a # by whitespace
734n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
735n/a self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
736n/a self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
737n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
738n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
739n/a self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
740n/a sys.maxsize-20)
741n/a self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
742n/a self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
743n/a self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
744n/a
745n/a self.checkequal([], ' ', 'rsplit')
746n/a self.checkequal(['a'], ' a ', 'rsplit')
747n/a self.checkequal(['a', 'b'], ' a b ', 'rsplit')
748n/a self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
749n/a self.checkequal([' a b c'], ' a b c ', 'rsplit',
750n/a None, 0)
751n/a self.checkequal([' a b','c'], ' a b c ', 'rsplit',
752n/a None, 1)
753n/a self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
754n/a None, 2)
755n/a self.checkequal(['a', 'b', 'c'], ' a b c ', 'rsplit',
756n/a None, 3)
757n/a self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
758n/a aaa = ' a '*20
759n/a self.checkequal(['a']*20, aaa, 'rsplit')
760n/a self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
761n/a self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
762n/a
763n/a for b in ('arf\tbarf', 'arf\nbarf', 'arf\rbarf',
764n/a 'arf\fbarf', 'arf\vbarf'):
765n/a self.checkequal(['arf', 'barf'], b, 'rsplit')
766n/a self.checkequal(['arf', 'barf'], b, 'rsplit', None)
767n/a self.checkequal(['arf', 'barf'], b, 'rsplit', None, 2)
768n/a
769n/a def test_strip_whitespace(self):
770n/a self.checkequal('hello', ' hello ', 'strip')
771n/a self.checkequal('hello ', ' hello ', 'lstrip')
772n/a self.checkequal(' hello', ' hello ', 'rstrip')
773n/a self.checkequal('hello', 'hello', 'strip')
774n/a
775n/a b = ' \t\n\r\f\vabc \t\n\r\f\v'
776n/a self.checkequal('abc', b, 'strip')
777n/a self.checkequal('abc \t\n\r\f\v', b, 'lstrip')
778n/a self.checkequal(' \t\n\r\f\vabc', b, 'rstrip')
779n/a
780n/a # strip/lstrip/rstrip with None arg
781n/a self.checkequal('hello', ' hello ', 'strip', None)
782n/a self.checkequal('hello ', ' hello ', 'lstrip', None)
783n/a self.checkequal(' hello', ' hello ', 'rstrip', None)
784n/a self.checkequal('hello', 'hello', 'strip', None)
785n/a
786n/a def test_strip(self):
787n/a # strip/lstrip/rstrip with str arg
788n/a self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
789n/a self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
790n/a self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
791n/a self.checkequal('hello', 'hello', 'strip', 'xyz')
792n/a self.checkequal('', 'mississippi', 'strip', 'mississippi')
793n/a
794n/a # only trim the start and end; does not strip internal characters
795n/a self.checkequal('mississipp', 'mississippi', 'strip', 'i')
796n/a
797n/a self.checkraises(TypeError, 'hello', 'strip', 42, 42)
798n/a self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
799n/a self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
800n/a
801n/a def test_ljust(self):
802n/a self.checkequal('abc ', 'abc', 'ljust', 10)
803n/a self.checkequal('abc ', 'abc', 'ljust', 6)
804n/a self.checkequal('abc', 'abc', 'ljust', 3)
805n/a self.checkequal('abc', 'abc', 'ljust', 2)
806n/a self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
807n/a self.checkraises(TypeError, 'abc', 'ljust')
808n/a
809n/a def test_rjust(self):
810n/a self.checkequal(' abc', 'abc', 'rjust', 10)
811n/a self.checkequal(' abc', 'abc', 'rjust', 6)
812n/a self.checkequal('abc', 'abc', 'rjust', 3)
813n/a self.checkequal('abc', 'abc', 'rjust', 2)
814n/a self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
815n/a self.checkraises(TypeError, 'abc', 'rjust')
816n/a
817n/a def test_center(self):
818n/a self.checkequal(' abc ', 'abc', 'center', 10)
819n/a self.checkequal(' abc ', 'abc', 'center', 6)
820n/a self.checkequal('abc', 'abc', 'center', 3)
821n/a self.checkequal('abc', 'abc', 'center', 2)
822n/a self.checkequal('***abc****', 'abc', 'center', 10, '*')
823n/a self.checkraises(TypeError, 'abc', 'center')
824n/a
825n/a def test_swapcase(self):
826n/a self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
827n/a
828n/a self.checkraises(TypeError, 'hello', 'swapcase', 42)
829n/a
830n/a def test_zfill(self):
831n/a self.checkequal('123', '123', 'zfill', 2)
832n/a self.checkequal('123', '123', 'zfill', 3)
833n/a self.checkequal('0123', '123', 'zfill', 4)
834n/a self.checkequal('+123', '+123', 'zfill', 3)
835n/a self.checkequal('+123', '+123', 'zfill', 4)
836n/a self.checkequal('+0123', '+123', 'zfill', 5)
837n/a self.checkequal('-123', '-123', 'zfill', 3)
838n/a self.checkequal('-123', '-123', 'zfill', 4)
839n/a self.checkequal('-0123', '-123', 'zfill', 5)
840n/a self.checkequal('000', '', 'zfill', 3)
841n/a self.checkequal('34', '34', 'zfill', 1)
842n/a self.checkequal('0034', '34', 'zfill', 4)
843n/a
844n/a self.checkraises(TypeError, '123', 'zfill')
845n/a
846n/a def test_islower(self):
847n/a self.checkequal(False, '', 'islower')
848n/a self.checkequal(True, 'a', 'islower')
849n/a self.checkequal(False, 'A', 'islower')
850n/a self.checkequal(False, '\n', 'islower')
851n/a self.checkequal(True, 'abc', 'islower')
852n/a self.checkequal(False, 'aBc', 'islower')
853n/a self.checkequal(True, 'abc\n', 'islower')
854n/a self.checkraises(TypeError, 'abc', 'islower', 42)
855n/a
856n/a def test_isupper(self):
857n/a self.checkequal(False, '', 'isupper')
858n/a self.checkequal(False, 'a', 'isupper')
859n/a self.checkequal(True, 'A', 'isupper')
860n/a self.checkequal(False, '\n', 'isupper')
861n/a self.checkequal(True, 'ABC', 'isupper')
862n/a self.checkequal(False, 'AbC', 'isupper')
863n/a self.checkequal(True, 'ABC\n', 'isupper')
864n/a self.checkraises(TypeError, 'abc', 'isupper', 42)
865n/a
866n/a def test_istitle(self):
867n/a self.checkequal(False, '', 'istitle')
868n/a self.checkequal(False, 'a', 'istitle')
869n/a self.checkequal(True, 'A', 'istitle')
870n/a self.checkequal(False, '\n', 'istitle')
871n/a self.checkequal(True, 'A Titlecased Line', 'istitle')
872n/a self.checkequal(True, 'A\nTitlecased Line', 'istitle')
873n/a self.checkequal(True, 'A Titlecased, Line', 'istitle')
874n/a self.checkequal(False, 'Not a capitalized String', 'istitle')
875n/a self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
876n/a self.checkequal(False, 'Not--a Titlecase String', 'istitle')
877n/a self.checkequal(False, 'NOT', 'istitle')
878n/a self.checkraises(TypeError, 'abc', 'istitle', 42)
879n/a
880n/a def test_isspace(self):
881n/a self.checkequal(False, '', 'isspace')
882n/a self.checkequal(False, 'a', 'isspace')
883n/a self.checkequal(True, ' ', 'isspace')
884n/a self.checkequal(True, '\t', 'isspace')
885n/a self.checkequal(True, '\r', 'isspace')
886n/a self.checkequal(True, '\n', 'isspace')
887n/a self.checkequal(True, ' \t\r\n', 'isspace')
888n/a self.checkequal(False, ' \t\r\na', 'isspace')
889n/a self.checkraises(TypeError, 'abc', 'isspace', 42)
890n/a
891n/a def test_isalpha(self):
892n/a self.checkequal(False, '', 'isalpha')
893n/a self.checkequal(True, 'a', 'isalpha')
894n/a self.checkequal(True, 'A', 'isalpha')
895n/a self.checkequal(False, '\n', 'isalpha')
896n/a self.checkequal(True, 'abc', 'isalpha')
897n/a self.checkequal(False, 'aBc123', 'isalpha')
898n/a self.checkequal(False, 'abc\n', 'isalpha')
899n/a self.checkraises(TypeError, 'abc', 'isalpha', 42)
900n/a
901n/a def test_isalnum(self):
902n/a self.checkequal(False, '', 'isalnum')
903n/a self.checkequal(True, 'a', 'isalnum')
904n/a self.checkequal(True, 'A', 'isalnum')
905n/a self.checkequal(False, '\n', 'isalnum')
906n/a self.checkequal(True, '123abc456', 'isalnum')
907n/a self.checkequal(True, 'a1b3c', 'isalnum')
908n/a self.checkequal(False, 'aBc000 ', 'isalnum')
909n/a self.checkequal(False, 'abc\n', 'isalnum')
910n/a self.checkraises(TypeError, 'abc', 'isalnum', 42)
911n/a
912n/a def test_isdigit(self):
913n/a self.checkequal(False, '', 'isdigit')
914n/a self.checkequal(False, 'a', 'isdigit')
915n/a self.checkequal(True, '0', 'isdigit')
916n/a self.checkequal(True, '0123456789', 'isdigit')
917n/a self.checkequal(False, '0123456789a', 'isdigit')
918n/a
919n/a self.checkraises(TypeError, 'abc', 'isdigit', 42)
920n/a
921n/a def test_title(self):
922n/a self.checkequal(' Hello ', ' hello ', 'title')
923n/a self.checkequal('Hello ', 'hello ', 'title')
924n/a self.checkequal('Hello ', 'Hello ', 'title')
925n/a self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
926n/a self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
927n/a self.checkequal('Getint', "getInt", 'title')
928n/a self.checkraises(TypeError, 'hello', 'title', 42)
929n/a
930n/a def test_splitlines(self):
931n/a self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
932n/a self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
933n/a self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
934n/a self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
935n/a self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
936n/a self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
937n/a self.checkequal(['', 'abc', 'def', 'ghi', ''],
938n/a "\nabc\ndef\r\nghi\n\r", 'splitlines', False)
939n/a self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
940n/a "\nabc\ndef\r\nghi\n\r", 'splitlines', True)
941n/a self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r",
942n/a 'splitlines', keepends=False)
943n/a self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
944n/a "\nabc\ndef\r\nghi\n\r", 'splitlines', keepends=True)
945n/a
946n/a self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
947n/a
948n/a
949n/aclass CommonTest(BaseTest):
950n/a # This testcase contains tests that can be used in all
951n/a # stringlike classes. Currently this is str and UserString.
952n/a
953n/a def test_hash(self):
954n/a # SF bug 1054139: += optimization was not invalidating cached hash value
955n/a a = self.type2test('DNSSEC')
956n/a b = self.type2test('')
957n/a for c in a:
958n/a b += c
959n/a hash(b)
960n/a self.assertEqual(hash(a), hash(b))
961n/a
962n/a def test_capitalize_nonascii(self):
963n/a # check that titlecased chars are lowered correctly
964n/a # \u1ffc is the titlecased char
965n/a self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3',
966n/a '\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
967n/a # check with cased non-letter chars
968n/a self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
969n/a '\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize')
970n/a self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
971n/a '\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize')
972n/a self.checkequal('\u2160\u2171\u2172',
973n/a '\u2160\u2161\u2162', 'capitalize')
974n/a self.checkequal('\u2160\u2171\u2172',
975n/a '\u2170\u2171\u2172', 'capitalize')
976n/a # check with Ll chars with no upper - nothing changes here
977n/a self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7',
978n/a '\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
979n/a
980n/a
981n/aclass MixinStrUnicodeUserStringTest:
982n/a # additional tests that only work for
983n/a # stringlike objects, i.e. str, UserString
984n/a
985n/a def test_startswith(self):
986n/a self.checkequal(True, 'hello', 'startswith', 'he')
987n/a self.checkequal(True, 'hello', 'startswith', 'hello')
988n/a self.checkequal(False, 'hello', 'startswith', 'hello world')
989n/a self.checkequal(True, 'hello', 'startswith', '')
990n/a self.checkequal(False, 'hello', 'startswith', 'ello')
991n/a self.checkequal(True, 'hello', 'startswith', 'ello', 1)
992n/a self.checkequal(True, 'hello', 'startswith', 'o', 4)
993n/a self.checkequal(False, 'hello', 'startswith', 'o', 5)
994n/a self.checkequal(True, 'hello', 'startswith', '', 5)
995n/a self.checkequal(False, 'hello', 'startswith', 'lo', 6)
996n/a self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
997n/a self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
998n/a self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
999n/a self.checkequal(True, '', 'startswith', '', 0, 1)
1000n/a self.checkequal(True, '', 'startswith', '', 0, 0)
1001n/a self.checkequal(False, '', 'startswith', '', 1, 0)
1002n/a
1003n/a # test negative indices
1004n/a self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
1005n/a self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
1006n/a self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
1007n/a self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
1008n/a self.checkequal(False, 'hello', 'startswith', 'ello', -5)
1009n/a self.checkequal(True, 'hello', 'startswith', 'ello', -4)
1010n/a self.checkequal(False, 'hello', 'startswith', 'o', -2)
1011n/a self.checkequal(True, 'hello', 'startswith', 'o', -1)
1012n/a self.checkequal(True, 'hello', 'startswith', '', -3, -3)
1013n/a self.checkequal(False, 'hello', 'startswith', 'lo', -9)
1014n/a
1015n/a self.checkraises(TypeError, 'hello', 'startswith')
1016n/a self.checkraises(TypeError, 'hello', 'startswith', 42)
1017n/a
1018n/a # test tuple arguments
1019n/a self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
1020n/a self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
1021n/a self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
1022n/a self.checkequal(False, 'hello', 'startswith', ())
1023n/a self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
1024n/a 'rld', 'lowo'), 3)
1025n/a self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
1026n/a 'rld'), 3)
1027n/a self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
1028n/a self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
1029n/a self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
1030n/a
1031n/a self.checkraises(TypeError, 'hello', 'startswith', (42,))
1032n/a
1033n/a def test_endswith(self):
1034n/a self.checkequal(True, 'hello', 'endswith', 'lo')
1035n/a self.checkequal(False, 'hello', 'endswith', 'he')
1036n/a self.checkequal(True, 'hello', 'endswith', '')
1037n/a self.checkequal(False, 'hello', 'endswith', 'hello world')
1038n/a self.checkequal(False, 'helloworld', 'endswith', 'worl')
1039n/a self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
1040n/a self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
1041n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
1042n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
1043n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
1044n/a self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
1045n/a self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
1046n/a self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
1047n/a self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
1048n/a self.checkequal(True, '', 'endswith', '', 0, 1)
1049n/a self.checkequal(True, '', 'endswith', '', 0, 0)
1050n/a self.checkequal(False, '', 'endswith', '', 1, 0)
1051n/a
1052n/a # test negative indices
1053n/a self.checkequal(True, 'hello', 'endswith', 'lo', -2)
1054n/a self.checkequal(False, 'hello', 'endswith', 'he', -2)
1055n/a self.checkequal(True, 'hello', 'endswith', '', -3, -3)
1056n/a self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
1057n/a self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
1058n/a self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
1059n/a self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
1060n/a self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
1061n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
1062n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
1063n/a self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
1064n/a self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
1065n/a self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
1066n/a
1067n/a self.checkraises(TypeError, 'hello', 'endswith')
1068n/a self.checkraises(TypeError, 'hello', 'endswith', 42)
1069n/a
1070n/a # test tuple arguments
1071n/a self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1072n/a self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1073n/a self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1074n/a self.checkequal(False, 'hello', 'endswith', ())
1075n/a self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1076n/a 'rld', 'lowo'), 3)
1077n/a self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1078n/a 'rld'), 3, -1)
1079n/a self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1080n/a self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
1081n/a self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
1082n/a
1083n/a self.checkraises(TypeError, 'hello', 'endswith', (42,))
1084n/a
1085n/a def test___contains__(self):
1086n/a self.checkequal(True, '', '__contains__', '')
1087n/a self.checkequal(True, 'abc', '__contains__', '')
1088n/a self.checkequal(False, 'abc', '__contains__', '\0')
1089n/a self.checkequal(True, '\0abc', '__contains__', '\0')
1090n/a self.checkequal(True, 'abc\0', '__contains__', '\0')
1091n/a self.checkequal(True, '\0abc', '__contains__', 'a')
1092n/a self.checkequal(True, 'asdf', '__contains__', 'asdf')
1093n/a self.checkequal(False, 'asd', '__contains__', 'asdf')
1094n/a self.checkequal(False, '', '__contains__', 'asdf')
1095n/a
1096n/a def test_subscript(self):
1097n/a self.checkequal('a', 'abc', '__getitem__', 0)
1098n/a self.checkequal('c', 'abc', '__getitem__', -1)
1099n/a self.checkequal('a', 'abc', '__getitem__', 0)
1100n/a self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1101n/a self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1102n/a self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
1103n/a self.checkequal('', 'abc', '__getitem__', slice(0, 0))
1104n/a
1105n/a self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1106n/a
1107n/a def test_slice(self):
1108n/a self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1109n/a self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1110n/a self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
1111n/a self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
1112n/a self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
1113n/a self.checkequal('', 'abc', '__getitem__', slice(2, 2))
1114n/a self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
1115n/a self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
1116n/a self.checkequal('', 'abc', '__getitem__', slice(2, 1))
1117n/a
1118n/a self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1119n/a
1120n/a def test_extended_getslice(self):
1121n/a # Test extended slicing by comparing with list slicing.
1122n/a s = string.ascii_letters + string.digits
1123n/a indices = (0, None, 1, 3, 41, -1, -2, -37)
1124n/a for start in indices:
1125n/a for stop in indices:
1126n/a # Skip step 0 (invalid)
1127n/a for step in indices[1:]:
1128n/a L = list(s)[start:stop:step]
1129n/a self.checkequal("".join(L), s, '__getitem__',
1130n/a slice(start, stop, step))
1131n/a
1132n/a def test_mul(self):
1133n/a self.checkequal('', 'abc', '__mul__', -1)
1134n/a self.checkequal('', 'abc', '__mul__', 0)
1135n/a self.checkequal('abc', 'abc', '__mul__', 1)
1136n/a self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1137n/a self.checkraises(TypeError, 'abc', '__mul__')
1138n/a self.checkraises(TypeError, 'abc', '__mul__', '')
1139n/a # XXX: on a 64-bit system, this doesn't raise an overflow error,
1140n/a # but either raises a MemoryError, or succeeds (if you have 54TiB)
1141n/a #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
1142n/a
1143n/a def test_join(self):
1144n/a # join now works with any sequence type
1145n/a # moved here, because the argument order is
1146n/a # different in string.join
1147n/a self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1148n/a self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
1149n/a self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1150n/a self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
1151n/a self.checkequal('w x y z', ' ', 'join', Sequence())
1152n/a self.checkequal('abc', 'a', 'join', ('abc',))
1153n/a self.checkequal('z', 'a', 'join', UserList(['z']))
1154n/a self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
1155n/a self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
1156n/a for i in [5, 25, 125]:
1157n/a self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1158n/a ['a' * i] * i)
1159n/a self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1160n/a ('a' * i,) * i)
1161n/a
1162n/a #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
1163n/a self.checkequal('a b c', ' ', 'join', BadSeq2())
1164n/a
1165n/a self.checkraises(TypeError, ' ', 'join')
1166n/a self.checkraises(TypeError, ' ', 'join', None)
1167n/a self.checkraises(TypeError, ' ', 'join', 7)
1168n/a self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
1169n/a try:
1170n/a def f():
1171n/a yield 4 + ""
1172n/a self.fixtype(' ').join(f())
1173n/a except TypeError as e:
1174n/a if '+' not in str(e):
1175n/a self.fail('join() ate exception message')
1176n/a else:
1177n/a self.fail('exception not raised')
1178n/a
1179n/a def test_formatting(self):
1180n/a self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1181n/a self.checkequal('+10+', '+%d+', '__mod__', 10)
1182n/a self.checkequal('a', "%c", '__mod__', "a")
1183n/a self.checkequal('a', "%c", '__mod__', "a")
1184n/a self.checkequal('"', "%c", '__mod__', 34)
1185n/a self.checkequal('$', "%c", '__mod__', 36)
1186n/a self.checkequal('10', "%d", '__mod__', 10)
1187n/a self.checkequal('\x7f', "%c", '__mod__', 0x7f)
1188n/a
1189n/a for ordinal in (-100, 0x200000):
1190n/a # unicode raises ValueError, str raises OverflowError
1191n/a self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1192n/a
1193n/a longvalue = sys.maxsize + 10
1194n/a slongvalue = str(longvalue)
1195n/a self.checkequal(' 42', '%3ld', '__mod__', 42)
1196n/a self.checkequal('42', '%d', '__mod__', 42.0)
1197n/a self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1198n/a self.checkcall('%d', '__mod__', float(longvalue))
1199n/a self.checkequal('0042.00', '%07.2f', '__mod__', 42)
1200n/a self.checkequal('0042.00', '%07.2F', '__mod__', 42)
1201n/a
1202n/a self.checkraises(TypeError, 'abc', '__mod__')
1203n/a self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1204n/a self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1205n/a self.checkraises(TypeError, '%c', '__mod__', (None,))
1206n/a self.checkraises(ValueError, '%(foo', '__mod__', {})
1207n/a self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1208n/a self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1209n/a self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
1210n/a
1211n/a # argument names with properly nested brackets are supported
1212n/a self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1213n/a
1214n/a # 100 is a magic number in PyUnicode_Format, this forces a resize
1215n/a self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1216n/a
1217n/a self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1218n/a self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1219n/a self.checkraises(ValueError, '%10', '__mod__', (42,))
1220n/a
1221n/a # Outrageously large width or precision should raise ValueError.
1222n/a self.checkraises(ValueError, '%%%df' % (2**64), '__mod__', (3.2))
1223n/a self.checkraises(ValueError, '%%.%df' % (2**64), '__mod__', (3.2))
1224n/a self.checkraises(OverflowError, '%*s', '__mod__',
1225n/a (sys.maxsize + 1, ''))
1226n/a self.checkraises(OverflowError, '%.*f', '__mod__',
1227n/a (sys.maxsize + 1, 1. / 7))
1228n/a
1229n/a class X(object): pass
1230n/a self.checkraises(TypeError, 'abc', '__mod__', X())
1231n/a
1232n/a @support.cpython_only
1233n/a def test_formatting_c_limits(self):
1234n/a from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX
1235n/a SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1
1236n/a self.checkraises(OverflowError, '%*s', '__mod__',
1237n/a (PY_SSIZE_T_MAX + 1, ''))
1238n/a self.checkraises(OverflowError, '%.*f', '__mod__',
1239n/a (INT_MAX + 1, 1. / 7))
1240n/a # Issue 15989
1241n/a self.checkraises(OverflowError, '%*s', '__mod__',
1242n/a (SIZE_MAX + 1, ''))
1243n/a self.checkraises(OverflowError, '%.*f', '__mod__',
1244n/a (UINT_MAX + 1, 1. / 7))
1245n/a
1246n/a def test_floatformatting(self):
1247n/a # float formatting
1248n/a for prec in range(100):
1249n/a format = '%%.%if' % prec
1250n/a value = 0.01
1251n/a for x in range(60):
1252n/a value = value * 3.14159265359 / 3.0 * 10.0
1253n/a self.checkcall(format, "__mod__", value)
1254n/a
1255n/a def test_inplace_rewrites(self):
1256n/a # Check that strings don't copy and modify cached single-character strings
1257n/a self.checkequal('a', 'A', 'lower')
1258n/a self.checkequal(True, 'A', 'isupper')
1259n/a self.checkequal('A', 'a', 'upper')
1260n/a self.checkequal(True, 'a', 'islower')
1261n/a
1262n/a self.checkequal('a', 'A', 'replace', 'A', 'a')
1263n/a self.checkequal(True, 'A', 'isupper')
1264n/a
1265n/a self.checkequal('A', 'a', 'capitalize')
1266n/a self.checkequal(True, 'a', 'islower')
1267n/a
1268n/a self.checkequal('A', 'a', 'swapcase')
1269n/a self.checkequal(True, 'a', 'islower')
1270n/a
1271n/a self.checkequal('A', 'a', 'title')
1272n/a self.checkequal(True, 'a', 'islower')
1273n/a
1274n/a def test_partition(self):
1275n/a
1276n/a self.checkequal(('this is the par', 'ti', 'tion method'),
1277n/a 'this is the partition method', 'partition', 'ti')
1278n/a
1279n/a # from raymond's original specification
1280n/a S = 'http://www.python.org'
1281n/a self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1282n/a self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1283n/a self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1284n/a self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1285n/a
1286n/a self.checkraises(ValueError, S, 'partition', '')
1287n/a self.checkraises(TypeError, S, 'partition', None)
1288n/a
1289n/a def test_rpartition(self):
1290n/a
1291n/a self.checkequal(('this is the rparti', 'ti', 'on method'),
1292n/a 'this is the rpartition method', 'rpartition', 'ti')
1293n/a
1294n/a # from raymond's original specification
1295n/a S = 'http://www.python.org'
1296n/a self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
1297n/a self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
1298n/a self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1299n/a self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1300n/a
1301n/a self.checkraises(ValueError, S, 'rpartition', '')
1302n/a self.checkraises(TypeError, S, 'rpartition', None)
1303n/a
1304n/a def test_none_arguments(self):
1305n/a # issue 11828
1306n/a s = 'hello'
1307n/a self.checkequal(2, s, 'find', 'l', None)
1308n/a self.checkequal(3, s, 'find', 'l', -2, None)
1309n/a self.checkequal(2, s, 'find', 'l', None, -2)
1310n/a self.checkequal(0, s, 'find', 'h', None, None)
1311n/a
1312n/a self.checkequal(3, s, 'rfind', 'l', None)
1313n/a self.checkequal(3, s, 'rfind', 'l', -2, None)
1314n/a self.checkequal(2, s, 'rfind', 'l', None, -2)
1315n/a self.checkequal(0, s, 'rfind', 'h', None, None)
1316n/a
1317n/a self.checkequal(2, s, 'index', 'l', None)
1318n/a self.checkequal(3, s, 'index', 'l', -2, None)
1319n/a self.checkequal(2, s, 'index', 'l', None, -2)
1320n/a self.checkequal(0, s, 'index', 'h', None, None)
1321n/a
1322n/a self.checkequal(3, s, 'rindex', 'l', None)
1323n/a self.checkequal(3, s, 'rindex', 'l', -2, None)
1324n/a self.checkequal(2, s, 'rindex', 'l', None, -2)
1325n/a self.checkequal(0, s, 'rindex', 'h', None, None)
1326n/a
1327n/a self.checkequal(2, s, 'count', 'l', None)
1328n/a self.checkequal(1, s, 'count', 'l', -2, None)
1329n/a self.checkequal(1, s, 'count', 'l', None, -2)
1330n/a self.checkequal(0, s, 'count', 'x', None, None)
1331n/a
1332n/a self.checkequal(True, s, 'endswith', 'o', None)
1333n/a self.checkequal(True, s, 'endswith', 'lo', -2, None)
1334n/a self.checkequal(True, s, 'endswith', 'l', None, -2)
1335n/a self.checkequal(False, s, 'endswith', 'x', None, None)
1336n/a
1337n/a self.checkequal(True, s, 'startswith', 'h', None)
1338n/a self.checkequal(True, s, 'startswith', 'l', -2, None)
1339n/a self.checkequal(True, s, 'startswith', 'h', None, -2)
1340n/a self.checkequal(False, s, 'startswith', 'x', None, None)
1341n/a
1342n/a def test_find_etc_raise_correct_error_messages(self):
1343n/a # issue 11828
1344n/a s = 'hello'
1345n/a x = 'x'
1346n/a self.assertRaisesRegex(TypeError, r'^find\(', s.find,
1347n/a x, None, None, None)
1348n/a self.assertRaisesRegex(TypeError, r'^rfind\(', s.rfind,
1349n/a x, None, None, None)
1350n/a self.assertRaisesRegex(TypeError, r'^index\(', s.index,
1351n/a x, None, None, None)
1352n/a self.assertRaisesRegex(TypeError, r'^rindex\(', s.rindex,
1353n/a x, None, None, None)
1354n/a self.assertRaisesRegex(TypeError, r'^count\(', s.count,
1355n/a x, None, None, None)
1356n/a self.assertRaisesRegex(TypeError, r'^startswith\(', s.startswith,
1357n/a x, None, None, None)
1358n/a self.assertRaisesRegex(TypeError, r'^endswith\(', s.endswith,
1359n/a x, None, None, None)
1360n/a
1361n/a # issue #15534
1362n/a self.checkequal(10, "...\u043c......<", "find", "<")
1363n/a
1364n/a
1365n/aclass MixinStrUnicodeTest:
1366n/a # Additional tests that only work with str.
1367n/a
1368n/a def test_bug1001011(self):
1369n/a # Make sure join returns a NEW object for single item sequences
1370n/a # involving a subclass.
1371n/a # Make sure that it is of the appropriate type.
1372n/a # Check the optimisation still occurs for standard objects.
1373n/a t = self.type2test
1374n/a class subclass(t):
1375n/a pass
1376n/a s1 = subclass("abcd")
1377n/a s2 = t().join([s1])
1378n/a self.assertIsNot(s1, s2)
1379n/a self.assertIs(type(s2), t)
1380n/a
1381n/a s1 = t("abcd")
1382n/a s2 = t().join([s1])
1383n/a self.assertIs(s1, s2)