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