1 # Python test set -- part 4a, built-in functions a-m
3 from test_support
import *
9 try: __import__('spamspam')
10 except ImportError: pass
11 else: raise TestFailed
, "__import__('spamspam') should fail"
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)'
28 if args
!= (): raise TestFailed
, 'f0 called with ' + `args`
30 if a1
!= 1: raise TestFailed
, 'f1 called with ' + `a1`
32 if a1
!= 1 or a2
!= 2:
33 raise TestFailed
, 'f2 called with ' + `a1
, a2`
35 if a1
!= 1 or a2
!= 2 or a3
!= 3:
36 raise TestFailed
, 'f3 called with ' + `a1
, a2
, a3`
43 if not callable(len):raise TestFailed
, 'callable(len)'
45 if not callable(f
): raise TestFailed
, 'callable(f)'
48 if not callable(C
): raise TestFailed
, 'callable(C)'
50 if not callable(x
.meth
): raise TestFailed
, 'callable(x.meth)'
51 if callable(x
): raise TestFailed
, 'callable(x)'
53 def __call__(self
): pass
55 if not callable(y
): raise TestFailed
, 'callable(y)'
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)'
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)'
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)'
73 compile('print 1\n', '', 'exec')
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)'
98 def __complex__(self
): return 3.14j
100 if complex(z
) <> 3.14j
: raise TestFailed
, 'complex(classinstance)'
109 if 'x' not in dir(): raise TestFailed
, 'dir()'
111 if 'modules' not in dir(sys
): raise TestFailed
, 'dir(sys)'
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)'
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)"
150 f
= open(TESTFN
, 'w')
155 if z
<> 2: raise TestFailed
, "execfile(1)"
157 execfile(TESTFN
, globals)
158 if globals['z'] <> 2: raise TestFailed
, "execfile(1)"
160 execfile(TESTFN
, globals, locals)
161 if locals['z'] <> 2: raise TestFailed
, "execfile(1)"
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)'
172 def __init__(self
, max):
175 def __len__(self
): return len(self
.sofar
)
176 def __getitem__(self
, i
):
177 if not 0 <= i
< self
.max: raise IndexError
180 self
.sofar
.append(n
*n
)
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))'
188 def __init__(self
, max):
192 return len(self
.sofar
)
193 def __getitem__(self
, i
):
194 if not 0 <= i
< self
.max:
198 self
.sofar
.append(str(n
*n
))
203 filter(identity
, Squares(5))
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)'
212 if getattr(sys
, 'stdout') is not sys
.stdout
: raise TestFailed
, 'getattr'
216 if not hasattr(sys
, 'stdout'): raise TestFailed
, 'hasattr'
220 if not hash(1) == hash(1L) == hash(1.0): raise TestFailed
, 'numeric hash()'
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)'
241 id({'spam': 1, 'eggs': 2, 'ham': 3})
243 # Test input() later, together with raw_input
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
266 (' \t\t 314 \t\t ', 314),
267 (`sys
.maxint`
, sys
.maxint
),
270 (' \t\t ', ValueError),
273 for sign
in "", "+", "-":
274 for prefix
in "", " ", "\t", " \t\t ":
275 ss
= prefix
+ sign
+ s
277 if sign
== "-" and v
is not ValueError:
281 raise TestFailed
, "int(%s)" % `ss`
284 except ValueError, e
:
285 raise TestFailed
, "int(%s) raised ValueError: %s" % (`ss`
, e
)
287 if int(s
)+1 != -sys
.maxint
:
288 raise TestFailed
, "int(%s)" % `s`
294 raise TestFailed
, "int(%s)" % `s
[1:]`
+ " should raise ValueError"
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)'
313 raise TestFailed
, 'isinstance(E, "foo")'
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)'
323 raise TestFailed
, 'issubclass("foo", E)'
328 raise TestFailed
, 'issubclass(E, "foo")'
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})'
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)
352 ('1' + '0'*20, 10L**20),
353 ('1' + '0'*100, 10L**100),
356 for sign
in "", "+", "-":
357 for prefix
in "", " ", "\t", " \t\t ":
358 ss
= prefix
+ sign
+ s
360 if sign
== "-" and v
is not ValueError:
363 if long(ss
) != long(vv
):
364 raise TestFailed
, "int(%s)" % `ss`
367 except ValueError, e
:
368 raise TestFailed
, "int(%s) raised ValueError: %s" % (`ss`
, e
)
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))'
381 from math
import sqrt
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])'
391 for i
in v
: accu
= accu
+ i
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))'
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)'
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)'