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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | import test.support, unittest |
| 4 | from test.support import fcmp, TESTFN, unlink, run_unittest, \ |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 5 | 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 | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 8 | import sys, warnings, random, collections, io, fractions |
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 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 14 | class Squares: |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 15 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 16 | def __init__(self, max): |
| 17 | self.max = max |
| 18 | self.sofar = [] |
| 19 | |
| 20 | def __len__(self): return len(self.sofar) |
| 21 | |
| 22 | def __getitem__(self, i): |
| 23 | if not 0 <= i < self.max: raise IndexError |
| 24 | n = len(self.sofar) |
| 25 | while n <= i: |
| 26 | self.sofar.append(n*n) |
| 27 | n += 1 |
| 28 | return self.sofar[i] |
| 29 | |
| 30 | class StrSquares: |
| 31 | |
| 32 | def __init__(self, max): |
| 33 | self.max = max |
| 34 | self.sofar = [] |
| 35 | |
| 36 | def __len__(self): |
| 37 | return len(self.sofar) |
| 38 | |
| 39 | def __getitem__(self, i): |
| 40 | if not 0 <= i < self.max: |
| 41 | raise IndexError |
| 42 | n = len(self.sofar) |
| 43 | while n <= i: |
| 44 | self.sofar.append(str(n*n)) |
| 45 | n += 1 |
| 46 | return self.sofar[i] |
| 47 | |
| 48 | class BitBucket: |
| 49 | def write(self, line): |
| 50 | pass |
| 51 | |
Facundo Batista | 2336bdd | 2008-01-19 19:12:01 +0000 | [diff] [blame] | 52 | test_conv_no_sign = [ |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 53 | ('0', 0), |
| 54 | ('1', 1), |
| 55 | ('9', 9), |
| 56 | ('10', 10), |
| 57 | ('99', 99), |
| 58 | ('100', 100), |
| 59 | ('314', 314), |
| 60 | (' 314', 314), |
| 61 | ('314 ', 314), |
| 62 | (' \t\t 314 \t\t ', 314), |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 63 | (repr(sys.maxsize), sys.maxsize), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 64 | (' 1x', ValueError), |
| 65 | (' 1 ', 1), |
| 66 | (' 1\02 ', ValueError), |
| 67 | ('', ValueError), |
| 68 | (' ', ValueError), |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 69 | (' \t\t ', ValueError), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 70 | (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 71 | (chr(0x200), ValueError), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 72 | ] |
| 73 | |
Facundo Batista | 2336bdd | 2008-01-19 19:12:01 +0000 | [diff] [blame] | 74 | test_conv_sign = [ |
| 75 | ('0', 0), |
| 76 | ('1', 1), |
| 77 | ('9', 9), |
| 78 | ('10', 10), |
| 79 | ('99', 99), |
| 80 | ('100', 100), |
| 81 | ('314', 314), |
| 82 | (' 314', ValueError), |
| 83 | ('314 ', 314), |
| 84 | (' \t\t 314 \t\t ', ValueError), |
| 85 | (repr(sys.maxsize), sys.maxsize), |
| 86 | (' 1x', ValueError), |
| 87 | (' 1 ', ValueError), |
| 88 | (' 1\02 ', ValueError), |
| 89 | ('', ValueError), |
| 90 | (' ', ValueError), |
| 91 | (' \t\t ', ValueError), |
| 92 | (str(b'\u0663\u0661\u0664 ','raw-unicode-escape'), 314), |
| 93 | (chr(0x200), ValueError), |
| 94 | ] |
| 95 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 96 | class TestFailingBool: |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 97 | def __bool__(self): |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 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') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | __import__(name='sys') |
| 111 | __import__(name='time', level=0) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 112 | self.assertRaises(ImportError, __import__, 'spamspam') |
| 113 | self.assertRaises(TypeError, __import__, 1, 2, 3, 4) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | self.assertRaises(ValueError, __import__, '') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | self.assertRaises(TypeError, __import__, 'sys', name='sys') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 116 | |
| 117 | def test_abs(self): |
| 118 | # int |
| 119 | self.assertEqual(abs(0), 0) |
| 120 | self.assertEqual(abs(1234), 1234) |
| 121 | self.assertEqual(abs(-1234), 1234) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 122 | self.assertTrue(abs(-sys.maxsize-1) > 0) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 123 | # float |
| 124 | self.assertEqual(abs(0.0), 0.0) |
| 125 | self.assertEqual(abs(3.14), 3.14) |
| 126 | self.assertEqual(abs(-3.14), 3.14) |
| 127 | # long |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 128 | self.assertEqual(abs(0), 0) |
| 129 | self.assertEqual(abs(1234), 1234) |
| 130 | self.assertEqual(abs(-1234), 1234) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 131 | # str |
| 132 | self.assertRaises(TypeError, abs, 'a') |
| 133 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 134 | def test_all(self): |
| 135 | self.assertEqual(all([2, 4, 6]), True) |
| 136 | self.assertEqual(all([2, None, 6]), False) |
| 137 | self.assertRaises(RuntimeError, all, [2, TestFailingBool(), 6]) |
| 138 | self.assertRaises(RuntimeError, all, TestFailingIter()) |
| 139 | self.assertRaises(TypeError, all, 10) # Non-iterable |
| 140 | self.assertRaises(TypeError, all) # No args |
| 141 | self.assertRaises(TypeError, all, [2, 4, 6], []) # Too many args |
| 142 | self.assertEqual(all([]), True) # Empty iterator |
| 143 | S = [50, 60] |
| 144 | self.assertEqual(all(x > 42 for x in S), True) |
| 145 | S = [50, 40, 60] |
| 146 | self.assertEqual(all(x > 42 for x in S), False) |
| 147 | |
| 148 | def test_any(self): |
| 149 | self.assertEqual(any([None, None, None]), False) |
| 150 | self.assertEqual(any([None, 4, None]), True) |
| 151 | self.assertRaises(RuntimeError, any, [None, TestFailingBool(), 6]) |
| 152 | self.assertRaises(RuntimeError, all, TestFailingIter()) |
| 153 | self.assertRaises(TypeError, any, 10) # Non-iterable |
| 154 | self.assertRaises(TypeError, any) # No args |
| 155 | self.assertRaises(TypeError, any, [2, 4, 6], []) # Too many args |
| 156 | self.assertEqual(any([]), False) # Empty iterator |
| 157 | S = [40, 60, 30] |
| 158 | self.assertEqual(any(x > 42 for x in S), True) |
| 159 | S = [10, 20, 30] |
| 160 | self.assertEqual(any(x > 42 for x in S), False) |
| 161 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 162 | def test_neg(self): |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 163 | x = -sys.maxsize-1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 164 | self.assert_(isinstance(x, int)) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 165 | self.assertEqual(-x, sys.maxsize+1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 166 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 167 | # XXX(nnorwitz): This test case for callable should probably be removed. |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 168 | def test_callable(self): |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 169 | self.assert_(hasattr(len, '__call__')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 170 | def f(): pass |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 171 | self.assert_(hasattr(f, '__call__')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 172 | class C: |
| 173 | def meth(self): pass |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 174 | self.assert_(hasattr(C, '__call__')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 175 | x = C() |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 176 | self.assert_(hasattr(x.meth, '__call__')) |
| 177 | self.assert_(not hasattr(x, '__call__')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 178 | class D(C): |
| 179 | def __call__(self): pass |
| 180 | y = D() |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 181 | self.assert_(hasattr(y, '__call__')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 182 | y() |
| 183 | |
| 184 | def test_chr(self): |
| 185 | self.assertEqual(chr(32), ' ') |
| 186 | self.assertEqual(chr(65), 'A') |
| 187 | self.assertEqual(chr(97), 'a') |
| 188 | self.assertEqual(chr(0xff), '\xff') |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 189 | self.assertRaises(ValueError, chr, 1<<24) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 190 | self.assertEqual(chr(sys.maxunicode), |
| 191 | str(('\\U%08x' % (sys.maxunicode)).encode("ascii"), |
| 192 | 'unicode-escape')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 193 | self.assertRaises(TypeError, chr) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 194 | self.assertEqual(chr(0x0000FFFF), "\U0000FFFF") |
| 195 | self.assertEqual(chr(0x00010000), "\U00010000") |
| 196 | self.assertEqual(chr(0x00010001), "\U00010001") |
| 197 | self.assertEqual(chr(0x000FFFFE), "\U000FFFFE") |
| 198 | self.assertEqual(chr(0x000FFFFF), "\U000FFFFF") |
| 199 | self.assertEqual(chr(0x00100000), "\U00100000") |
| 200 | self.assertEqual(chr(0x00100001), "\U00100001") |
| 201 | self.assertEqual(chr(0x0010FFFE), "\U0010FFFE") |
| 202 | self.assertEqual(chr(0x0010FFFF), "\U0010FFFF") |
| 203 | self.assertRaises(ValueError, chr, -1) |
| 204 | self.assertRaises(ValueError, chr, 0x00110000) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 206 | def test_cmp(self): |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 207 | self.assertEqual(cmp(-1, 1), -1) |
| 208 | self.assertEqual(cmp(1, -1), 1) |
| 209 | self.assertEqual(cmp(1, 1), 0) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 210 | # verify that circular objects are not handled |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 211 | a = []; a.append(a) |
| 212 | b = []; b.append(b) |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 213 | from collections import UserList |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 214 | c = UserList(); c.append(c) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 215 | self.assertRaises(RuntimeError, cmp, a, b) |
| 216 | self.assertRaises(RuntimeError, cmp, b, c) |
| 217 | self.assertRaises(RuntimeError, cmp, c, a) |
| 218 | self.assertRaises(RuntimeError, cmp, a, c) |
| 219 | # okay, now break the cycles |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 220 | a.pop(); b.pop(); c.pop() |
| 221 | self.assertRaises(TypeError, cmp) |
| 222 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 223 | def test_compile(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 224 | compile('print(1)\n', '', 'exec') |
Amaury Forgeot d'Arc | af59346 | 2007-11-22 20:53:01 +0000 | [diff] [blame] | 225 | bom = b'\xef\xbb\xbf' |
| 226 | compile(bom + b'print(1)\n', '', 'exec') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 227 | compile(source='pass', filename='?', mode='exec') |
| 228 | compile(dont_inherit=0, filename='tmp', source='0', mode='eval') |
| 229 | compile('pass', '?', dont_inherit=1, mode='exec') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 230 | self.assertRaises(TypeError, compile) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 231 | self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode') |
| 232 | self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 233 | self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 234 | self.assertRaises(TypeError, compile, 'pass', '?', 'exec', |
| 235 | mode='eval', source='0', filename='tmp') |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 236 | compile('print("\xe5")\n', '', 'exec') |
| 237 | self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') |
| 238 | self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 239 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 240 | def test_delattr(self): |
| 241 | import sys |
| 242 | sys.spam = 1 |
| 243 | delattr(sys, 'spam') |
| 244 | self.assertRaises(TypeError, delattr) |
| 245 | |
| 246 | def test_dir(self): |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 247 | # dir(wrong number of arguments) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 248 | self.assertRaises(TypeError, dir, 42, 42) |
| 249 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 250 | # dir() - local scope |
| 251 | local_var = 1 |
| 252 | self.assert_('local_var' in dir()) |
| 253 | |
| 254 | # dir(module) |
| 255 | import sys |
| 256 | self.assert_('exit' in dir(sys)) |
| 257 | |
| 258 | # dir(module_with_invalid__dict__) |
| 259 | import types |
| 260 | class Foo(types.ModuleType): |
| 261 | __dict__ = 8 |
| 262 | f = Foo("foo") |
| 263 | self.assertRaises(TypeError, dir, f) |
| 264 | |
| 265 | # dir(type) |
| 266 | self.assert_("strip" in dir(str)) |
| 267 | self.assert_("__mro__" not in dir(str)) |
| 268 | |
| 269 | # dir(obj) |
| 270 | class Foo(object): |
| 271 | def __init__(self): |
| 272 | self.x = 7 |
| 273 | self.y = 8 |
| 274 | self.z = 9 |
| 275 | f = Foo() |
| 276 | self.assert_("y" in dir(f)) |
| 277 | |
| 278 | # dir(obj_no__dict__) |
| 279 | class Foo(object): |
| 280 | __slots__ = [] |
| 281 | f = Foo() |
| 282 | self.assert_("__repr__" in dir(f)) |
| 283 | |
| 284 | # dir(obj_no__class__with__dict__) |
| 285 | # (an ugly trick to cause getattr(f, "__class__") to fail) |
| 286 | class Foo(object): |
| 287 | __slots__ = ["__class__", "__dict__"] |
| 288 | def __init__(self): |
| 289 | self.bar = "wow" |
| 290 | f = Foo() |
| 291 | self.assert_("__repr__" not in dir(f)) |
| 292 | self.assert_("bar" in dir(f)) |
| 293 | |
| 294 | # dir(obj_using __dir__) |
| 295 | class Foo(object): |
| 296 | def __dir__(self): |
| 297 | return ["kan", "ga", "roo"] |
| 298 | f = Foo() |
| 299 | self.assert_(dir(f) == ["ga", "kan", "roo"]) |
| 300 | |
| 301 | # dir(obj__dir__not_list) |
| 302 | class Foo(object): |
| 303 | def __dir__(self): |
| 304 | return 7 |
| 305 | f = Foo() |
| 306 | self.assertRaises(TypeError, dir, f) |
| 307 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 308 | # dir(traceback) |
| 309 | try: |
| 310 | raise IndexError |
| 311 | except: |
| 312 | self.assertEqual(len(dir(sys.exc_info()[2])), 4) |
| 313 | |
| 314 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 315 | def test_divmod(self): |
| 316 | self.assertEqual(divmod(12, 7), (1, 5)) |
| 317 | self.assertEqual(divmod(-12, 7), (-2, 2)) |
| 318 | self.assertEqual(divmod(12, -7), (-2, -2)) |
| 319 | self.assertEqual(divmod(-12, -7), (1, -5)) |
| 320 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 321 | self.assertEqual(divmod(12, 7), (1, 5)) |
| 322 | self.assertEqual(divmod(-12, 7), (-2, 2)) |
| 323 | self.assertEqual(divmod(12, -7), (-2, -2)) |
| 324 | self.assertEqual(divmod(-12, -7), (1, -5)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 326 | self.assertEqual(divmod(12, 7), (1, 5)) |
| 327 | self.assertEqual(divmod(-12, 7), (-2, 2)) |
| 328 | self.assertEqual(divmod(12, -7), (-2, -2)) |
| 329 | self.assertEqual(divmod(-12, -7), (1, -5)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 330 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 331 | self.assertEqual(divmod(-sys.maxsize-1, -1), |
| 332 | (sys.maxsize+1, 0)) |
Raymond Hettinger | 5ea7e31 | 2004-09-30 07:47:20 +0000 | [diff] [blame] | 333 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 334 | self.assert_(not fcmp(divmod(3.25, 1.0), (3.0, 0.25))) |
| 335 | self.assert_(not fcmp(divmod(-3.25, 1.0), (-4.0, 0.75))) |
| 336 | self.assert_(not fcmp(divmod(3.25, -1.0), (-4.0, -0.75))) |
| 337 | self.assert_(not fcmp(divmod(-3.25, -1.0), (3.0, -0.25))) |
| 338 | |
| 339 | self.assertRaises(TypeError, divmod) |
| 340 | |
| 341 | def test_eval(self): |
| 342 | self.assertEqual(eval('1+1'), 2) |
| 343 | self.assertEqual(eval(' 1+1\n'), 2) |
| 344 | globals = {'a': 1, 'b': 2} |
| 345 | locals = {'b': 200, 'c': 300} |
| 346 | self.assertEqual(eval('a', globals) , 1) |
| 347 | self.assertEqual(eval('a', globals, locals), 1) |
| 348 | self.assertEqual(eval('b', globals, locals), 200) |
| 349 | self.assertEqual(eval('c', globals, locals), 300) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 350 | globals = {'a': 1, 'b': 2} |
| 351 | locals = {'b': 200, 'c': 300} |
Amaury Forgeot d'Arc | af59346 | 2007-11-22 20:53:01 +0000 | [diff] [blame] | 352 | bom = b'\xef\xbb\xbf' |
| 353 | self.assertEqual(eval(bom + b'a', globals, locals), 1) |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 354 | self.assertEqual(eval('"\xe5"', globals), "\xe5") |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 355 | self.assertRaises(TypeError, eval) |
| 356 | self.assertRaises(TypeError, eval, ()) |
Amaury Forgeot d'Arc | af59346 | 2007-11-22 20:53:01 +0000 | [diff] [blame] | 357 | self.assertRaises(SyntaxError, eval, bom[:2] + b'a') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 358 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 359 | def test_general_eval(self): |
| 360 | # Tests that general mappings can be used for the locals argument |
| 361 | |
| 362 | class M: |
| 363 | "Test mapping interface versus possible calls from eval()." |
| 364 | def __getitem__(self, key): |
| 365 | if key == 'a': |
| 366 | return 12 |
| 367 | raise KeyError |
Guido van Rossum | cd70eee | 2007-02-11 18:53:00 +0000 | [diff] [blame] | 368 | def keys(self): |
| 369 | return list('xyz') |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 370 | |
| 371 | m = M() |
| 372 | g = globals() |
| 373 | self.assertEqual(eval('a', g, m), 12) |
| 374 | self.assertRaises(NameError, eval, 'b', g, m) |
| 375 | self.assertEqual(eval('dir()', g, m), list('xyz')) |
| 376 | self.assertEqual(eval('globals()', g, m), g) |
| 377 | self.assertEqual(eval('locals()', g, m), m) |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 378 | self.assertRaises(TypeError, eval, 'a', m) |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 379 | class A: |
| 380 | "Non-mapping" |
| 381 | pass |
| 382 | m = A() |
| 383 | self.assertRaises(TypeError, eval, 'a', g, m) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 384 | |
| 385 | # Verify that dict subclasses work as well |
| 386 | class D(dict): |
| 387 | def __getitem__(self, key): |
| 388 | if key == 'a': |
| 389 | return 12 |
| 390 | return dict.__getitem__(self, key) |
Guido van Rossum | cd70eee | 2007-02-11 18:53:00 +0000 | [diff] [blame] | 391 | def keys(self): |
| 392 | return list('xyz') |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 393 | |
| 394 | d = D() |
| 395 | self.assertEqual(eval('a', g, d), 12) |
| 396 | self.assertRaises(NameError, eval, 'b', g, d) |
| 397 | self.assertEqual(eval('dir()', g, d), list('xyz')) |
| 398 | self.assertEqual(eval('globals()', g, d), g) |
| 399 | self.assertEqual(eval('locals()', g, d), d) |
| 400 | |
| 401 | # Verify locals stores (used by list comps) |
| 402 | eval('[locals() for i in (2,3)]', g, d) |
Raymond Hettinger | f80680d | 2008-02-06 00:07:11 +0000 | [diff] [blame] | 403 | eval('[locals() for i in (2,3)]', g, collections.UserDict()) |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 404 | |
| 405 | class SpreadSheet: |
| 406 | "Sample application showing nested, calculated lookups." |
| 407 | _cells = {} |
| 408 | def __setitem__(self, key, formula): |
| 409 | self._cells[key] = formula |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 410 | def __getitem__(self, key): |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 411 | return eval(self._cells[key], globals(), self) |
| 412 | |
| 413 | ss = SpreadSheet() |
| 414 | ss['a1'] = '5' |
| 415 | ss['a2'] = 'a1*6' |
| 416 | ss['a3'] = 'a2*7' |
| 417 | self.assertEqual(ss['a3'], 210) |
| 418 | |
Raymond Hettinger | 2a7dede | 2004-08-07 04:55:30 +0000 | [diff] [blame] | 419 | # Verify that dir() catches a non-list returned by eval |
| 420 | # SF bug #1004669 |
| 421 | class C: |
| 422 | def __getitem__(self, item): |
| 423 | raise KeyError(item) |
Guido van Rossum | cd70eee | 2007-02-11 18:53:00 +0000 | [diff] [blame] | 424 | def keys(self): |
| 425 | return 1 # used to be 'a' but that's no longer an error |
Raymond Hettinger | 2a7dede | 2004-08-07 04:55:30 +0000 | [diff] [blame] | 426 | self.assertRaises(TypeError, eval, 'dir()', globals(), C()) |
| 427 | |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 428 | def test_exec(self): |
| 429 | g = {} |
| 430 | exec('z = 1', g) |
| 431 | if '__builtins__' in g: |
| 432 | del g['__builtins__'] |
| 433 | self.assertEqual(g, {'z': 1}) |
| 434 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 435 | exec('z = 1+1', g) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 436 | if '__builtins__' in g: |
| 437 | del g['__builtins__'] |
| 438 | self.assertEqual(g, {'z': 2}) |
| 439 | g = {} |
| 440 | l = {} |
| 441 | |
| 442 | import warnings |
| 443 | warnings.filterwarnings("ignore", "global statement", module="<string>") |
| 444 | exec('global a; a = 1; b = 2', g, l) |
| 445 | if '__builtins__' in g: |
| 446 | del g['__builtins__'] |
| 447 | if '__builtins__' in l: |
| 448 | del l['__builtins__'] |
| 449 | self.assertEqual((g, l), ({'a': 1}, {'b': 2})) |
| 450 | |
Amaury Forgeot d'Arc | 9ed7735 | 2008-04-04 23:25:27 +0000 | [diff] [blame] | 451 | def test_exec_redirected(self): |
| 452 | savestdout = sys.stdout |
| 453 | sys.stdout = None # Whatever that cannot flush() |
| 454 | try: |
| 455 | # Used to raise SystemError('error return without exception set') |
| 456 | exec('a') |
| 457 | except NameError: |
| 458 | pass |
| 459 | finally: |
| 460 | sys.stdout = savestdout |
| 461 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 462 | def test_filter(self): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 463 | self.assertEqual(list(filter(lambda c: 'a' <= c <= 'z', 'Hello World')), list('elloorld')) |
| 464 | self.assertEqual(list(filter(None, [1, 'hello', [], [3], '', None, 9, 0])), [1, 'hello', [3], 9]) |
| 465 | self.assertEqual(list(filter(lambda x: x > 0, [1, -3, 9, 0, 2])), [1, 9, 2]) |
| 466 | self.assertEqual(list(filter(None, Squares(10))), [1, 4, 9, 16, 25, 36, 49, 64, 81]) |
| 467 | self.assertEqual(list(filter(lambda x: x%2, Squares(10))), [1, 9, 25, 49, 81]) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 468 | def identity(item): |
| 469 | return 1 |
| 470 | filter(identity, Squares(5)) |
| 471 | self.assertRaises(TypeError, filter) |
| 472 | class BadSeq(object): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 473 | def __getitem__(self, index): |
| 474 | if index<4: |
| 475 | return 42 |
| 476 | raise ValueError |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 477 | self.assertRaises(ValueError, list, filter(lambda x: x, BadSeq())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 478 | def badfunc(): |
| 479 | pass |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 480 | self.assertRaises(TypeError, list, filter(badfunc, range(5))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 481 | |
Walter Dörwald | bf51707 | 2003-01-27 15:57:14 +0000 | [diff] [blame] | 482 | # test bltinmodule.c::filtertuple() |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 483 | self.assertEqual(list(filter(None, (1, 2))), [1, 2]) |
| 484 | self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) |
| 485 | self.assertRaises(TypeError, list, filter(42, (1, 2))) |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 486 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 487 | def test_getattr(self): |
| 488 | import sys |
| 489 | self.assert_(getattr(sys, 'stdout') is sys.stdout) |
| 490 | self.assertRaises(TypeError, getattr, sys, 1) |
| 491 | self.assertRaises(TypeError, getattr, sys, 1, "foo") |
| 492 | self.assertRaises(TypeError, getattr) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 493 | self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 494 | |
| 495 | def test_hasattr(self): |
| 496 | import sys |
| 497 | self.assert_(hasattr(sys, 'stdout')) |
| 498 | self.assertRaises(TypeError, hasattr, sys, 1) |
| 499 | self.assertRaises(TypeError, hasattr) |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 500 | self.assertEqual(False, hasattr(sys, chr(sys.maxunicode))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 501 | |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 502 | # Check that hasattr allows SystemExit and KeyboardInterrupts by |
| 503 | class A: |
| 504 | def __getattr__(self, what): |
| 505 | raise KeyboardInterrupt |
| 506 | self.assertRaises(KeyboardInterrupt, hasattr, A(), "b") |
| 507 | class B: |
| 508 | def __getattr__(self, what): |
| 509 | raise SystemExit |
| 510 | self.assertRaises(SystemExit, hasattr, B(), "b") |
| 511 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 512 | def test_hash(self): |
| 513 | hash(None) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 514 | self.assertEqual(hash(1), hash(1)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 515 | self.assertEqual(hash(1), hash(1.0)) |
| 516 | hash('spam') |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 517 | self.assertEqual(hash('spam'), hash(b'spam')) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 518 | hash((0,1,2,3)) |
| 519 | def f(): pass |
| 520 | self.assertRaises(TypeError, hash, []) |
| 521 | self.assertRaises(TypeError, hash, {}) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 522 | # Bug 1536021: Allow hash to return long objects |
| 523 | class X: |
| 524 | def __hash__(self): |
| 525 | return 2**100 |
| 526 | self.assertEquals(type(hash(X())), int) |
| 527 | class Y(object): |
| 528 | def __hash__(self): |
| 529 | return 2**100 |
| 530 | self.assertEquals(type(hash(Y())), int) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 531 | class Z(int): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 532 | def __hash__(self): |
| 533 | return self |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 534 | self.assertEquals(hash(Z(42)), hash(42)) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 535 | |
| 536 | def test_hex(self): |
| 537 | self.assertEqual(hex(16), '0x10') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 538 | self.assertEqual(hex(16), '0x10') |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 539 | self.assertEqual(hex(-16), '-0x10') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 540 | self.assertEqual(hex(-16), '-0x10') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 541 | self.assertRaises(TypeError, hex, {}) |
| 542 | |
| 543 | def test_id(self): |
| 544 | id(None) |
| 545 | id(1) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 546 | id(1) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 547 | id(1.0) |
| 548 | id('spam') |
| 549 | id((0,1,2,3)) |
| 550 | id([0,1,2,3]) |
| 551 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 552 | |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 553 | # Test input() later, alphabetized as if it were raw_input |
| 554 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 555 | def test_iter(self): |
| 556 | self.assertRaises(TypeError, iter) |
| 557 | self.assertRaises(TypeError, iter, 42, 42) |
| 558 | lists = [("1", "2"), ["1", "2"], "12"] |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 559 | for l in lists: |
| 560 | i = iter(l) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 561 | self.assertEqual(next(i), '1') |
| 562 | self.assertEqual(next(i), '2') |
| 563 | self.assertRaises(StopIteration, next, i) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 564 | |
| 565 | def test_isinstance(self): |
| 566 | class C: |
| 567 | pass |
| 568 | class D(C): |
| 569 | pass |
| 570 | class E: |
| 571 | pass |
| 572 | c = C() |
| 573 | d = D() |
| 574 | e = E() |
| 575 | self.assert_(isinstance(c, C)) |
| 576 | self.assert_(isinstance(d, C)) |
| 577 | self.assert_(not isinstance(e, C)) |
| 578 | self.assert_(not isinstance(c, D)) |
| 579 | self.assert_(not isinstance('foo', E)) |
| 580 | self.assertRaises(TypeError, isinstance, E, 'foo') |
| 581 | self.assertRaises(TypeError, isinstance) |
| 582 | |
| 583 | def test_issubclass(self): |
| 584 | class C: |
| 585 | pass |
| 586 | class D(C): |
| 587 | pass |
| 588 | class E: |
| 589 | pass |
| 590 | c = C() |
| 591 | d = D() |
| 592 | e = E() |
| 593 | self.assert_(issubclass(D, C)) |
| 594 | self.assert_(issubclass(C, C)) |
| 595 | self.assert_(not issubclass(C, D)) |
| 596 | self.assertRaises(TypeError, issubclass, 'foo', E) |
| 597 | self.assertRaises(TypeError, issubclass, E, 'foo') |
| 598 | self.assertRaises(TypeError, issubclass) |
| 599 | |
| 600 | def test_len(self): |
| 601 | self.assertEqual(len('123'), 3) |
| 602 | self.assertEqual(len(()), 0) |
| 603 | self.assertEqual(len((1, 2, 3, 4)), 4) |
| 604 | self.assertEqual(len([1, 2, 3, 4]), 4) |
| 605 | self.assertEqual(len({}), 0) |
| 606 | self.assertEqual(len({'a':1, 'b': 2}), 2) |
| 607 | class BadSeq: |
| 608 | def __len__(self): |
| 609 | raise ValueError |
| 610 | self.assertRaises(ValueError, len, BadSeq()) |
| 611 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 612 | def test_map(self): |
| 613 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 614 | list(map(lambda x: x*x, range(1,4))), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 615 | [1, 4, 9] |
| 616 | ) |
| 617 | try: |
| 618 | from math import sqrt |
| 619 | except ImportError: |
| 620 | def sqrt(x): |
| 621 | return pow(x, 0.5) |
| 622 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 623 | list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 624 | [[4.0, 2.0], [9.0, 3.0]] |
| 625 | ) |
| 626 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 627 | list(map(lambda x, y: x+y, [1,3,2], [9,1,4])), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 628 | [10, 4, 6] |
| 629 | ) |
| 630 | |
| 631 | def plus(*v): |
| 632 | accu = 0 |
| 633 | for i in v: accu = accu + i |
| 634 | return accu |
| 635 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 636 | list(map(plus, [1, 3, 7])), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 637 | [1, 3, 7] |
| 638 | ) |
| 639 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 640 | list(map(plus, [1, 3, 7], [4, 9, 2])), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 641 | [1+4, 3+9, 7+2] |
| 642 | ) |
| 643 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 644 | list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 645 | [1+4+1, 3+9+1, 7+2+0] |
| 646 | ) |
| 647 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 648 | list(map(int, Squares(10))), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 649 | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |
| 650 | ) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 651 | def Max(a, b): |
| 652 | if a is None: |
| 653 | return b |
| 654 | if b is None: |
| 655 | return a |
| 656 | return max(a, b) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 657 | self.assertEqual( |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 658 | list(map(Max, Squares(3), Squares(2))), |
| 659 | [0, 1] |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 660 | ) |
| 661 | self.assertRaises(TypeError, map) |
| 662 | self.assertRaises(TypeError, map, lambda x: x, 42) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 663 | class BadSeq: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 664 | def __iter__(self): |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 665 | raise ValueError |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 666 | yield None |
| 667 | self.assertRaises(ValueError, list, map(lambda x: x, BadSeq())) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 668 | def badfunc(x): |
| 669 | raise RuntimeError |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 670 | self.assertRaises(RuntimeError, list, map(badfunc, range(5))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 671 | |
| 672 | def test_max(self): |
| 673 | self.assertEqual(max('123123'), '3') |
| 674 | self.assertEqual(max(1, 2, 3), 3) |
| 675 | self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3) |
| 676 | self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3) |
| 677 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 678 | self.assertEqual(max(1, 2, 3.0), 3.0) |
| 679 | self.assertEqual(max(1, 2.0, 3), 3) |
| 680 | self.assertEqual(max(1.0, 2, 3), 3) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 681 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 682 | for stmt in ( |
| 683 | "max(key=int)", # no args |
| 684 | "max(1, key=int)", # single arg not iterable |
| 685 | "max(1, 2, keystone=int)", # wrong keyword |
| 686 | "max(1, 2, key=int, abc=int)", # two many keywords |
| 687 | "max(1, 2, key=1)", # keyfunc is not callable |
| 688 | ): |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 689 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 690 | exec(stmt, globals()) |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 691 | except TypeError: |
| 692 | pass |
| 693 | else: |
| 694 | self.fail(stmt) |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 695 | |
| 696 | self.assertEqual(max((1,), key=neg), 1) # one elem iterable |
| 697 | self.assertEqual(max((1,2), key=neg), 1) # two elem iterable |
| 698 | self.assertEqual(max(1, 2, key=neg), 1) # two elems |
| 699 | |
| 700 | data = [random.randrange(200) for i in range(100)] |
| 701 | keys = dict((elem, random.randrange(50)) for elem in data) |
| 702 | f = keys.__getitem__ |
| 703 | self.assertEqual(max(data, key=f), |
| 704 | sorted(reversed(data), key=f)[-1]) |
| 705 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 706 | def test_min(self): |
| 707 | self.assertEqual(min('123123'), '1') |
| 708 | self.assertEqual(min(1, 2, 3), 1) |
| 709 | self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1) |
| 710 | self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1) |
| 711 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 712 | self.assertEqual(min(1, 2, 3.0), 1) |
| 713 | self.assertEqual(min(1, 2.0, 3), 1) |
| 714 | self.assertEqual(min(1.0, 2, 3), 1.0) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 715 | |
| 716 | self.assertRaises(TypeError, min) |
| 717 | self.assertRaises(TypeError, min, 42) |
| 718 | self.assertRaises(ValueError, min, ()) |
| 719 | class BadSeq: |
| 720 | def __getitem__(self, index): |
| 721 | raise ValueError |
| 722 | self.assertRaises(ValueError, min, BadSeq()) |
| 723 | class BadNumber: |
| 724 | def __cmp__(self, other): |
| 725 | raise ValueError |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 726 | self.assertRaises(TypeError, min, (42, BadNumber())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 727 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 728 | for stmt in ( |
| 729 | "min(key=int)", # no args |
| 730 | "min(1, key=int)", # single arg not iterable |
| 731 | "min(1, 2, keystone=int)", # wrong keyword |
| 732 | "min(1, 2, key=int, abc=int)", # two many keywords |
| 733 | "min(1, 2, key=1)", # keyfunc is not callable |
| 734 | ): |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 735 | try: |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 736 | exec(stmt, globals()) |
Tim Peters | 7f06187 | 2004-12-07 21:17:46 +0000 | [diff] [blame] | 737 | except TypeError: |
| 738 | pass |
| 739 | else: |
| 740 | self.fail(stmt) |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 741 | |
| 742 | self.assertEqual(min((1,), key=neg), 1) # one elem iterable |
| 743 | self.assertEqual(min((1,2), key=neg), 2) # two elem iterable |
| 744 | self.assertEqual(min(1, 2, key=neg), 2) # two elems |
| 745 | |
| 746 | data = [random.randrange(200) for i in range(100)] |
| 747 | keys = dict((elem, random.randrange(50)) for elem in data) |
| 748 | f = keys.__getitem__ |
| 749 | self.assertEqual(min(data, key=f), |
| 750 | sorted(data, key=f)[0]) |
| 751 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 752 | def test_next(self): |
| 753 | it = iter(range(2)) |
| 754 | self.assertEqual(next(it), 0) |
| 755 | self.assertEqual(next(it), 1) |
| 756 | self.assertRaises(StopIteration, next, it) |
| 757 | self.assertRaises(StopIteration, next, it) |
| 758 | self.assertEquals(next(it, 42), 42) |
| 759 | |
| 760 | class Iter(object): |
| 761 | def __iter__(self): |
| 762 | return self |
| 763 | def __next__(self): |
| 764 | raise StopIteration |
| 765 | |
| 766 | it = iter(Iter()) |
| 767 | self.assertEquals(next(it, 42), 42) |
| 768 | self.assertRaises(StopIteration, next, it) |
| 769 | |
| 770 | def gen(): |
| 771 | yield 1 |
| 772 | return |
| 773 | |
| 774 | it = gen() |
| 775 | self.assertEquals(next(it), 1) |
| 776 | self.assertRaises(StopIteration, next, it) |
| 777 | self.assertEquals(next(it, 42), 42) |
| 778 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 779 | def test_oct(self): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 780 | self.assertEqual(oct(100), '0o144') |
| 781 | self.assertEqual(oct(100), '0o144') |
| 782 | self.assertEqual(oct(-100), '-0o144') |
| 783 | self.assertEqual(oct(-100), '-0o144') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 784 | self.assertRaises(TypeError, oct, ()) |
| 785 | |
| 786 | def write_testfile(self): |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 787 | # NB the first 4 lines are also used to test input, below |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 788 | fp = open(TESTFN, 'w') |
| 789 | try: |
| 790 | fp.write('1+1\n') |
| 791 | fp.write('1+1\n') |
| 792 | fp.write('The quick brown fox jumps over the lazy dog') |
| 793 | fp.write('.\n') |
| 794 | fp.write('Dear John\n') |
| 795 | fp.write('XXX'*100) |
| 796 | fp.write('YYY'*100) |
| 797 | finally: |
| 798 | fp.close() |
| 799 | |
| 800 | def test_open(self): |
| 801 | self.write_testfile() |
| 802 | fp = open(TESTFN, 'r') |
| 803 | try: |
| 804 | self.assertEqual(fp.readline(4), '1+1\n') |
| 805 | self.assertEqual(fp.readline(4), '1+1\n') |
| 806 | self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n') |
| 807 | self.assertEqual(fp.readline(4), 'Dear') |
| 808 | self.assertEqual(fp.readline(100), ' John\n') |
| 809 | self.assertEqual(fp.read(300), 'XXX'*100) |
| 810 | self.assertEqual(fp.read(1000), 'YYY'*100) |
| 811 | finally: |
| 812 | fp.close() |
| 813 | unlink(TESTFN) |
| 814 | |
| 815 | def test_ord(self): |
| 816 | self.assertEqual(ord(' '), 32) |
| 817 | self.assertEqual(ord('A'), 65) |
| 818 | self.assertEqual(ord('a'), 97) |
Guido van Rossum | f9e91c9 | 2007-05-08 21:05:48 +0000 | [diff] [blame] | 819 | self.assertEqual(ord('\x80'), 128) |
| 820 | self.assertEqual(ord('\xff'), 255) |
| 821 | |
| 822 | self.assertEqual(ord(b' '), 32) |
| 823 | self.assertEqual(ord(b'A'), 65) |
| 824 | self.assertEqual(ord(b'a'), 97) |
| 825 | self.assertEqual(ord(b'\x80'), 128) |
| 826 | self.assertEqual(ord(b'\xff'), 255) |
| 827 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 828 | self.assertEqual(ord(chr(sys.maxunicode)), sys.maxunicode) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 829 | self.assertRaises(TypeError, ord, 42) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 831 | self.assertEqual(ord(chr(0x10FFFF)), 0x10FFFF) |
| 832 | self.assertEqual(ord("\U0000FFFF"), 0x0000FFFF) |
| 833 | self.assertEqual(ord("\U00010000"), 0x00010000) |
| 834 | self.assertEqual(ord("\U00010001"), 0x00010001) |
| 835 | self.assertEqual(ord("\U000FFFFE"), 0x000FFFFE) |
| 836 | self.assertEqual(ord("\U000FFFFF"), 0x000FFFFF) |
| 837 | self.assertEqual(ord("\U00100000"), 0x00100000) |
| 838 | self.assertEqual(ord("\U00100001"), 0x00100001) |
| 839 | self.assertEqual(ord("\U0010FFFE"), 0x0010FFFE) |
| 840 | self.assertEqual(ord("\U0010FFFF"), 0x0010FFFF) |
| 841 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 842 | def test_pow(self): |
| 843 | self.assertEqual(pow(0,0), 1) |
| 844 | self.assertEqual(pow(0,1), 0) |
| 845 | self.assertEqual(pow(1,0), 1) |
| 846 | self.assertEqual(pow(1,1), 1) |
| 847 | |
| 848 | self.assertEqual(pow(2,0), 1) |
| 849 | self.assertEqual(pow(2,10), 1024) |
| 850 | self.assertEqual(pow(2,20), 1024*1024) |
| 851 | self.assertEqual(pow(2,30), 1024*1024*1024) |
| 852 | |
| 853 | self.assertEqual(pow(-2,0), 1) |
| 854 | self.assertEqual(pow(-2,1), -2) |
| 855 | self.assertEqual(pow(-2,2), 4) |
| 856 | self.assertEqual(pow(-2,3), -8) |
| 857 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 858 | self.assertEqual(pow(0,0), 1) |
| 859 | self.assertEqual(pow(0,1), 0) |
| 860 | self.assertEqual(pow(1,0), 1) |
| 861 | self.assertEqual(pow(1,1), 1) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 862 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 863 | self.assertEqual(pow(2,0), 1) |
| 864 | self.assertEqual(pow(2,10), 1024) |
| 865 | self.assertEqual(pow(2,20), 1024*1024) |
| 866 | self.assertEqual(pow(2,30), 1024*1024*1024) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 867 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 868 | self.assertEqual(pow(-2,0), 1) |
| 869 | self.assertEqual(pow(-2,1), -2) |
| 870 | self.assertEqual(pow(-2,2), 4) |
| 871 | self.assertEqual(pow(-2,3), -8) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 872 | |
| 873 | self.assertAlmostEqual(pow(0.,0), 1.) |
| 874 | self.assertAlmostEqual(pow(0.,1), 0.) |
| 875 | self.assertAlmostEqual(pow(1.,0), 1.) |
| 876 | self.assertAlmostEqual(pow(1.,1), 1.) |
| 877 | |
| 878 | self.assertAlmostEqual(pow(2.,0), 1.) |
| 879 | self.assertAlmostEqual(pow(2.,10), 1024.) |
| 880 | self.assertAlmostEqual(pow(2.,20), 1024.*1024.) |
| 881 | self.assertAlmostEqual(pow(2.,30), 1024.*1024.*1024.) |
| 882 | |
| 883 | self.assertAlmostEqual(pow(-2.,0), 1.) |
| 884 | self.assertAlmostEqual(pow(-2.,1), -2.) |
| 885 | self.assertAlmostEqual(pow(-2.,2), 4.) |
| 886 | self.assertAlmostEqual(pow(-2.,3), -8.) |
| 887 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 888 | for x in 2, 2, 2.0: |
| 889 | for y in 10, 10, 10.0: |
| 890 | for z in 1000, 1000, 1000.0: |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 891 | if isinstance(x, float) or \ |
| 892 | isinstance(y, float) or \ |
| 893 | isinstance(z, float): |
| 894 | self.assertRaises(TypeError, pow, x, y, z) |
| 895 | else: |
| 896 | self.assertAlmostEqual(pow(x, y, z), 24.0) |
| 897 | |
Jeffrey Yasskin | 3404b3c | 2007-09-07 15:15:49 +0000 | [diff] [blame] | 898 | self.assertAlmostEqual(pow(-1, 0.5), 1j) |
| 899 | self.assertAlmostEqual(pow(-1, 1/3), 0.5 + 0.8660254037844386j) |
| 900 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 901 | self.assertRaises(TypeError, pow, -1, -2, 3) |
| 902 | self.assertRaises(ValueError, pow, 1, 2, 0) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 903 | self.assertRaises(TypeError, pow, -1, -2, 3) |
| 904 | self.assertRaises(ValueError, pow, 1, 2, 0) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 905 | |
| 906 | self.assertRaises(TypeError, pow) |
| 907 | |
| 908 | def test_range(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 909 | self.assertEqual(list(range(3)), [0, 1, 2]) |
| 910 | self.assertEqual(list(range(1, 5)), [1, 2, 3, 4]) |
| 911 | self.assertEqual(list(range(0)), []) |
| 912 | self.assertEqual(list(range(-3)), []) |
| 913 | self.assertEqual(list(range(1, 10, 3)), [1, 4, 7]) |
| 914 | #self.assertEqual(list(range(5, -5, -3)), [5, 2, -1, -4]) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 915 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 916 | """ XXX(nnorwitz): |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 917 | # Now test range() with longs |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 918 | self.assertEqual(list(range(-2**100)), []) |
| 919 | self.assertEqual(list(range(0, -2**100)), []) |
| 920 | self.assertEqual(list(range(0, 2**100, -1)), []) |
| 921 | self.assertEqual(list(range(0, 2**100, -1)), []) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 922 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 923 | a = int(10 * sys.maxsize) |
| 924 | b = int(100 * sys.maxsize) |
| 925 | c = int(50 * sys.maxsize) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 927 | self.assertEqual(list(range(a, a+2)), [a, a+1]) |
| 928 | self.assertEqual(list(range(a+2, a, -1)), [a+2, a+1]) |
| 929 | self.assertEqual(list(range(a+4, a, -2)), [a+4, a+2]) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 930 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 931 | seq = list(range(a, b, c)) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 932 | self.assert_(a in seq) |
| 933 | self.assert_(b not in seq) |
| 934 | self.assertEqual(len(seq), 2) |
| 935 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 936 | seq = list(range(b, a, -c)) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 937 | self.assert_(b in seq) |
| 938 | self.assert_(a not in seq) |
| 939 | self.assertEqual(len(seq), 2) |
| 940 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 941 | seq = list(range(-a, -b, -c)) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 942 | self.assert_(-a in seq) |
| 943 | self.assert_(-b not in seq) |
| 944 | self.assertEqual(len(seq), 2) |
| 945 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 946 | self.assertRaises(TypeError, range) |
| 947 | self.assertRaises(TypeError, range, 1, 2, 3, 4) |
| 948 | self.assertRaises(ValueError, range, 1, 2, 0) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 949 | self.assertRaises(ValueError, range, a, a + 1, int(0)) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 950 | |
| 951 | class badzero(int): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 952 | def __eq__(self, other): |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 953 | raise RuntimeError |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 954 | __ne__ = __lt__ = __gt__ = __le__ = __ge__ = __eq__ |
| 955 | |
| 956 | # XXX This won't (but should!) raise RuntimeError if a is an int... |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 957 | self.assertRaises(RuntimeError, range, a, a + 1, badzero(1)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 958 | """ |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 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 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 967 | #NEAL self.assertRaises(OverflowError, range, -sys.maxsize, sys.maxsize) |
| 968 | #NEAL self.assertRaises(OverflowError, range, 0, 2*sys.maxsize) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 969 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 970 | self.assertRaises(OverflowError, len, range(0, sys.maxsize**10)) |
Walter Dörwald | 357981e | 2003-04-15 18:59:28 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 972 | def test_input(self): |
| 973 | self.write_testfile() |
| 974 | fp = open(TESTFN, 'r') |
| 975 | savestdin = sys.stdin |
| 976 | savestdout = sys.stdout # Eats the echo |
| 977 | try: |
| 978 | sys.stdin = fp |
| 979 | sys.stdout = BitBucket() |
| 980 | self.assertEqual(input(), "1+1") |
| 981 | self.assertEqual(input('testing\n'), "1+1") |
| 982 | self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.') |
| 983 | self.assertEqual(input('testing\n'), 'Dear John') |
| 984 | |
| 985 | # SF 1535165: don't segfault on closed stdin |
| 986 | # sys.stdout must be a regular file for triggering |
| 987 | sys.stdout = savestdout |
| 988 | sys.stdin.close() |
| 989 | self.assertRaises(ValueError, input) |
| 990 | |
| 991 | sys.stdout = BitBucket() |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 992 | sys.stdin = io.StringIO("NULL\0") |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 993 | self.assertRaises(TypeError, input, 42, 42) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 994 | sys.stdin = io.StringIO(" 'whitespace'") |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 995 | self.assertEqual(input(), " 'whitespace'") |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 996 | sys.stdin = io.StringIO() |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 997 | self.assertRaises(EOFError, input) |
| 998 | |
| 999 | del sys.stdout |
| 1000 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1001 | del sys.stdin |
| 1002 | self.assertRaises(RuntimeError, input, 'prompt') |
| 1003 | finally: |
| 1004 | sys.stdin = savestdin |
| 1005 | sys.stdout = savestdout |
| 1006 | fp.close() |
| 1007 | unlink(TESTFN) |
| 1008 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1009 | def test_repr(self): |
| 1010 | self.assertEqual(repr(''), '\'\'') |
| 1011 | self.assertEqual(repr(0), '0') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1012 | self.assertEqual(repr(0), '0') |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1013 | self.assertEqual(repr(()), '()') |
| 1014 | self.assertEqual(repr([]), '[]') |
| 1015 | self.assertEqual(repr({}), '{}') |
| 1016 | a = [] |
| 1017 | a.append(a) |
| 1018 | self.assertEqual(repr(a), '[[...]]') |
| 1019 | a = {} |
| 1020 | a[0] = a |
| 1021 | self.assertEqual(repr(a), '{0: {...}}') |
| 1022 | |
| 1023 | def test_round(self): |
| 1024 | self.assertEqual(round(0.0), 0.0) |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1025 | self.assertEqual(type(round(0.0)), int) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1026 | self.assertEqual(round(1.0), 1.0) |
| 1027 | self.assertEqual(round(10.0), 10.0) |
| 1028 | self.assertEqual(round(1000000000.0), 1000000000.0) |
| 1029 | self.assertEqual(round(1e20), 1e20) |
| 1030 | |
| 1031 | self.assertEqual(round(-1.0), -1.0) |
| 1032 | self.assertEqual(round(-10.0), -10.0) |
| 1033 | self.assertEqual(round(-1000000000.0), -1000000000.0) |
| 1034 | self.assertEqual(round(-1e20), -1e20) |
| 1035 | |
| 1036 | self.assertEqual(round(0.1), 0.0) |
| 1037 | self.assertEqual(round(1.1), 1.0) |
| 1038 | self.assertEqual(round(10.1), 10.0) |
| 1039 | self.assertEqual(round(1000000000.1), 1000000000.0) |
| 1040 | |
| 1041 | self.assertEqual(round(-1.1), -1.0) |
| 1042 | self.assertEqual(round(-10.1), -10.0) |
| 1043 | self.assertEqual(round(-1000000000.1), -1000000000.0) |
| 1044 | |
| 1045 | self.assertEqual(round(0.9), 1.0) |
| 1046 | self.assertEqual(round(9.9), 10.0) |
| 1047 | self.assertEqual(round(999999999.9), 1000000000.0) |
| 1048 | |
| 1049 | self.assertEqual(round(-0.9), -1.0) |
| 1050 | self.assertEqual(round(-9.9), -10.0) |
| 1051 | self.assertEqual(round(-999999999.9), -1000000000.0) |
| 1052 | |
| 1053 | self.assertEqual(round(-8.0, -1), -10.0) |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1054 | self.assertEqual(type(round(-8.0, -1)), float) |
| 1055 | |
| 1056 | self.assertEqual(type(round(-8.0, 0)), float) |
| 1057 | self.assertEqual(type(round(-8.0, 1)), float) |
| 1058 | |
| 1059 | # Check even / odd rounding behaviour |
| 1060 | self.assertEqual(round(5.5), 6) |
| 1061 | self.assertEqual(round(6.5), 6) |
| 1062 | self.assertEqual(round(-5.5), -6) |
| 1063 | self.assertEqual(round(-6.5), -6) |
| 1064 | |
| 1065 | # Check behavior on ints |
| 1066 | self.assertEqual(round(0), 0) |
| 1067 | self.assertEqual(round(8), 8) |
| 1068 | self.assertEqual(round(-8), -8) |
| 1069 | self.assertEqual(type(round(0)), int) |
| 1070 | self.assertEqual(type(round(-8, -1)), float) |
| 1071 | self.assertEqual(type(round(-8, 0)), float) |
| 1072 | self.assertEqual(type(round(-8, 1)), float) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1073 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1074 | # test new kwargs |
| 1075 | self.assertEqual(round(number=-8.0, ndigits=-1), -10.0) |
| 1076 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1077 | self.assertRaises(TypeError, round) |
| 1078 | |
Alex Martelli | ae211f9 | 2007-08-22 23:21:33 +0000 | [diff] [blame] | 1079 | # test generic rounding delegation for reals |
| 1080 | class TestRound: |
| 1081 | def __round__(self): |
| 1082 | return 23 |
| 1083 | |
| 1084 | class TestNoRound: |
| 1085 | pass |
| 1086 | |
| 1087 | self.assertEqual(round(TestRound()), 23) |
| 1088 | |
| 1089 | self.assertRaises(TypeError, round, 1, 2, 3) |
| 1090 | self.assertRaises(TypeError, round, TestNoRound()) |
| 1091 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1092 | t = TestNoRound() |
| 1093 | t.__round__ = lambda *args: args |
| 1094 | self.assertRaises(TypeError, round, t) |
| 1095 | self.assertRaises(TypeError, round, t, 0) |
| 1096 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1097 | def test_setattr(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 1098 | setattr(sys, 'spam', 1) |
| 1099 | self.assertEqual(sys.spam, 1) |
| 1100 | self.assertRaises(TypeError, setattr, sys, 1, 'spam') |
| 1101 | self.assertRaises(TypeError, setattr) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1102 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1103 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1104 | def test_sum(self): |
| 1105 | self.assertEqual(sum([]), 0) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1106 | self.assertEqual(sum(list(range(2,8))), 27) |
| 1107 | self.assertEqual(sum(iter(list(range(2,8)))), 27) |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1108 | self.assertEqual(sum(Squares(10)), 285) |
| 1109 | self.assertEqual(sum(iter(Squares(10))), 285) |
| 1110 | self.assertEqual(sum([[1], [2], [3]], []), [1, 2, 3]) |
| 1111 | |
| 1112 | self.assertRaises(TypeError, sum) |
| 1113 | self.assertRaises(TypeError, sum, 42) |
| 1114 | self.assertRaises(TypeError, sum, ['a', 'b', 'c']) |
| 1115 | self.assertRaises(TypeError, sum, ['a', 'b', 'c'], '') |
| 1116 | self.assertRaises(TypeError, sum, [[1], [2], [3]]) |
| 1117 | self.assertRaises(TypeError, sum, [{2:3}]) |
| 1118 | self.assertRaises(TypeError, sum, [{2:3}]*2, {2:3}) |
| 1119 | |
| 1120 | class BadSeq: |
| 1121 | def __getitem__(self, index): |
| 1122 | raise ValueError |
| 1123 | self.assertRaises(ValueError, sum, BadSeq()) |
| 1124 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1125 | def test_type(self): |
| 1126 | self.assertEqual(type(''), type('123')) |
| 1127 | self.assertNotEqual(type(''), type(())) |
| 1128 | |
Guido van Rossum | fee7b93 | 2005-01-16 00:21:28 +0000 | [diff] [blame] | 1129 | # We don't want self in vars(), so these are static methods |
| 1130 | |
| 1131 | @staticmethod |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1132 | def get_vars_f0(): |
| 1133 | return vars() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1134 | |
Guido van Rossum | fee7b93 | 2005-01-16 00:21:28 +0000 | [diff] [blame] | 1135 | @staticmethod |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1136 | def get_vars_f2(): |
| 1137 | BuiltinTest.get_vars_f0() |
| 1138 | a = 1 |
| 1139 | b = 2 |
| 1140 | return vars() |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1141 | |
| 1142 | def test_vars(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1143 | self.assertEqual(set(vars()), set(dir())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1144 | import sys |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1145 | self.assertEqual(set(vars(sys)), set(dir(sys))) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1146 | self.assertEqual(self.get_vars_f0(), {}) |
| 1147 | self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2}) |
| 1148 | self.assertRaises(TypeError, vars, 42, 42) |
| 1149 | self.assertRaises(TypeError, vars, 42) |
| 1150 | |
| 1151 | def test_zip(self): |
| 1152 | a = (1, 2, 3) |
| 1153 | b = (4, 5, 6) |
| 1154 | t = [(1, 4), (2, 5), (3, 6)] |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1155 | self.assertEqual(list(zip(a, b)), t) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1156 | b = [4, 5, 6] |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1157 | self.assertEqual(list(zip(a, b)), t) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1158 | b = (4, 5, 6, 7) |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1159 | self.assertEqual(list(zip(a, b)), t) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1160 | class I: |
| 1161 | def __getitem__(self, i): |
| 1162 | if i < 0 or i > 2: raise IndexError |
| 1163 | return i + 4 |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1164 | self.assertEqual(list(zip(a, I())), t) |
| 1165 | self.assertEqual(list(zip()), []) |
| 1166 | self.assertEqual(list(zip(*[])), []) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1167 | self.assertRaises(TypeError, zip, None) |
| 1168 | class G: |
| 1169 | pass |
| 1170 | self.assertRaises(TypeError, zip, a, G()) |
| 1171 | |
| 1172 | # Make sure zip doesn't try to allocate a billion elements for the |
| 1173 | # result list when one of its arguments doesn't say how long it is. |
| 1174 | # A MemoryError is the most likely failure mode. |
| 1175 | class SequenceWithoutALength: |
| 1176 | def __getitem__(self, i): |
| 1177 | if i == 5: |
| 1178 | raise IndexError |
| 1179 | else: |
| 1180 | return i |
| 1181 | self.assertEqual( |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1182 | list(zip(SequenceWithoutALength(), range(2**30))), |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1183 | list(enumerate(range(5))) |
| 1184 | ) |
| 1185 | |
| 1186 | class BadSeq: |
| 1187 | def __getitem__(self, i): |
| 1188 | if i == 5: |
| 1189 | raise ValueError |
| 1190 | else: |
| 1191 | return i |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1192 | self.assertRaises(ValueError, list, zip(BadSeq(), BadSeq())) |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1193 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 1194 | def test_bin(self): |
| 1195 | self.assertEqual(bin(0), '0b0') |
| 1196 | self.assertEqual(bin(1), '0b1') |
| 1197 | self.assertEqual(bin(-1), '-0b1') |
| 1198 | self.assertEqual(bin(2**65), '0b1' + '0' * 65) |
| 1199 | self.assertEqual(bin(2**65-1), '0b' + '1' * 65) |
| 1200 | self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65) |
| 1201 | self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65) |
| 1202 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1203 | class TestSorted(unittest.TestCase): |
| 1204 | |
| 1205 | def test_basic(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1206 | data = list(range(100)) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1207 | copy = data[:] |
| 1208 | random.shuffle(copy) |
| 1209 | self.assertEqual(data, sorted(copy)) |
| 1210 | self.assertNotEqual(data, copy) |
| 1211 | |
| 1212 | data.reverse() |
| 1213 | random.shuffle(copy) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1214 | self.assertEqual(data, sorted(copy, key=lambda x: -x)) |
| 1215 | self.assertNotEqual(data, copy) |
| 1216 | random.shuffle(copy) |
| 1217 | self.assertEqual(data, sorted(copy, reverse=1)) |
| 1218 | self.assertNotEqual(data, copy) |
| 1219 | |
| 1220 | def test_inputtypes(self): |
| 1221 | s = 'abracadabra' |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1222 | types = [list, tuple, str] |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 1223 | for T in types: |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1224 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1225 | |
Walter Dörwald | 1f5947b | 2007-05-22 16:52:54 +0000 | [diff] [blame] | 1226 | s = ''.join(set(s)) # unique letters only |
| 1227 | types = [str, set, frozenset, list, tuple, dict.fromkeys] |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame] | 1228 | for T in types: |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1229 | self.assertEqual(sorted(s), sorted(T(s))) |
| 1230 | |
| 1231 | def test_baddecorator(self): |
| 1232 | data = 'The quick Brown fox Jumped over The lazy Dog'.split() |
| 1233 | self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) |
| 1234 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1235 | def test_main(verbose=None): |
| 1236 | test_classes = (BuiltinTest, TestSorted) |
| 1237 | |
| 1238 | run_unittest(*test_classes) |
| 1239 | |
| 1240 | # verify reference counting |
| 1241 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 1242 | import gc |
| 1243 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1244 | for i in range(len(counts)): |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1245 | run_unittest(*test_classes) |
| 1246 | gc.collect() |
| 1247 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1248 | print(counts) |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1249 | |
Walter Dörwald | 919497e | 2003-01-19 16:23:59 +0000 | [diff] [blame] | 1250 | |
| 1251 | if __name__ == "__main__": |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1252 | test_main(verbose=True) |