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