Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 4a, built-in functions a-m |
| 2 | |
| 3 | from test_support import * |
| 4 | |
Guido van Rossum | eecf035 | 1994-12-30 17:17:46 +0000 | [diff] [blame] | 5 | print '__import__' |
| 6 | __import__('sys') |
Barry Warsaw | 3a9d061 | 2000-09-01 06:53:52 +0000 | [diff] [blame] | 7 | __import__('time') |
Guido van Rossum | eecf035 | 1994-12-30 17:17:46 +0000 | [diff] [blame] | 8 | __import__('string') |
| 9 | try: __import__('spamspam') |
| 10 | except ImportError: pass |
| 11 | else: raise TestFailed, "__import__('spamspam') should fail" |
| 12 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 13 | print 'abs' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 14 | if abs(0) != 0: raise TestFailed, 'abs(0)' |
| 15 | if abs(1234) != 1234: raise TestFailed, 'abs(1234)' |
| 16 | if abs(-1234) != 1234: raise TestFailed, 'abs(-1234)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 17 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 18 | if abs(0.0) != 0.0: raise TestFailed, 'abs(0.0)' |
| 19 | if abs(3.14) != 3.14: raise TestFailed, 'abs(3.14)' |
| 20 | if abs(-3.14) != 3.14: raise TestFailed, 'abs(-3.14)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 21 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 22 | if abs(0L) != 0L: raise TestFailed, 'abs(0L)' |
| 23 | if abs(1234L) != 1234L: raise TestFailed, 'abs(1234L)' |
| 24 | if abs(-1234L) != 1234L: raise TestFailed, 'abs(-1234L)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 25 | |
| 26 | print 'apply' |
| 27 | def f0(*args): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 28 | if args != (): raise TestFailed, 'f0 called with ' + `args` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 29 | def f1(a1): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 30 | if a1 != 1: raise TestFailed, 'f1 called with ' + `a1` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 31 | def f2(a1, a2): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 32 | if a1 != 1 or a2 != 2: |
| 33 | raise TestFailed, 'f2 called with ' + `a1, a2` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 34 | def f3(a1, a2, a3): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 35 | if a1 != 1 or a2 != 2 or a3 != 3: |
| 36 | raise TestFailed, 'f3 called with ' + `a1, a2, a3` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 37 | apply(f0, ()) |
| 38 | apply(f1, (1,)) |
| 39 | apply(f2, (1, 2)) |
| 40 | apply(f3, (1, 2, 3)) |
| 41 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 42 | print 'callable' |
| 43 | if not callable(len):raise TestFailed, 'callable(len)' |
| 44 | def f(): pass |
| 45 | if not callable(f): raise TestFailed, 'callable(f)' |
| 46 | class C: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 47 | def meth(self): pass |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 48 | if not callable(C): raise TestFailed, 'callable(C)' |
| 49 | x = C() |
| 50 | if not callable(x.meth): raise TestFailed, 'callable(x.meth)' |
| 51 | if callable(x): raise TestFailed, 'callable(x)' |
| 52 | class D(C): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 53 | def __call__(self): pass |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 54 | y = D() |
| 55 | if not callable(y): raise TestFailed, 'callable(y)' |
Jeremy Hylton | 5ab3661 | 2001-01-03 23:53:31 +0000 | [diff] [blame^] | 56 | y() |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 58 | print 'chr' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 59 | if chr(32) != ' ': raise TestFailed, 'chr(32)' |
| 60 | if chr(65) != 'A': raise TestFailed, 'chr(65)' |
| 61 | if chr(97) != 'a': raise TestFailed, 'chr(97)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 63 | print 'cmp' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 64 | if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)' |
| 65 | if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)' |
| 66 | if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)' |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 67 | # verify that circular objects are handled |
| 68 | a = []; a.append(a) |
| 69 | b = []; b.append(b) |
| 70 | from UserList import UserList |
| 71 | c = UserList(); c.append(c) |
| 72 | if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b) |
| 73 | if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c) |
| 74 | if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a) |
| 75 | if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c) |
Vladimir Marangozov | d57f5cf | 2000-07-14 04:32:09 +0000 | [diff] [blame] | 76 | # okay, now break the cycles |
| 77 | a.pop(); b.pop(); c.pop() |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 78 | |
| 79 | print 'coerce' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 80 | if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 81 | if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 82 | if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 84 | print 'compile' |
| 85 | compile('print 1\n', '', 'exec') |
| 86 | |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 87 | print 'complex' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 88 | if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)' |
| 89 | if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)' |
| 90 | if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)' |
| 91 | if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)' |
| 92 | if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)' |
| 93 | if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)' |
| 94 | if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)' |
| 95 | if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)' |
| 96 | if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)' |
| 97 | if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 98 | if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 99 | if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)' |
| 100 | if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)' |
| 101 | if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)' |
| 102 | if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)' |
| 103 | if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)' |
| 104 | if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)' |
| 105 | if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)' |
| 106 | if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)' |
| 107 | if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)' |
| 108 | if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)' |
| 109 | if complex(" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"' |
| 110 | if complex(u" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(u" 3.14+J )"' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 111 | class Z: |
| 112 | def __complex__(self): return 3.14j |
| 113 | z = Z() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 114 | if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 116 | print 'delattr' |
| 117 | import sys |
| 118 | sys.spam = 1 |
| 119 | delattr(sys, 'spam') |
| 120 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 121 | print 'dir' |
| 122 | x = 1 |
| 123 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 124 | import sys |
| 125 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 126 | |
| 127 | print 'divmod' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 128 | if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 129 | if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 130 | if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 131 | if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 132 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 133 | if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)' |
| 134 | if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)' |
| 135 | if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)' |
| 136 | if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 137 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 138 | if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)' |
| 139 | if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)' |
| 140 | if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)' |
| 141 | if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 142 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 143 | if fcmp(divmod(3.25, 1.0), (3.0, 0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 144 | raise TestFailed, 'divmod(3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 145 | if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 146 | raise TestFailed, 'divmod(-3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 147 | if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 148 | raise TestFailed, 'divmod(3.25, -1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 149 | if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 150 | raise TestFailed, 'divmod(-3.25, -1.0)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 151 | |
| 152 | print 'eval' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 153 | if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')' |
| 154 | if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')' |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 155 | globals = {'a': 1, 'b': 2} |
| 156 | locals = {'b': 200, 'c': 300} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 157 | if eval('a', globals) != 1: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 158 | raise TestFailed, "eval(1) == %s" % eval('a', globals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 159 | if eval('a', globals, locals) != 1: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 160 | raise TestFailed, "eval(2)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 161 | if eval('b', globals, locals) != 200: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 162 | raise TestFailed, "eval(3)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 163 | if eval('c', globals, locals) != 300: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 164 | raise TestFailed, "eval(4)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 165 | if eval(u'1+1') != 2: raise TestFailed, 'eval(u\'1+1\')' |
| 166 | if eval(u' 1+1\n') != 2: raise TestFailed, 'eval(u\' 1+1\\n\')' |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 167 | globals = {'a': 1, 'b': 2} |
| 168 | locals = {'b': 200, 'c': 300} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 169 | if eval(u'a', globals) != 1: |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 170 | raise TestFailed, "eval(1) == %s" % eval(u'a', globals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 171 | if eval(u'a', globals, locals) != 1: |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 172 | raise TestFailed, "eval(2)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 173 | if eval(u'b', globals, locals) != 200: |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 174 | raise TestFailed, "eval(3)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 175 | if eval(u'c', globals, locals) != 300: |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 176 | raise TestFailed, "eval(4)" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 177 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 178 | print 'execfile' |
| 179 | z = 0 |
| 180 | f = open(TESTFN, 'w') |
| 181 | f.write('z = z+1\n') |
| 182 | f.write('z = z*2\n') |
| 183 | f.close() |
| 184 | execfile(TESTFN) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 185 | if z != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 186 | globals['z'] = 0 |
| 187 | execfile(TESTFN, globals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 188 | if globals['z'] != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 189 | locals['z'] = 0 |
| 190 | execfile(TESTFN, globals, locals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 191 | if locals['z'] != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 192 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 194 | print 'filter' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 195 | if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld': |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 196 | raise TestFailed, 'filter (filter a string)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 197 | if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 198 | raise TestFailed, 'filter (remove false values)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 199 | if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 200 | raise TestFailed, 'filter (keep positives)' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 201 | class Squares: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 202 | def __init__(self, max): |
| 203 | self.max = max |
| 204 | self.sofar = [] |
| 205 | def __len__(self): return len(self.sofar) |
| 206 | def __getitem__(self, i): |
| 207 | if not 0 <= i < self.max: raise IndexError |
| 208 | n = len(self.sofar) |
| 209 | while n <= i: |
| 210 | self.sofar.append(n*n) |
| 211 | n = n+1 |
| 212 | return self.sofar[i] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 213 | if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 214 | raise TestFailed, 'filter(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 215 | if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 216 | raise TestFailed, 'filter(oddp, Squares(10))' |
Barry Warsaw | ab11f60 | 1999-01-28 19:44:06 +0000 | [diff] [blame] | 217 | class StrSquares: |
| 218 | def __init__(self, max): |
| 219 | self.max = max |
| 220 | self.sofar = [] |
| 221 | def __len__(self): |
| 222 | return len(self.sofar) |
| 223 | def __getitem__(self, i): |
| 224 | if not 0 <= i < self.max: |
| 225 | raise IndexError |
| 226 | n = len(self.sofar) |
| 227 | while n <= i: |
| 228 | self.sofar.append(str(n*n)) |
| 229 | n = n+1 |
| 230 | return self.sofar[i] |
| 231 | def identity(item): |
| 232 | return 1 |
| 233 | filter(identity, Squares(5)) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 235 | print 'float' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 236 | if float(3.14) != 3.14: raise TestFailed, 'float(3.14)' |
| 237 | if float(314) != 314.0: raise TestFailed, 'float(314)' |
| 238 | if float(314L) != 314.0: raise TestFailed, 'float(314L)' |
| 239 | if float(" 3.14 ") != 3.14: raise TestFailed, 'float(" 3.14 ")' |
| 240 | if float(u" 3.14 ") != 3.14: raise TestFailed, 'float(u" 3.14 ")' |
| 241 | if float(u" \u0663.\u0661\u0664 ") != 3.14: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 242 | raise TestFailed, 'float(u" \u0663.\u0661\u0664 ")' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 243 | |
| 244 | print 'getattr' |
| 245 | import sys |
| 246 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
| 247 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 248 | print 'hasattr' |
| 249 | import sys |
| 250 | if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr' |
| 251 | |
| 252 | print 'hash' |
| 253 | hash(None) |
| 254 | if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()' |
| 255 | hash('spam') |
| 256 | hash((0,1,2,3)) |
| 257 | def f(): pass |
| 258 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 259 | print 'hex' |
| 260 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 261 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
Guido van Rossum | c1c96d1 | 1997-05-14 21:37:23 +0000 | [diff] [blame] | 262 | if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))' |
| 263 | if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'): |
| 264 | raise TestFailed, 'hex(-16)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 265 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 266 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 267 | print 'id' |
| 268 | id(None) |
| 269 | id(1) |
| 270 | id(1L) |
| 271 | id(1.0) |
| 272 | id('spam') |
| 273 | id((0,1,2,3)) |
| 274 | id([0,1,2,3]) |
| 275 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 276 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 277 | # Test input() later, together with raw_input |
| 278 | |
| 279 | print 'int' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 280 | if int(314) != 314: raise TestFailed, 'int(314)' |
| 281 | if int(3.14) != 3: raise TestFailed, 'int(3.14)' |
| 282 | if int(314L) != 314: raise TestFailed, 'int(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 283 | # Check that conversion from float truncates towards zero |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 284 | if int(-3.14) != -3: raise TestFailed, 'int(-3.14)' |
| 285 | if int(3.9) != 3: raise TestFailed, 'int(3.9)' |
| 286 | if int(-3.9) != -3: raise TestFailed, 'int(-3.9)' |
| 287 | if int(3.5) != 3: raise TestFailed, 'int(3.5)' |
| 288 | if int(-3.5) != -3: raise TestFailed, 'int(-3.5)' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 289 | # Different base: |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 290 | if int("10",16) != 16L: raise TestFailed, 'int("10",16)' |
| 291 | if int(u"10",16) != 16L: raise TestFailed, 'int(u"10",16)' |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 292 | # Test conversion from strings and various anomalies |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 293 | L = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 294 | ('0', 0), |
| 295 | ('1', 1), |
| 296 | ('9', 9), |
| 297 | ('10', 10), |
| 298 | ('99', 99), |
| 299 | ('100', 100), |
| 300 | ('314', 314), |
| 301 | (' 314', 314), |
| 302 | ('314 ', 314), |
| 303 | (' \t\t 314 \t\t ', 314), |
| 304 | (`sys.maxint`, sys.maxint), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 305 | (' 1x', ValueError), |
| 306 | (' 1 ', 1), |
| 307 | (' 1\02 ', ValueError), |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 308 | ('', ValueError), |
| 309 | (' ', ValueError), |
| 310 | (' \t\t ', ValueError), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 311 | (u'0', 0), |
| 312 | (u'1', 1), |
| 313 | (u'9', 9), |
| 314 | (u'10', 10), |
| 315 | (u'99', 99), |
| 316 | (u'100', 100), |
| 317 | (u'314', 314), |
| 318 | (u' 314', 314), |
| 319 | (u'\u0663\u0661\u0664 ', 314), |
| 320 | (u' \t\t 314 \t\t ', 314), |
| 321 | (u' 1x', ValueError), |
| 322 | (u' 1 ', 1), |
| 323 | (u' 1\02 ', ValueError), |
| 324 | (u'', ValueError), |
| 325 | (u' ', ValueError), |
| 326 | (u' \t\t ', ValueError), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 327 | ] |
| 328 | for s, v in L: |
| 329 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 330 | for prefix in "", " ", "\t", " \t\t ": |
| 331 | ss = prefix + sign + s |
| 332 | vv = v |
| 333 | if sign == "-" and v is not ValueError: |
| 334 | vv = -v |
| 335 | try: |
| 336 | if int(ss) != vv: |
| 337 | raise TestFailed, "int(%s)" % `ss` |
| 338 | except v: |
| 339 | pass |
| 340 | except ValueError, e: |
| 341 | raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 342 | s = `-1-sys.maxint` |
| 343 | if int(s)+1 != -sys.maxint: |
| 344 | raise TestFailed, "int(%s)" % `s` |
| 345 | try: |
| 346 | int(s[1:]) |
| 347 | except ValueError: |
| 348 | pass |
| 349 | else: |
| 350 | raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 351 | |
Barry Warsaw | d543077 | 1997-08-22 21:27:03 +0000 | [diff] [blame] | 352 | print 'isinstance' |
| 353 | class C: |
| 354 | pass |
| 355 | class D(C): |
| 356 | pass |
| 357 | class E: |
| 358 | pass |
| 359 | c = C() |
| 360 | d = D() |
| 361 | e = E() |
| 362 | if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)' |
| 363 | if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)' |
| 364 | if isinstance(e, C): raise TestFailed, 'isinstance(e, C)' |
| 365 | if isinstance(c, D): raise TestFailed, 'isinstance(c, D)' |
| 366 | if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)' |
| 367 | try: |
| 368 | isinstance(E, 'foo') |
| 369 | raise TestFailed, 'isinstance(E, "foo")' |
| 370 | except TypeError: |
| 371 | pass |
| 372 | |
| 373 | print 'issubclass' |
| 374 | if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)' |
| 375 | if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)' |
| 376 | if issubclass(C, D): raise TestFailed, 'issubclass(C, D)' |
| 377 | try: |
| 378 | issubclass('foo', E) |
| 379 | raise TestFailed, 'issubclass("foo", E)' |
| 380 | except TypeError: |
| 381 | pass |
| 382 | try: |
| 383 | issubclass(E, 'foo') |
| 384 | raise TestFailed, 'issubclass(E, "foo")' |
| 385 | except TypeError: |
| 386 | pass |
| 387 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 388 | print 'len' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 389 | if len('123') != 3: raise TestFailed, 'len(\'123\')' |
| 390 | if len(()) != 0: raise TestFailed, 'len(())' |
| 391 | if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 392 | if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 393 | if len({}) != 0: raise TestFailed, 'len({})' |
| 394 | if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 395 | |
| 396 | print 'long' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 397 | if long(314) != 314L: raise TestFailed, 'long(314)' |
| 398 | if long(3.14) != 3L: raise TestFailed, 'long(3.14)' |
| 399 | if long(314L) != 314L: raise TestFailed, 'long(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 400 | # Check that conversion from float truncates towards zero |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 401 | if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)' |
| 402 | if long(3.9) != 3L: raise TestFailed, 'long(3.9)' |
| 403 | if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)' |
| 404 | if long(3.5) != 3L: raise TestFailed, 'long(3.5)' |
| 405 | if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)' |
| 406 | if long("-3") != -3L: raise TestFailed, 'long("-3")' |
| 407 | if long(u"-3") != -3L: raise TestFailed, 'long(u"-3")' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 408 | # Different base: |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 409 | if long("10",16) != 16L: raise TestFailed, 'long("10",16)' |
| 410 | if long(u"10",16) != 16L: raise TestFailed, 'long(u"10",16)' |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 411 | # Check conversions from string (same test set as for int(), and then some) |
| 412 | LL = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 413 | ('1' + '0'*20, 10L**20), |
| 414 | ('1' + '0'*100, 10L**100), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 415 | (u'1' + u'0'*20, 10L**20), |
| 416 | (u'1' + u'0'*100, 10L**100), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 417 | ] |
| 418 | for s, v in L + LL: |
| 419 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 420 | for prefix in "", " ", "\t", " \t\t ": |
| 421 | ss = prefix + sign + s |
| 422 | vv = v |
| 423 | if sign == "-" and v is not ValueError: |
| 424 | vv = -v |
| 425 | try: |
| 426 | if long(ss) != long(vv): |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 427 | raise TestFailed, "long(%s)" % `ss` |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 428 | except v: |
| 429 | pass |
| 430 | except ValueError, e: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 431 | raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 433 | print 'map' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 434 | if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 435 | raise TestFailed, 'map(None, \'hello world\')' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 436 | if map(None, 'abcd', 'efg') != \ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 437 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]: |
| 438 | raise TestFailed, 'map(None, \'abcd\', \'efg\')' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 439 | if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 440 | raise TestFailed, 'map(None, range(10))' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 441 | if map(lambda x: x*x, range(1,4)) != [1, 4, 9]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 442 | raise TestFailed, 'map(lambda x: x*x, range(1,4))' |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 443 | try: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 444 | from math import sqrt |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 445 | except ImportError: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 446 | def sqrt(x): |
| 447 | return pow(x, 0.5) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 448 | if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 449 | raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 450 | if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 451 | raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 452 | def plus(*v): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 453 | accu = 0 |
| 454 | for i in v: accu = accu + i |
| 455 | return accu |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 456 | if map(plus, [1, 3, 7]) != [1, 3, 7]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 457 | raise TestFailed, 'map(plus, [1, 3, 7])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 458 | if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 459 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 460 | if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 461 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 462 | if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 463 | raise TestFailed, 'map(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 464 | if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 465 | raise TestFailed, 'map(int, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 466 | if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 467 | raise TestFailed, 'map(None, Squares(3), Squares(2))' |
Guido van Rossum | 6925661 | 1998-06-11 22:25:59 +0000 | [diff] [blame] | 468 | if map(max, Squares(3), Squares(2)) != [0, 1, None]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 469 | raise TestFailed, 'map(max, Squares(3), Squares(2))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 471 | print 'max' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 472 | if max('123123') != '3': raise TestFailed, 'max(\'123123\')' |
| 473 | if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)' |
| 474 | if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 475 | if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 476 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 477 | if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 478 | if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 479 | if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 480 | |
| 481 | print 'min' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 482 | if min('123123') != '1': raise TestFailed, 'min(\'123123\')' |
| 483 | if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)' |
| 484 | if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 485 | if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 486 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 487 | if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 488 | if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 489 | if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |