Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1 | # Python test set -- built-in functions |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 2 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 3 | import test.test_support, unittest |
| 4 | from test.test_support import fcmp, have_unicode, TESTFN, unlink |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 5 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 6 | import sys, warnings, cStringIO, random, UserDict |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 7 | warnings.filterwarnings("ignore", "hex../oct.. of negative int", |
| 8 | FutureWarning, __name__) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 9 | warnings.filterwarnings("ignore", "integer argument expected", |
| 10 | DeprecationWarning, "unittest") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 11 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 12 | class Squares: |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 13 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 14 | def __init__(self, max): |
| 15 | self.max = max |
| 16 | self.sofar = [] |
| 17 | |
| 18 | def __len__(self): return len(self.sofar) |
| 19 | |
| 20 | def __getitem__(self, i): |
| 21 | if not 0 <= i < self.max: raise IndexError |
| 22 | n = len(self.sofar) |
| 23 | while n <= i: |
| 24 | self.sofar.append(n*n) |
| 25 | n += 1 |
| 26 | return self.sofar[i] |
| 27 | |
| 28 | class StrSquares: |
| 29 | |
| 30 | def __init__(self, max): |
| 31 | self.max = max |
| 32 | self.sofar = [] |
| 33 | |
| 34 | def __len__(self): |
| 35 | return len(self.sofar) |
| 36 | |
| 37 | def __getitem__(self, i): |
| 38 | if not 0 <= i < self.max: |
| 39 | raise IndexError |
| 40 | n = len(self.sofar) |
| 41 | while n <= i: |
| 42 | self.sofar.append(str(n*n)) |
| 43 | n += 1 |
| 44 | return self.sofar[i] |
| 45 | |
| 46 | class BitBucket: |
| 47 | def write(self, line): |
| 48 | pass |
| 49 | |
| 50 | L = [ |
| 51 | ('0', 0), |
| 52 | ('1', 1), |
| 53 | ('9', 9), |
| 54 | ('10', 10), |
| 55 | ('99', 99), |
| 56 | ('100', 100), |
| 57 | ('314', 314), |
| 58 | (' 314', 314), |
| 59 | ('314 ', 314), |
| 60 | (' \t\t 314 \t\t ', 314), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 61 | (repr(sys.maxint), sys.maxint), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 62 | (' 1x', ValueError), |
| 63 | (' 1 ', 1), |
| 64 | (' 1\02 ', ValueError), |
| 65 | ('', ValueError), |
| 66 | (' ', ValueError), |
| 67 | (' \t\t ', ValueError) |
| 68 | ] |
| 69 | if have_unicode: |
| 70 | L += [ |
| 71 | (unicode('0'), 0), |
| 72 | (unicode('1'), 1), |
| 73 | (unicode('9'), 9), |
| 74 | (unicode('10'), 10), |
| 75 | (unicode('99'), 99), |
| 76 | (unicode('100'), 100), |
| 77 | (unicode('314'), 314), |
| 78 | (unicode(' 314'), 314), |
| 79 | (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314), |
| 80 | (unicode(' \t\t 314 \t\t '), 314), |
| 81 | (unicode(' 1x'), ValueError), |
| 82 | (unicode(' 1 '), 1), |
| 83 | (unicode(' 1\02 '), ValueError), |
| 84 | (unicode(''), ValueError), |
| 85 | (unicode(' '), ValueError), |
| 86 | (unicode(' \t\t '), ValueError), |
| 87 | (unichr(0x200), ValueError), |
| 88 | ] |
| 89 | |
| 90 | class BuiltinTest(unittest.TestCase): |
| 91 | |
| 92 | def test_import(self): |
| 93 | __import__('sys') |
| 94 | __import__('time') |
| 95 | __import__('string') |
| 96 | self.assertRaises(ImportError, __import__, 'spamspam') |
| 97 | self.assertRaises(TypeError, __import__, 1, 2, 3, 4) |
| 98 | |
| 99 | def test_abs(self): |
| 100 | # int |
| 101 | self.assertEqual(abs(0), 0) |
| 102 | self.assertEqual(abs(1234), 1234) |
| 103 | self.assertEqual(abs(-1234), 1234) |
| 104 | # float |
| 105 | self.assertEqual(abs(0.0), 0.0) |
| 106 | self.assertEqual(abs(3.14), 3.14) |
| 107 | self.assertEqual(abs(-3.14), 3.14) |
| 108 | # long |
| 109 | self.assertEqual(abs(0L), 0L) |
| 110 | self.assertEqual(abs(1234L), 1234L) |
| 111 | self.assertEqual(abs(-1234L), 1234L) |
| 112 | # str |
| 113 | self.assertRaises(TypeError, abs, 'a') |
| 114 | |
| 115 | def test_apply(self): |
| 116 | def f0(*args): |
| 117 | self.assertEqual(args, ()) |
| 118 | def f1(a1): |
| 119 | self.assertEqual(a1, 1) |
| 120 | def f2(a1, a2): |
| 121 | self.assertEqual(a1, 1) |
| 122 | self.assertEqual(a2, 2) |
| 123 | def f3(a1, a2, a3): |
| 124 | self.assertEqual(a1, 1) |
| 125 | self.assertEqual(a2, 2) |
| 126 | self.assertEqual(a3, 3) |
| 127 | apply(f0, ()) |
| 128 | apply(f1, (1,)) |
| 129 | apply(f2, (1, 2)) |
| 130 | apply(f3, (1, 2, 3)) |
| 131 | |
| 132 | # A PyCFunction that takes only positional parameters should allow an |
| 133 | # empty keyword dictionary to pass without a complaint, but raise a |
| 134 | # TypeError if the dictionary is non-empty. |
| 135 | apply(id, (1,), {}) |
| 136 | self.assertRaises(TypeError, apply, id, (1,), {"foo": 1}) |
| 137 | self.assertRaises(TypeError, apply) |
| 138 | self.assertRaises(TypeError, apply, id, 42) |
| 139 | self.assertRaises(TypeError, apply, id, (42,), 42) |
| 140 | |
| 141 | def test_callable(self): |
| 142 | self.assert_(callable(len)) |
| 143 | def f(): pass |
| 144 | self.assert_(callable(f)) |
| 145 | class C: |
| 146 | def meth(self): pass |
| 147 | self.assert_(callable(C)) |
| 148 | x = C() |
| 149 | self.assert_(callable(x.meth)) |
| 150 | self.assert_(not callable(x)) |
| 151 | class D(C): |
| 152 | def __call__(self): pass |
| 153 | y = D() |
| 154 | self.assert_(callable(y)) |
| 155 | y() |
| 156 | |
| 157 | def test_chr(self): |
| 158 | self.assertEqual(chr(32), ' ') |
| 159 | self.assertEqual(chr(65), 'A') |
| 160 | self.assertEqual(chr(97), 'a') |
| 161 | self.assertEqual(chr(0xff), '\xff') |
| 162 | self.assertRaises(ValueError, chr, 256) |
| 163 | self.assertRaises(TypeError, chr) |
| 164 | |
| 165 | def test_cmp(self): |
| 166 | self.assertEqual(cmp(-1, 1), -1) |
| 167 | self.assertEqual(cmp(1, -1), 1) |
| 168 | self.assertEqual(cmp(1, 1), 0) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 169 | # verify that circular objects are not handled |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 170 | a = []; a.append(a) |
| 171 | b = []; b.append(b) |
| 172 | from UserList import UserList |
| 173 | c = UserList(); c.append(c) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 174 | self.assertRaises(RuntimeError, cmp, a, b) |
| 175 | self.assertRaises(RuntimeError, cmp, b, c) |
| 176 | self.assertRaises(RuntimeError, cmp, c, a) |
| 177 | self.assertRaises(RuntimeError, cmp, a, c) |
| 178 | # okay, now break the cycles |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 179 | a.pop(); b.pop(); c.pop() |
| 180 | self.assertRaises(TypeError, cmp) |
| 181 | |
| 182 | def test_coerce(self): |
| 183 | self.assert_(not fcmp(coerce(1, 1.1), (1.0, 1.1))) |
| 184 | self.assertEqual(coerce(1, 1L), (1L, 1L)) |
| 185 | self.assert_(not fcmp(coerce(1L, 1.1), (1.0, 1.1))) |
| 186 | self.assertRaises(TypeError, coerce) |
| 187 | class BadNumber: |
| 188 | def __coerce__(self, other): |
| 189 | raise ValueError |
| 190 | self.assertRaises(ValueError, coerce, 42, BadNumber()) |
Neal Norwitz | abcb0c0 | 2003-01-28 19:21:24 +0000 | [diff] [blame] | 191 | self.assertRaises(OverflowError, coerce, 0.5, int("12345" * 1000)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 192 | |
| 193 | def test_compile(self): |
| 194 | compile('print 1\n', '', 'exec') |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 195 | bom = '\xef\xbb\xbf' |
| 196 | compile(bom + 'print 1\n', '', 'exec') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 197 | self.assertRaises(TypeError, compile) |
| 198 | self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'badmode') |
| 199 | self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'single', 0xff) |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 200 | if have_unicode: |
| 201 | compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 202 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 203 | def test_delattr(self): |
| 204 | import sys |
| 205 | sys.spam = 1 |
| 206 | delattr(sys, 'spam') |
| 207 | self.assertRaises(TypeError, delattr) |
| 208 | |
| 209 | def test_dir(self): |
| 210 | x = 1 |
| 211 | self.assert_('x' in dir()) |
| 212 | import sys |
| 213 | self.assert_('modules' in dir(sys)) |
| 214 | self.assertRaises(TypeError, dir, 42, 42) |
| 215 | |
| 216 | def test_divmod(self): |
| 217 | self.assertEqual(divmod(12, 7), (1, 5)) |
| 218 | self.assertEqual(divmod(-12, 7), (-2, 2)) |
| 219 | self.assertEqual(divmod(12, -7), (-2, -2)) |
| 220 | self.assertEqual(divmod(-12, -7), (1, -5)) |
| 221 | |
| 222 | self.assertEqual(divmod(12L, 7L), (1L, 5L)) |
| 223 | self.assertEqual(divmod(-12L, 7L), (-2L, 2L)) |
| 224 | self.assertEqual(divmod(12L, -7L), (-2L, -2L)) |
| 225 | self.assertEqual(divmod(-12L, -7L), (1L, -5L)) |
| 226 | |
| 227 | self.assertEqual(divmod(12, 7L), (1, 5L)) |
| 228 | self.assertEqual(divmod(-12, 7L), (-2, 2L)) |
| 229 | self.assertEqual(divmod(12L, -7), (-2L, -2)) |
| 230 | self.assertEqual(divmod(-12L, -7), (1L, -5)) |
| 231 | |
| 232 | self.assert_(not fcmp(divmod(3.25, 1.0), (3.0, 0.25))) |
| 233 | self.assert_(not fcmp(divmod(-3.25, 1.0), (-4.0, 0.75))) |
| 234 | self.assert_(not fcmp(divmod(3.25, -1.0), (-4.0, -0.75))) |
| 235 | self.assert_(not fcmp(divmod(-3.25, -1.0), (3.0, -0.25))) |
| 236 | |
| 237 | self.assertRaises(TypeError, divmod) |
| 238 | |
| 239 | def test_eval(self): |
| 240 | self.assertEqual(eval('1+1'), 2) |
| 241 | self.assertEqual(eval(' 1+1\n'), 2) |
| 242 | globals = {'a': 1, 'b': 2} |
| 243 | locals = {'b': 200, 'c': 300} |
| 244 | self.assertEqual(eval('a', globals) , 1) |
| 245 | self.assertEqual(eval('a', globals, locals), 1) |
| 246 | self.assertEqual(eval('b', globals, locals), 200) |
| 247 | self.assertEqual(eval('c', globals, locals), 300) |
| 248 | if have_unicode: |
| 249 | self.assertEqual(eval(unicode('1+1')), 2) |
| 250 | self.assertEqual(eval(unicode(' 1+1\n')), 2) |
| 251 | globals = {'a': 1, 'b': 2} |
| 252 | locals = {'b': 200, 'c': 300} |
| 253 | if have_unicode: |
| 254 | self.assertEqual(eval(unicode('a'), globals), 1) |
| 255 | self.assertEqual(eval(unicode('a'), globals, locals), 1) |
| 256 | self.assertEqual(eval(unicode('b'), globals, locals), 200) |
| 257 | self.assertEqual(eval(unicode('c'), globals, locals), 300) |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 258 | bom = '\xef\xbb\xbf' |
| 259 | self.assertEqual(eval(bom + 'a', globals, locals), 1) |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 260 | self.assertEqual(eval(unicode('u"\xc3\xa5"', 'utf8'), globals), |
| 261 | unicode('\xc3\xa5', 'utf8')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 262 | self.assertRaises(TypeError, eval) |
| 263 | self.assertRaises(TypeError, eval, ()) |
| 264 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 265 | def test_general_eval(self): |
| 266 | # Tests that general mappings can be used for the locals argument |
| 267 | |
| 268 | class M: |
| 269 | "Test mapping interface versus possible calls from eval()." |
| 270 | def __getitem__(self, key): |
| 271 | if key == 'a': |
| 272 | return 12 |
| 273 | raise KeyError |
| 274 | def keys(self): |
| 275 | return list('xyz') |
| 276 | |
| 277 | m = M() |
| 278 | g = globals() |
| 279 | self.assertEqual(eval('a', g, m), 12) |
| 280 | self.assertRaises(NameError, eval, 'b', g, m) |
| 281 | self.assertEqual(eval('dir()', g, m), list('xyz')) |
| 282 | self.assertEqual(eval('globals()', g, m), g) |
| 283 | self.assertEqual(eval('locals()', g, m), m) |
| 284 | |
| 285 | # Verify that dict subclasses work as well |
| 286 | class D(dict): |
| 287 | def __getitem__(self, key): |
| 288 | if key == 'a': |
| 289 | return 12 |
| 290 | return dict.__getitem__(self, key) |
| 291 | def keys(self): |
| 292 | return list('xyz') |
| 293 | |
| 294 | d = D() |
| 295 | self.assertEqual(eval('a', g, d), 12) |
| 296 | self.assertRaises(NameError, eval, 'b', g, d) |
| 297 | self.assertEqual(eval('dir()', g, d), list('xyz')) |
| 298 | self.assertEqual(eval('globals()', g, d), g) |
| 299 | self.assertEqual(eval('locals()', g, d), d) |
| 300 | |
| 301 | # Verify locals stores (used by list comps) |
| 302 | eval('[locals() for i in (2,3)]', g, d) |
| 303 | eval('[locals() for i in (2,3)]', g, UserDict.UserDict()) |
| 304 | |
| 305 | class SpreadSheet: |
| 306 | "Sample application showing nested, calculated lookups." |
| 307 | _cells = {} |
| 308 | def __setitem__(self, key, formula): |
| 309 | self._cells[key] = formula |
| 310 | def __getitem__(self, key ): |
| 311 | return eval(self._cells[key], globals(), self) |
| 312 | |
| 313 | ss = SpreadSheet() |
| 314 | ss['a1'] = '5' |
| 315 | ss['a2'] = 'a1*6' |
| 316 | ss['a3'] = 'a2*7' |
| 317 | self.assertEqual(ss['a3'], 210) |
| 318 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 319 | # Done outside of the method test_z to get the correct scope |
| 320 | z = 0 |
| 321 | f = open(TESTFN, 'w') |
| 322 | f.write('z = z+1\n') |
| 323 | f.write('z = z*2\n') |
| 324 | f.close() |
| 325 | execfile(TESTFN) |
| 326 | |
| 327 | def test_execfile(self): |
| 328 | globals = {'a': 1, 'b': 2} |
| 329 | locals = {'b': 200, 'c': 300} |
| 330 | |
| 331 | self.assertEqual(self.__class__.z, 2) |
| 332 | globals['z'] = 0 |
| 333 | execfile(TESTFN, globals) |
| 334 | self.assertEqual(globals['z'], 2) |
| 335 | locals['z'] = 0 |
| 336 | execfile(TESTFN, globals, locals) |
| 337 | self.assertEqual(locals['z'], 2) |
| 338 | unlink(TESTFN) |
| 339 | self.assertRaises(TypeError, execfile) |
| 340 | import os |
| 341 | self.assertRaises(IOError, execfile, os.curdir) |
| 342 | self.assertRaises(IOError, execfile, "I_dont_exist") |
| 343 | |
| 344 | def test_filter(self): |
| 345 | self.assertEqual(filter(lambda c: 'a' <= c <= 'z', 'Hello World'), 'elloorld') |
| 346 | self.assertEqual(filter(None, [1, 'hello', [], [3], '', None, 9, 0]), [1, 'hello', [3], 9]) |
| 347 | self.assertEqual(filter(lambda x: x > 0, [1, -3, 9, 0, 2]), [1, 9, 2]) |
| 348 | self.assertEqual(filter(None, Squares(10)), [1, 4, 9, 16, 25, 36, 49, 64, 81]) |
| 349 | self.assertEqual(filter(lambda x: x%2, Squares(10)), [1, 9, 25, 49, 81]) |
| 350 | def identity(item): |
| 351 | return 1 |
| 352 | filter(identity, Squares(5)) |
| 353 | self.assertRaises(TypeError, filter) |
| 354 | class BadSeq(object): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 355 | def __getitem__(self, index): |
| 356 | if index<4: |
| 357 | return 42 |
| 358 | raise ValueError |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 359 | self.assertRaises(ValueError, filter, lambda x: x, BadSeq()) |
| 360 | def badfunc(): |
| 361 | pass |
| 362 | self.assertRaises(TypeError, filter, badfunc, range(5)) |
| 363 | |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 364 | # test bltinmodule.c::filtertuple() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 365 | self.assertEqual(filter(None, (1, 2)), (1, 2)) |
| 366 | self.assertEqual(filter(lambda x: x>=3, (1, 2, 3, 4)), (3, 4)) |
| 367 | self.assertRaises(TypeError, filter, 42, (1, 2)) |
| 368 | |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 369 | # test bltinmodule.c::filterstring() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 370 | self.assertEqual(filter(None, "12"), "12") |
| 371 | self.assertEqual(filter(lambda x: x>="3", "1234"), "34") |
| 372 | self.assertRaises(TypeError, filter, 42, "12") |
| 373 | class badstr(str): |
| 374 | def __getitem__(self, index): |
| 375 | raise ValueError |
| 376 | self.assertRaises(ValueError, filter, lambda x: x >="3", badstr("1234")) |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 377 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 378 | class badstr2(str): |
| 379 | def __getitem__(self, index): |
| 380 | return 42 |
| 381 | self.assertRaises(TypeError, filter, lambda x: x >=42, badstr2("1234")) |
| 382 | |
| 383 | class weirdstr(str): |
| 384 | def __getitem__(self, index): |
| 385 | return weirdstr(2*str.__getitem__(self, index)) |
| 386 | self.assertEqual(filter(lambda x: x>="33", weirdstr("1234")), "3344") |
| 387 | |
Walter Dörwald | 5e61e24 | 2003-02-04 17:04:01 +0000 | [diff] [blame] | 388 | class shiftstr(str): |
| 389 | def __getitem__(self, index): |
| 390 | return chr(ord(str.__getitem__(self, index))+1) |
| 391 | self.assertEqual(filter(lambda x: x>="3", shiftstr("1234")), "345") |
| 392 | |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 393 | if have_unicode: |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 394 | # test bltinmodule.c::filterunicode() |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 395 | self.assertEqual(filter(None, unicode("12")), unicode("12")) |
| 396 | self.assertEqual(filter(lambda x: x>="3", unicode("1234")), unicode("34")) |
| 397 | self.assertRaises(TypeError, filter, 42, unicode("12")) |
| 398 | self.assertRaises(ValueError, filter, lambda x: x >="3", badstr(unicode("1234"))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 399 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 400 | class badunicode(unicode): |
| 401 | def __getitem__(self, index): |
| 402 | return 42 |
| 403 | self.assertRaises(TypeError, filter, lambda x: x >=42, badunicode("1234")) |
| 404 | |
| 405 | class weirdunicode(unicode): |
| 406 | def __getitem__(self, index): |
| 407 | return weirdunicode(2*unicode.__getitem__(self, index)) |
| 408 | self.assertEqual( |
| 409 | filter(lambda x: x>=unicode("33"), weirdunicode("1234")), unicode("3344")) |
| 410 | |
Walter Dörwald | 5e61e24 | 2003-02-04 17:04:01 +0000 | [diff] [blame] | 411 | class shiftunicode(unicode): |
| 412 | def __getitem__(self, index): |
| 413 | return unichr(ord(unicode.__getitem__(self, index))+1) |
| 414 | self.assertEqual( |
| 415 | filter(lambda x: x>=unicode("3"), shiftunicode("1234")), |
| 416 | unicode("345") |
| 417 | ) |
| 418 | |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 419 | def test_filter_subclasses(self): |
Walter Dörwald | c8cb5d9 | 2003-08-15 17:52:39 +0000 | [diff] [blame] | 420 | # test that filter() never returns tuple, str or unicode subclasses |
| 421 | # and that the result always goes through __getitem__ |
| 422 | funcs = (None, bool, lambda x: True) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 423 | class tuple2(tuple): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 424 | def __getitem__(self, index): |
| 425 | return 2*tuple.__getitem__(self, index) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 426 | class str2(str): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 427 | def __getitem__(self, index): |
| 428 | return 2*str.__getitem__(self, index) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 429 | inputs = { |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 430 | tuple2: {(): (), (1, 2, 3): (2, 4, 6)}, |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 431 | str2: {"": "", "123": "112233"} |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 432 | } |
| 433 | if have_unicode: |
| 434 | class unicode2(unicode): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 435 | def __getitem__(self, index): |
| 436 | return 2*unicode.__getitem__(self, index) |
| 437 | inputs[unicode2] = { |
| 438 | unicode(): unicode(), |
| 439 | unicode("123"): unicode("112233") |
| 440 | } |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 441 | |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 442 | for (cls, inps) in inputs.iteritems(): |
| 443 | for (inp, exp) in inps.iteritems(): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 444 | # make sure the output goes through __getitem__ |
| 445 | # even if func is None |
| 446 | self.assertEqual( |
| 447 | filter(funcs[0], cls(inp)), |
| 448 | filter(funcs[1], cls(inp)) |
| 449 | ) |
| 450 | for func in funcs: |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 451 | outp = filter(func, cls(inp)) |
| 452 | self.assertEqual(outp, exp) |
| 453 | self.assert_(not isinstance(outp, cls)) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 454 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 455 | def test_float(self): |
| 456 | self.assertEqual(float(3.14), 3.14) |
| 457 | self.assertEqual(float(314), 314.0) |
| 458 | self.assertEqual(float(314L), 314.0) |
| 459 | self.assertEqual(float(" 3.14 "), 3.14) |
| 460 | if have_unicode: |
| 461 | self.assertEqual(float(unicode(" 3.14 ")), 3.14) |
| 462 | self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14) |
| 463 | |
| 464 | def test_getattr(self): |
| 465 | import sys |
| 466 | self.assert_(getattr(sys, 'stdout') is sys.stdout) |
| 467 | self.assertRaises(TypeError, getattr, sys, 1) |
| 468 | self.assertRaises(TypeError, getattr, sys, 1, "foo") |
| 469 | self.assertRaises(TypeError, getattr) |
| 470 | self.assertRaises(UnicodeError, getattr, sys, unichr(sys.maxunicode)) |
| 471 | |
| 472 | def test_hasattr(self): |
| 473 | import sys |
| 474 | self.assert_(hasattr(sys, 'stdout')) |
| 475 | self.assertRaises(TypeError, hasattr, sys, 1) |
| 476 | self.assertRaises(TypeError, hasattr) |
| 477 | self.assertRaises(UnicodeError, hasattr, sys, unichr(sys.maxunicode)) |
| 478 | |
| 479 | def test_hash(self): |
| 480 | hash(None) |
| 481 | self.assertEqual(hash(1), hash(1L)) |
| 482 | self.assertEqual(hash(1), hash(1.0)) |
| 483 | hash('spam') |
| 484 | if have_unicode: |
| 485 | self.assertEqual(hash('spam'), hash(unicode('spam'))) |
| 486 | hash((0,1,2,3)) |
| 487 | def f(): pass |
| 488 | self.assertRaises(TypeError, hash, []) |
| 489 | self.assertRaises(TypeError, hash, {}) |
| 490 | |
| 491 | def test_hex(self): |
| 492 | self.assertEqual(hex(16), '0x10') |
| 493 | self.assertEqual(hex(16L), '0x10L') |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 494 | self.assertEqual(hex(-16), '-0x10') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 495 | self.assertEqual(hex(-16L), '-0x10L') |
| 496 | self.assertRaises(TypeError, hex, {}) |
| 497 | |
| 498 | def test_id(self): |
| 499 | id(None) |
| 500 | id(1) |
| 501 | id(1L) |
| 502 | id(1.0) |
| 503 | id('spam') |
| 504 | id((0,1,2,3)) |
| 505 | id([0,1,2,3]) |
| 506 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 507 | |
| 508 | # Test input() later, together with raw_input |
| 509 | |
| 510 | def test_int(self): |
| 511 | self.assertEqual(int(314), 314) |
| 512 | self.assertEqual(int(3.14), 3) |
| 513 | self.assertEqual(int(314L), 314) |
| 514 | # Check that conversion from float truncates towards zero |
| 515 | self.assertEqual(int(-3.14), -3) |
| 516 | self.assertEqual(int(3.9), 3) |
| 517 | self.assertEqual(int(-3.9), -3) |
| 518 | self.assertEqual(int(3.5), 3) |
| 519 | self.assertEqual(int(-3.5), -3) |
| 520 | # Different base: |
| 521 | self.assertEqual(int("10",16), 16L) |
| 522 | if have_unicode: |
| 523 | self.assertEqual(int(unicode("10"),16), 16L) |
| 524 | # Test conversion from strings and various anomalies |
| 525 | for s, v in L: |
| 526 | for sign in "", "+", "-": |
| 527 | for prefix in "", " ", "\t", " \t\t ": |
| 528 | ss = prefix + sign + s |
| 529 | vv = v |
| 530 | if sign == "-" and v is not ValueError: |
| 531 | vv = -v |
| 532 | try: |
| 533 | self.assertEqual(int(ss), vv) |
| 534 | except v: |
| 535 | pass |
| 536 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 537 | s = repr(-1-sys.maxint) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 538 | self.assertEqual(int(s)+1, -sys.maxint) |
| 539 | # should return long |
| 540 | int(s[1:]) |
| 541 | |
| 542 | # should return long |
| 543 | x = int(1e100) |
| 544 | self.assert_(isinstance(x, long)) |
| 545 | x = int(-1e100) |
| 546 | self.assert_(isinstance(x, long)) |
| 547 | |
| 548 | |
| 549 | # SF bug 434186: 0x80000000/2 != 0x80000000>>1. |
| 550 | # Worked by accident in Windows release build, but failed in debug build. |
| 551 | # Failed in all Linux builds. |
| 552 | x = -1-sys.maxint |
| 553 | self.assertEqual(x >> 1, x//2) |
| 554 | |
| 555 | self.assertRaises(ValueError, int, '123\0') |
| 556 | self.assertRaises(ValueError, int, '53', 40) |
| 557 | |
| 558 | x = int('1' * 600) |
| 559 | self.assert_(isinstance(x, long)) |
| 560 | |
| 561 | if have_unicode: |
| 562 | x = int(unichr(0x661) * 600) |
| 563 | self.assert_(isinstance(x, long)) |
| 564 | |
| 565 | self.assertRaises(TypeError, int, 1, 12) |
| 566 | |
| 567 | self.assertEqual(int('0123', 0), 83) |
| 568 | |
| 569 | def test_intern(self): |
| 570 | self.assertRaises(TypeError, intern) |
| 571 | s = "never interned before" |
| 572 | self.assert_(intern(s) is s) |
| 573 | s2 = s.swapcase().swapcase() |
| 574 | self.assert_(intern(s2) is s) |
| 575 | |
| 576 | def test_iter(self): |
| 577 | self.assertRaises(TypeError, iter) |
| 578 | self.assertRaises(TypeError, iter, 42, 42) |
| 579 | lists = [("1", "2"), ["1", "2"], "12"] |
| 580 | if have_unicode: |
| 581 | lists.append(unicode("12")) |
| 582 | for l in lists: |
| 583 | i = iter(l) |
| 584 | self.assertEqual(i.next(), '1') |
| 585 | self.assertEqual(i.next(), '2') |
| 586 | self.assertRaises(StopIteration, i.next) |
| 587 | |
| 588 | def test_isinstance(self): |
| 589 | class C: |
| 590 | pass |
| 591 | class D(C): |
| 592 | pass |
| 593 | class E: |
| 594 | pass |
| 595 | c = C() |
| 596 | d = D() |
| 597 | e = E() |
| 598 | self.assert_(isinstance(c, C)) |
| 599 | self.assert_(isinstance(d, C)) |
| 600 | self.assert_(not isinstance(e, C)) |
| 601 | self.assert_(not isinstance(c, D)) |
| 602 | self.assert_(not isinstance('foo', E)) |
| 603 | self.assertRaises(TypeError, isinstance, E, 'foo') |
| 604 | self.assertRaises(TypeError, isinstance) |
| 605 | |
| 606 | def test_issubclass(self): |
| 607 | class C: |
| 608 | pass |
| 609 | class D(C): |
| 610 | pass |
| 611 | class E: |
| 612 | pass |
| 613 | c = C() |
| 614 | d = D() |
| 615 | e = E() |
| 616 | self.assert_(issubclass(D, C)) |
| 617 | self.assert_(issubclass(C, C)) |
| 618 | self.assert_(not issubclass(C, D)) |
| 619 | self.assertRaises(TypeError, issubclass, 'foo', E) |
| 620 | self.assertRaises(TypeError, issubclass, E, 'foo') |
| 621 | self.assertRaises(TypeError, issubclass) |
| 622 | |
| 623 | def test_len(self): |
| 624 | self.assertEqual(len('123'), 3) |
| 625 | self.assertEqual(len(()), 0) |
| 626 | self.assertEqual(len((1, 2, 3, 4)), 4) |
| 627 | self.assertEqual(len([1, 2, 3, 4]), 4) |
| 628 | self.assertEqual(len({}), 0) |
| 629 | self.assertEqual(len({'a':1, 'b': 2}), 2) |
| 630 | class BadSeq: |
| 631 | def __len__(self): |
| 632 | raise ValueError |
| 633 | self.assertRaises(ValueError, len, BadSeq()) |
| 634 | |
| 635 | def test_list(self): |
| 636 | self.assertEqual(list([]), []) |
| 637 | l0_3 = [0, 1, 2, 3] |
| 638 | l0_3_bis = list(l0_3) |
| 639 | self.assertEqual(l0_3, l0_3_bis) |
| 640 | self.assert_(l0_3 is not l0_3_bis) |
| 641 | self.assertEqual(list(()), []) |
| 642 | self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) |
| 643 | self.assertEqual(list(''), []) |
| 644 | self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) |
| 645 | |
| 646 | if sys.maxint == 0x7fffffff: |
| 647 | # This test can currently only work on 32-bit machines. |
| 648 | # XXX If/when PySequence_Length() returns a ssize_t, it should be |
| 649 | # XXX re-enabled. |
| 650 | # Verify clearing of bug #556025. |
| 651 | # This assumes that the max data size (sys.maxint) == max |
| 652 | # address size this also assumes that the address size is at |
| 653 | # least 4 bytes with 8 byte addresses, the bug is not well |
| 654 | # tested |
| 655 | # |
| 656 | # Note: This test is expected to SEGV under Cygwin 1.3.12 or |
| 657 | # earlier due to a newlib bug. See the following mailing list |
| 658 | # thread for the details: |
| 659 | |
| 660 | # http://sources.redhat.com/ml/newlib/2002/msg00369.html |
| 661 | self.assertRaises(MemoryError, list, xrange(sys.maxint // 2)) |
| 662 | |
| 663 | def test_long(self): |
| 664 | self.assertEqual(long(314), 314L) |
| 665 | self.assertEqual(long(3.14), 3L) |
| 666 | self.assertEqual(long(314L), 314L) |
| 667 | # Check that conversion from float truncates towards zero |
| 668 | self.assertEqual(long(-3.14), -3L) |
| 669 | self.assertEqual(long(3.9), 3L) |
| 670 | self.assertEqual(long(-3.9), -3L) |
| 671 | self.assertEqual(long(3.5), 3L) |
| 672 | self.assertEqual(long(-3.5), -3L) |
| 673 | self.assertEqual(long("-3"), -3L) |
| 674 | if have_unicode: |
| 675 | self.assertEqual(long(unicode("-3")), -3L) |
| 676 | # Different base: |
| 677 | self.assertEqual(long("10",16), 16L) |
| 678 | if have_unicode: |
| 679 | self.assertEqual(long(unicode("10"),16), 16L) |
| 680 | # Check conversions from string (same test set as for int(), and then some) |
| 681 | LL = [ |
| 682 | ('1' + '0'*20, 10L**20), |
| 683 | ('1' + '0'*100, 10L**100) |
| 684 | ] |
Walter Dörwald | c8cb5d9 | 2003-08-15 17:52:39 +0000 | [diff] [blame] | 685 | L2 = L[:] |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 686 | if have_unicode: |
| 687 | L2 += [ |
| 688 | (unicode('1') + unicode('0')*20, 10L**20), |
| 689 | (unicode('1') + unicode('0')*100, 10L**100), |
| 690 | ] |
| 691 | for s, v in L2 + LL: |
| 692 | for sign in "", "+", "-": |
| 693 | for prefix in "", " ", "\t", " \t\t ": |
| 694 | ss = prefix + sign + s |
| 695 | vv = v |
| 696 | if sign == "-" and v is not ValueError: |
| 697 | vv = -v |
| 698 | try: |
| 699 | self.assertEqual(long(ss), long(vv)) |
| 700 | except v: |
| 701 | pass |
| 702 | |
| 703 | self.assertRaises(ValueError, long, '123\0') |
| 704 | self.assertRaises(ValueError, long, '53', 40) |
| 705 | self.assertRaises(TypeError, long, 1, 12) |
| 706 | |
| 707 | def test_map(self): |
| 708 | self.assertEqual( |
| 709 | map(None, 'hello world'), |
| 710 | ['h','e','l','l','o',' ','w','o','r','l','d'] |
| 711 | ) |
| 712 | self.assertEqual( |
| 713 | map(None, 'abcd', 'efg'), |
| 714 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)] |
| 715 | ) |
| 716 | self.assertEqual( |
| 717 | map(None, range(10)), |
| 718 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 719 | ) |
| 720 | self.assertEqual( |
| 721 | map(lambda x: x*x, range(1,4)), |
| 722 | [1, 4, 9] |
| 723 | ) |
| 724 | try: |
| 725 | from math import sqrt |
| 726 | except ImportError: |
| 727 | def sqrt(x): |
| 728 | return pow(x, 0.5) |
| 729 | self.assertEqual( |
| 730 | map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]), |
| 731 | [[4.0, 2.0], [9.0, 3.0]] |
| 732 | ) |
| 733 | self.assertEqual( |
| 734 | map(lambda x, y: x+y, [1,3,2], [9,1,4]), |
| 735 | [10, 4, 6] |
| 736 | ) |
| 737 | |
| 738 | def plus(*v): |
| 739 | accu = 0 |
| 740 | for i in v: accu = accu + i |
| 741 | return accu |
| 742 | self.assertEqual( |
| 743 | map(plus, [1, 3, 7]), |
| 744 | [1, 3, 7] |
| 745 | ) |
| 746 | self.assertEqual( |
| 747 | map(plus, [1, 3, 7], [4, 9, 2]), |
| 748 | [1+4, 3+9, 7+2] |
| 749 | ) |
| 750 | self.assertEqual( |
| 751 | map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]), |
| 752 | [1+4+1, 3+9+1, 7+2+0] |
| 753 | ) |
| 754 | self.assertEqual( |
| 755 | map(None, Squares(10)), |
| 756 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 757 | ) |
| 758 | self.assertEqual( |
| 759 | map(int, Squares(10)), |
| 760 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 761 | ) |
| 762 | self.assertEqual( |
| 763 | map(None, Squares(3), Squares(2)), |
| 764 | [(0,0), (1,1), (4,None)] |
| 765 | ) |
| 766 | self.assertEqual( |
| 767 | map(max, Squares(3), Squares(2)), |
| 768 | [0, 1, 4] |
| 769 | ) |
| 770 | self.assertRaises(TypeError, map) |
| 771 | self.assertRaises(TypeError, map, lambda x: x, 42) |
| 772 | self.assertEqual(map(None, [42]), [42]) |
| 773 | class BadSeq: |
| 774 | def __getitem__(self, index): |
| 775 | raise ValueError |
| 776 | self.assertRaises(ValueError, map, lambda x: x, BadSeq()) |
| 777 | |
| 778 | def test_max(self): |
| 779 | self.assertEqual(max('123123'), '3') |
| 780 | self.assertEqual(max(1, 2, 3), 3) |
| 781 | self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3) |
| 782 | self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3) |
| 783 | |
| 784 | self.assertEqual(max(1, 2L, 3.0), 3.0) |
| 785 | self.assertEqual(max(1L, 2.0, 3), 3) |
| 786 | self.assertEqual(max(1.0, 2, 3L), 3L) |
| 787 | |
| 788 | def test_min(self): |
| 789 | self.assertEqual(min('123123'), '1') |
| 790 | self.assertEqual(min(1, 2, 3), 1) |
| 791 | self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1) |
| 792 | self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1) |
| 793 | |
| 794 | self.assertEqual(min(1, 2L, 3.0), 1) |
| 795 | self.assertEqual(min(1L, 2.0, 3), 1L) |
| 796 | self.assertEqual(min(1.0, 2, 3L), 1.0) |
| 797 | |
| 798 | self.assertRaises(TypeError, min) |
| 799 | self.assertRaises(TypeError, min, 42) |
| 800 | self.assertRaises(ValueError, min, ()) |
| 801 | class BadSeq: |
| 802 | def __getitem__(self, index): |
| 803 | raise ValueError |
| 804 | self.assertRaises(ValueError, min, BadSeq()) |
| 805 | class BadNumber: |
| 806 | def __cmp__(self, other): |
| 807 | raise ValueError |
| 808 | self.assertRaises(ValueError, min, (42, BadNumber())) |
| 809 | |
| 810 | def test_oct(self): |
| 811 | self.assertEqual(oct(100), '0144') |
| 812 | self.assertEqual(oct(100L), '0144L') |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 813 | self.assertEqual(oct(-100), '-0144') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 814 | self.assertEqual(oct(-100L), '-0144L') |
| 815 | self.assertRaises(TypeError, oct, ()) |
| 816 | |
| 817 | def write_testfile(self): |
| 818 | # NB the first 4 lines are also used to test input and raw_input, below |
| 819 | fp = open(TESTFN, 'w') |
| 820 | try: |
| 821 | fp.write('1+1\n') |
| 822 | fp.write('1+1\n') |
| 823 | fp.write('The quick brown fox jumps over the lazy dog') |
| 824 | fp.write('.\n') |
| 825 | fp.write('Dear John\n') |
| 826 | fp.write('XXX'*100) |
| 827 | fp.write('YYY'*100) |
| 828 | finally: |
| 829 | fp.close() |
| 830 | |
| 831 | def test_open(self): |
| 832 | self.write_testfile() |
| 833 | fp = open(TESTFN, 'r') |
| 834 | try: |
| 835 | self.assertEqual(fp.readline(4), '1+1\n') |
| 836 | self.assertEqual(fp.readline(4), '1+1\n') |
| 837 | self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n') |
| 838 | self.assertEqual(fp.readline(4), 'Dear') |
| 839 | self.assertEqual(fp.readline(100), ' John\n') |
| 840 | self.assertEqual(fp.read(300), 'XXX'*100) |
| 841 | self.assertEqual(fp.read(1000), 'YYY'*100) |
| 842 | finally: |
| 843 | fp.close() |
| 844 | unlink(TESTFN) |
| 845 | |
| 846 | def test_ord(self): |
| 847 | self.assertEqual(ord(' '), 32) |
| 848 | self.assertEqual(ord('A'), 65) |
| 849 | self.assertEqual(ord('a'), 97) |
| 850 | if have_unicode: |
| 851 | self.assertEqual(ord(unichr(sys.maxunicode)), sys.maxunicode) |
| 852 | self.assertRaises(TypeError, ord, 42) |
| 853 | self.assertRaises(TypeError, ord, unicode("12")) |
| 854 | |
| 855 | def test_pow(self): |
| 856 | self.assertEqual(pow(0,0), 1) |
| 857 | self.assertEqual(pow(0,1), 0) |
| 858 | self.assertEqual(pow(1,0), 1) |
| 859 | self.assertEqual(pow(1,1), 1) |
| 860 | |
| 861 | self.assertEqual(pow(2,0), 1) |
| 862 | self.assertEqual(pow(2,10), 1024) |
| 863 | self.assertEqual(pow(2,20), 1024*1024) |
| 864 | self.assertEqual(pow(2,30), 1024*1024*1024) |
| 865 | |
| 866 | self.assertEqual(pow(-2,0), 1) |
| 867 | self.assertEqual(pow(-2,1), -2) |
| 868 | self.assertEqual(pow(-2,2), 4) |
| 869 | self.assertEqual(pow(-2,3), -8) |
| 870 | |
| 871 | self.assertEqual(pow(0L,0), 1) |
| 872 | self.assertEqual(pow(0L,1), 0) |
| 873 | self.assertEqual(pow(1L,0), 1) |
| 874 | self.assertEqual(pow(1L,1), 1) |
| 875 | |
| 876 | self.assertEqual(pow(2L,0), 1) |
| 877 | self.assertEqual(pow(2L,10), 1024) |
| 878 | self.assertEqual(pow(2L,20), 1024*1024) |
| 879 | self.assertEqual(pow(2L,30), 1024*1024*1024) |
| 880 | |
| 881 | self.assertEqual(pow(-2L,0), 1) |
| 882 | self.assertEqual(pow(-2L,1), -2) |
| 883 | self.assertEqual(pow(-2L,2), 4) |
| 884 | self.assertEqual(pow(-2L,3), -8) |
| 885 | |
| 886 | self.assertAlmostEqual(pow(0.,0), 1.) |
| 887 | self.assertAlmostEqual(pow(0.,1), 0.) |
| 888 | self.assertAlmostEqual(pow(1.,0), 1.) |
| 889 | self.assertAlmostEqual(pow(1.,1), 1.) |
| 890 | |
| 891 | self.assertAlmostEqual(pow(2.,0), 1.) |
| 892 | self.assertAlmostEqual(pow(2.,10), 1024.) |
| 893 | self.assertAlmostEqual(pow(2.,20), 1024.*1024.) |
| 894 | self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.) |
| 895 | |
| 896 | self.assertAlmostEqual(pow(-2.,0), 1.) |
| 897 | self.assertAlmostEqual(pow(-2.,1), -2.) |
| 898 | self.assertAlmostEqual(pow(-2.,2), 4.) |
| 899 | self.assertAlmostEqual(pow(-2.,3), -8.) |
| 900 | |
| 901 | for x in 2, 2L, 2.0: |
| 902 | for y in 10, 10L, 10.0: |
| 903 | for z in 1000, 1000L, 1000.0: |
| 904 | if isinstance(x, float) or \ |
| 905 | isinstance(y, float) or \ |
| 906 | isinstance(z, float): |
| 907 | self.assertRaises(TypeError, pow, x, y, z) |
| 908 | else: |
| 909 | self.assertAlmostEqual(pow(x, y, z), 24.0) |
| 910 | |
| 911 | self.assertRaises(TypeError, pow, -1, -2, 3) |
| 912 | self.assertRaises(ValueError, pow, 1, 2, 0) |
| 913 | self.assertRaises(TypeError, pow, -1L, -2L, 3L) |
| 914 | self.assertRaises(ValueError, pow, 1L, 2L, 0L) |
| 915 | self.assertRaises(ValueError, pow, -342.43, 0.234) |
| 916 | |
| 917 | self.assertRaises(TypeError, pow) |
| 918 | |
| 919 | def test_range(self): |
| 920 | self.assertEqual(range(3), [0, 1, 2]) |
| 921 | self.assertEqual(range(1, 5), [1, 2, 3, 4]) |
| 922 | self.assertEqual(range(0), []) |
| 923 | self.assertEqual(range(-3), []) |
| 924 | self.assertEqual(range(1, 10, 3), [1, 4, 7]) |
| 925 | self.assertEqual(range(5, -5, -3), [5, 2, -1, -4]) |
| 926 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 927 | # Now test range() with longs |
| 928 | self.assertEqual(range(-2**100), []) |
| 929 | self.assertEqual(range(0, -2**100), []) |
| 930 | self.assertEqual(range(0, 2**100, -1), []) |
| 931 | self.assertEqual(range(0, 2**100, -1), []) |
| 932 | |
| 933 | a = long(10 * sys.maxint) |
| 934 | b = long(100 * sys.maxint) |
| 935 | c = long(50 * sys.maxint) |
| 936 | |
| 937 | self.assertEqual(range(a, a+2), [a, a+1]) |
| 938 | self.assertEqual(range(a+2, a, -1L), [a+2, a+1]) |
| 939 | self.assertEqual(range(a+4, a, -2), [a+4, a+2]) |
| 940 | |
| 941 | seq = range(a, b, c) |
| 942 | self.assert_(a in seq) |
| 943 | self.assert_(b not in seq) |
| 944 | self.assertEqual(len(seq), 2) |
| 945 | |
| 946 | seq = range(b, a, -c) |
| 947 | self.assert_(b in seq) |
| 948 | self.assert_(a not in seq) |
| 949 | self.assertEqual(len(seq), 2) |
| 950 | |
| 951 | seq = range(-a, -b, -c) |
| 952 | self.assert_(-a in seq) |
| 953 | self.assert_(-b not in seq) |
| 954 | self.assertEqual(len(seq), 2) |
| 955 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 956 | self.assertRaises(TypeError, range) |
| 957 | self.assertRaises(TypeError, range, 1, 2, 3, 4) |
| 958 | self.assertRaises(ValueError, range, 1, 2, 0) |
| 959 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 960 | # Reject floats when it would require PyLongs to represent. |
| 961 | # (smaller floats still accepted, but deprecated) |
Tim Peters | 299b3df | 2003-04-15 14:40:03 +0000 | [diff] [blame] | 962 | self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 963 | |
Walter Dörwald | 357981e | 2003-04-15 18:59:28 +0000 | [diff] [blame] | 964 | self.assertRaises(TypeError, range, 0, "spam") |
| 965 | self.assertRaises(TypeError, range, 0, 42, "spam") |
| 966 | |
| 967 | self.assertRaises(OverflowError, range, -sys.maxint, sys.maxint) |
| 968 | self.assertRaises(OverflowError, range, 0, 2*sys.maxint) |
| 969 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 970 | def test_input_and_raw_input(self): |
| 971 | self.write_testfile() |
| 972 | fp = open(TESTFN, 'r') |
| 973 | savestdin = sys.stdin |
| 974 | savestdout = sys.stdout # Eats the echo |
| 975 | try: |
| 976 | sys.stdin = fp |
| 977 | sys.stdout = BitBucket() |
| 978 | self.assertEqual(input(), 2) |
| 979 | self.assertEqual(input('testing\n'), 2) |
| 980 | self.assertEqual(raw_input(), 'The quick brown fox jumps over the lazy dog.') |
| 981 | self.assertEqual(raw_input('testing\n'), 'Dear John') |
| 982 | sys.stdin = cStringIO.StringIO("NULL\0") |
| 983 | self.assertRaises(TypeError, input, 42, 42) |
| 984 | sys.stdin = cStringIO.StringIO(" 'whitespace'") |
| 985 | self.assertEqual(input(), 'whitespace') |
| 986 | sys.stdin = cStringIO.StringIO() |
| 987 | self.assertRaises(EOFError, input) |
Hye-Shik Chang | ff83c2b | 2004-02-02 13:39:01 +0000 | [diff] [blame] | 988 | |
| 989 | # SF 876178: make sure input() respect future options. |
| 990 | sys.stdin = cStringIO.StringIO('1/2') |
| 991 | sys.stdout = cStringIO.StringIO() |
| 992 | exec compile('print input()', 'test_builtin_tmp', 'exec') |
| 993 | sys.stdin.seek(0, 0) |
| 994 | exec compile('from __future__ import division;print input()', |
| 995 | 'test_builtin_tmp', 'exec') |
| 996 | sys.stdin.seek(0, 0) |
| 997 | exec compile('print input()', 'test_builtin_tmp', 'exec') |
| 998 | self.assertEqual(sys.stdout.getvalue().splitlines(), |
| 999 | ['0', '0.5', '0']) |
| 1000 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1001 | del sys.stdout |
| 1002 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1003 | del sys.stdin |
| 1004 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1005 | finally: |
| 1006 | sys.stdin = savestdin |
| 1007 | sys.stdout = savestdout |
| 1008 | fp.close() |
| 1009 | unlink(TESTFN) |
| 1010 | |
| 1011 | def test_reduce(self): |
| 1012 | self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc') |
| 1013 | self.assertEqual( |
| 1014 | reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []), |
| 1015 | ['a','c','d','w'] |
| 1016 | ) |
| 1017 | self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040) |
| 1018 | self.assertEqual( |
| 1019 | reduce(lambda x, y: x*y, range(2,21), 1L), |
| 1020 | 2432902008176640000L |
| 1021 | ) |
| 1022 | self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285) |
| 1023 | self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285) |
| 1024 | self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0) |
| 1025 | self.assertRaises(TypeError, reduce) |
| 1026 | self.assertRaises(TypeError, reduce, 42, 42) |
| 1027 | self.assertRaises(TypeError, reduce, 42, 42, 42) |
| 1028 | self.assertEqual(reduce(42, "1"), "1") # func is never called with one item |
| 1029 | self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item |
| 1030 | self.assertRaises(TypeError, reduce, 42, (42, 42)) |
| 1031 | |
| 1032 | class BadSeq: |
| 1033 | def __getitem__(self, index): |
| 1034 | raise ValueError |
| 1035 | self.assertRaises(ValueError, reduce, 42, BadSeq()) |
| 1036 | |
| 1037 | def test_reload(self): |
| 1038 | import marshal |
| 1039 | reload(marshal) |
| 1040 | import string |
| 1041 | reload(string) |
| 1042 | ## import sys |
| 1043 | ## self.assertRaises(ImportError, reload, sys) |
| 1044 | |
| 1045 | def test_repr(self): |
| 1046 | self.assertEqual(repr(''), '\'\'') |
| 1047 | self.assertEqual(repr(0), '0') |
| 1048 | self.assertEqual(repr(0L), '0L') |
| 1049 | self.assertEqual(repr(()), '()') |
| 1050 | self.assertEqual(repr([]), '[]') |
| 1051 | self.assertEqual(repr({}), '{}') |
| 1052 | a = [] |
| 1053 | a.append(a) |
| 1054 | self.assertEqual(repr(a), '[[...]]') |
| 1055 | a = {} |
| 1056 | a[0] = a |
| 1057 | self.assertEqual(repr(a), '{0: {...}}') |
| 1058 | |
| 1059 | def test_round(self): |
| 1060 | self.assertEqual(round(0.0), 0.0) |
| 1061 | self.assertEqual(round(1.0), 1.0) |
| 1062 | self.assertEqual(round(10.0), 10.0) |
| 1063 | self.assertEqual(round(1000000000.0), 1000000000.0) |
| 1064 | self.assertEqual(round(1e20), 1e20) |
| 1065 | |
| 1066 | self.assertEqual(round(-1.0), -1.0) |
| 1067 | self.assertEqual(round(-10.0), -10.0) |
| 1068 | self.assertEqual(round(-1000000000.0), -1000000000.0) |
| 1069 | self.assertEqual(round(-1e20), -1e20) |
| 1070 | |
| 1071 | self.assertEqual(round(0.1), 0.0) |
| 1072 | self.assertEqual(round(1.1), 1.0) |
| 1073 | self.assertEqual(round(10.1), 10.0) |
| 1074 | self.assertEqual(round(1000000000.1), 1000000000.0) |
| 1075 | |
| 1076 | self.assertEqual(round(-1.1), -1.0) |
| 1077 | self.assertEqual(round(-10.1), -10.0) |
| 1078 | self.assertEqual(round(-1000000000.1), -1000000000.0) |
| 1079 | |
| 1080 | self.assertEqual(round(0.9), 1.0) |
| 1081 | self.assertEqual(round(9.9), 10.0) |
| 1082 | self.assertEqual(round(999999999.9), 1000000000.0) |
| 1083 | |
| 1084 | self.assertEqual(round(-0.9), -1.0) |
| 1085 | self.assertEqual(round(-9.9), -10.0) |
| 1086 | self.assertEqual(round(-999999999.9), -1000000000.0) |
| 1087 | |
| 1088 | self.assertEqual(round(-8.0, -1), -10.0) |
| 1089 | |
| 1090 | self.assertRaises(TypeError, round) |
| 1091 | |
| 1092 | def test_setattr(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 1093 | setattr(sys, 'spam', 1) |
| 1094 | self.assertEqual(sys.spam, 1) |
| 1095 | self.assertRaises(TypeError, setattr, sys, 1, 'spam') |
| 1096 | self.assertRaises(TypeError, setattr) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1097 | |
| 1098 | def test_str(self): |
| 1099 | self.assertEqual(str(''), '') |
| 1100 | self.assertEqual(str(0), '0') |
| 1101 | self.assertEqual(str(0L), '0') |
| 1102 | self.assertEqual(str(()), '()') |
| 1103 | self.assertEqual(str([]), '[]') |
| 1104 | self.assertEqual(str({}), '{}') |
| 1105 | a = [] |
| 1106 | a.append(a) |
| 1107 | self.assertEqual(str(a), '[[...]]') |
| 1108 | a = {} |
| 1109 | a[0] = a |
| 1110 | self.assertEqual(str(a), '{0: {...}}') |
| 1111 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1112 | def test_sum(self): |
| 1113 | self.assertEqual(sum([]), 0) |
| 1114 | self.assertEqual(sum(range(2,8)), 27) |
| 1115 | self.assertEqual(sum(iter(range(2,8))), 27) |
| 1116 | self.assertEqual(sum(Squares(10)), 285) |
| 1117 | self.assertEqual(sum(iter(Squares(10))), 285) |
| 1118 | self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3]) |
| 1119 | |
| 1120 | self.assertRaises(TypeError, sum) |
| 1121 | self.assertRaises(TypeError, sum, 42) |
| 1122 | self.assertRaises(TypeError, sum, ['a', 'b', 'c']) |
| 1123 | self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '') |
| 1124 | self.assertRaises(TypeError, sum, [[1], [2], [3]]) |
| 1125 | self.assertRaises(TypeError, sum, [{2:3}]) |
| 1126 | self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3}) |
| 1127 | |
| 1128 | class BadSeq: |
| 1129 | def __getitem__(self, index): |
| 1130 | raise ValueError |
| 1131 | self.assertRaises(ValueError, sum, BadSeq()) |
| 1132 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1133 | def test_tuple(self): |
| 1134 | self.assertEqual(tuple(()), ()) |
| 1135 | t0_3 = (0, 1, 2, 3) |
| 1136 | t0_3_bis = tuple(t0_3) |
| 1137 | self.assert_(t0_3 is t0_3_bis) |
| 1138 | self.assertEqual(tuple([]), ()) |
| 1139 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 1140 | self.assertEqual(tuple(''), ()) |
| 1141 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
| 1142 | |
| 1143 | def test_type(self): |
| 1144 | self.assertEqual(type(''), type('123')) |
| 1145 | self.assertNotEqual(type(''), type(())) |
| 1146 | |
| 1147 | def test_unichr(self): |
| 1148 | if have_unicode: |
| 1149 | self.assertEqual(unichr(32), unicode(' ')) |
| 1150 | self.assertEqual(unichr(65), unicode('A')) |
| 1151 | self.assertEqual(unichr(97), unicode('a')) |
| 1152 | self.assertEqual( |
| 1153 | unichr(sys.maxunicode), |
| 1154 | unicode('\\U%08x' % (sys.maxunicode), 'unicode-escape') |
| 1155 | ) |
| 1156 | self.assertRaises(ValueError, unichr, sys.maxunicode+1) |
| 1157 | self.assertRaises(TypeError, unichr) |
| 1158 | |
| 1159 | def get_vars_f0(): |
| 1160 | return vars() |
| 1161 | # we don't want self in vars(), so use staticmethod |
| 1162 | get_vars_f0 = staticmethod(get_vars_f0) |
| 1163 | |
| 1164 | def get_vars_f2(): |
| 1165 | BuiltinTest.get_vars_f0() |
| 1166 | a = 1 |
| 1167 | b = 2 |
| 1168 | return vars() |
| 1169 | get_vars_f2 = staticmethod(get_vars_f2) |
| 1170 | |
| 1171 | def test_vars(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1172 | self.assertEqual(set(vars()), set(dir())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1173 | import sys |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1174 | self.assertEqual(set(vars(sys)), set(dir(sys))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1175 | self.assertEqual(self.get_vars_f0(), {}) |
| 1176 | self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2}) |
| 1177 | self.assertRaises(TypeError, vars, 42, 42) |
| 1178 | self.assertRaises(TypeError, vars, 42) |
| 1179 | |
| 1180 | def test_zip(self): |
| 1181 | a = (1, 2, 3) |
| 1182 | b = (4, 5, 6) |
| 1183 | t = [(1, 4), (2, 5), (3, 6)] |
| 1184 | self.assertEqual(zip(a, b), t) |
| 1185 | b = [4, 5, 6] |
| 1186 | self.assertEqual(zip(a, b), t) |
| 1187 | b = (4, 5, 6, 7) |
| 1188 | self.assertEqual(zip(a, b), t) |
| 1189 | class I: |
| 1190 | def __getitem__(self, i): |
| 1191 | if i < 0 or i > 2: raise IndexError |
| 1192 | return i + 4 |
| 1193 | self.assertEqual(zip(a, I()), t) |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 1194 | self.assertEqual(zip(), []) |
| 1195 | self.assertEqual(zip(*[]), []) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1196 | self.assertRaises(TypeError, zip, None) |
| 1197 | class G: |
| 1198 | pass |
| 1199 | self.assertRaises(TypeError, zip, a, G()) |
| 1200 | |
| 1201 | # Make sure zip doesn't try to allocate a billion elements for the |
| 1202 | # result list when one of its arguments doesn't say how long it is. |
| 1203 | # A MemoryError is the most likely failure mode. |
| 1204 | class SequenceWithoutALength: |
| 1205 | def __getitem__(self, i): |
| 1206 | if i == 5: |
| 1207 | raise IndexError |
| 1208 | else: |
| 1209 | return i |
| 1210 | self.assertEqual( |
| 1211 | zip(SequenceWithoutALength(), xrange(2**30)), |
| 1212 | list(enumerate(range(5))) |
| 1213 | ) |
| 1214 | |
| 1215 | class BadSeq: |
| 1216 | def __getitem__(self, i): |
| 1217 | if i == 5: |
| 1218 | raise ValueError |
| 1219 | else: |
| 1220 | return i |
| 1221 | self.assertRaises(ValueError, zip, BadSeq(), BadSeq()) |
| 1222 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1223 | class TestSorted(unittest.TestCase): |
| 1224 | |
| 1225 | def test_basic(self): |
| 1226 | data = range(100) |
| 1227 | copy = data[:] |
| 1228 | random.shuffle(copy) |
| 1229 | self.assertEqual(data, sorted(copy)) |
| 1230 | self.assertNotEqual(data, copy) |
| 1231 | |
| 1232 | data.reverse() |
| 1233 | random.shuffle(copy) |
| 1234 | self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x))) |
| 1235 | self.assertNotEqual(data, copy) |
| 1236 | random.shuffle(copy) |
| 1237 | self.assertEqual(data, sorted(copy, key=lambda x: -x)) |
| 1238 | self.assertNotEqual(data, copy) |
| 1239 | random.shuffle(copy) |
| 1240 | self.assertEqual(data, sorted(copy, reverse=1)) |
| 1241 | self.assertNotEqual(data, copy) |
| 1242 | |
| 1243 | def test_inputtypes(self): |
| 1244 | s = 'abracadabra' |
| 1245 | for T in [unicode, list, tuple]: |
| 1246 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1247 | |
| 1248 | s = ''.join(dict.fromkeys(s).keys()) # unique letters only |
| 1249 | for T in [unicode, set, frozenset, list, tuple, dict.fromkeys]: |
| 1250 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1251 | |
| 1252 | def test_baddecorator(self): |
| 1253 | data = 'The quick Brown fox Jumped over The lazy Dog'.split() |
| 1254 | self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) |
| 1255 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1256 | def test_main(): |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1257 | test.test_support.run_unittest(BuiltinTest, TestSorted) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1258 | |
| 1259 | if __name__ == "__main__": |
| 1260 | test_main() |