Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Lib / test / test_b1.py
blobd9bc04d52a0c68f352e043a979bcf73fe73ebdb1
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)'
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)
75 # okay, now break the cycles
76 a.pop(); b.pop(); c.pop()
78 print 'coerce'
79 if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
80 if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
81 if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
83 print 'compile'
84 compile('print 1\n', '', 'exec')
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)'
108 if complex(" 3.14+J ") <> 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"'
109 if complex(u" 3.14+J ") <> 3.14+1j: raise TestFailed, 'complex(u" 3.14+J )"'
110 class Z:
111 def __complex__(self): return 3.14j
112 z = Z()
113 if complex(z) <> 3.14j: raise TestFailed, 'complex(classinstance)'
115 print 'delattr'
116 import sys
117 sys.spam = 1
118 delattr(sys, 'spam')
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)'
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)'
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)'
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)'
142 if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
143 raise TestFailed, 'divmod(3.25, 1.0)'
144 if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
145 raise TestFailed, 'divmod(-3.25, 1.0)'
146 if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
147 raise TestFailed, 'divmod(3.25, -1.0)'
148 if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
149 raise TestFailed, 'divmod(-3.25, -1.0)'
151 print 'eval'
152 if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
153 if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
154 globals = {'a': 1, 'b': 2}
155 locals = {'b': 200, 'c': 300}
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)"
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)
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)"
179 unlink(TESTFN)
181 print 'filter'
182 if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
183 raise TestFailed, 'filter (filter a string)'
184 if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
185 raise TestFailed, 'filter (remove false values)'
186 if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
187 raise TestFailed, 'filter (keep positives)'
188 class Squares:
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]
200 if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
201 raise TestFailed, 'filter(None, Squares(10))'
202 if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
203 raise TestFailed, 'filter(oddp, Squares(10))'
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))
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)'
226 if float(" 3.14 ") <> 3.14: raise TestFailed, 'float(" 3.14 ")'
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 ")'
231 print 'getattr'
232 import sys
233 if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
235 print 'hasattr'
236 import sys
237 if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
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
246 print 'hex'
247 if hex(16) != '0x10': raise TestFailed, 'hex(16)'
248 if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
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)'
252 if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
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})
264 # Test input() later, together with raw_input
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)'
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)'
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)'
279 # Test conversion from strings and various anomalies
280 L = [
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),
292 (' 1x', ValueError),
293 (' 1 ', 1),
294 (' 1\02 ', ValueError),
295 ('', ValueError),
296 (' ', ValueError),
297 (' \t\t ', ValueError),
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),
315 for s, v in L:
316 for sign in "", "+", "-":
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)
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"
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
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
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})'
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)'
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)'
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)'
398 # Check conversions from string (same test set as for int(), and then some)
399 LL = [
400 ('1' + '0'*20, 10L**20),
401 ('1' + '0'*100, 10L**100),
402 (u'1' + u'0'*20, 10L**20),
403 (u'1' + u'0'*100, 10L**100),
405 for s, v in L + LL:
406 for sign in "", "+", "-":
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):
414 raise TestFailed, "long(%s)" % `ss`
415 except v:
416 pass
417 except ValueError, e:
418 raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
420 print 'map'
421 if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
422 raise TestFailed, 'map(None, \'hello world\')'
423 if map(None, 'abcd', 'efg') <> \
424 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
425 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
426 if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
427 raise TestFailed, 'map(None, range(10))'
428 if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
429 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
430 try:
431 from math import sqrt
432 except ImportError:
433 def sqrt(x):
434 return pow(x, 0.5)
435 if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
436 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
437 if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
438 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
439 def plus(*v):
440 accu = 0
441 for i in v: accu = accu + i
442 return accu
443 if map(plus, [1, 3, 7]) <> [1, 3, 7]:
444 raise TestFailed, 'map(plus, [1, 3, 7])'
445 if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
446 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
447 if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
448 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
449 if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
450 raise TestFailed, 'map(None, Squares(10))'
451 if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
452 raise TestFailed, 'map(int, Squares(10))'
453 if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
454 raise TestFailed, 'map(None, Squares(3), Squares(2))'
455 if map(max, Squares(3), Squares(2)) != [0, 1, None]:
456 raise TestFailed, 'map(max, Squares(3), Squares(2))'
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])'
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)'
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])'
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)'