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