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 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 26 | try: abs('a') |
| 27 | except TypeError: pass |
| 28 | else: raise TestFailed, 'abs("a")' |
| 29 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 30 | print 'apply' |
| 31 | def f0(*args): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 32 | if args != (): raise TestFailed, 'f0 called with ' + `args` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 33 | def f1(a1): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 34 | if a1 != 1: raise TestFailed, 'f1 called with ' + `a1` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 35 | def f2(a1, a2): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 36 | if a1 != 1 or a2 != 2: |
| 37 | raise TestFailed, 'f2 called with ' + `a1, a2` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 38 | def f3(a1, a2, a3): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 39 | if a1 != 1 or a2 != 2 or a3 != 3: |
| 40 | raise TestFailed, 'f3 called with ' + `a1, a2, a3` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 41 | apply(f0, ()) |
| 42 | apply(f1, (1,)) |
| 43 | apply(f2, (1, 2)) |
| 44 | apply(f3, (1, 2, 3)) |
| 45 | |
Fred Drake | 1a7aab7 | 2001-01-04 22:33:02 +0000 | [diff] [blame] | 46 | # A PyCFunction that takes only positional parameters should allow an |
| 47 | # empty keyword dictionary to pass without a complaint, but raise a |
| 48 | # TypeError if the dictionary is non-empty. |
| 49 | apply(id, (1,), {}) |
| 50 | try: |
| 51 | apply(id, (1,), {"foo": 1}) |
| 52 | except TypeError: |
| 53 | pass |
| 54 | else: |
| 55 | raise TestFailed, 'expected TypeError; no exception raised' |
| 56 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 57 | print 'callable' |
| 58 | if not callable(len):raise TestFailed, 'callable(len)' |
| 59 | def f(): pass |
| 60 | if not callable(f): raise TestFailed, 'callable(f)' |
| 61 | class C: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 62 | def meth(self): pass |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 63 | if not callable(C): raise TestFailed, 'callable(C)' |
| 64 | x = C() |
| 65 | if not callable(x.meth): raise TestFailed, 'callable(x.meth)' |
| 66 | if callable(x): raise TestFailed, 'callable(x)' |
| 67 | class D(C): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 68 | def __call__(self): pass |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 69 | y = D() |
| 70 | if not callable(y): raise TestFailed, 'callable(y)' |
Jeremy Hylton | 5ab3661 | 2001-01-03 23:53:31 +0000 | [diff] [blame] | 71 | y() |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 73 | print 'chr' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 74 | if chr(32) != ' ': raise TestFailed, 'chr(32)' |
| 75 | if chr(65) != 'A': raise TestFailed, 'chr(65)' |
| 76 | if chr(97) != 'a': raise TestFailed, 'chr(97)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 77 | |
Neil Schemenauer | 38f0223 | 2001-01-04 01:33:41 +0000 | [diff] [blame] | 78 | # cmp |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 79 | print 'cmp' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 80 | if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)' |
| 81 | if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)' |
| 82 | if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)' |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 83 | # verify that circular objects are handled |
| 84 | a = []; a.append(a) |
| 85 | b = []; b.append(b) |
| 86 | from UserList import UserList |
| 87 | c = UserList(); c.append(c) |
| 88 | if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b) |
| 89 | if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c) |
| 90 | if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a) |
| 91 | if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c) |
Vladimir Marangozov | d57f5cf | 2000-07-14 04:32:09 +0000 | [diff] [blame] | 92 | # okay, now break the cycles |
| 93 | a.pop(); b.pop(); c.pop() |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 94 | |
| 95 | print 'coerce' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 96 | 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] | 97 | if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 98 | 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] | 99 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 100 | print 'compile' |
| 101 | compile('print 1\n', '', 'exec') |
| 102 | |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 103 | print 'complex' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 104 | if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)' |
| 105 | if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)' |
| 106 | if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)' |
| 107 | if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)' |
| 108 | if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)' |
| 109 | if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)' |
| 110 | if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)' |
| 111 | if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)' |
| 112 | if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)' |
| 113 | if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 114 | if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 115 | if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)' |
| 116 | if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)' |
| 117 | if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)' |
| 118 | if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)' |
| 119 | if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)' |
| 120 | if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)' |
| 121 | if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)' |
| 122 | if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)' |
| 123 | if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)' |
| 124 | if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)' |
Fred Drake | 526c7a0 | 2001-12-13 19:52:22 +0000 | [diff] [blame] | 125 | if complex("1") != 1+0j: raise TestFailed, 'complex("1")' |
| 126 | if complex("1j") != 1j: raise TestFailed, 'complex("1j")' |
| 127 | try: complex("1", "1") |
| 128 | except TypeError: pass |
| 129 | else: raise TestFailed, 'complex("1", "1")' |
| 130 | try: complex(1, "1") |
| 131 | except TypeError: pass |
| 132 | else: raise TestFailed, 'complex(1, "1")' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 133 | if complex(" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"' |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 134 | if have_unicode: |
| 135 | if complex(unicode(" 3.14+J ")) != 3.14+1j: |
| 136 | raise TestFailed, 'complex(u" 3.14+J )"' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 137 | class Z: |
| 138 | def __complex__(self): return 3.14j |
| 139 | z = Z() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 140 | if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 142 | print 'delattr' |
| 143 | import sys |
| 144 | sys.spam = 1 |
| 145 | delattr(sys, 'spam') |
| 146 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 147 | print 'dir' |
| 148 | x = 1 |
| 149 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 150 | import sys |
| 151 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 152 | |
| 153 | print 'divmod' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 154 | if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 155 | if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 156 | if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 157 | if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 158 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 159 | if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)' |
| 160 | if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)' |
| 161 | if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)' |
| 162 | if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 163 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 164 | if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)' |
| 165 | if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)' |
| 166 | if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)' |
| 167 | if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 168 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 169 | if fcmp(divmod(3.25, 1.0), (3.0, 0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 170 | raise TestFailed, 'divmod(3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 171 | if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 172 | raise TestFailed, 'divmod(-3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 173 | if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 174 | raise TestFailed, 'divmod(3.25, -1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 175 | if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 176 | raise TestFailed, 'divmod(-3.25, -1.0)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 177 | |
| 178 | print 'eval' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 179 | if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')' |
| 180 | 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] | 181 | globals = {'a': 1, 'b': 2} |
| 182 | locals = {'b': 200, 'c': 300} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 183 | if eval('a', globals) != 1: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 184 | raise TestFailed, "eval(1) == %s" % eval('a', globals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 185 | if eval('a', globals, locals) != 1: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 186 | raise TestFailed, "eval(2)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 187 | if eval('b', globals, locals) != 200: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 188 | raise TestFailed, "eval(3)" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 189 | if eval('c', globals, locals) != 300: |
Jeremy Hylton | cf29bb2 | 2000-08-23 21:11:05 +0000 | [diff] [blame] | 190 | raise TestFailed, "eval(4)" |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 191 | if have_unicode: |
| 192 | if eval(unicode('1+1')) != 2: raise TestFailed, 'eval(u\'1+1\')' |
| 193 | if eval(unicode(' 1+1\n')) != 2: raise TestFailed, 'eval(u\' 1+1\\n\')' |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 194 | globals = {'a': 1, 'b': 2} |
| 195 | locals = {'b': 200, 'c': 300} |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 196 | if have_unicode: |
| 197 | if eval(unicode('a'), globals) != 1: |
| 198 | raise TestFailed, "eval(1) == %s" % eval(unicode('a'), globals) |
| 199 | if eval(unicode('a'), globals, locals) != 1: |
| 200 | raise TestFailed, "eval(2)" |
| 201 | if eval(unicode('b'), globals, locals) != 200: |
| 202 | raise TestFailed, "eval(3)" |
| 203 | if eval(unicode('c'), globals, locals) != 300: |
| 204 | raise TestFailed, "eval(4)" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 206 | print 'execfile' |
| 207 | z = 0 |
| 208 | f = open(TESTFN, 'w') |
| 209 | f.write('z = z+1\n') |
| 210 | f.write('z = z*2\n') |
| 211 | f.close() |
| 212 | execfile(TESTFN) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 213 | if z != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 214 | globals['z'] = 0 |
| 215 | execfile(TESTFN, globals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 216 | if globals['z'] != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 217 | locals['z'] = 0 |
| 218 | execfile(TESTFN, globals, locals) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 219 | if locals['z'] != 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 220 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 222 | print 'filter' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 223 | if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld': |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 224 | raise TestFailed, 'filter (filter a string)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 225 | 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] | 226 | raise TestFailed, 'filter (remove false values)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 227 | 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] | 228 | raise TestFailed, 'filter (keep positives)' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 229 | class Squares: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 230 | def __init__(self, max): |
| 231 | self.max = max |
| 232 | self.sofar = [] |
| 233 | def __len__(self): return len(self.sofar) |
| 234 | def __getitem__(self, i): |
| 235 | if not 0 <= i < self.max: raise IndexError |
| 236 | n = len(self.sofar) |
| 237 | while n <= i: |
| 238 | self.sofar.append(n*n) |
| 239 | n = n+1 |
| 240 | return self.sofar[i] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | 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] | 242 | raise TestFailed, 'filter(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | 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] | 244 | raise TestFailed, 'filter(oddp, Squares(10))' |
Barry Warsaw | ab11f60 | 1999-01-28 19:44:06 +0000 | [diff] [blame] | 245 | class StrSquares: |
| 246 | def __init__(self, max): |
| 247 | self.max = max |
| 248 | self.sofar = [] |
| 249 | def __len__(self): |
| 250 | return len(self.sofar) |
| 251 | def __getitem__(self, i): |
| 252 | if not 0 <= i < self.max: |
| 253 | raise IndexError |
| 254 | n = len(self.sofar) |
| 255 | while n <= i: |
| 256 | self.sofar.append(str(n*n)) |
| 257 | n = n+1 |
| 258 | return self.sofar[i] |
| 259 | def identity(item): |
| 260 | return 1 |
| 261 | filter(identity, Squares(5)) |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 263 | print 'float' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 264 | if float(3.14) != 3.14: raise TestFailed, 'float(3.14)' |
| 265 | if float(314) != 314.0: raise TestFailed, 'float(314)' |
| 266 | if float(314L) != 314.0: raise TestFailed, 'float(314L)' |
| 267 | if float(" 3.14 ") != 3.14: raise TestFailed, 'float(" 3.14 ")' |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 268 | if have_unicode: |
| 269 | if float(unicode(" 3.14 ")) != 3.14: |
| 270 | raise TestFailed, 'float(u" 3.14 ")' |
Martin v. Löwis | 10d7255 | 2001-08-17 22:08:34 +0000 | [diff] [blame] | 271 | if float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')) != 3.14: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 272 | raise TestFailed, 'float(u" \u0663.\u0661\u0664 ")' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 273 | |
| 274 | print 'getattr' |
| 275 | import sys |
| 276 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
Jeremy Hylton | b7a7731 | 2001-07-30 22:49:11 +0000 | [diff] [blame] | 277 | try: |
| 278 | getattr(sys, 1) |
| 279 | except TypeError: |
| 280 | pass |
| 281 | else: |
| 282 | raise TestFailed, "getattr(sys, 1) should raise an exception" |
| 283 | try: |
| 284 | getattr(sys, 1, "foo") |
| 285 | except TypeError: |
| 286 | pass |
| 287 | else: |
| 288 | raise TestFailed, 'getattr(sys, 1, "foo") should raise an exception' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 290 | print 'hasattr' |
| 291 | import sys |
| 292 | if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr' |
Jeremy Hylton | b7a7731 | 2001-07-30 22:49:11 +0000 | [diff] [blame] | 293 | try: |
| 294 | hasattr(sys, 1) |
| 295 | except TypeError: |
| 296 | pass |
| 297 | else: |
| 298 | raise TestFailed, "hasattr(sys, 1) should raise an exception" |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 299 | |
| 300 | print 'hash' |
| 301 | hash(None) |
| 302 | if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()' |
| 303 | hash('spam') |
| 304 | hash((0,1,2,3)) |
| 305 | def f(): pass |
Guido van Rossum | 0c6614c | 2001-01-18 23:36:14 +0000 | [diff] [blame] | 306 | try: hash([]) |
| 307 | except TypeError: pass |
| 308 | else: raise TestFailed, "hash([]) should raise an exception" |
| 309 | try: hash({}) |
| 310 | except TypeError: pass |
| 311 | else: raise TestFailed, "hash({}) should raise an exception" |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 313 | print 'hex' |
| 314 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 315 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
Guido van Rossum | c1c96d1 | 1997-05-14 21:37:23 +0000 | [diff] [blame] | 316 | if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))' |
| 317 | if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'): |
| 318 | raise TestFailed, 'hex(-16)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 319 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 320 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 321 | print 'id' |
| 322 | id(None) |
| 323 | id(1) |
| 324 | id(1L) |
| 325 | id(1.0) |
| 326 | id('spam') |
| 327 | id((0,1,2,3)) |
| 328 | id([0,1,2,3]) |
| 329 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 330 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 331 | # Test input() later, together with raw_input |
| 332 | |
| 333 | print 'int' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 334 | if int(314) != 314: raise TestFailed, 'int(314)' |
| 335 | if int(3.14) != 3: raise TestFailed, 'int(3.14)' |
| 336 | if int(314L) != 314: raise TestFailed, 'int(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 337 | # Check that conversion from float truncates towards zero |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 338 | if int(-3.14) != -3: raise TestFailed, 'int(-3.14)' |
| 339 | if int(3.9) != 3: raise TestFailed, 'int(3.9)' |
| 340 | if int(-3.9) != -3: raise TestFailed, 'int(-3.9)' |
| 341 | if int(3.5) != 3: raise TestFailed, 'int(3.5)' |
| 342 | if int(-3.5) != -3: raise TestFailed, 'int(-3.5)' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 343 | # Different base: |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 344 | if int("10",16) != 16L: raise TestFailed, 'int("10",16)' |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 345 | if have_unicode: |
| 346 | if int(unicode("10"),16) != 16L: |
| 347 | raise TestFailed, 'int(u"10",16)' |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 348 | # Test conversion from strings and various anomalies |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 349 | L = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 350 | ('0', 0), |
| 351 | ('1', 1), |
| 352 | ('9', 9), |
| 353 | ('10', 10), |
| 354 | ('99', 99), |
| 355 | ('100', 100), |
| 356 | ('314', 314), |
| 357 | (' 314', 314), |
| 358 | ('314 ', 314), |
| 359 | (' \t\t 314 \t\t ', 314), |
| 360 | (`sys.maxint`, sys.maxint), |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 361 | (' 1x', ValueError), |
| 362 | (' 1 ', 1), |
| 363 | (' 1\02 ', ValueError), |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 364 | ('', ValueError), |
| 365 | (' ', ValueError), |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 366 | (' \t\t ', ValueError) |
| 367 | ] |
| 368 | if have_unicode: |
| 369 | L += [ |
| 370 | (unicode('0'), 0), |
| 371 | (unicode('1'), 1), |
| 372 | (unicode('9'), 9), |
| 373 | (unicode('10'), 10), |
| 374 | (unicode('99'), 99), |
| 375 | (unicode('100'), 100), |
| 376 | (unicode('314'), 314), |
| 377 | (unicode(' 314'), 314), |
Martin v. Löwis | 10d7255 | 2001-08-17 22:08:34 +0000 | [diff] [blame] | 378 | (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314), |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 379 | (unicode(' \t\t 314 \t\t '), 314), |
| 380 | (unicode(' 1x'), ValueError), |
| 381 | (unicode(' 1 '), 1), |
| 382 | (unicode(' 1\02 '), ValueError), |
| 383 | (unicode(''), ValueError), |
| 384 | (unicode(' '), ValueError), |
| 385 | (unicode(' \t\t '), ValueError), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 386 | ] |
| 387 | for s, v in L: |
| 388 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 389 | for prefix in "", " ", "\t", " \t\t ": |
| 390 | ss = prefix + sign + s |
| 391 | vv = v |
| 392 | if sign == "-" and v is not ValueError: |
| 393 | vv = -v |
| 394 | try: |
| 395 | if int(ss) != vv: |
| 396 | raise TestFailed, "int(%s)" % `ss` |
| 397 | except v: |
| 398 | pass |
| 399 | except ValueError, e: |
| 400 | raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 401 | s = `-1-sys.maxint` |
| 402 | if int(s)+1 != -sys.maxint: |
| 403 | raise TestFailed, "int(%s)" % `s` |
| 404 | try: |
| 405 | int(s[1:]) |
| 406 | except ValueError: |
| 407 | pass |
| 408 | else: |
| 409 | raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError" |
Tim Peters | 7321ec4 | 2001-07-26 20:02:17 +0000 | [diff] [blame] | 410 | try: |
| 411 | int(1e100) |
| 412 | except OverflowError: |
| 413 | pass |
| 414 | else: |
| 415 | raise TestFailed("int(1e100) expected OverflowError") |
| 416 | try: |
| 417 | int(-1e100) |
| 418 | except OverflowError: |
| 419 | pass |
| 420 | else: |
| 421 | raise TestFailed("int(-1e100) expected OverflowError") |
| 422 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 423 | |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 424 | # SF bug 434186: 0x80000000/2 != 0x80000000>>1. |
| 425 | # Worked by accident in Windows release build, but failed in debug build. |
| 426 | # Failed in all Linux builds. |
| 427 | x = -1-sys.maxint |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 428 | if x >> 1 != x//2: |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 429 | raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint") |
| 430 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 431 | try: int('123\0') |
| 432 | except ValueError: pass |
| 433 | else: raise TestFailed("int('123\0') didn't raise exception") |
| 434 | |
Neal Norwitz | 32f4153 | 2001-12-29 00:35:20 +0000 | [diff] [blame] | 435 | try: int('53', 40) |
| 436 | except ValueError: pass |
| 437 | else: raise TestFailed("int('53', 40) didn't raise ValueError") |
| 438 | |
| 439 | try: int('1' * 512) |
| 440 | except ValueError: pass |
| 441 | else: raise TestFailed("int('1' * 512) didn't raise ValueError") |
| 442 | |
| 443 | try: int(1, 12) |
| 444 | except TypeError: pass |
| 445 | else: raise TestFailed("int(1, 12) didn't raise TypeError") |
| 446 | |
| 447 | if int('0123', 0) != 83: |
| 448 | raise TestFailed("int('0123', 0) != 83") |
| 449 | |
Barry Warsaw | d543077 | 1997-08-22 21:27:03 +0000 | [diff] [blame] | 450 | print 'isinstance' |
| 451 | class C: |
| 452 | pass |
| 453 | class D(C): |
| 454 | pass |
| 455 | class E: |
| 456 | pass |
| 457 | c = C() |
| 458 | d = D() |
| 459 | e = E() |
| 460 | if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)' |
| 461 | if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)' |
| 462 | if isinstance(e, C): raise TestFailed, 'isinstance(e, C)' |
| 463 | if isinstance(c, D): raise TestFailed, 'isinstance(c, D)' |
| 464 | if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)' |
| 465 | try: |
| 466 | isinstance(E, 'foo') |
| 467 | raise TestFailed, 'isinstance(E, "foo")' |
| 468 | except TypeError: |
| 469 | pass |
| 470 | |
| 471 | print 'issubclass' |
| 472 | if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)' |
| 473 | if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)' |
| 474 | if issubclass(C, D): raise TestFailed, 'issubclass(C, D)' |
| 475 | try: |
| 476 | issubclass('foo', E) |
| 477 | raise TestFailed, 'issubclass("foo", E)' |
| 478 | except TypeError: |
| 479 | pass |
| 480 | try: |
| 481 | issubclass(E, 'foo') |
| 482 | raise TestFailed, 'issubclass(E, "foo")' |
| 483 | except TypeError: |
| 484 | pass |
| 485 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 486 | print 'len' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 487 | if len('123') != 3: raise TestFailed, 'len(\'123\')' |
| 488 | if len(()) != 0: raise TestFailed, 'len(())' |
| 489 | if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 490 | if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 491 | if len({}) != 0: raise TestFailed, 'len({})' |
| 492 | 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] | 493 | |
Guido van Rossum | 06ee253 | 2002-02-26 22:39:23 +0000 | [diff] [blame] | 494 | print 'list' |
| 495 | if list([]) != []: raise TestFailed, 'list([])' |
| 496 | l0_3 = [0, 1, 2, 3] |
| 497 | l0_3_bis = list(l0_3) |
| 498 | if l0_3 != l0_3_bis or l0_3 is l0_3_bis: raise TestFailed, 'list([0, 1, 2, 3])' |
| 499 | if list(()) != []: raise TestFailed, 'list(())' |
| 500 | if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed, 'list((0, 1, 2, 3))' |
| 501 | if list('') != []: raise TestFailed, 'list('')' |
| 502 | if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed, "list('spam')" |
| 503 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 504 | print 'long' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 505 | if long(314) != 314L: raise TestFailed, 'long(314)' |
| 506 | if long(3.14) != 3L: raise TestFailed, 'long(3.14)' |
| 507 | if long(314L) != 314L: raise TestFailed, 'long(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 508 | # Check that conversion from float truncates towards zero |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 509 | if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)' |
| 510 | if long(3.9) != 3L: raise TestFailed, 'long(3.9)' |
| 511 | if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)' |
| 512 | if long(3.5) != 3L: raise TestFailed, 'long(3.5)' |
| 513 | if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)' |
| 514 | if long("-3") != -3L: raise TestFailed, 'long("-3")' |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 515 | if have_unicode: |
| 516 | if long(unicode("-3")) != -3L: |
| 517 | raise TestFailed, 'long(u"-3")' |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 518 | # Different base: |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 519 | if long("10",16) != 16L: raise TestFailed, 'long("10",16)' |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 520 | if have_unicode: |
| 521 | if long(unicode("10"),16) != 16L: |
| 522 | raise TestFailed, 'long(u"10",16)' |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 523 | # Check conversions from string (same test set as for int(), and then some) |
| 524 | LL = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 525 | ('1' + '0'*20, 10L**20), |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 526 | ('1' + '0'*100, 10L**100) |
| 527 | ] |
| 528 | if have_unicode: |
| 529 | L+=[ |
| 530 | (unicode('1') + unicode('0')*20, 10L**20), |
| 531 | (unicode('1') + unicode('0')*100, 10L**100), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 532 | ] |
| 533 | for s, v in L + LL: |
| 534 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 535 | for prefix in "", " ", "\t", " \t\t ": |
| 536 | ss = prefix + sign + s |
| 537 | vv = v |
| 538 | if sign == "-" and v is not ValueError: |
| 539 | vv = -v |
| 540 | try: |
| 541 | if long(ss) != long(vv): |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 542 | raise TestFailed, "long(%s)" % `ss` |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 543 | except v: |
| 544 | pass |
| 545 | except ValueError, e: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 546 | raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 547 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 548 | try: long('123\0') |
| 549 | except ValueError: pass |
| 550 | else: raise TestFailed("long('123\0') didn't raise exception") |
| 551 | |
Neal Norwitz | 32f4153 | 2001-12-29 00:35:20 +0000 | [diff] [blame] | 552 | try: long('53', 40) |
| 553 | except ValueError: pass |
| 554 | else: raise TestFailed("long('53', 40) didn't raise ValueError") |
| 555 | |
| 556 | try: long(1, 12) |
| 557 | except TypeError: pass |
| 558 | else: raise TestFailed("long(1, 12) didn't raise TypeError") |
| 559 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 560 | print 'map' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 561 | 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] | 562 | raise TestFailed, 'map(None, \'hello world\')' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 563 | if map(None, 'abcd', 'efg') != \ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 564 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]: |
| 565 | raise TestFailed, 'map(None, \'abcd\', \'efg\')' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 566 | 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] | 567 | raise TestFailed, 'map(None, range(10))' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 568 | 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] | 569 | raise TestFailed, 'map(lambda x: x*x, range(1,4))' |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 570 | try: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 571 | from math import sqrt |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 572 | except ImportError: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 573 | def sqrt(x): |
| 574 | return pow(x, 0.5) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 575 | 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] | 576 | raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 577 | 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] | 578 | 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] | 579 | def plus(*v): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 580 | accu = 0 |
| 581 | for i in v: accu = accu + i |
| 582 | return accu |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 583 | if map(plus, [1, 3, 7]) != [1, 3, 7]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 584 | raise TestFailed, 'map(plus, [1, 3, 7])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 585 | 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] | 586 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 587 | 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] | 588 | 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] | 589 | 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] | 590 | raise TestFailed, 'map(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 591 | 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] | 592 | raise TestFailed, 'map(int, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 593 | 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] | 594 | raise TestFailed, 'map(None, Squares(3), Squares(2))' |
Guido van Rossum | 7f9b5e0 | 2001-01-22 19:30:07 +0000 | [diff] [blame] | 595 | if map(max, Squares(3), Squares(2)) != [0, 1, 4]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 596 | raise TestFailed, 'map(max, Squares(3), Squares(2))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 598 | print 'max' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 599 | if max('123123') != '3': raise TestFailed, 'max(\'123123\')' |
| 600 | if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)' |
| 601 | if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 602 | 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] | 603 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 604 | if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 605 | if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 606 | 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] | 607 | |
| 608 | print 'min' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 609 | if min('123123') != '1': raise TestFailed, 'min(\'123123\')' |
| 610 | if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)' |
| 611 | if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 612 | 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] | 613 | # |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 614 | if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 615 | if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 616 | if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |