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') |
| 7 | __import__('strop') |
| 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' |
| 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)' |
| 17 | # |
| 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)' |
| 21 | # |
| 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)' |
| 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)' |
| 56 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 57 | print 'chr' |
| 58 | if chr(32) <> ' ': raise TestFailed, 'chr(32)' |
| 59 | if chr(65) <> 'A': raise TestFailed, 'chr(65)' |
| 60 | if chr(97) <> 'a': raise TestFailed, 'chr(97)' |
| 61 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 62 | print 'cmp' |
| 63 | if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)' |
| 64 | if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)' |
| 65 | if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)' |
| 66 | |
| 67 | print 'coerce' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 68 | if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 69 | if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 70 | 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] | 71 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 72 | print 'compile' |
| 73 | compile('print 1\n', '', 'exec') |
| 74 | |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 75 | print 'complex' |
| 76 | if complex(1,10) <> 1+10j: raise TestFailed, 'complex(1,10)' |
| 77 | if complex(1,10L) <> 1+10j: raise TestFailed, 'complex(1,10L)' |
| 78 | if complex(1,10.0) <> 1+10j: raise TestFailed, 'complex(1,10.0)' |
| 79 | if complex(1L,10) <> 1+10j: raise TestFailed, 'complex(1L,10)' |
| 80 | if complex(1L,10L) <> 1+10j: raise TestFailed, 'complex(1L,10L)' |
| 81 | if complex(1L,10.0) <> 1+10j: raise TestFailed, 'complex(1L,10.0)' |
| 82 | if complex(1.0,10) <> 1+10j: raise TestFailed, 'complex(1.0,10)' |
| 83 | if complex(1.0,10L) <> 1+10j: raise TestFailed, 'complex(1.0,10L)' |
| 84 | if complex(1.0,10.0) <> 1+10j: raise TestFailed, 'complex(1.0,10.0)' |
| 85 | if complex(3.14+0j) <> 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 86 | if complex(3.14) <> 3.14+0j: raise TestFailed, 'complex(3.14)' |
| 87 | if complex(314) <> 314.0+0j: raise TestFailed, 'complex(314)' |
| 88 | if complex(314L) <> 314.0+0j: raise TestFailed, 'complex(314L)' |
| 89 | if complex(3.14+0j, 0j) <> 3.14+0j: raise TestFailed, 'complex(3.14, 0j)' |
| 90 | if complex(3.14, 0.0) <> 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)' |
| 91 | if complex(314, 0) <> 314.0+0j: raise TestFailed, 'complex(314, 0)' |
| 92 | if complex(314L, 0L) <> 314.0+0j: raise TestFailed, 'complex(314L, 0L)' |
| 93 | if complex(0j, 3.14j) <> -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)' |
| 94 | if complex(0.0, 3.14j) <> -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)' |
| 95 | if complex(0j, 3.14) <> 3.14j: raise TestFailed, 'complex(0j, 3.14)' |
| 96 | if complex(0.0, 3.14) <> 3.14j: raise TestFailed, 'complex(0.0, 3.14)' |
| 97 | class Z: |
| 98 | def __complex__(self): return 3.14j |
| 99 | z = Z() |
| 100 | if complex(z) <> 3.14j: raise TestFailed, 'complex(classinstance)' |
| 101 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 102 | print 'delattr' |
| 103 | import sys |
| 104 | sys.spam = 1 |
| 105 | delattr(sys, 'spam') |
| 106 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 107 | print 'dir' |
| 108 | x = 1 |
| 109 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 110 | import sys |
| 111 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 112 | |
| 113 | print 'divmod' |
| 114 | if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 115 | if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 116 | if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 117 | if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)' |
| 118 | # |
| 119 | if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)' |
| 120 | if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)' |
| 121 | if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)' |
| 122 | if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)' |
| 123 | # |
| 124 | if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)' |
| 125 | if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)' |
| 126 | if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)' |
| 127 | if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)' |
| 128 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 129 | if fcmp(divmod(3.25, 1.0), (3.0, 0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 130 | raise TestFailed, 'divmod(3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 131 | if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 132 | raise TestFailed, 'divmod(-3.25, 1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 133 | if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 134 | raise TestFailed, 'divmod(3.25, -1.0)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 135 | if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 136 | raise TestFailed, 'divmod(-3.25, -1.0)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 137 | |
| 138 | print 'eval' |
| 139 | if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 140 | 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] | 141 | globals = {'a': 1, 'b': 2} |
| 142 | locals = {'b': 200, 'c': 300} |
| 143 | if eval('a', globals) <> 1: raise TestFailed, "eval(1)" |
| 144 | if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)" |
| 145 | if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)" |
| 146 | if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 148 | print 'execfile' |
| 149 | z = 0 |
| 150 | f = open(TESTFN, 'w') |
| 151 | f.write('z = z+1\n') |
| 152 | f.write('z = z*2\n') |
| 153 | f.close() |
| 154 | execfile(TESTFN) |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 155 | if z <> 2: raise TestFailed, "execfile(1)" |
| 156 | globals['z'] = 0 |
| 157 | execfile(TESTFN, globals) |
| 158 | if globals['z'] <> 2: raise TestFailed, "execfile(1)" |
| 159 | locals['z'] = 0 |
| 160 | execfile(TESTFN, globals, locals) |
| 161 | if locals['z'] <> 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 162 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 163 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 164 | print 'filter' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 165 | if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld': |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 166 | raise TestFailed, 'filter (filter a string)' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 167 | 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] | 168 | raise TestFailed, 'filter (remove false values)' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 169 | 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] | 170 | raise TestFailed, 'filter (keep positives)' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 171 | class Squares: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 172 | def __init__(self, max): |
| 173 | self.max = max |
| 174 | self.sofar = [] |
| 175 | def __len__(self): return len(self.sofar) |
| 176 | def __getitem__(self, i): |
| 177 | if not 0 <= i < self.max: raise IndexError |
| 178 | n = len(self.sofar) |
| 179 | while n <= i: |
| 180 | self.sofar.append(n*n) |
| 181 | n = n+1 |
| 182 | return self.sofar[i] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | 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] | 184 | raise TestFailed, 'filter(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 185 | 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] | 186 | raise TestFailed, 'filter(oddp, Squares(10))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 188 | print 'float' |
| 189 | if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' |
| 190 | if float(314) <> 314.0: raise TestFailed, 'float(314)' |
| 191 | if float(314L) <> 314.0: raise TestFailed, 'float(314L)' |
| 192 | |
| 193 | print 'getattr' |
| 194 | import sys |
| 195 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
| 196 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 197 | print 'hasattr' |
| 198 | import sys |
| 199 | if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr' |
| 200 | |
| 201 | print 'hash' |
| 202 | hash(None) |
| 203 | if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()' |
| 204 | hash('spam') |
| 205 | hash((0,1,2,3)) |
| 206 | def f(): pass |
| 207 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 208 | print 'hex' |
| 209 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 210 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
Guido van Rossum | c1c96d1 | 1997-05-14 21:37:23 +0000 | [diff] [blame] | 211 | if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))' |
| 212 | if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'): |
| 213 | raise TestFailed, 'hex(-16)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 214 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 215 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 216 | print 'id' |
| 217 | id(None) |
| 218 | id(1) |
| 219 | id(1L) |
| 220 | id(1.0) |
| 221 | id('spam') |
| 222 | id((0,1,2,3)) |
| 223 | id([0,1,2,3]) |
| 224 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 225 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 226 | # Test input() later, together with raw_input |
| 227 | |
| 228 | print 'int' |
| 229 | if int(314) <> 314: raise TestFailed, 'int(314)' |
| 230 | if int(3.14) <> 3: raise TestFailed, 'int(3.14)' |
| 231 | if int(314L) <> 314: raise TestFailed, 'int(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 232 | # Check that conversion from float truncates towards zero |
| 233 | if int(-3.14) <> -3: raise TestFailed, 'int(-3.14)' |
| 234 | if int(3.9) <> 3: raise TestFailed, 'int(3.9)' |
| 235 | if int(-3.9) <> -3: raise TestFailed, 'int(-3.9)' |
| 236 | if int(3.5) <> 3: raise TestFailed, 'int(3.5)' |
| 237 | if int(-3.5) <> -3: raise TestFailed, 'int(-3.5)' |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 238 | # Test conversion fron strings and various anomalies |
| 239 | L = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 240 | ('0', 0), |
| 241 | ('1', 1), |
| 242 | ('9', 9), |
| 243 | ('10', 10), |
| 244 | ('99', 99), |
| 245 | ('100', 100), |
| 246 | ('314', 314), |
| 247 | (' 314', 314), |
| 248 | ('314 ', 314), |
| 249 | (' \t\t 314 \t\t ', 314), |
| 250 | (`sys.maxint`, sys.maxint), |
| 251 | ('', ValueError), |
| 252 | (' ', ValueError), |
| 253 | (' \t\t ', ValueError), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 254 | ] |
| 255 | for s, v in L: |
| 256 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 257 | for prefix in "", " ", "\t", " \t\t ": |
| 258 | ss = prefix + sign + s |
| 259 | vv = v |
| 260 | if sign == "-" and v is not ValueError: |
| 261 | vv = -v |
| 262 | try: |
| 263 | if int(ss) != vv: |
| 264 | raise TestFailed, "int(%s)" % `ss` |
| 265 | except v: |
| 266 | pass |
| 267 | except ValueError, e: |
| 268 | raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 269 | s = `-1-sys.maxint` |
| 270 | if int(s)+1 != -sys.maxint: |
| 271 | raise TestFailed, "int(%s)" % `s` |
| 272 | try: |
| 273 | int(s[1:]) |
| 274 | except ValueError: |
| 275 | pass |
| 276 | else: |
| 277 | raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 278 | |
Barry Warsaw | d543077 | 1997-08-22 21:27:03 +0000 | [diff] [blame] | 279 | print 'isinstance' |
| 280 | class C: |
| 281 | pass |
| 282 | class D(C): |
| 283 | pass |
| 284 | class E: |
| 285 | pass |
| 286 | c = C() |
| 287 | d = D() |
| 288 | e = E() |
| 289 | if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)' |
| 290 | if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)' |
| 291 | if isinstance(e, C): raise TestFailed, 'isinstance(e, C)' |
| 292 | if isinstance(c, D): raise TestFailed, 'isinstance(c, D)' |
| 293 | if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)' |
| 294 | try: |
| 295 | isinstance(E, 'foo') |
| 296 | raise TestFailed, 'isinstance(E, "foo")' |
| 297 | except TypeError: |
| 298 | pass |
| 299 | |
| 300 | print 'issubclass' |
| 301 | if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)' |
| 302 | if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)' |
| 303 | if issubclass(C, D): raise TestFailed, 'issubclass(C, D)' |
| 304 | try: |
| 305 | issubclass('foo', E) |
| 306 | raise TestFailed, 'issubclass("foo", E)' |
| 307 | except TypeError: |
| 308 | pass |
| 309 | try: |
| 310 | issubclass(E, 'foo') |
| 311 | raise TestFailed, 'issubclass(E, "foo")' |
| 312 | except TypeError: |
| 313 | pass |
| 314 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 315 | print 'len' |
| 316 | if len('123') <> 3: raise TestFailed, 'len(\'123\')' |
| 317 | if len(()) <> 0: raise TestFailed, 'len(())' |
| 318 | if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 319 | if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 320 | if len({}) <> 0: raise TestFailed, 'len({})' |
| 321 | if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})' |
| 322 | |
| 323 | print 'long' |
| 324 | if long(314) <> 314L: raise TestFailed, 'long(314)' |
| 325 | if long(3.14) <> 3L: raise TestFailed, 'long(3.14)' |
| 326 | if long(314L) <> 314L: raise TestFailed, 'long(314L)' |
Guido van Rossum | dddf7a6 | 1997-03-31 17:13:17 +0000 | [diff] [blame] | 327 | # Check that conversion from float truncates towards zero |
| 328 | if long(-3.14) <> -3L: raise TestFailed, 'long(-3.14)' |
| 329 | if long(3.9) <> 3L: raise TestFailed, 'long(3.9)' |
| 330 | if long(-3.9) <> -3L: raise TestFailed, 'long(-3.9)' |
| 331 | if long(3.5) <> 3L: raise TestFailed, 'long(3.5)' |
| 332 | if long(-3.5) <> -3L: raise TestFailed, 'long(-3.5)' |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 333 | # Check conversions from string (same test set as for int(), and then some) |
| 334 | LL = [ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 335 | ('1' + '0'*20, 10L**20), |
| 336 | ('1' + '0'*100, 10L**100), |
Guido van Rossum | 7011504 | 1998-06-30 17:02:20 +0000 | [diff] [blame] | 337 | ] |
| 338 | for s, v in L + LL: |
| 339 | for sign in "", "+", "-": |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 340 | for prefix in "", " ", "\t", " \t\t ": |
| 341 | ss = prefix + sign + s |
| 342 | vv = v |
| 343 | if sign == "-" and v is not ValueError: |
| 344 | vv = -v |
| 345 | try: |
| 346 | if long(ss) != long(vv): |
| 347 | raise TestFailed, "int(%s)" % `ss` |
| 348 | except v: |
| 349 | pass |
| 350 | except ValueError, e: |
| 351 | raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 353 | print 'map' |
| 354 | 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] | 355 | raise TestFailed, 'map(None, \'hello world\')' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 356 | if map(None, 'abcd', 'efg') <> \ |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 357 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]: |
| 358 | raise TestFailed, 'map(None, \'abcd\', \'efg\')' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 359 | 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] | 360 | raise TestFailed, 'map(None, range(10))' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 361 | 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] | 362 | raise TestFailed, 'map(lambda x: x*x, range(1,4))' |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 363 | try: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 364 | from math import sqrt |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 365 | except ImportError: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 366 | def sqrt(x): |
| 367 | return pow(x, 0.5) |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 368 | 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] | 369 | raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 370 | 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] | 371 | 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] | 372 | def plus(*v): |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 373 | accu = 0 |
| 374 | for i in v: accu = accu + i |
| 375 | return accu |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 376 | if map(plus, [1, 3, 7]) <> [1, 3, 7]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 377 | raise TestFailed, 'map(plus, [1, 3, 7])' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 378 | 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] | 379 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 380 | 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] | 381 | 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] | 382 | 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] | 383 | raise TestFailed, 'map(None, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 384 | 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] | 385 | raise TestFailed, 'map(int, Squares(10))' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 386 | 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] | 387 | raise TestFailed, 'map(None, Squares(3), Squares(2))' |
Guido van Rossum | 6925661 | 1998-06-11 22:25:59 +0000 | [diff] [blame] | 388 | if map(max, Squares(3), Squares(2)) != [0, 1, None]: |
Guido van Rossum | 27d445f | 1998-08-10 20:12:34 +0000 | [diff] [blame] | 389 | raise TestFailed, 'map(max, Squares(3), Squares(2))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 391 | print 'max' |
| 392 | if max('123123') <> '3': raise TestFailed, 'max(\'123123\')' |
| 393 | if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)' |
| 394 | if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 395 | if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
| 396 | # |
| 397 | if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 398 | if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 399 | if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)' |
| 400 | |
| 401 | print 'min' |
| 402 | if min('123123') <> '1': raise TestFailed, 'min(\'123123\')' |
| 403 | if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)' |
| 404 | if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 405 | if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
| 406 | # |
| 407 | if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 408 | if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 409 | if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |