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