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 |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 4 | from test.test_support import fcmp, have_unicode, TESTFN, unlink, \ |
| 5 | run_unittest, run_with_locale |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 6 | from operator import neg |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 7 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 8 | import sys, warnings, cStringIO, random, UserDict |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 9 | warnings.filterwarnings("ignore", "hex../oct.. of negative int", |
| 10 | FutureWarning, __name__) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 11 | warnings.filterwarnings("ignore", "integer argument expected", |
| 12 | DeprecationWarning, "unittest") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 13 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 14 | # count the number of test runs. |
| 15 | # used to skip running test_execfile() multiple times |
| 16 | numruns = 0 |
| 17 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 18 | class Squares: |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 19 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 20 | def __init__(self, max): |
| 21 | self.max = max |
| 22 | self.sofar = [] |
| 23 | |
| 24 | def __len__(self): return len(self.sofar) |
| 25 | |
| 26 | def __getitem__(self, i): |
| 27 | if not 0 <= i < self.max: raise IndexError |
| 28 | n = len(self.sofar) |
| 29 | while n <= i: |
| 30 | self.sofar.append(n*n) |
| 31 | n += 1 |
| 32 | return self.sofar[i] |
| 33 | |
| 34 | class StrSquares: |
| 35 | |
| 36 | def __init__(self, max): |
| 37 | self.max = max |
| 38 | self.sofar = [] |
| 39 | |
| 40 | def __len__(self): |
| 41 | return len(self.sofar) |
| 42 | |
| 43 | def __getitem__(self, i): |
| 44 | if not 0 <= i < self.max: |
| 45 | raise IndexError |
| 46 | n = len(self.sofar) |
| 47 | while n <= i: |
| 48 | self.sofar.append(str(n*n)) |
| 49 | n += 1 |
| 50 | return self.sofar[i] |
| 51 | |
| 52 | class BitBucket: |
| 53 | def write(self, line): |
| 54 | pass |
| 55 | |
| 56 | L = [ |
| 57 | ('0', 0), |
| 58 | ('1', 1), |
| 59 | ('9', 9), |
| 60 | ('10', 10), |
| 61 | ('99', 99), |
| 62 | ('100', 100), |
| 63 | ('314', 314), |
| 64 | (' 314', 314), |
| 65 | ('314 ', 314), |
| 66 | (' \t\t 314 \t\t ', 314), |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 67 | (repr(sys.maxint), sys.maxint), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 68 | (' 1x', ValueError), |
| 69 | (' 1 ', 1), |
| 70 | (' 1\02 ', ValueError), |
| 71 | ('', ValueError), |
| 72 | (' ', ValueError), |
| 73 | (' \t\t ', ValueError) |
| 74 | ] |
| 75 | if have_unicode: |
| 76 | L += [ |
| 77 | (unicode('0'), 0), |
| 78 | (unicode('1'), 1), |
| 79 | (unicode('9'), 9), |
| 80 | (unicode('10'), 10), |
| 81 | (unicode('99'), 99), |
| 82 | (unicode('100'), 100), |
| 83 | (unicode('314'), 314), |
| 84 | (unicode(' 314'), 314), |
| 85 | (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314), |
| 86 | (unicode(' \t\t 314 \t\t '), 314), |
| 87 | (unicode(' 1x'), ValueError), |
| 88 | (unicode(' 1 '), 1), |
| 89 | (unicode(' 1\02 '), ValueError), |
| 90 | (unicode(''), ValueError), |
| 91 | (unicode(' '), ValueError), |
| 92 | (unicode(' \t\t '), ValueError), |
| 93 | (unichr(0x200), ValueError), |
| 94 | ] |
| 95 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 96 | class TestFailingBool: |
| 97 | def __nonzero__(self): |
| 98 | raise RuntimeError |
| 99 | |
| 100 | class TestFailingIter: |
| 101 | def __iter__(self): |
| 102 | raise RuntimeError |
| 103 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 104 | class BuiltinTest(unittest.TestCase): |
| 105 | |
| 106 | def test_import(self): |
| 107 | __import__('sys') |
| 108 | __import__('time') |
| 109 | __import__('string') |
| 110 | self.assertRaises(ImportError, __import__, 'spamspam') |
| 111 | self.assertRaises(TypeError, __import__, 1, 2, 3, 4) |
Thomas Wouters | 8ddab27 | 2006-04-04 16:17:02 +0000 | [diff] [blame] | 112 | self.assertRaises(ValueError, __import__, '') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 113 | |
| 114 | def test_abs(self): |
| 115 | # int |
| 116 | self.assertEqual(abs(0), 0) |
| 117 | self.assertEqual(abs(1234), 1234) |
| 118 | self.assertEqual(abs(-1234), 1234) |
Martin v. Löwis | 10525ad | 2006-10-04 05:47:47 +0000 | [diff] [blame] | 119 | self.assertTrue(abs(-sys.maxint-1) > 0) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 120 | # float |
| 121 | self.assertEqual(abs(0.0), 0.0) |
| 122 | self.assertEqual(abs(3.14), 3.14) |
| 123 | self.assertEqual(abs(-3.14), 3.14) |
| 124 | # long |
| 125 | self.assertEqual(abs(0L), 0L) |
| 126 | self.assertEqual(abs(1234L), 1234L) |
| 127 | self.assertEqual(abs(-1234L), 1234L) |
| 128 | # str |
| 129 | self.assertRaises(TypeError, abs, 'a') |
| 130 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 131 | def test_all(self): |
| 132 | self.assertEqual(all([2, 4, 6]), True) |
| 133 | self.assertEqual(all([2, None, 6]), False) |
| 134 | self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6]) |
| 135 | self.assertRaises(RuntimeError, all, TestFailingIter()) |
| 136 | self.assertRaises(TypeError, all, 10) # Non-iterable |
| 137 | self.assertRaises(TypeError, all) # No args |
| 138 | self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args |
| 139 | self.assertEqual(all([]), True) # Empty iterator |
| 140 | S = [50, 60] |
| 141 | self.assertEqual(all(x > 42 for x in S), True) |
| 142 | S = [50, 40, 60] |
| 143 | self.assertEqual(all(x > 42 for x in S), False) |
| 144 | |
| 145 | def test_any(self): |
| 146 | self.assertEqual(any([None, None, None]), False) |
| 147 | self.assertEqual(any([None, 4, None]), True) |
| 148 | self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6]) |
| 149 | self.assertRaises(RuntimeError, all, TestFailingIter()) |
| 150 | self.assertRaises(TypeError, any, 10) # Non-iterable |
| 151 | self.assertRaises(TypeError, any) # No args |
| 152 | self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args |
| 153 | self.assertEqual(any([]), False) # Empty iterator |
| 154 | S = [40, 60, 30] |
| 155 | self.assertEqual(any(x > 42 for x in S), True) |
| 156 | S = [10, 20, 30] |
| 157 | self.assertEqual(any(x > 42 for x in S), False) |
| 158 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 159 | def test_apply(self): |
| 160 | def f0(*args): |
| 161 | self.assertEqual(args, ()) |
| 162 | def f1(a1): |
| 163 | self.assertEqual(a1, 1) |
| 164 | def f2(a1, a2): |
| 165 | self.assertEqual(a1, 1) |
| 166 | self.assertEqual(a2, 2) |
| 167 | def f3(a1, a2, a3): |
| 168 | self.assertEqual(a1, 1) |
| 169 | self.assertEqual(a2, 2) |
| 170 | self.assertEqual(a3, 3) |
| 171 | apply(f0, ()) |
| 172 | apply(f1, (1,)) |
| 173 | apply(f2, (1, 2)) |
| 174 | apply(f3, (1, 2, 3)) |
| 175 | |
| 176 | # A PyCFunction that takes only positional parameters should allow an |
| 177 | # empty keyword dictionary to pass without a complaint, but raise a |
| 178 | # TypeError if the dictionary is non-empty. |
| 179 | apply(id, (1,), {}) |
| 180 | self.assertRaises(TypeError, apply, id, (1,), {"foo": 1}) |
| 181 | self.assertRaises(TypeError, apply) |
| 182 | self.assertRaises(TypeError, apply, id, 42) |
| 183 | self.assertRaises(TypeError, apply, id, (42,), 42) |
| 184 | |
| 185 | def test_callable(self): |
| 186 | self.assert_(callable(len)) |
| 187 | def f(): pass |
| 188 | self.assert_(callable(f)) |
| 189 | class C: |
| 190 | def meth(self): pass |
| 191 | self.assert_(callable(C)) |
| 192 | x = C() |
| 193 | self.assert_(callable(x.meth)) |
| 194 | self.assert_(not callable(x)) |
| 195 | class D(C): |
| 196 | def __call__(self): pass |
| 197 | y = D() |
| 198 | self.assert_(callable(y)) |
| 199 | y() |
| 200 | |
| 201 | def test_chr(self): |
| 202 | self.assertEqual(chr(32), ' ') |
| 203 | self.assertEqual(chr(65), 'A') |
| 204 | self.assertEqual(chr(97), 'a') |
| 205 | self.assertEqual(chr(0xff), '\xff') |
| 206 | self.assertRaises(ValueError, chr, 256) |
| 207 | self.assertRaises(TypeError, chr) |
| 208 | |
| 209 | def test_cmp(self): |
| 210 | self.assertEqual(cmp(-1, 1), -1) |
| 211 | self.assertEqual(cmp(1, -1), 1) |
| 212 | self.assertEqual(cmp(1, 1), 0) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 213 | # verify that circular objects are not handled |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 214 | a = []; a.append(a) |
| 215 | b = []; b.append(b) |
| 216 | from UserList import UserList |
| 217 | c = UserList(); c.append(c) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 218 | self.assertRaises(RuntimeError, cmp, a, b) |
| 219 | self.assertRaises(RuntimeError, cmp, b, c) |
| 220 | self.assertRaises(RuntimeError, cmp, c, a) |
| 221 | self.assertRaises(RuntimeError, cmp, a, c) |
| 222 | # okay, now break the cycles |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 223 | a.pop(); b.pop(); c.pop() |
| 224 | self.assertRaises(TypeError, cmp) |
| 225 | |
| 226 | def test_coerce(self): |
| 227 | self.assert_(not fcmp(coerce(1, 1.1), (1.0, 1.1))) |
| 228 | self.assertEqual(coerce(1, 1L), (1L, 1L)) |
| 229 | self.assert_(not fcmp(coerce(1L, 1.1), (1.0, 1.1))) |
| 230 | self.assertRaises(TypeError, coerce) |
| 231 | class BadNumber: |
| 232 | def __coerce__(self, other): |
| 233 | raise ValueError |
| 234 | self.assertRaises(ValueError, coerce, 42, BadNumber()) |
Neal Norwitz | abcb0c0 | 2003-01-28 19:21:24 +0000 | [diff] [blame] | 235 | self.assertRaises(OverflowError, coerce, 0.5, int("12345" * 1000)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 236 | |
| 237 | def test_compile(self): |
| 238 | compile('print 1\n', '', 'exec') |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 239 | bom = '\xef\xbb\xbf' |
| 240 | compile(bom + 'print 1\n', '', 'exec') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 241 | self.assertRaises(TypeError, compile) |
| 242 | self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'badmode') |
| 243 | self.assertRaises(ValueError, compile, 'print 42\n', '<string>', 'single', 0xff) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 244 | self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 245 | if have_unicode: |
| 246 | compile(unicode('print u"\xc3\xa5"\n', 'utf8'), '', 'exec') |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 247 | self.assertRaises(TypeError, compile, unichr(0), 'f', 'exec') |
| 248 | self.assertRaises(ValueError, compile, unicode('a = 1'), 'f', 'bad') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 249 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 250 | def test_delattr(self): |
| 251 | import sys |
| 252 | sys.spam = 1 |
| 253 | delattr(sys, 'spam') |
| 254 | self.assertRaises(TypeError, delattr) |
| 255 | |
| 256 | def test_dir(self): |
| 257 | x = 1 |
| 258 | self.assert_('x' in dir()) |
| 259 | import sys |
| 260 | self.assert_('modules' in dir(sys)) |
| 261 | self.assertRaises(TypeError, dir, 42, 42) |
| 262 | |
| 263 | def test_divmod(self): |
| 264 | self.assertEqual(divmod(12, 7), (1, 5)) |
| 265 | self.assertEqual(divmod(-12, 7), (-2, 2)) |
| 266 | self.assertEqual(divmod(12, -7), (-2, -2)) |
| 267 | self.assertEqual(divmod(-12, -7), (1, -5)) |
| 268 | |
| 269 | self.assertEqual(divmod(12L, 7L), (1L, 5L)) |
| 270 | self.assertEqual(divmod(-12L, 7L), (-2L, 2L)) |
| 271 | self.assertEqual(divmod(12L, -7L), (-2L, -2L)) |
| 272 | self.assertEqual(divmod(-12L, -7L), (1L, -5L)) |
| 273 | |
| 274 | self.assertEqual(divmod(12, 7L), (1, 5L)) |
| 275 | self.assertEqual(divmod(-12, 7L), (-2, 2L)) |
| 276 | self.assertEqual(divmod(12L, -7), (-2L, -2)) |
| 277 | self.assertEqual(divmod(-12L, -7), (1L, -5)) |
| 278 | |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 279 | self.assertEqual(divmod(-sys.maxint-1, -1), |
| 280 | (sys.maxint+1, 0)) |
| 281 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 282 | self.assert_(not fcmp(divmod(3.25, 1.0), (3.0, 0.25))) |
| 283 | self.assert_(not fcmp(divmod(-3.25, 1.0), (-4.0, 0.75))) |
| 284 | self.assert_(not fcmp(divmod(3.25, -1.0), (-4.0, -0.75))) |
| 285 | self.assert_(not fcmp(divmod(-3.25, -1.0), (3.0, -0.25))) |
| 286 | |
| 287 | self.assertRaises(TypeError, divmod) |
| 288 | |
| 289 | def test_eval(self): |
| 290 | self.assertEqual(eval('1+1'), 2) |
| 291 | self.assertEqual(eval(' 1+1\n'), 2) |
| 292 | globals = {'a': 1, 'b': 2} |
| 293 | locals = {'b': 200, 'c': 300} |
| 294 | self.assertEqual(eval('a', globals) , 1) |
| 295 | self.assertEqual(eval('a', globals, locals), 1) |
| 296 | self.assertEqual(eval('b', globals, locals), 200) |
| 297 | self.assertEqual(eval('c', globals, locals), 300) |
| 298 | if have_unicode: |
| 299 | self.assertEqual(eval(unicode('1+1')), 2) |
| 300 | self.assertEqual(eval(unicode(' 1+1\n')), 2) |
| 301 | globals = {'a': 1, 'b': 2} |
| 302 | locals = {'b': 200, 'c': 300} |
| 303 | if have_unicode: |
| 304 | self.assertEqual(eval(unicode('a'), globals), 1) |
| 305 | self.assertEqual(eval(unicode('a'), globals, locals), 1) |
| 306 | self.assertEqual(eval(unicode('b'), globals, locals), 200) |
| 307 | self.assertEqual(eval(unicode('c'), globals, locals), 300) |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 308 | bom = '\xef\xbb\xbf' |
| 309 | self.assertEqual(eval(bom + 'a', globals, locals), 1) |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 310 | self.assertEqual(eval(unicode('u"\xc3\xa5"', 'utf8'), globals), |
| 311 | unicode('\xc3\xa5', 'utf8')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 312 | self.assertRaises(TypeError, eval) |
| 313 | self.assertRaises(TypeError, eval, ()) |
| 314 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 315 | def test_general_eval(self): |
| 316 | # Tests that general mappings can be used for the locals argument |
| 317 | |
| 318 | class M: |
| 319 | "Test mapping interface versus possible calls from eval()." |
| 320 | def __getitem__(self, key): |
| 321 | if key == 'a': |
| 322 | return 12 |
| 323 | raise KeyError |
| 324 | def keys(self): |
| 325 | return list('xyz') |
| 326 | |
| 327 | m = M() |
| 328 | g = globals() |
| 329 | self.assertEqual(eval('a', g, m), 12) |
| 330 | self.assertRaises(NameError, eval, 'b', g, m) |
| 331 | self.assertEqual(eval('dir()', g, m), list('xyz')) |
| 332 | self.assertEqual(eval('globals()', g, m), g) |
| 333 | self.assertEqual(eval('locals()', g, m), m) |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 334 | self.assertRaises(TypeError, eval, 'a', m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 335 | class A: |
| 336 | "Non-mapping" |
| 337 | pass |
| 338 | m = A() |
| 339 | self.assertRaises(TypeError, eval, 'a', g, m) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 340 | |
| 341 | # Verify that dict subclasses work as well |
| 342 | class D(dict): |
| 343 | def __getitem__(self, key): |
| 344 | if key == 'a': |
| 345 | return 12 |
| 346 | return dict.__getitem__(self, key) |
| 347 | def keys(self): |
| 348 | return list('xyz') |
| 349 | |
| 350 | d = D() |
| 351 | self.assertEqual(eval('a', g, d), 12) |
| 352 | self.assertRaises(NameError, eval, 'b', g, d) |
| 353 | self.assertEqual(eval('dir()', g, d), list('xyz')) |
| 354 | self.assertEqual(eval('globals()', g, d), g) |
| 355 | self.assertEqual(eval('locals()', g, d), d) |
| 356 | |
| 357 | # Verify locals stores (used by list comps) |
| 358 | eval('[locals() for i in (2,3)]', g, d) |
| 359 | eval('[locals() for i in (2,3)]', g, UserDict.UserDict()) |
| 360 | |
| 361 | class SpreadSheet: |
| 362 | "Sample application showing nested, calculated lookups." |
| 363 | _cells = {} |
| 364 | def __setitem__(self, key, formula): |
| 365 | self._cells[key] = formula |
Martin Blais | 215f13d | 2006-06-06 12:46:55 +0000 | [diff] [blame] | 366 | def __getitem__(self, key): |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 367 | return eval(self._cells[key], globals(), self) |
| 368 | |
| 369 | ss = SpreadSheet() |
| 370 | ss['a1'] = '5' |
| 371 | ss['a2'] = 'a1*6' |
| 372 | ss['a3'] = 'a2*7' |
| 373 | self.assertEqual(ss['a3'], 210) |
| 374 | |
Raymond Hettinger | 2a7dede | 2004-08-07 04:55:30 +0000 | [diff] [blame] | 375 | # Verify that dir() catches a non-list returned by eval |
| 376 | # SF bug #1004669 |
| 377 | class C: |
| 378 | def __getitem__(self, item): |
| 379 | raise KeyError(item) |
| 380 | def keys(self): |
| 381 | return 'a' |
| 382 | self.assertRaises(TypeError, eval, 'dir()', globals(), C()) |
| 383 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 384 | # Done outside of the method test_z to get the correct scope |
| 385 | z = 0 |
| 386 | f = open(TESTFN, 'w') |
| 387 | f.write('z = z+1\n') |
| 388 | f.write('z = z*2\n') |
| 389 | f.close() |
| 390 | execfile(TESTFN) |
| 391 | |
| 392 | def test_execfile(self): |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 393 | global numruns |
| 394 | if numruns: |
| 395 | return |
| 396 | numruns += 1 |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 397 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 398 | globals = {'a': 1, 'b': 2} |
| 399 | locals = {'b': 200, 'c': 300} |
| 400 | |
| 401 | self.assertEqual(self.__class__.z, 2) |
| 402 | globals['z'] = 0 |
| 403 | execfile(TESTFN, globals) |
| 404 | self.assertEqual(globals['z'], 2) |
| 405 | locals['z'] = 0 |
| 406 | execfile(TESTFN, globals, locals) |
| 407 | self.assertEqual(locals['z'], 2) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 408 | |
| 409 | class M: |
| 410 | "Test mapping interface versus possible calls from execfile()." |
| 411 | def __init__(self): |
| 412 | self.z = 10 |
| 413 | def __getitem__(self, key): |
| 414 | if key == 'z': |
| 415 | return self.z |
| 416 | raise KeyError |
| 417 | def __setitem__(self, key, value): |
| 418 | if key == 'z': |
| 419 | self.z = value |
| 420 | return |
| 421 | raise KeyError |
| 422 | |
| 423 | locals = M() |
| 424 | locals['z'] = 0 |
| 425 | execfile(TESTFN, globals, locals) |
| 426 | self.assertEqual(locals['z'], 2) |
| 427 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 428 | unlink(TESTFN) |
| 429 | self.assertRaises(TypeError, execfile) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 430 | self.assertRaises(TypeError, execfile, TESTFN, {}, ()) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 431 | import os |
| 432 | self.assertRaises(IOError, execfile, os.curdir) |
| 433 | self.assertRaises(IOError, execfile, "I_dont_exist") |
| 434 | |
| 435 | def test_filter(self): |
| 436 | self.assertEqual(filter(lambda c: 'a' <= c <= 'z', 'Hello World'), 'elloorld') |
| 437 | self.assertEqual(filter(None, [1, 'hello', [], [3], '', None, 9, 0]), [1, 'hello', [3], 9]) |
| 438 | self.assertEqual(filter(lambda x: x > 0, [1, -3, 9, 0, 2]), [1, 9, 2]) |
| 439 | self.assertEqual(filter(None, Squares(10)), [1, 4, 9, 16, 25, 36, 49, 64, 81]) |
| 440 | self.assertEqual(filter(lambda x: x%2, Squares(10)), [1, 9, 25, 49, 81]) |
| 441 | def identity(item): |
| 442 | return 1 |
| 443 | filter(identity, Squares(5)) |
| 444 | self.assertRaises(TypeError, filter) |
| 445 | class BadSeq(object): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 446 | def __getitem__(self, index): |
| 447 | if index<4: |
| 448 | return 42 |
| 449 | raise ValueError |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 450 | self.assertRaises(ValueError, filter, lambda x: x, BadSeq()) |
| 451 | def badfunc(): |
| 452 | pass |
| 453 | self.assertRaises(TypeError, filter, badfunc, range(5)) |
| 454 | |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 455 | # test bltinmodule.c::filtertuple() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 456 | self.assertEqual(filter(None, (1, 2)), (1, 2)) |
| 457 | self.assertEqual(filter(lambda x: x>=3, (1, 2, 3, 4)), (3, 4)) |
| 458 | self.assertRaises(TypeError, filter, 42, (1, 2)) |
| 459 | |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 460 | # test bltinmodule.c::filterstring() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 461 | self.assertEqual(filter(None, "12"), "12") |
| 462 | self.assertEqual(filter(lambda x: x>="3", "1234"), "34") |
| 463 | self.assertRaises(TypeError, filter, 42, "12") |
| 464 | class badstr(str): |
| 465 | def __getitem__(self, index): |
| 466 | raise ValueError |
| 467 | self.assertRaises(ValueError, filter, lambda x: x >="3", badstr("1234")) |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 468 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 469 | class badstr2(str): |
| 470 | def __getitem__(self, index): |
| 471 | return 42 |
| 472 | self.assertRaises(TypeError, filter, lambda x: x >=42, badstr2("1234")) |
| 473 | |
| 474 | class weirdstr(str): |
| 475 | def __getitem__(self, index): |
| 476 | return weirdstr(2*str.__getitem__(self, index)) |
| 477 | self.assertEqual(filter(lambda x: x>="33", weirdstr("1234")), "3344") |
| 478 | |
Walter Dörwald | 5e61e24 | 2003-02-04 17:04:01 +0000 | [diff] [blame] | 479 | class shiftstr(str): |
| 480 | def __getitem__(self, index): |
| 481 | return chr(ord(str.__getitem__(self, index))+1) |
| 482 | self.assertEqual(filter(lambda x: x>="3", shiftstr("1234")), "345") |
| 483 | |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 484 | if have_unicode: |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 485 | # test bltinmodule.c::filterunicode() |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 486 | self.assertEqual(filter(None, unicode("12")), unicode("12")) |
| 487 | self.assertEqual(filter(lambda x: x>="3", unicode("1234")), unicode("34")) |
| 488 | self.assertRaises(TypeError, filter, 42, unicode("12")) |
| 489 | self.assertRaises(ValueError, filter, lambda x: x >="3", badstr(unicode("1234"))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 490 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 491 | class badunicode(unicode): |
| 492 | def __getitem__(self, index): |
| 493 | return 42 |
| 494 | self.assertRaises(TypeError, filter, lambda x: x >=42, badunicode("1234")) |
| 495 | |
| 496 | class weirdunicode(unicode): |
| 497 | def __getitem__(self, index): |
| 498 | return weirdunicode(2*unicode.__getitem__(self, index)) |
| 499 | self.assertEqual( |
| 500 | filter(lambda x: x>=unicode("33"), weirdunicode("1234")), unicode("3344")) |
| 501 | |
Walter Dörwald | 5e61e24 | 2003-02-04 17:04:01 +0000 | [diff] [blame] | 502 | class shiftunicode(unicode): |
| 503 | def __getitem__(self, index): |
| 504 | return unichr(ord(unicode.__getitem__(self, index))+1) |
| 505 | self.assertEqual( |
| 506 | filter(lambda x: x>=unicode("3"), shiftunicode("1234")), |
| 507 | unicode("345") |
| 508 | ) |
| 509 | |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 510 | def test_filter_subclasses(self): |
Walter Dörwald | c8cb5d9 | 2003-08-15 17:52:39 +0000 | [diff] [blame] | 511 | # test that filter() never returns tuple, str or unicode subclasses |
| 512 | # and that the result always goes through __getitem__ |
| 513 | funcs = (None, bool, lambda x: True) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 514 | class tuple2(tuple): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 515 | def __getitem__(self, index): |
| 516 | return 2*tuple.__getitem__(self, index) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 517 | class str2(str): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 518 | def __getitem__(self, index): |
| 519 | return 2*str.__getitem__(self, index) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 520 | inputs = { |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 521 | tuple2: {(): (), (1, 2, 3): (2, 4, 6)}, |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 522 | str2: {"": "", "123": "112233"} |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 523 | } |
| 524 | if have_unicode: |
| 525 | class unicode2(unicode): |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 526 | def __getitem__(self, index): |
| 527 | return 2*unicode.__getitem__(self, index) |
| 528 | inputs[unicode2] = { |
| 529 | unicode(): unicode(), |
| 530 | unicode("123"): unicode("112233") |
| 531 | } |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 532 | |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 533 | for (cls, inps) in inputs.iteritems(): |
| 534 | for (inp, exp) in inps.iteritems(): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 535 | # make sure the output goes through __getitem__ |
| 536 | # even if func is None |
| 537 | self.assertEqual( |
| 538 | filter(funcs[0], cls(inp)), |
| 539 | filter(funcs[1], cls(inp)) |
| 540 | ) |
| 541 | for func in funcs: |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 542 | outp = filter(func, cls(inp)) |
| 543 | self.assertEqual(outp, exp) |
| 544 | self.assert_(not isinstance(outp, cls)) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 545 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 546 | def test_float(self): |
| 547 | self.assertEqual(float(3.14), 3.14) |
| 548 | self.assertEqual(float(314), 314.0) |
| 549 | self.assertEqual(float(314L), 314.0) |
| 550 | self.assertEqual(float(" 3.14 "), 3.14) |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 551 | self.assertRaises(ValueError, float, " 0x3.1 ") |
| 552 | self.assertRaises(ValueError, float, " -0x3.p-1 ") |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 553 | if have_unicode: |
| 554 | self.assertEqual(float(unicode(" 3.14 ")), 3.14) |
| 555 | self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14) |
Walter Dörwald | ede187f | 2005-11-29 15:45:14 +0000 | [diff] [blame] | 556 | # Implementation limitation in PyFloat_FromString() |
| 557 | self.assertRaises(ValueError, float, unicode("1"*10000)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 558 | |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 559 | @run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE') |
Neal Norwitz | 5898fa2 | 2005-11-22 05:17:40 +0000 | [diff] [blame] | 560 | def test_float_with_comma(self): |
| 561 | # set locale to something that doesn't use '.' for the decimal point |
Martin v. Löwis | fcfff0a | 2006-07-03 12:19:50 +0000 | [diff] [blame] | 562 | # float must not accept the locale specific decimal point but |
| 563 | # it still has to accept the normal python syntac |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 564 | import locale |
| 565 | if not locale.localeconv()['decimal_point'] == ',': |
Neal Norwitz | 5898fa2 | 2005-11-22 05:17:40 +0000 | [diff] [blame] | 566 | return |
| 567 | |
Martin v. Löwis | fcfff0a | 2006-07-03 12:19:50 +0000 | [diff] [blame] | 568 | self.assertEqual(float(" 3.14 "), 3.14) |
| 569 | self.assertEqual(float("+3.14 "), 3.14) |
| 570 | self.assertEqual(float("-3.14 "), -3.14) |
| 571 | self.assertEqual(float(".14 "), .14) |
| 572 | self.assertEqual(float("3. "), 3.0) |
| 573 | self.assertEqual(float("3.e3 "), 3000.0) |
| 574 | self.assertEqual(float("3.2e3 "), 3200.0) |
| 575 | self.assertEqual(float("2.5e-1 "), 0.25) |
| 576 | self.assertEqual(float("5e-1"), 0.5) |
| 577 | self.assertRaises(ValueError, float, " 3,14 ") |
| 578 | self.assertRaises(ValueError, float, " +3,14 ") |
| 579 | self.assertRaises(ValueError, float, " -3,14 ") |
Georg Brandl | de9b624 | 2006-04-30 11:13:56 +0000 | [diff] [blame] | 580 | self.assertRaises(ValueError, float, " 0x3.1 ") |
| 581 | self.assertRaises(ValueError, float, " -0x3.p-1 ") |
| 582 | self.assertEqual(float(" 25.e-1 "), 2.5) |
| 583 | self.assertEqual(fcmp(float(" .25e-1 "), .025), 0) |
Neal Norwitz | 5898fa2 | 2005-11-22 05:17:40 +0000 | [diff] [blame] | 584 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 585 | def test_floatconversion(self): |
| 586 | # Make sure that calls to __float__() work properly |
| 587 | class Foo0: |
| 588 | def __float__(self): |
| 589 | return 42. |
| 590 | |
| 591 | class Foo1(object): |
| 592 | def __float__(self): |
| 593 | return 42. |
| 594 | |
| 595 | class Foo2(float): |
| 596 | def __float__(self): |
| 597 | return 42. |
| 598 | |
| 599 | class Foo3(float): |
| 600 | def __new__(cls, value=0.): |
| 601 | return float.__new__(cls, 2*value) |
| 602 | |
| 603 | def __float__(self): |
| 604 | return self |
| 605 | |
| 606 | class Foo4(float): |
| 607 | def __float__(self): |
| 608 | return 42 |
| 609 | |
| 610 | self.assertAlmostEqual(float(Foo0()), 42.) |
| 611 | self.assertAlmostEqual(float(Foo1()), 42.) |
| 612 | self.assertAlmostEqual(float(Foo2()), 42.) |
| 613 | self.assertAlmostEqual(float(Foo3(21)), 42.) |
| 614 | self.assertRaises(TypeError, float, Foo4(42)) |
| 615 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 616 | def test_getattr(self): |
| 617 | import sys |
| 618 | self.assert_(getattr(sys, 'stdout') is sys.stdout) |
| 619 | self.assertRaises(TypeError, getattr, sys, 1) |
| 620 | self.assertRaises(TypeError, getattr, sys, 1, "foo") |
| 621 | self.assertRaises(TypeError, getattr) |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 622 | if have_unicode: |
| 623 | self.assertRaises(UnicodeError, getattr, sys, unichr(sys.maxunicode)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 624 | |
| 625 | def test_hasattr(self): |
| 626 | import sys |
| 627 | self.assert_(hasattr(sys, 'stdout')) |
| 628 | self.assertRaises(TypeError, hasattr, sys, 1) |
| 629 | self.assertRaises(TypeError, hasattr) |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 630 | if have_unicode: |
| 631 | self.assertRaises(UnicodeError, hasattr, sys, unichr(sys.maxunicode)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 632 | |
| 633 | def test_hash(self): |
| 634 | hash(None) |
| 635 | self.assertEqual(hash(1), hash(1L)) |
| 636 | self.assertEqual(hash(1), hash(1.0)) |
| 637 | hash('spam') |
| 638 | if have_unicode: |
| 639 | self.assertEqual(hash('spam'), hash(unicode('spam'))) |
| 640 | hash((0,1,2,3)) |
| 641 | def f(): pass |
| 642 | self.assertRaises(TypeError, hash, []) |
| 643 | self.assertRaises(TypeError, hash, {}) |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 644 | # Bug 1536021: Allow hash to return long objects |
| 645 | class X: |
| 646 | def __hash__(self): |
| 647 | return 2**100 |
| 648 | self.assertEquals(type(hash(X())), int) |
| 649 | class Y(object): |
| 650 | def __hash__(self): |
| 651 | return 2**100 |
| 652 | self.assertEquals(type(hash(Y())), int) |
Armin Rigo | 51fc8c4 | 2006-08-09 14:55:26 +0000 | [diff] [blame] | 653 | class Z(long): |
| 654 | def __hash__(self): |
| 655 | return self |
| 656 | self.assertEquals(hash(Z(42)), hash(42L)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 657 | |
| 658 | def test_hex(self): |
| 659 | self.assertEqual(hex(16), '0x10') |
| 660 | self.assertEqual(hex(16L), '0x10L') |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 661 | self.assertEqual(hex(-16), '-0x10') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 662 | self.assertEqual(hex(-16L), '-0x10L') |
| 663 | self.assertRaises(TypeError, hex, {}) |
| 664 | |
| 665 | def test_id(self): |
| 666 | id(None) |
| 667 | id(1) |
| 668 | id(1L) |
| 669 | id(1.0) |
| 670 | id('spam') |
| 671 | id((0,1,2,3)) |
| 672 | id([0,1,2,3]) |
| 673 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 674 | |
| 675 | # Test input() later, together with raw_input |
| 676 | |
| 677 | def test_int(self): |
| 678 | self.assertEqual(int(314), 314) |
| 679 | self.assertEqual(int(3.14), 3) |
| 680 | self.assertEqual(int(314L), 314) |
| 681 | # Check that conversion from float truncates towards zero |
| 682 | self.assertEqual(int(-3.14), -3) |
| 683 | self.assertEqual(int(3.9), 3) |
| 684 | self.assertEqual(int(-3.9), -3) |
| 685 | self.assertEqual(int(3.5), 3) |
| 686 | self.assertEqual(int(-3.5), -3) |
| 687 | # Different base: |
| 688 | self.assertEqual(int("10",16), 16L) |
| 689 | if have_unicode: |
| 690 | self.assertEqual(int(unicode("10"),16), 16L) |
| 691 | # Test conversion from strings and various anomalies |
| 692 | for s, v in L: |
| 693 | for sign in "", "+", "-": |
| 694 | for prefix in "", " ", "\t", " \t\t ": |
| 695 | ss = prefix + sign + s |
| 696 | vv = v |
| 697 | if sign == "-" and v is not ValueError: |
| 698 | vv = -v |
| 699 | try: |
| 700 | self.assertEqual(int(ss), vv) |
| 701 | except v: |
| 702 | pass |
| 703 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 704 | s = repr(-1-sys.maxint) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 705 | self.assertEqual(int(s)+1, -sys.maxint) |
| 706 | # should return long |
| 707 | int(s[1:]) |
| 708 | |
| 709 | # should return long |
| 710 | x = int(1e100) |
| 711 | self.assert_(isinstance(x, long)) |
| 712 | x = int(-1e100) |
| 713 | self.assert_(isinstance(x, long)) |
| 714 | |
| 715 | |
| 716 | # SF bug 434186: 0x80000000/2 != 0x80000000>>1. |
| 717 | # Worked by accident in Windows release build, but failed in debug build. |
| 718 | # Failed in all Linux builds. |
| 719 | x = -1-sys.maxint |
| 720 | self.assertEqual(x >> 1, x//2) |
| 721 | |
| 722 | self.assertRaises(ValueError, int, '123\0') |
| 723 | self.assertRaises(ValueError, int, '53', 40) |
| 724 | |
| 725 | x = int('1' * 600) |
| 726 | self.assert_(isinstance(x, long)) |
| 727 | |
| 728 | if have_unicode: |
| 729 | x = int(unichr(0x661) * 600) |
| 730 | self.assert_(isinstance(x, long)) |
| 731 | |
| 732 | self.assertRaises(TypeError, int, 1, 12) |
| 733 | |
| 734 | self.assertEqual(int('0123', 0), 83) |
Neal Norwitz | 5898fa2 | 2005-11-22 05:17:40 +0000 | [diff] [blame] | 735 | self.assertEqual(int('0x123', 16), 291) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 736 | |
Tim Peters | b713ec2 | 2006-05-23 18:45:30 +0000 | [diff] [blame] | 737 | # SF bug 1334662: int(string, base) wrong answers |
| 738 | # Various representations of 2**32 evaluated to 0 |
| 739 | # rather than 2**32 in previous versions |
| 740 | |
| 741 | self.assertEqual(int('100000000000000000000000000000000', 2), 4294967296L) |
| 742 | self.assertEqual(int('102002022201221111211', 3), 4294967296L) |
| 743 | self.assertEqual(int('10000000000000000', 4), 4294967296L) |
| 744 | self.assertEqual(int('32244002423141', 5), 4294967296L) |
| 745 | self.assertEqual(int('1550104015504', 6), 4294967296L) |
| 746 | self.assertEqual(int('211301422354', 7), 4294967296L) |
| 747 | self.assertEqual(int('40000000000', 8), 4294967296L) |
| 748 | self.assertEqual(int('12068657454', 9), 4294967296L) |
| 749 | self.assertEqual(int('4294967296', 10), 4294967296L) |
| 750 | self.assertEqual(int('1904440554', 11), 4294967296L) |
| 751 | self.assertEqual(int('9ba461594', 12), 4294967296L) |
| 752 | self.assertEqual(int('535a79889', 13), 4294967296L) |
| 753 | self.assertEqual(int('2ca5b7464', 14), 4294967296L) |
| 754 | self.assertEqual(int('1a20dcd81', 15), 4294967296L) |
| 755 | self.assertEqual(int('100000000', 16), 4294967296L) |
| 756 | self.assertEqual(int('a7ffda91', 17), 4294967296L) |
| 757 | self.assertEqual(int('704he7g4', 18), 4294967296L) |
| 758 | self.assertEqual(int('4f5aff66', 19), 4294967296L) |
| 759 | self.assertEqual(int('3723ai4g', 20), 4294967296L) |
| 760 | self.assertEqual(int('281d55i4', 21), 4294967296L) |
| 761 | self.assertEqual(int('1fj8b184', 22), 4294967296L) |
| 762 | self.assertEqual(int('1606k7ic', 23), 4294967296L) |
| 763 | self.assertEqual(int('mb994ag', 24), 4294967296L) |
| 764 | self.assertEqual(int('hek2mgl', 25), 4294967296L) |
| 765 | self.assertEqual(int('dnchbnm', 26), 4294967296L) |
| 766 | self.assertEqual(int('b28jpdm', 27), 4294967296L) |
| 767 | self.assertEqual(int('8pfgih4', 28), 4294967296L) |
| 768 | self.assertEqual(int('76beigg', 29), 4294967296L) |
| 769 | self.assertEqual(int('5qmcpqg', 30), 4294967296L) |
| 770 | self.assertEqual(int('4q0jto4', 31), 4294967296L) |
| 771 | self.assertEqual(int('4000000', 32), 4294967296L) |
| 772 | self.assertEqual(int('3aokq94', 33), 4294967296L) |
| 773 | self.assertEqual(int('2qhxjli', 34), 4294967296L) |
| 774 | self.assertEqual(int('2br45qb', 35), 4294967296L) |
| 775 | self.assertEqual(int('1z141z4', 36), 4294967296L) |
| 776 | |
| 777 | # SF bug 1334662: int(string, base) wrong answers |
| 778 | # Checks for proper evaluation of 2**32 + 1 |
| 779 | self.assertEqual(int('100000000000000000000000000000001', 2), 4294967297L) |
| 780 | self.assertEqual(int('102002022201221111212', 3), 4294967297L) |
| 781 | self.assertEqual(int('10000000000000001', 4), 4294967297L) |
| 782 | self.assertEqual(int('32244002423142', 5), 4294967297L) |
| 783 | self.assertEqual(int('1550104015505', 6), 4294967297L) |
| 784 | self.assertEqual(int('211301422355', 7), 4294967297L) |
| 785 | self.assertEqual(int('40000000001', 8), 4294967297L) |
| 786 | self.assertEqual(int('12068657455', 9), 4294967297L) |
| 787 | self.assertEqual(int('4294967297', 10), 4294967297L) |
| 788 | self.assertEqual(int('1904440555', 11), 4294967297L) |
| 789 | self.assertEqual(int('9ba461595', 12), 4294967297L) |
| 790 | self.assertEqual(int('535a7988a', 13), 4294967297L) |
| 791 | self.assertEqual(int('2ca5b7465', 14), 4294967297L) |
| 792 | self.assertEqual(int('1a20dcd82', 15), 4294967297L) |
| 793 | self.assertEqual(int('100000001', 16), 4294967297L) |
| 794 | self.assertEqual(int('a7ffda92', 17), 4294967297L) |
| 795 | self.assertEqual(int('704he7g5', 18), 4294967297L) |
| 796 | self.assertEqual(int('4f5aff67', 19), 4294967297L) |
| 797 | self.assertEqual(int('3723ai4h', 20), 4294967297L) |
| 798 | self.assertEqual(int('281d55i5', 21), 4294967297L) |
| 799 | self.assertEqual(int('1fj8b185', 22), 4294967297L) |
| 800 | self.assertEqual(int('1606k7id', 23), 4294967297L) |
| 801 | self.assertEqual(int('mb994ah', 24), 4294967297L) |
| 802 | self.assertEqual(int('hek2mgm', 25), 4294967297L) |
| 803 | self.assertEqual(int('dnchbnn', 26), 4294967297L) |
| 804 | self.assertEqual(int('b28jpdn', 27), 4294967297L) |
| 805 | self.assertEqual(int('8pfgih5', 28), 4294967297L) |
| 806 | self.assertEqual(int('76beigh', 29), 4294967297L) |
| 807 | self.assertEqual(int('5qmcpqh', 30), 4294967297L) |
| 808 | self.assertEqual(int('4q0jto5', 31), 4294967297L) |
| 809 | self.assertEqual(int('4000001', 32), 4294967297L) |
| 810 | self.assertEqual(int('3aokq95', 33), 4294967297L) |
| 811 | self.assertEqual(int('2qhxjlj', 34), 4294967297L) |
| 812 | self.assertEqual(int('2br45qc', 35), 4294967297L) |
| 813 | self.assertEqual(int('1z141z5', 36), 4294967297L) |
| 814 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 815 | def test_intconversion(self): |
| 816 | # Test __int__() |
| 817 | class Foo0: |
| 818 | def __int__(self): |
| 819 | return 42 |
| 820 | |
| 821 | class Foo1(object): |
| 822 | def __int__(self): |
| 823 | return 42 |
| 824 | |
| 825 | class Foo2(int): |
| 826 | def __int__(self): |
| 827 | return 42 |
| 828 | |
| 829 | class Foo3(int): |
| 830 | def __int__(self): |
| 831 | return self |
| 832 | |
| 833 | class Foo4(int): |
| 834 | def __int__(self): |
| 835 | return 42L |
| 836 | |
| 837 | class Foo5(int): |
| 838 | def __int__(self): |
| 839 | return 42. |
| 840 | |
| 841 | self.assertEqual(int(Foo0()), 42) |
| 842 | self.assertEqual(int(Foo1()), 42) |
| 843 | self.assertEqual(int(Foo2()), 42) |
| 844 | self.assertEqual(int(Foo3()), 0) |
| 845 | self.assertEqual(int(Foo4()), 42L) |
| 846 | self.assertRaises(TypeError, int, Foo5()) |
| 847 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 848 | def test_intern(self): |
| 849 | self.assertRaises(TypeError, intern) |
| 850 | s = "never interned before" |
| 851 | self.assert_(intern(s) is s) |
| 852 | s2 = s.swapcase().swapcase() |
| 853 | self.assert_(intern(s2) is s) |
| 854 | |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 855 | # Subclasses of string can't be interned, because they |
| 856 | # provide too much opportunity for insane things to happen. |
| 857 | # We don't want them in the interned dict and if they aren't |
| 858 | # actually interned, we don't want to create the appearance |
| 859 | # that they are by allowing intern() to succeeed. |
| 860 | class S(str): |
| 861 | def __hash__(self): |
| 862 | return 123 |
| 863 | |
| 864 | self.assertRaises(TypeError, intern, S("abc")) |
| 865 | |
| 866 | # It's still safe to pass these strings to routines that |
| 867 | # call intern internally, e.g. PyObject_SetAttr(). |
| 868 | s = S("abc") |
| 869 | setattr(s, s, s) |
| 870 | self.assertEqual(getattr(s, s), s) |
| 871 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 872 | def test_iter(self): |
| 873 | self.assertRaises(TypeError, iter) |
| 874 | self.assertRaises(TypeError, iter, 42, 42) |
| 875 | lists = [("1", "2"), ["1", "2"], "12"] |
| 876 | if have_unicode: |
| 877 | lists.append(unicode("12")) |
| 878 | for l in lists: |
| 879 | i = iter(l) |
| 880 | self.assertEqual(i.next(), '1') |
| 881 | self.assertEqual(i.next(), '2') |
| 882 | self.assertRaises(StopIteration, i.next) |
| 883 | |
| 884 | def test_isinstance(self): |
| 885 | class C: |
| 886 | pass |
| 887 | class D(C): |
| 888 | pass |
| 889 | class E: |
| 890 | pass |
| 891 | c = C() |
| 892 | d = D() |
| 893 | e = E() |
| 894 | self.assert_(isinstance(c, C)) |
| 895 | self.assert_(isinstance(d, C)) |
| 896 | self.assert_(not isinstance(e, C)) |
| 897 | self.assert_(not isinstance(c, D)) |
| 898 | self.assert_(not isinstance('foo', E)) |
| 899 | self.assertRaises(TypeError, isinstance, E, 'foo') |
| 900 | self.assertRaises(TypeError, isinstance) |
| 901 | |
| 902 | def test_issubclass(self): |
| 903 | class C: |
| 904 | pass |
| 905 | class D(C): |
| 906 | pass |
| 907 | class E: |
| 908 | pass |
| 909 | c = C() |
| 910 | d = D() |
| 911 | e = E() |
| 912 | self.assert_(issubclass(D, C)) |
| 913 | self.assert_(issubclass(C, C)) |
| 914 | self.assert_(not issubclass(C, D)) |
| 915 | self.assertRaises(TypeError, issubclass, 'foo', E) |
| 916 | self.assertRaises(TypeError, issubclass, E, 'foo') |
| 917 | self.assertRaises(TypeError, issubclass) |
| 918 | |
| 919 | def test_len(self): |
| 920 | self.assertEqual(len('123'), 3) |
| 921 | self.assertEqual(len(()), 0) |
| 922 | self.assertEqual(len((1, 2, 3, 4)), 4) |
| 923 | self.assertEqual(len([1, 2, 3, 4]), 4) |
| 924 | self.assertEqual(len({}), 0) |
| 925 | self.assertEqual(len({'a':1, 'b': 2}), 2) |
| 926 | class BadSeq: |
| 927 | def __len__(self): |
| 928 | raise ValueError |
| 929 | self.assertRaises(ValueError, len, BadSeq()) |
| 930 | |
| 931 | def test_list(self): |
| 932 | self.assertEqual(list([]), []) |
| 933 | l0_3 = [0, 1, 2, 3] |
| 934 | l0_3_bis = list(l0_3) |
| 935 | self.assertEqual(l0_3, l0_3_bis) |
| 936 | self.assert_(l0_3 is not l0_3_bis) |
| 937 | self.assertEqual(list(()), []) |
| 938 | self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3]) |
| 939 | self.assertEqual(list(''), []) |
| 940 | self.assertEqual(list('spam'), ['s', 'p', 'a', 'm']) |
| 941 | |
| 942 | if sys.maxint == 0x7fffffff: |
| 943 | # This test can currently only work on 32-bit machines. |
| 944 | # XXX If/when PySequence_Length() returns a ssize_t, it should be |
| 945 | # XXX re-enabled. |
| 946 | # Verify clearing of bug #556025. |
| 947 | # This assumes that the max data size (sys.maxint) == max |
| 948 | # address size this also assumes that the address size is at |
| 949 | # least 4 bytes with 8 byte addresses, the bug is not well |
| 950 | # tested |
| 951 | # |
| 952 | # Note: This test is expected to SEGV under Cygwin 1.3.12 or |
| 953 | # earlier due to a newlib bug. See the following mailing list |
| 954 | # thread for the details: |
| 955 | |
| 956 | # http://sources.redhat.com/ml/newlib/2002/msg00369.html |
| 957 | self.assertRaises(MemoryError, list, xrange(sys.maxint // 2)) |
| 958 | |
Raymond Hettinger | aa241e0 | 2004-09-26 19:24:20 +0000 | [diff] [blame] | 959 | # This code used to segfault in Py2.4a3 |
| 960 | x = [] |
| 961 | x.extend(-y for y in x) |
| 962 | self.assertEqual(x, []) |
| 963 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 964 | def test_long(self): |
| 965 | self.assertEqual(long(314), 314L) |
| 966 | self.assertEqual(long(3.14), 3L) |
| 967 | self.assertEqual(long(314L), 314L) |
| 968 | # Check that conversion from float truncates towards zero |
| 969 | self.assertEqual(long(-3.14), -3L) |
| 970 | self.assertEqual(long(3.9), 3L) |
| 971 | self.assertEqual(long(-3.9), -3L) |
| 972 | self.assertEqual(long(3.5), 3L) |
| 973 | self.assertEqual(long(-3.5), -3L) |
| 974 | self.assertEqual(long("-3"), -3L) |
| 975 | if have_unicode: |
| 976 | self.assertEqual(long(unicode("-3")), -3L) |
| 977 | # Different base: |
| 978 | self.assertEqual(long("10",16), 16L) |
| 979 | if have_unicode: |
| 980 | self.assertEqual(long(unicode("10"),16), 16L) |
| 981 | # Check conversions from string (same test set as for int(), and then some) |
| 982 | LL = [ |
| 983 | ('1' + '0'*20, 10L**20), |
| 984 | ('1' + '0'*100, 10L**100) |
| 985 | ] |
Walter Dörwald | c8cb5d9 | 2003-08-15 17:52:39 +0000 | [diff] [blame] | 986 | L2 = L[:] |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 987 | if have_unicode: |
| 988 | L2 += [ |
| 989 | (unicode('1') + unicode('0')*20, 10L**20), |
| 990 | (unicode('1') + unicode('0')*100, 10L**100), |
| 991 | ] |
| 992 | for s, v in L2 + LL: |
| 993 | for sign in "", "+", "-": |
| 994 | for prefix in "", " ", "\t", " \t\t ": |
| 995 | ss = prefix + sign + s |
| 996 | vv = v |
| 997 | if sign == "-" and v is not ValueError: |
| 998 | vv = -v |
| 999 | try: |
| 1000 | self.assertEqual(long(ss), long(vv)) |
| 1001 | except v: |
| 1002 | pass |
| 1003 | |
| 1004 | self.assertRaises(ValueError, long, '123\0') |
| 1005 | self.assertRaises(ValueError, long, '53', 40) |
| 1006 | self.assertRaises(TypeError, long, 1, 12) |
| 1007 | |
Tim Peters | 696cf43 | 2006-05-24 21:10:40 +0000 | [diff] [blame] | 1008 | self.assertEqual(long('100000000000000000000000000000000', 2), |
| 1009 | 4294967296) |
| 1010 | self.assertEqual(long('102002022201221111211', 3), 4294967296) |
| 1011 | self.assertEqual(long('10000000000000000', 4), 4294967296) |
| 1012 | self.assertEqual(long('32244002423141', 5), 4294967296) |
| 1013 | self.assertEqual(long('1550104015504', 6), 4294967296) |
| 1014 | self.assertEqual(long('211301422354', 7), 4294967296) |
| 1015 | self.assertEqual(long('40000000000', 8), 4294967296) |
| 1016 | self.assertEqual(long('12068657454', 9), 4294967296) |
| 1017 | self.assertEqual(long('4294967296', 10), 4294967296) |
| 1018 | self.assertEqual(long('1904440554', 11), 4294967296) |
| 1019 | self.assertEqual(long('9ba461594', 12), 4294967296) |
| 1020 | self.assertEqual(long('535a79889', 13), 4294967296) |
| 1021 | self.assertEqual(long('2ca5b7464', 14), 4294967296) |
| 1022 | self.assertEqual(long('1a20dcd81', 15), 4294967296) |
| 1023 | self.assertEqual(long('100000000', 16), 4294967296) |
| 1024 | self.assertEqual(long('a7ffda91', 17), 4294967296) |
| 1025 | self.assertEqual(long('704he7g4', 18), 4294967296) |
| 1026 | self.assertEqual(long('4f5aff66', 19), 4294967296) |
| 1027 | self.assertEqual(long('3723ai4g', 20), 4294967296) |
| 1028 | self.assertEqual(long('281d55i4', 21), 4294967296) |
| 1029 | self.assertEqual(long('1fj8b184', 22), 4294967296) |
| 1030 | self.assertEqual(long('1606k7ic', 23), 4294967296) |
| 1031 | self.assertEqual(long('mb994ag', 24), 4294967296) |
| 1032 | self.assertEqual(long('hek2mgl', 25), 4294967296) |
| 1033 | self.assertEqual(long('dnchbnm', 26), 4294967296) |
| 1034 | self.assertEqual(long('b28jpdm', 27), 4294967296) |
| 1035 | self.assertEqual(long('8pfgih4', 28), 4294967296) |
| 1036 | self.assertEqual(long('76beigg', 29), 4294967296) |
| 1037 | self.assertEqual(long('5qmcpqg', 30), 4294967296) |
| 1038 | self.assertEqual(long('4q0jto4', 31), 4294967296) |
| 1039 | self.assertEqual(long('4000000', 32), 4294967296) |
| 1040 | self.assertEqual(long('3aokq94', 33), 4294967296) |
| 1041 | self.assertEqual(long('2qhxjli', 34), 4294967296) |
| 1042 | self.assertEqual(long('2br45qb', 35), 4294967296) |
| 1043 | self.assertEqual(long('1z141z4', 36), 4294967296) |
| 1044 | |
| 1045 | self.assertEqual(long('100000000000000000000000000000001', 2), |
| 1046 | 4294967297) |
| 1047 | self.assertEqual(long('102002022201221111212', 3), 4294967297) |
| 1048 | self.assertEqual(long('10000000000000001', 4), 4294967297) |
| 1049 | self.assertEqual(long('32244002423142', 5), 4294967297) |
| 1050 | self.assertEqual(long('1550104015505', 6), 4294967297) |
| 1051 | self.assertEqual(long('211301422355', 7), 4294967297) |
| 1052 | self.assertEqual(long('40000000001', 8), 4294967297) |
| 1053 | self.assertEqual(long('12068657455', 9), 4294967297) |
| 1054 | self.assertEqual(long('4294967297', 10), 4294967297) |
| 1055 | self.assertEqual(long('1904440555', 11), 4294967297) |
| 1056 | self.assertEqual(long('9ba461595', 12), 4294967297) |
| 1057 | self.assertEqual(long('535a7988a', 13), 4294967297) |
| 1058 | self.assertEqual(long('2ca5b7465', 14), 4294967297) |
| 1059 | self.assertEqual(long('1a20dcd82', 15), 4294967297) |
| 1060 | self.assertEqual(long('100000001', 16), 4294967297) |
| 1061 | self.assertEqual(long('a7ffda92', 17), 4294967297) |
| 1062 | self.assertEqual(long('704he7g5', 18), 4294967297) |
| 1063 | self.assertEqual(long('4f5aff67', 19), 4294967297) |
| 1064 | self.assertEqual(long('3723ai4h', 20), 4294967297) |
| 1065 | self.assertEqual(long('281d55i5', 21), 4294967297) |
| 1066 | self.assertEqual(long('1fj8b185', 22), 4294967297) |
| 1067 | self.assertEqual(long('1606k7id', 23), 4294967297) |
| 1068 | self.assertEqual(long('mb994ah', 24), 4294967297) |
| 1069 | self.assertEqual(long('hek2mgm', 25), 4294967297) |
| 1070 | self.assertEqual(long('dnchbnn', 26), 4294967297) |
| 1071 | self.assertEqual(long('b28jpdn', 27), 4294967297) |
| 1072 | self.assertEqual(long('8pfgih5', 28), 4294967297) |
| 1073 | self.assertEqual(long('76beigh', 29), 4294967297) |
| 1074 | self.assertEqual(long('5qmcpqh', 30), 4294967297) |
| 1075 | self.assertEqual(long('4q0jto5', 31), 4294967297) |
| 1076 | self.assertEqual(long('4000001', 32), 4294967297) |
| 1077 | self.assertEqual(long('3aokq95', 33), 4294967297) |
| 1078 | self.assertEqual(long('2qhxjlj', 34), 4294967297) |
| 1079 | self.assertEqual(long('2br45qc', 35), 4294967297) |
| 1080 | self.assertEqual(long('1z141z5', 36), 4294967297) |
| 1081 | |
| 1082 | |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 1083 | def test_longconversion(self): |
| 1084 | # Test __long__() |
| 1085 | class Foo0: |
| 1086 | def __long__(self): |
| 1087 | return 42L |
| 1088 | |
| 1089 | class Foo1(object): |
| 1090 | def __long__(self): |
| 1091 | return 42L |
| 1092 | |
| 1093 | class Foo2(long): |
| 1094 | def __long__(self): |
| 1095 | return 42L |
| 1096 | |
| 1097 | class Foo3(long): |
| 1098 | def __long__(self): |
| 1099 | return self |
| 1100 | |
| 1101 | class Foo4(long): |
| 1102 | def __long__(self): |
| 1103 | return 42 |
| 1104 | |
| 1105 | class Foo5(long): |
| 1106 | def __long__(self): |
| 1107 | return 42. |
| 1108 | |
| 1109 | self.assertEqual(long(Foo0()), 42L) |
| 1110 | self.assertEqual(long(Foo1()), 42L) |
| 1111 | self.assertEqual(long(Foo2()), 42L) |
| 1112 | self.assertEqual(long(Foo3()), 0) |
| 1113 | self.assertEqual(long(Foo4()), 42) |
| 1114 | self.assertRaises(TypeError, long, Foo5()) |
| 1115 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1116 | def test_map(self): |
| 1117 | self.assertEqual( |
| 1118 | map(None, 'hello world'), |
| 1119 | ['h','e','l','l','o',' ','w','o','r','l','d'] |
| 1120 | ) |
| 1121 | self.assertEqual( |
| 1122 | map(None, 'abcd', 'efg'), |
| 1123 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)] |
| 1124 | ) |
| 1125 | self.assertEqual( |
| 1126 | map(None, range(10)), |
| 1127 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1128 | ) |
| 1129 | self.assertEqual( |
| 1130 | map(lambda x: x*x, range(1,4)), |
| 1131 | [1, 4, 9] |
| 1132 | ) |
| 1133 | try: |
| 1134 | from math import sqrt |
| 1135 | except ImportError: |
| 1136 | def sqrt(x): |
| 1137 | return pow(x, 0.5) |
| 1138 | self.assertEqual( |
| 1139 | map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]), |
| 1140 | [[4.0, 2.0], [9.0, 3.0]] |
| 1141 | ) |
| 1142 | self.assertEqual( |
| 1143 | map(lambda x, y: x+y, [1,3,2], [9,1,4]), |
| 1144 | [10, 4, 6] |
| 1145 | ) |
| 1146 | |
| 1147 | def plus(*v): |
| 1148 | accu = 0 |
| 1149 | for i in v: accu = accu + i |
| 1150 | return accu |
| 1151 | self.assertEqual( |
| 1152 | map(plus, [1, 3, 7]), |
| 1153 | [1, 3, 7] |
| 1154 | ) |
| 1155 | self.assertEqual( |
| 1156 | map(plus, [1, 3, 7], [4, 9, 2]), |
| 1157 | [1+4, 3+9, 7+2] |
| 1158 | ) |
| 1159 | self.assertEqual( |
| 1160 | map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]), |
| 1161 | [1+4+1, 3+9+1, 7+2+0] |
| 1162 | ) |
| 1163 | self.assertEqual( |
| 1164 | map(None, Squares(10)), |
| 1165 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 1166 | ) |
| 1167 | self.assertEqual( |
| 1168 | map(int, Squares(10)), |
| 1169 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 1170 | ) |
| 1171 | self.assertEqual( |
| 1172 | map(None, Squares(3), Squares(2)), |
| 1173 | [(0,0), (1,1), (4,None)] |
| 1174 | ) |
| 1175 | self.assertEqual( |
| 1176 | map(max, Squares(3), Squares(2)), |
| 1177 | [0, 1, 4] |
| 1178 | ) |
| 1179 | self.assertRaises(TypeError, map) |
| 1180 | self.assertRaises(TypeError, map, lambda x: x, 42) |
| 1181 | self.assertEqual(map(None, [42]), [42]) |
| 1182 | class BadSeq: |
| 1183 | def __getitem__(self, index): |
| 1184 | raise ValueError |
| 1185 | self.assertRaises(ValueError, map, lambda x: x, BadSeq()) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 1186 | def badfunc(x): |
| 1187 | raise RuntimeError |
| 1188 | self.assertRaises(RuntimeError, map, badfunc, range(5)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1189 | |
| 1190 | def test_max(self): |
| 1191 | self.assertEqual(max('123123'), '3') |
| 1192 | self.assertEqual(max(1, 2, 3), 3) |
| 1193 | self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3) |
| 1194 | self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3) |
| 1195 | |
| 1196 | self.assertEqual(max(1, 2L, 3.0), 3.0) |
| 1197 | self.assertEqual(max(1L, 2.0, 3), 3) |
| 1198 | self.assertEqual(max(1.0, 2, 3L), 3L) |
| 1199 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1200 | for stmt in ( |
| 1201 | "max(key=int)", # no args |
| 1202 | "max(1, key=int)", # single arg not iterable |
| 1203 | "max(1, 2, keystone=int)", # wrong keyword |
| 1204 | "max(1, 2, key=int, abc=int)", # two many keywords |
| 1205 | "max(1, 2, key=1)", # keyfunc is not callable |
| 1206 | ): |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 1207 | try: |
| 1208 | exec(stmt) in globals() |
| 1209 | except TypeError: |
| 1210 | pass |
| 1211 | else: |
| 1212 | self.fail(stmt) |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1213 | |
| 1214 | self.assertEqual(max((1,), key=neg), 1) # one elem iterable |
| 1215 | self.assertEqual(max((1,2), key=neg), 1) # two elem iterable |
| 1216 | self.assertEqual(max(1, 2, key=neg), 1) # two elems |
| 1217 | |
| 1218 | data = [random.randrange(200) for i in range(100)] |
| 1219 | keys = dict((elem, random.randrange(50)) for elem in data) |
| 1220 | f = keys.__getitem__ |
| 1221 | self.assertEqual(max(data, key=f), |
| 1222 | sorted(reversed(data), key=f)[-1]) |
| 1223 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1224 | def test_min(self): |
| 1225 | self.assertEqual(min('123123'), '1') |
| 1226 | self.assertEqual(min(1, 2, 3), 1) |
| 1227 | self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1) |
| 1228 | self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1) |
| 1229 | |
| 1230 | self.assertEqual(min(1, 2L, 3.0), 1) |
| 1231 | self.assertEqual(min(1L, 2.0, 3), 1L) |
| 1232 | self.assertEqual(min(1.0, 2, 3L), 1.0) |
| 1233 | |
| 1234 | self.assertRaises(TypeError, min) |
| 1235 | self.assertRaises(TypeError, min, 42) |
| 1236 | self.assertRaises(ValueError, min, ()) |
| 1237 | class BadSeq: |
| 1238 | def __getitem__(self, index): |
| 1239 | raise ValueError |
| 1240 | self.assertRaises(ValueError, min, BadSeq()) |
| 1241 | class BadNumber: |
| 1242 | def __cmp__(self, other): |
| 1243 | raise ValueError |
| 1244 | self.assertRaises(ValueError, min, (42, BadNumber())) |
| 1245 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1246 | for stmt in ( |
| 1247 | "min(key=int)", # no args |
| 1248 | "min(1, key=int)", # single arg not iterable |
| 1249 | "min(1, 2, keystone=int)", # wrong keyword |
| 1250 | "min(1, 2, key=int, abc=int)", # two many keywords |
| 1251 | "min(1, 2, key=1)", # keyfunc is not callable |
| 1252 | ): |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 1253 | try: |
| 1254 | exec(stmt) in globals() |
| 1255 | except TypeError: |
| 1256 | pass |
| 1257 | else: |
| 1258 | self.fail(stmt) |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1259 | |
| 1260 | self.assertEqual(min((1,), key=neg), 1) # one elem iterable |
| 1261 | self.assertEqual(min((1,2), key=neg), 2) # two elem iterable |
| 1262 | self.assertEqual(min(1, 2, key=neg), 2) # two elems |
| 1263 | |
| 1264 | data = [random.randrange(200) for i in range(100)] |
| 1265 | keys = dict((elem, random.randrange(50)) for elem in data) |
| 1266 | f = keys.__getitem__ |
| 1267 | self.assertEqual(min(data, key=f), |
| 1268 | sorted(data, key=f)[0]) |
| 1269 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1270 | def test_oct(self): |
| 1271 | self.assertEqual(oct(100), '0144') |
| 1272 | self.assertEqual(oct(100L), '0144L') |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 1273 | self.assertEqual(oct(-100), '-0144') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1274 | self.assertEqual(oct(-100L), '-0144L') |
| 1275 | self.assertRaises(TypeError, oct, ()) |
| 1276 | |
| 1277 | def write_testfile(self): |
| 1278 | # NB the first 4 lines are also used to test input and raw_input, below |
| 1279 | fp = open(TESTFN, 'w') |
| 1280 | try: |
| 1281 | fp.write('1+1\n') |
| 1282 | fp.write('1+1\n') |
| 1283 | fp.write('The quick brown fox jumps over the lazy dog') |
| 1284 | fp.write('.\n') |
| 1285 | fp.write('Dear John\n') |
| 1286 | fp.write('XXX'*100) |
| 1287 | fp.write('YYY'*100) |
| 1288 | finally: |
| 1289 | fp.close() |
| 1290 | |
| 1291 | def test_open(self): |
| 1292 | self.write_testfile() |
| 1293 | fp = open(TESTFN, 'r') |
| 1294 | try: |
| 1295 | self.assertEqual(fp.readline(4), '1+1\n') |
| 1296 | self.assertEqual(fp.readline(4), '1+1\n') |
| 1297 | self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n') |
| 1298 | self.assertEqual(fp.readline(4), 'Dear') |
| 1299 | self.assertEqual(fp.readline(100), ' John\n') |
| 1300 | self.assertEqual(fp.read(300), 'XXX'*100) |
| 1301 | self.assertEqual(fp.read(1000), 'YYY'*100) |
| 1302 | finally: |
| 1303 | fp.close() |
| 1304 | unlink(TESTFN) |
| 1305 | |
| 1306 | def test_ord(self): |
| 1307 | self.assertEqual(ord(' '), 32) |
| 1308 | self.assertEqual(ord('A'), 65) |
| 1309 | self.assertEqual(ord('a'), 97) |
| 1310 | if have_unicode: |
| 1311 | self.assertEqual(ord(unichr(sys.maxunicode)), sys.maxunicode) |
| 1312 | self.assertRaises(TypeError, ord, 42) |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 1313 | if have_unicode: |
| 1314 | self.assertRaises(TypeError, ord, unicode("12")) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1315 | |
| 1316 | def test_pow(self): |
| 1317 | self.assertEqual(pow(0,0), 1) |
| 1318 | self.assertEqual(pow(0,1), 0) |
| 1319 | self.assertEqual(pow(1,0), 1) |
| 1320 | self.assertEqual(pow(1,1), 1) |
| 1321 | |
| 1322 | self.assertEqual(pow(2,0), 1) |
| 1323 | self.assertEqual(pow(2,10), 1024) |
| 1324 | self.assertEqual(pow(2,20), 1024*1024) |
| 1325 | self.assertEqual(pow(2,30), 1024*1024*1024) |
| 1326 | |
| 1327 | self.assertEqual(pow(-2,0), 1) |
| 1328 | self.assertEqual(pow(-2,1), -2) |
| 1329 | self.assertEqual(pow(-2,2), 4) |
| 1330 | self.assertEqual(pow(-2,3), -8) |
| 1331 | |
| 1332 | self.assertEqual(pow(0L,0), 1) |
| 1333 | self.assertEqual(pow(0L,1), 0) |
| 1334 | self.assertEqual(pow(1L,0), 1) |
| 1335 | self.assertEqual(pow(1L,1), 1) |
| 1336 | |
| 1337 | self.assertEqual(pow(2L,0), 1) |
| 1338 | self.assertEqual(pow(2L,10), 1024) |
| 1339 | self.assertEqual(pow(2L,20), 1024*1024) |
| 1340 | self.assertEqual(pow(2L,30), 1024*1024*1024) |
| 1341 | |
| 1342 | self.assertEqual(pow(-2L,0), 1) |
| 1343 | self.assertEqual(pow(-2L,1), -2) |
| 1344 | self.assertEqual(pow(-2L,2), 4) |
| 1345 | self.assertEqual(pow(-2L,3), -8) |
| 1346 | |
| 1347 | self.assertAlmostEqual(pow(0.,0), 1.) |
| 1348 | self.assertAlmostEqual(pow(0.,1), 0.) |
| 1349 | self.assertAlmostEqual(pow(1.,0), 1.) |
| 1350 | self.assertAlmostEqual(pow(1.,1), 1.) |
| 1351 | |
| 1352 | self.assertAlmostEqual(pow(2.,0), 1.) |
| 1353 | self.assertAlmostEqual(pow(2.,10), 1024.) |
| 1354 | self.assertAlmostEqual(pow(2.,20), 1024.*1024.) |
| 1355 | self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.) |
| 1356 | |
| 1357 | self.assertAlmostEqual(pow(-2.,0), 1.) |
| 1358 | self.assertAlmostEqual(pow(-2.,1), -2.) |
| 1359 | self.assertAlmostEqual(pow(-2.,2), 4.) |
| 1360 | self.assertAlmostEqual(pow(-2.,3), -8.) |
| 1361 | |
| 1362 | for x in 2, 2L, 2.0: |
| 1363 | for y in 10, 10L, 10.0: |
| 1364 | for z in 1000, 1000L, 1000.0: |
| 1365 | if isinstance(x, float) or \ |
| 1366 | isinstance(y, float) or \ |
| 1367 | isinstance(z, float): |
| 1368 | self.assertRaises(TypeError, pow, x, y, z) |
| 1369 | else: |
| 1370 | self.assertAlmostEqual(pow(x, y, z), 24.0) |
| 1371 | |
| 1372 | self.assertRaises(TypeError, pow, -1, -2, 3) |
| 1373 | self.assertRaises(ValueError, pow, 1, 2, 0) |
| 1374 | self.assertRaises(TypeError, pow, -1L, -2L, 3L) |
| 1375 | self.assertRaises(ValueError, pow, 1L, 2L, 0L) |
| 1376 | self.assertRaises(ValueError, pow, -342.43, 0.234) |
| 1377 | |
| 1378 | self.assertRaises(TypeError, pow) |
| 1379 | |
| 1380 | def test_range(self): |
| 1381 | self.assertEqual(range(3), [0, 1, 2]) |
| 1382 | self.assertEqual(range(1, 5), [1, 2, 3, 4]) |
| 1383 | self.assertEqual(range(0), []) |
| 1384 | self.assertEqual(range(-3), []) |
| 1385 | self.assertEqual(range(1, 10, 3), [1, 4, 7]) |
| 1386 | self.assertEqual(range(5, -5, -3), [5, 2, -1, -4]) |
| 1387 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1388 | # Now test range() with longs |
| 1389 | self.assertEqual(range(-2**100), []) |
| 1390 | self.assertEqual(range(0, -2**100), []) |
| 1391 | self.assertEqual(range(0, 2**100, -1), []) |
| 1392 | self.assertEqual(range(0, 2**100, -1), []) |
| 1393 | |
| 1394 | a = long(10 * sys.maxint) |
| 1395 | b = long(100 * sys.maxint) |
| 1396 | c = long(50 * sys.maxint) |
| 1397 | |
| 1398 | self.assertEqual(range(a, a+2), [a, a+1]) |
| 1399 | self.assertEqual(range(a+2, a, -1L), [a+2, a+1]) |
| 1400 | self.assertEqual(range(a+4, a, -2), [a+4, a+2]) |
| 1401 | |
| 1402 | seq = range(a, b, c) |
| 1403 | self.assert_(a in seq) |
| 1404 | self.assert_(b not in seq) |
| 1405 | self.assertEqual(len(seq), 2) |
| 1406 | |
| 1407 | seq = range(b, a, -c) |
| 1408 | self.assert_(b in seq) |
| 1409 | self.assert_(a not in seq) |
| 1410 | self.assertEqual(len(seq), 2) |
| 1411 | |
| 1412 | seq = range(-a, -b, -c) |
| 1413 | self.assert_(-a in seq) |
| 1414 | self.assert_(-b not in seq) |
| 1415 | self.assertEqual(len(seq), 2) |
| 1416 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1417 | self.assertRaises(TypeError, range) |
| 1418 | self.assertRaises(TypeError, range, 1, 2, 3, 4) |
| 1419 | self.assertRaises(ValueError, range, 1, 2, 0) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 1420 | self.assertRaises(ValueError, range, a, a + 1, long(0)) |
| 1421 | |
| 1422 | class badzero(int): |
| 1423 | def __cmp__(self, other): |
| 1424 | raise RuntimeError |
| 1425 | self.assertRaises(RuntimeError, range, a, a + 1, badzero(1)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1427 | # Reject floats when it would require PyLongs to represent. |
| 1428 | # (smaller floats still accepted, but deprecated) |
Tim Peters | 299b3df | 2003-04-15 14:40:03 +0000 | [diff] [blame] | 1429 | self.assertRaises(TypeError, range, 1e100, 1e101, 1e101) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1430 | |
Walter Dörwald | 357981e | 2003-04-15 18:59:28 +0000 | [diff] [blame] | 1431 | self.assertRaises(TypeError, range, 0, "spam") |
| 1432 | self.assertRaises(TypeError, range, 0, 42, "spam") |
| 1433 | |
| 1434 | self.assertRaises(OverflowError, range, -sys.maxint, sys.maxint) |
| 1435 | self.assertRaises(OverflowError, range, 0, 2*sys.maxint) |
| 1436 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1437 | def test_input_and_raw_input(self): |
| 1438 | self.write_testfile() |
| 1439 | fp = open(TESTFN, 'r') |
| 1440 | savestdin = sys.stdin |
| 1441 | savestdout = sys.stdout # Eats the echo |
| 1442 | try: |
| 1443 | sys.stdin = fp |
| 1444 | sys.stdout = BitBucket() |
| 1445 | self.assertEqual(input(), 2) |
| 1446 | self.assertEqual(input('testing\n'), 2) |
| 1447 | self.assertEqual(raw_input(), 'The quick brown fox jumps over the lazy dog.') |
| 1448 | self.assertEqual(raw_input('testing\n'), 'Dear John') |
Tim Peters | 8e24a96 | 2006-08-09 00:52:26 +0000 | [diff] [blame] | 1449 | |
Georg Brandl | 7e3ba2a | 2006-08-06 08:23:54 +0000 | [diff] [blame] | 1450 | # SF 1535165: don't segfault on closed stdin |
| 1451 | # sys.stdout must be a regular file for triggering |
| 1452 | sys.stdout = savestdout |
| 1453 | sys.stdin.close() |
Georg Brandl | d336e98 | 2006-08-06 09:17:16 +0000 | [diff] [blame] | 1454 | self.assertRaises(ValueError, input) |
Georg Brandl | 7e3ba2a | 2006-08-06 08:23:54 +0000 | [diff] [blame] | 1455 | |
| 1456 | sys.stdout = BitBucket() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1457 | sys.stdin = cStringIO.StringIO("NULL\0") |
| 1458 | self.assertRaises(TypeError, input, 42, 42) |
| 1459 | sys.stdin = cStringIO.StringIO(" 'whitespace'") |
| 1460 | self.assertEqual(input(), 'whitespace') |
| 1461 | sys.stdin = cStringIO.StringIO() |
| 1462 | self.assertRaises(EOFError, input) |
Hye-Shik Chang | ff83c2b | 2004-02-02 13:39:01 +0000 | [diff] [blame] | 1463 | |
| 1464 | # SF 876178: make sure input() respect future options. |
| 1465 | sys.stdin = cStringIO.StringIO('1/2') |
| 1466 | sys.stdout = cStringIO.StringIO() |
| 1467 | exec compile('print input()', 'test_builtin_tmp', 'exec') |
| 1468 | sys.stdin.seek(0, 0) |
| 1469 | exec compile('from __future__ import division;print input()', |
| 1470 | 'test_builtin_tmp', 'exec') |
| 1471 | sys.stdin.seek(0, 0) |
| 1472 | exec compile('print input()', 'test_builtin_tmp', 'exec') |
Tim Peters | b82cb8d | 2006-03-28 07:39:22 +0000 | [diff] [blame] | 1473 | # The result we expect depends on whether new division semantics |
| 1474 | # are already in effect. |
| 1475 | if 1/2 == 0: |
| 1476 | # This test was compiled with old semantics. |
| 1477 | expected = ['0', '0.5', '0'] |
| 1478 | else: |
| 1479 | # This test was compiled with new semantics (e.g., -Qnew |
| 1480 | # was given on the command line. |
| 1481 | expected = ['0.5', '0.5', '0.5'] |
| 1482 | self.assertEqual(sys.stdout.getvalue().splitlines(), expected) |
Hye-Shik Chang | ff83c2b | 2004-02-02 13:39:01 +0000 | [diff] [blame] | 1483 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1484 | del sys.stdout |
| 1485 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1486 | del sys.stdin |
| 1487 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1488 | finally: |
| 1489 | sys.stdin = savestdin |
| 1490 | sys.stdout = savestdout |
| 1491 | fp.close() |
| 1492 | unlink(TESTFN) |
| 1493 | |
| 1494 | def test_reduce(self): |
| 1495 | self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc') |
| 1496 | self.assertEqual( |
| 1497 | reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []), |
| 1498 | ['a','c','d','w'] |
| 1499 | ) |
| 1500 | self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040) |
| 1501 | self.assertEqual( |
| 1502 | reduce(lambda x, y: x*y, range(2,21), 1L), |
| 1503 | 2432902008176640000L |
| 1504 | ) |
| 1505 | self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285) |
| 1506 | self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285) |
| 1507 | self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0) |
| 1508 | self.assertRaises(TypeError, reduce) |
| 1509 | self.assertRaises(TypeError, reduce, 42, 42) |
| 1510 | self.assertRaises(TypeError, reduce, 42, 42, 42) |
| 1511 | self.assertEqual(reduce(42, "1"), "1") # func is never called with one item |
| 1512 | self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item |
| 1513 | self.assertRaises(TypeError, reduce, 42, (42, 42)) |
| 1514 | |
| 1515 | class BadSeq: |
| 1516 | def __getitem__(self, index): |
| 1517 | raise ValueError |
| 1518 | self.assertRaises(ValueError, reduce, 42, BadSeq()) |
| 1519 | |
| 1520 | def test_reload(self): |
| 1521 | import marshal |
| 1522 | reload(marshal) |
| 1523 | import string |
| 1524 | reload(string) |
| 1525 | ## import sys |
| 1526 | ## self.assertRaises(ImportError, reload, sys) |
| 1527 | |
| 1528 | def test_repr(self): |
| 1529 | self.assertEqual(repr(''), '\'\'') |
| 1530 | self.assertEqual(repr(0), '0') |
| 1531 | self.assertEqual(repr(0L), '0L') |
| 1532 | self.assertEqual(repr(()), '()') |
| 1533 | self.assertEqual(repr([]), '[]') |
| 1534 | self.assertEqual(repr({}), '{}') |
| 1535 | a = [] |
| 1536 | a.append(a) |
| 1537 | self.assertEqual(repr(a), '[[...]]') |
| 1538 | a = {} |
| 1539 | a[0] = a |
| 1540 | self.assertEqual(repr(a), '{0: {...}}') |
| 1541 | |
| 1542 | def test_round(self): |
| 1543 | self.assertEqual(round(0.0), 0.0) |
| 1544 | self.assertEqual(round(1.0), 1.0) |
| 1545 | self.assertEqual(round(10.0), 10.0) |
| 1546 | self.assertEqual(round(1000000000.0), 1000000000.0) |
| 1547 | self.assertEqual(round(1e20), 1e20) |
| 1548 | |
| 1549 | self.assertEqual(round(-1.0), -1.0) |
| 1550 | self.assertEqual(round(-10.0), -10.0) |
| 1551 | self.assertEqual(round(-1000000000.0), -1000000000.0) |
| 1552 | self.assertEqual(round(-1e20), -1e20) |
| 1553 | |
| 1554 | self.assertEqual(round(0.1), 0.0) |
| 1555 | self.assertEqual(round(1.1), 1.0) |
| 1556 | self.assertEqual(round(10.1), 10.0) |
| 1557 | self.assertEqual(round(1000000000.1), 1000000000.0) |
| 1558 | |
| 1559 | self.assertEqual(round(-1.1), -1.0) |
| 1560 | self.assertEqual(round(-10.1), -10.0) |
| 1561 | self.assertEqual(round(-1000000000.1), -1000000000.0) |
| 1562 | |
| 1563 | self.assertEqual(round(0.9), 1.0) |
| 1564 | self.assertEqual(round(9.9), 10.0) |
| 1565 | self.assertEqual(round(999999999.9), 1000000000.0) |
| 1566 | |
| 1567 | self.assertEqual(round(-0.9), -1.0) |
| 1568 | self.assertEqual(round(-9.9), -10.0) |
| 1569 | self.assertEqual(round(-999999999.9), -1000000000.0) |
| 1570 | |
| 1571 | self.assertEqual(round(-8.0, -1), -10.0) |
| 1572 | |
Georg Brandl | ccadf84 | 2006-03-31 18:54:53 +0000 | [diff] [blame] | 1573 | # test new kwargs |
| 1574 | self.assertEqual(round(number=-8.0, ndigits=-1), -10.0) |
| 1575 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1576 | self.assertRaises(TypeError, round) |
| 1577 | |
| 1578 | def test_setattr(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 1579 | setattr(sys, 'spam', 1) |
| 1580 | self.assertEqual(sys.spam, 1) |
| 1581 | self.assertRaises(TypeError, setattr, sys, 1, 'spam') |
| 1582 | self.assertRaises(TypeError, setattr) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1583 | |
| 1584 | def test_str(self): |
| 1585 | self.assertEqual(str(''), '') |
| 1586 | self.assertEqual(str(0), '0') |
| 1587 | self.assertEqual(str(0L), '0') |
| 1588 | self.assertEqual(str(()), '()') |
| 1589 | self.assertEqual(str([]), '[]') |
| 1590 | self.assertEqual(str({}), '{}') |
| 1591 | a = [] |
| 1592 | a.append(a) |
| 1593 | self.assertEqual(str(a), '[[...]]') |
| 1594 | a = {} |
| 1595 | a[0] = a |
| 1596 | self.assertEqual(str(a), '{0: {...}}') |
| 1597 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1598 | def test_sum(self): |
| 1599 | self.assertEqual(sum([]), 0) |
| 1600 | self.assertEqual(sum(range(2,8)), 27) |
| 1601 | self.assertEqual(sum(iter(range(2,8))), 27) |
| 1602 | self.assertEqual(sum(Squares(10)), 285) |
| 1603 | self.assertEqual(sum(iter(Squares(10))), 285) |
| 1604 | self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3]) |
| 1605 | |
| 1606 | self.assertRaises(TypeError, sum) |
| 1607 | self.assertRaises(TypeError, sum, 42) |
| 1608 | self.assertRaises(TypeError, sum, ['a', 'b', 'c']) |
| 1609 | self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '') |
| 1610 | self.assertRaises(TypeError, sum, [[1], [2], [3]]) |
| 1611 | self.assertRaises(TypeError, sum, [{2:3}]) |
| 1612 | self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3}) |
| 1613 | |
| 1614 | class BadSeq: |
| 1615 | def __getitem__(self, index): |
| 1616 | raise ValueError |
| 1617 | self.assertRaises(ValueError, sum, BadSeq()) |
| 1618 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1619 | def test_tuple(self): |
| 1620 | self.assertEqual(tuple(()), ()) |
| 1621 | t0_3 = (0, 1, 2, 3) |
| 1622 | t0_3_bis = tuple(t0_3) |
| 1623 | self.assert_(t0_3 is t0_3_bis) |
| 1624 | self.assertEqual(tuple([]), ()) |
| 1625 | self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3)) |
| 1626 | self.assertEqual(tuple(''), ()) |
| 1627 | self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm')) |
| 1628 | |
| 1629 | def test_type(self): |
| 1630 | self.assertEqual(type(''), type('123')) |
| 1631 | self.assertNotEqual(type(''), type(())) |
| 1632 | |
| 1633 | def test_unichr(self): |
| 1634 | if have_unicode: |
| 1635 | self.assertEqual(unichr(32), unicode(' ')) |
| 1636 | self.assertEqual(unichr(65), unicode('A')) |
| 1637 | self.assertEqual(unichr(97), unicode('a')) |
| 1638 | self.assertEqual( |
| 1639 | unichr(sys.maxunicode), |
| 1640 | unicode('\\U%08x' % (sys.maxunicode), 'unicode-escape') |
| 1641 | ) |
| 1642 | self.assertRaises(ValueError, unichr, sys.maxunicode+1) |
| 1643 | self.assertRaises(TypeError, unichr) |
| 1644 | |
Guido van Rossum | fee7b93 | 2005-01-16 00:21:28 +0000 | [diff] [blame] | 1645 | # We don't want self in vars(), so these are static methods |
| 1646 | |
| 1647 | @staticmethod |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1648 | def get_vars_f0(): |
| 1649 | return vars() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1650 | |
Guido van Rossum | fee7b93 | 2005-01-16 00:21:28 +0000 | [diff] [blame] | 1651 | @staticmethod |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1652 | def get_vars_f2(): |
| 1653 | BuiltinTest.get_vars_f0() |
| 1654 | a = 1 |
| 1655 | b = 2 |
| 1656 | return vars() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1657 | |
| 1658 | def test_vars(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1659 | self.assertEqual(set(vars()), set(dir())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1660 | import sys |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1661 | self.assertEqual(set(vars(sys)), set(dir(sys))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1662 | self.assertEqual(self.get_vars_f0(), {}) |
| 1663 | self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2}) |
| 1664 | self.assertRaises(TypeError, vars, 42, 42) |
| 1665 | self.assertRaises(TypeError, vars, 42) |
| 1666 | |
| 1667 | def test_zip(self): |
| 1668 | a = (1, 2, 3) |
| 1669 | b = (4, 5, 6) |
| 1670 | t = [(1, 4), (2, 5), (3, 6)] |
| 1671 | self.assertEqual(zip(a, b), t) |
| 1672 | b = [4, 5, 6] |
| 1673 | self.assertEqual(zip(a, b), t) |
| 1674 | b = (4, 5, 6, 7) |
| 1675 | self.assertEqual(zip(a, b), t) |
| 1676 | class I: |
| 1677 | def __getitem__(self, i): |
| 1678 | if i < 0 or i > 2: raise IndexError |
| 1679 | return i + 4 |
| 1680 | self.assertEqual(zip(a, I()), t) |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 1681 | self.assertEqual(zip(), []) |
| 1682 | self.assertEqual(zip(*[]), []) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1683 | self.assertRaises(TypeError, zip, None) |
| 1684 | class G: |
| 1685 | pass |
| 1686 | self.assertRaises(TypeError, zip, a, G()) |
| 1687 | |
| 1688 | # Make sure zip doesn't try to allocate a billion elements for the |
| 1689 | # result list when one of its arguments doesn't say how long it is. |
| 1690 | # A MemoryError is the most likely failure mode. |
| 1691 | class SequenceWithoutALength: |
| 1692 | def __getitem__(self, i): |
| 1693 | if i == 5: |
| 1694 | raise IndexError |
| 1695 | else: |
| 1696 | return i |
| 1697 | self.assertEqual( |
| 1698 | zip(SequenceWithoutALength(), xrange(2**30)), |
| 1699 | list(enumerate(range(5))) |
| 1700 | ) |
| 1701 | |
| 1702 | class BadSeq: |
| 1703 | def __getitem__(self, i): |
| 1704 | if i == 5: |
| 1705 | raise ValueError |
| 1706 | else: |
| 1707 | return i |
| 1708 | self.assertRaises(ValueError, zip, BadSeq(), BadSeq()) |
| 1709 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1710 | class TestSorted(unittest.TestCase): |
| 1711 | |
| 1712 | def test_basic(self): |
| 1713 | data = range(100) |
| 1714 | copy = data[:] |
| 1715 | random.shuffle(copy) |
| 1716 | self.assertEqual(data, sorted(copy)) |
| 1717 | self.assertNotEqual(data, copy) |
| 1718 | |
| 1719 | data.reverse() |
| 1720 | random.shuffle(copy) |
| 1721 | self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x))) |
| 1722 | self.assertNotEqual(data, copy) |
| 1723 | random.shuffle(copy) |
| 1724 | self.assertEqual(data, sorted(copy, key=lambda x: -x)) |
| 1725 | self.assertNotEqual(data, copy) |
| 1726 | random.shuffle(copy) |
| 1727 | self.assertEqual(data, sorted(copy, reverse=1)) |
| 1728 | self.assertNotEqual(data, copy) |
| 1729 | |
| 1730 | def test_inputtypes(self): |
| 1731 | s = 'abracadabra' |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 1732 | types = [list, tuple] |
| 1733 | if have_unicode: |
| 1734 | types.insert(0, unicode) |
| 1735 | for T in types: |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1736 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1737 | |
| 1738 | s = ''.join(dict.fromkeys(s).keys()) # unique letters only |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 1739 | types = [set, frozenset, list, tuple, dict.fromkeys] |
| 1740 | if have_unicode: |
| 1741 | types.insert(0, unicode) |
| 1742 | for T in types: |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1743 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1744 | |
| 1745 | def test_baddecorator(self): |
| 1746 | data = 'The quick Brown fox Jumped over The lazy Dog'.split() |
| 1747 | self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) |
| 1748 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1749 | def test_main(verbose=None): |
| 1750 | test_classes = (BuiltinTest, TestSorted) |
| 1751 | |
| 1752 | run_unittest(*test_classes) |
| 1753 | |
| 1754 | # verify reference counting |
| 1755 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 1756 | import gc |
| 1757 | counts = [None] * 5 |
| 1758 | for i in xrange(len(counts)): |
| 1759 | run_unittest(*test_classes) |
| 1760 | gc.collect() |
| 1761 | counts[i] = sys.gettotalrefcount() |
| 1762 | print counts |
| 1763 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1764 | |
| 1765 | if __name__ == "__main__": |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1766 | test_main(verbose=True) |