Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Lib / test / test_b1.py
blobd9c6320cd32d4563b4656efc4fe0dce6f79e82b8
1 # Python test set -- part 4a, built-in functions a-m
3 from test_support import *
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"
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)'
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)'
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)'
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:
36 raise TestFailed, 'f3 called with ' + `a1, a2, a3`
37 apply(f0, ())
38 apply(f1, (1,))
39 apply(f2, (1, 2))
40 apply(f3, (1, 2, 3))
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)'
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)'
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)'
67 print 'coerce'
68 if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
69 if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
70 if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
72 print 'compile'
73 compile('print 1\n', '', 'exec')
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 if complex(" 3.14+J ") <> 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"'
98 class Z:
99 def __complex__(self): return 3.14j
100 z = Z()
101 if complex(z) <> 3.14j: raise TestFailed, 'complex(classinstance)'
103 print 'delattr'
104 import sys
105 sys.spam = 1
106 delattr(sys, 'spam')
108 print 'dir'
109 x = 1
110 if 'x' not in dir(): raise TestFailed, 'dir()'
111 import sys
112 if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
114 print 'divmod'
115 if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
116 if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
117 if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
118 if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
120 if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
121 if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
122 if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
123 if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
125 if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
126 if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
127 if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
128 if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
130 if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
131 raise TestFailed, 'divmod(3.25, 1.0)'
132 if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
133 raise TestFailed, 'divmod(-3.25, 1.0)'
134 if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
135 raise TestFailed, 'divmod(3.25, -1.0)'
136 if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
137 raise TestFailed, 'divmod(-3.25, -1.0)'
139 print 'eval'
140 if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
141 if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
142 globals = {'a': 1, 'b': 2}
143 locals = {'b': 200, 'c': 300}
144 if eval('a', globals) <> 1: raise TestFailed, "eval(1)"
145 if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)"
146 if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)"
147 if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)"
149 print 'execfile'
150 z = 0
151 f = open(TESTFN, 'w')
152 f.write('z = z+1\n')
153 f.write('z = z*2\n')
154 f.close()
155 execfile(TESTFN)
156 if z <> 2: raise TestFailed, "execfile(1)"
157 globals['z'] = 0
158 execfile(TESTFN, globals)
159 if globals['z'] <> 2: raise TestFailed, "execfile(1)"
160 locals['z'] = 0
161 execfile(TESTFN, globals, locals)
162 if locals['z'] <> 2: raise TestFailed, "execfile(1)"
163 unlink(TESTFN)
165 print 'filter'
166 if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
167 raise TestFailed, 'filter (filter a string)'
168 if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
169 raise TestFailed, 'filter (remove false values)'
170 if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
171 raise TestFailed, 'filter (keep positives)'
172 class Squares:
173 def __init__(self, max):
174 self.max = max
175 self.sofar = []
176 def __len__(self): return len(self.sofar)
177 def __getitem__(self, i):
178 if not 0 <= i < self.max: raise IndexError
179 n = len(self.sofar)
180 while n <= i:
181 self.sofar.append(n*n)
182 n = n+1
183 return self.sofar[i]
184 if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
185 raise TestFailed, 'filter(None, Squares(10))'
186 if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
187 raise TestFailed, 'filter(oddp, Squares(10))'
188 class StrSquares:
189 def __init__(self, max):
190 self.max = max
191 self.sofar = []
192 def __len__(self):
193 return len(self.sofar)
194 def __getitem__(self, i):
195 if not 0 <= i < self.max:
196 raise IndexError
197 n = len(self.sofar)
198 while n <= i:
199 self.sofar.append(str(n*n))
200 n = n+1
201 return self.sofar[i]
202 def identity(item):
203 return 1
204 filter(identity, Squares(5))
206 print 'float'
207 if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
208 if float(314) <> 314.0: raise TestFailed, 'float(314)'
209 if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
210 if float(" 3.14 ") <> 3.14: raise TestFailed, 'float(" 3.14 ")'
212 print 'getattr'
213 import sys
214 if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
216 print 'hasattr'
217 import sys
218 if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
220 print 'hash'
221 hash(None)
222 if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
223 hash('spam')
224 hash((0,1,2,3))
225 def f(): pass
227 print 'hex'
228 if hex(16) != '0x10': raise TestFailed, 'hex(16)'
229 if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
230 if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
231 if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
232 raise TestFailed, 'hex(-16)'
233 if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
235 print 'id'
236 id(None)
237 id(1)
238 id(1L)
239 id(1.0)
240 id('spam')
241 id((0,1,2,3))
242 id([0,1,2,3])
243 id({'spam': 1, 'eggs': 2, 'ham': 3})
245 # Test input() later, together with raw_input
247 print 'int'
248 if int(314) <> 314: raise TestFailed, 'int(314)'
249 if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
250 if int(314L) <> 314: raise TestFailed, 'int(314L)'
251 # Check that conversion from float truncates towards zero
252 if int(-3.14) <> -3: raise TestFailed, 'int(-3.14)'
253 if int(3.9) <> 3: raise TestFailed, 'int(3.9)'
254 if int(-3.9) <> -3: raise TestFailed, 'int(-3.9)'
255 if int(3.5) <> 3: raise TestFailed, 'int(3.5)'
256 if int(-3.5) <> -3: raise TestFailed, 'int(-3.5)'
257 # Test conversion fron strings and various anomalies
258 L = [
259 ('0', 0),
260 ('1', 1),
261 ('9', 9),
262 ('10', 10),
263 ('99', 99),
264 ('100', 100),
265 ('314', 314),
266 (' 314', 314),
267 ('314 ', 314),
268 (' \t\t 314 \t\t ', 314),
269 (`sys.maxint`, sys.maxint),
270 ('', ValueError),
271 (' ', ValueError),
272 (' \t\t ', ValueError),
274 for s, v in L:
275 for sign in "", "+", "-":
276 for prefix in "", " ", "\t", " \t\t ":
277 ss = prefix + sign + s
278 vv = v
279 if sign == "-" and v is not ValueError:
280 vv = -v
281 try:
282 if int(ss) != vv:
283 raise TestFailed, "int(%s)" % `ss`
284 except v:
285 pass
286 except ValueError, e:
287 raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
288 s = `-1-sys.maxint`
289 if int(s)+1 != -sys.maxint:
290 raise TestFailed, "int(%s)" % `s`
291 try:
292 int(s[1:])
293 except ValueError:
294 pass
295 else:
296 raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
298 print 'isinstance'
299 class C:
300 pass
301 class D(C):
302 pass
303 class E:
304 pass
305 c = C()
306 d = D()
307 e = E()
308 if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
309 if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
310 if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
311 if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
312 if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
313 try:
314 isinstance(E, 'foo')
315 raise TestFailed, 'isinstance(E, "foo")'
316 except TypeError:
317 pass
319 print 'issubclass'
320 if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
321 if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
322 if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
323 try:
324 issubclass('foo', E)
325 raise TestFailed, 'issubclass("foo", E)'
326 except TypeError:
327 pass
328 try:
329 issubclass(E, 'foo')
330 raise TestFailed, 'issubclass(E, "foo")'
331 except TypeError:
332 pass
334 print 'len'
335 if len('123') <> 3: raise TestFailed, 'len(\'123\')'
336 if len(()) <> 0: raise TestFailed, 'len(())'
337 if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
338 if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
339 if len({}) <> 0: raise TestFailed, 'len({})'
340 if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
342 print 'long'
343 if long(314) <> 314L: raise TestFailed, 'long(314)'
344 if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
345 if long(314L) <> 314L: raise TestFailed, 'long(314L)'
346 # Check that conversion from float truncates towards zero
347 if long(-3.14) <> -3L: raise TestFailed, 'long(-3.14)'
348 if long(3.9) <> 3L: raise TestFailed, 'long(3.9)'
349 if long(-3.9) <> -3L: raise TestFailed, 'long(-3.9)'
350 if long(3.5) <> 3L: raise TestFailed, 'long(3.5)'
351 if long(-3.5) <> -3L: raise TestFailed, 'long(-3.5)'
352 # Check conversions from string (same test set as for int(), and then some)
353 LL = [
354 ('1' + '0'*20, 10L**20),
355 ('1' + '0'*100, 10L**100),
357 for s, v in L + LL:
358 for sign in "", "+", "-":
359 for prefix in "", " ", "\t", " \t\t ":
360 ss = prefix + sign + s
361 vv = v
362 if sign == "-" and v is not ValueError:
363 vv = -v
364 try:
365 if long(ss) != long(vv):
366 raise TestFailed, "int(%s)" % `ss`
367 except v:
368 pass
369 except ValueError, e:
370 raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
372 print 'map'
373 if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
374 raise TestFailed, 'map(None, \'hello world\')'
375 if map(None, 'abcd', 'efg') <> \
376 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
377 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
378 if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
379 raise TestFailed, 'map(None, range(10))'
380 if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
381 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
382 try:
383 from math import sqrt
384 except ImportError:
385 def sqrt(x):
386 return pow(x, 0.5)
387 if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
388 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
389 if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
390 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
391 def plus(*v):
392 accu = 0
393 for i in v: accu = accu + i
394 return accu
395 if map(plus, [1, 3, 7]) <> [1, 3, 7]:
396 raise TestFailed, 'map(plus, [1, 3, 7])'
397 if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
398 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
399 if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
400 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
401 if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
402 raise TestFailed, 'map(None, Squares(10))'
403 if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
404 raise TestFailed, 'map(int, Squares(10))'
405 if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
406 raise TestFailed, 'map(None, Squares(3), Squares(2))'
407 if map(max, Squares(3), Squares(2)) != [0, 1, None]:
408 raise TestFailed, 'map(max, Squares(3), Squares(2))'
410 print 'max'
411 if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
412 if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
413 if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
414 if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
416 if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
417 if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
418 if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
420 print 'min'
421 if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
422 if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
423 if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
424 if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
426 if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
427 if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
428 if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'