Changeset 391 for python/trunk/Lib/test/string_tests.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/string_tests.py
r2 r391 6 6 from test import test_support 7 7 from UserList import UserList 8 import _testcapi 8 9 9 10 class Sequence: … … 63 64 object = subtype(object) 64 65 realresult = getattr(object, methodname)(*args) 65 self.assert _(object is not realresult)66 self.assertTrue(object is not realresult) 66 67 67 68 # check that object.method(*args) raises exc … … 186 187 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0) 187 188 189 # issue 7458 190 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0) 191 188 192 # For a variety of combinations, 189 193 # verify that str.find() matches __contains__ … … 206 210 r1 = (loc != -1) 207 211 r2 = j in i 208 if r1 != r2: 209 self.assertEqual(r1, r2) 212 self.assertEqual(r1, r2) 210 213 if loc != -1: 211 214 self.assertEqual(i[loc:loc+len(j)], j) … … 230 233 self.checkraises(TypeError, 'hello', 'rfind') 231 234 self.checkraises(TypeError, 'hello', 'rfind', 42) 235 236 # For a variety of combinations, 237 # verify that str.rfind() matches __contains__ 238 # and that the found substring is really at that location 239 charset = ['', 'a', 'b', 'c'] 240 digits = 5 241 base = len(charset) 242 teststrings = set() 243 for i in xrange(base ** digits): 244 entry = [] 245 for j in xrange(digits): 246 i, m = divmod(i, base) 247 entry.append(charset[m]) 248 teststrings.add(''.join(entry)) 249 teststrings = list(teststrings) 250 for i in teststrings: 251 i = self.fixtype(i) 252 for j in teststrings: 253 loc = i.rfind(j) 254 r1 = (loc != -1) 255 r2 = j in i 256 self.assertEqual(r1, r2) 257 if loc != -1: 258 self.assertEqual(i[loc:loc+len(j)], self.fixtype(j)) 259 260 # issue 7458 261 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0) 232 262 233 263 def test_index(self): … … 687 717 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby") 688 718 689 ba = buffer('a') 690 bb = buffer('b') 719 with test_support.check_py3k_warnings(): 720 ba = buffer('a') 721 bb = buffer('b') 691 722 EQ("bbc", "abc", "replace", ba, bb) 692 723 EQ("aac", "abc", "replace", bb, ba) … … 1084 1115 self.checkraises(ValueError, '%10', '__mod__', (42,)) 1085 1116 1117 width = int(_testcapi.PY_SSIZE_T_MAX + 1) 1118 if width <= sys.maxint: 1119 self.checkraises(OverflowError, '%*s', '__mod__', (width, '')) 1120 prec = int(_testcapi.INT_MAX + 1) 1121 if prec <= sys.maxint: 1122 self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7)) 1123 # Issue 15989 1124 width = int(1 << (_testcapi.PY_SSIZE_T_MAX.bit_length() + 1)) 1125 if width <= sys.maxint: 1126 self.checkraises(OverflowError, '%*s', '__mod__', (width, '')) 1127 prec = int(_testcapi.UINT_MAX + 1) 1128 if prec <= sys.maxint: 1129 self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7)) 1130 1131 class X(object): pass 1132 self.checkraises(TypeError, 'abc', '__mod__', X()) 1133 class X(Exception): 1134 def __getitem__(self, k): 1135 return k 1136 self.checkequal('melon apple', '%(melon)s %(apple)s', '__mod__', X()) 1137 1086 1138 def test_floatformatting(self): 1087 1139 # float formatting … … 1090 1142 value = 0.01 1091 1143 for x in xrange(60): 1092 value = value * 3.141592655 / 3.0 * 10.0 1093 # The formatfloat() code in stringobject.c and 1094 # unicodeobject.c uses a 120 byte buffer and switches from 1095 # 'f' formatting to 'g' at precision 50, so we expect 1096 # OverflowErrors for the ranges x < 50 and prec >= 67. 1097 if x < 50 and prec >= 67: 1098 self.checkraises(OverflowError, format, "__mod__", value) 1099 else: 1100 self.checkcall(format, "__mod__", value) 1144 value = value * 3.14159265359 / 3.0 * 10.0 1145 self.checkcall(format, "__mod__", value) 1101 1146 1102 1147 def test_inplace_rewrites(self): … … 1154 1199 # mixed use of str and unicode 1155 1200 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c')) 1201 1202 def test_none_arguments(self): 1203 # issue 11828 1204 s = 'hello' 1205 self.checkequal(2, s, 'find', 'l', None) 1206 self.checkequal(3, s, 'find', 'l', -2, None) 1207 self.checkequal(2, s, 'find', 'l', None, -2) 1208 self.checkequal(0, s, 'find', 'h', None, None) 1209 1210 self.checkequal(3, s, 'rfind', 'l', None) 1211 self.checkequal(3, s, 'rfind', 'l', -2, None) 1212 self.checkequal(2, s, 'rfind', 'l', None, -2) 1213 self.checkequal(0, s, 'rfind', 'h', None, None) 1214 1215 self.checkequal(2, s, 'index', 'l', None) 1216 self.checkequal(3, s, 'index', 'l', -2, None) 1217 self.checkequal(2, s, 'index', 'l', None, -2) 1218 self.checkequal(0, s, 'index', 'h', None, None) 1219 1220 self.checkequal(3, s, 'rindex', 'l', None) 1221 self.checkequal(3, s, 'rindex', 'l', -2, None) 1222 self.checkequal(2, s, 'rindex', 'l', None, -2) 1223 self.checkequal(0, s, 'rindex', 'h', None, None) 1224 1225 self.checkequal(2, s, 'count', 'l', None) 1226 self.checkequal(1, s, 'count', 'l', -2, None) 1227 self.checkequal(1, s, 'count', 'l', None, -2) 1228 self.checkequal(0, s, 'count', 'x', None, None) 1229 1230 self.checkequal(True, s, 'endswith', 'o', None) 1231 self.checkequal(True, s, 'endswith', 'lo', -2, None) 1232 self.checkequal(True, s, 'endswith', 'l', None, -2) 1233 self.checkequal(False, s, 'endswith', 'x', None, None) 1234 1235 self.checkequal(True, s, 'startswith', 'h', None) 1236 self.checkequal(True, s, 'startswith', 'l', -2, None) 1237 self.checkequal(True, s, 'startswith', 'h', None, -2) 1238 self.checkequal(False, s, 'startswith', 'x', None, None) 1239 1240 def test_find_etc_raise_correct_error_messages(self): 1241 # issue 11828 1242 s = 'hello' 1243 x = 'x' 1244 self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find, 1245 x, None, None, None) 1246 self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind, 1247 x, None, None, None) 1248 self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index, 1249 x, None, None, None) 1250 self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex, 1251 x, None, None, None) 1252 self.assertRaisesRegexp(TypeError, r'^count\(', s.count, 1253 x, None, None, None) 1254 self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith, 1255 x, None, None, None) 1256 self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith, 1257 x, None, None, None) 1156 1258 1157 1259 class MixinStrStringUserStringTest: … … 1221 1323 s1 = subclass("abcd") 1222 1324 s2 = t().join([s1]) 1223 self.assert _(s1 is not s2)1224 self.assert _(type(s2) is t)1325 self.assertTrue(s1 is not s2) 1326 self.assertTrue(type(s2) is t) 1225 1327 1226 1328 s1 = t("abcd") 1227 1329 s2 = t().join([s1]) 1228 self.assert _(s1 is s2)1330 self.assertTrue(s1 is s2) 1229 1331 1230 1332 # Should also test mixed-type join. … … 1232 1334 s1 = subclass("abcd") 1233 1335 s2 = "".join([s1]) 1234 self.assert _(s1 is not s2)1235 self.assert _(type(s2) is t)1336 self.assertTrue(s1 is not s2) 1337 self.assertTrue(type(s2) is t) 1236 1338 1237 1339 s1 = t("abcd") 1238 1340 s2 = "".join([s1]) 1239 self.assert _(s1 is s2)1341 self.assertTrue(s1 is s2) 1240 1342 1241 1343 elif t is str: 1242 1344 s1 = subclass("abcd") 1243 1345 s2 = u"".join([s1]) 1244 self.assert _(s1 is not s2)1245 self.assert _(type(s2) is unicode) # promotes!1346 self.assertTrue(s1 is not s2) 1347 self.assertTrue(type(s2) is unicode) # promotes! 1246 1348 1247 1349 s1 = t("abcd") 1248 1350 s2 = u"".join([s1]) 1249 self.assert _(s1 is not s2)1250 self.assert _(type(s2) is unicode) # promotes!1351 self.assertTrue(s1 is not s2) 1352 self.assertTrue(type(s2) is unicode) # promotes! 1251 1353 1252 1354 else:
Note:
See TracChangeset
for help on using the changeset viewer.