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): |
| 28 | if args != (): raise TestFailed, 'f0 called with ' + `args` |
| 29 | def f1(a1): |
| 30 | if a1 != 1: raise TestFailed, 'f1 called with ' + `a1` |
| 31 | def f2(a1, a2): |
| 32 | if a1 != 1 or a2 != 2: |
| 33 | raise TestFailed, 'f2 called with ' + `a1, a2` |
| 34 | def f3(a1, a2, a3): |
| 35 | if a1 != 1 or a2 != 2 or a3 != 3: |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 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: |
| 47 | def meth(self): pass |
| 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): |
| 53 | def __call__(self): pass |
| 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 | |
| 75 | print 'delattr' |
| 76 | import sys |
| 77 | sys.spam = 1 |
| 78 | delattr(sys, 'spam') |
| 79 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 80 | print 'dir' |
| 81 | x = 1 |
| 82 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 83 | import sys |
| 84 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 85 | |
| 86 | print 'divmod' |
| 87 | if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 88 | if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 89 | if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 90 | if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)' |
| 91 | # |
| 92 | if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)' |
| 93 | if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)' |
| 94 | if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)' |
| 95 | if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)' |
| 96 | # |
| 97 | if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)' |
| 98 | if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)' |
| 99 | if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)' |
| 100 | if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)' |
| 101 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 102 | if fcmp(divmod(3.25, 1.0), (3.0, 0.25)): |
| 103 | raise TestFailed, 'divmod(3.25, 1.0)' |
| 104 | if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)): |
| 105 | raise TestFailed, 'divmod(-3.25, 1.0)' |
| 106 | if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)): |
| 107 | raise TestFailed, 'divmod(3.25, -1.0)' |
| 108 | if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)): |
| 109 | raise TestFailed, 'divmod(-3.25, -1.0)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 110 | |
| 111 | print 'eval' |
| 112 | if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 113 | 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] | 114 | globals = {'a': 1, 'b': 2} |
| 115 | locals = {'b': 200, 'c': 300} |
| 116 | if eval('a', globals) <> 1: raise TestFailed, "eval(1)" |
| 117 | if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)" |
| 118 | if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)" |
| 119 | if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)" |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 121 | print 'execfile' |
| 122 | z = 0 |
| 123 | f = open(TESTFN, 'w') |
| 124 | f.write('z = z+1\n') |
| 125 | f.write('z = z*2\n') |
| 126 | f.close() |
| 127 | execfile(TESTFN) |
Guido van Rossum | 824de25 | 1995-01-02 18:38:42 +0000 | [diff] [blame] | 128 | if z <> 2: raise TestFailed, "execfile(1)" |
| 129 | globals['z'] = 0 |
| 130 | execfile(TESTFN, globals) |
| 131 | if globals['z'] <> 2: raise TestFailed, "execfile(1)" |
| 132 | locals['z'] = 0 |
| 133 | execfile(TESTFN, globals, locals) |
| 134 | if locals['z'] <> 2: raise TestFailed, "execfile(1)" |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 135 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 137 | print 'filter' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 138 | if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld': |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 139 | raise TestFailed, 'filter (filter a string)' |
| 140 | if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]: |
| 141 | raise TestFailed, 'filter (remove false values)' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 142 | if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 143 | raise TestFailed, 'filter (keep positives)' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 144 | class Squares: |
| 145 | def __init__(self, max): |
| 146 | self.max = max |
| 147 | self.sofar = [] |
| 148 | def __len__(self): return len(self.sofar) |
| 149 | def __getitem__(self, i): |
| 150 | if not 0 <= i < self.max: raise IndexError |
| 151 | n = len(self.sofar) |
| 152 | while n <= i: |
| 153 | self.sofar.append(n*n) |
| 154 | n = n+1 |
| 155 | return self.sofar[i] |
| 156 | if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 157 | raise TestFailed, 'filter(None, Squares(10))' |
| 158 | if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]: |
| 159 | raise TestFailed, 'filter(oddp, Squares(10))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 161 | print 'float' |
| 162 | if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' |
| 163 | if float(314) <> 314.0: raise TestFailed, 'float(314)' |
| 164 | if float(314L) <> 314.0: raise TestFailed, 'float(314L)' |
| 165 | |
| 166 | print 'getattr' |
| 167 | import sys |
| 168 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
| 169 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 170 | print 'hasattr' |
| 171 | import sys |
| 172 | if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr' |
| 173 | |
| 174 | print 'hash' |
| 175 | hash(None) |
| 176 | if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()' |
| 177 | hash('spam') |
| 178 | hash((0,1,2,3)) |
| 179 | def f(): pass |
| 180 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 181 | print 'hex' |
| 182 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 183 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
| 184 | if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)' |
| 185 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 186 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 187 | print 'id' |
| 188 | id(None) |
| 189 | id(1) |
| 190 | id(1L) |
| 191 | id(1.0) |
| 192 | id('spam') |
| 193 | id((0,1,2,3)) |
| 194 | id([0,1,2,3]) |
| 195 | id({'spam': 1, 'eggs': 2, 'ham': 3}) |
| 196 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 197 | # Test input() later, together with raw_input |
| 198 | |
| 199 | print 'int' |
| 200 | if int(314) <> 314: raise TestFailed, 'int(314)' |
| 201 | if int(3.14) <> 3: raise TestFailed, 'int(3.14)' |
| 202 | if int(314L) <> 314: raise TestFailed, 'int(314L)' |
| 203 | |
| 204 | print 'len' |
| 205 | if len('123') <> 3: raise TestFailed, 'len(\'123\')' |
| 206 | if len(()) <> 0: raise TestFailed, 'len(())' |
| 207 | if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 208 | if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 209 | if len({}) <> 0: raise TestFailed, 'len({})' |
| 210 | if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})' |
| 211 | |
| 212 | print 'long' |
| 213 | if long(314) <> 314L: raise TestFailed, 'long(314)' |
| 214 | if long(3.14) <> 3L: raise TestFailed, 'long(3.14)' |
| 215 | if long(314L) <> 314L: raise TestFailed, 'long(314L)' |
| 216 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 217 | print 'map' |
| 218 | if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']: |
| 219 | raise TestFailed, 'map(None, \'hello world\')' |
| 220 | if map(None, 'abcd', 'efg') <> \ |
| 221 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]: |
| 222 | raise TestFailed, 'map(None, \'abcd\', \'efg\')' |
| 223 | if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: |
| 224 | raise TestFailed, 'map(None, range(10))' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 225 | if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]: |
| 226 | raise TestFailed, 'map(lambda x: x*x, range(1,4))' |
Guido van Rossum | 51b1c1c | 1995-03-04 22:30:54 +0000 | [diff] [blame] | 227 | try: |
| 228 | from math import sqrt |
| 229 | except ImportError: |
| 230 | def sqrt(x): |
| 231 | return pow(x, 0.5) |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 232 | if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]: |
| 233 | raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])' |
| 234 | if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]: |
| 235 | 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] | 236 | def plus(*v): |
| 237 | accu = 0 |
| 238 | for i in v: accu = accu + i |
| 239 | return accu |
| 240 | if map(plus, [1, 3, 7]) <> [1, 3, 7]: |
| 241 | raise TestFailed, 'map(plus, [1, 3, 7])' |
| 242 | if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]: |
| 243 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])' |
| 244 | if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]: |
| 245 | 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] | 246 | if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 247 | raise TestFailed, 'map(None, Squares(10))' |
| 248 | if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 249 | raise TestFailed, 'map(int, Squares(10))' |
| 250 | if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]: |
| 251 | raise TestFailed, 'map(None: x, Squares(3), Squares(2))' |
| 252 | if map(max, Squares(3), Squares(2)) != [0, 1, 4]: |
| 253 | raise TestFailed, 'map(None: x, Squares(3), Squares(2))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 255 | print 'max' |
| 256 | if max('123123') <> '3': raise TestFailed, 'max(\'123123\')' |
| 257 | if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)' |
| 258 | if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 259 | if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
| 260 | # |
| 261 | if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 262 | if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 263 | if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)' |
| 264 | |
| 265 | print 'min' |
| 266 | if min('123123') <> '1': raise TestFailed, 'min(\'123123\')' |
| 267 | if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)' |
| 268 | if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 269 | if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
| 270 | # |
| 271 | if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 272 | if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 273 | if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |