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