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