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)'
97 if complex(" 3.14+J ") <> 3.14+1j
: raise TestFailed
, 'complex(" 3.14+J )"'
99 def __complex__(self
): return 3.14j
101 if complex(z
) <> 3.14j
: raise TestFailed
, 'complex(classinstance)'
110 if 'x' not in dir(): raise TestFailed
, 'dir()'
112 if 'modules' not in dir(sys
): raise TestFailed
, 'dir(sys)'
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)'
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)"
151 f
= open(TESTFN
, 'w')
156 if z
<> 2: raise TestFailed
, "execfile(1)"
158 execfile(TESTFN
, globals)
159 if globals['z'] <> 2: raise TestFailed
, "execfile(1)"
161 execfile(TESTFN
, globals, locals)
162 if locals['z'] <> 2: raise TestFailed
, "execfile(1)"
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)'
173 def __init__(self
, max):
176 def __len__(self
): return len(self
.sofar
)
177 def __getitem__(self
, i
):
178 if not 0 <= i
< self
.max: raise IndexError
181 self
.sofar
.append(n
*n
)
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))'
189 def __init__(self
, max):
193 return len(self
.sofar
)
194 def __getitem__(self
, i
):
195 if not 0 <= i
< self
.max:
199 self
.sofar
.append(str(n
*n
))
204 filter(identity
, Squares(5))
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 ")'
214 if getattr(sys
, 'stdout') is not sys
.stdout
: raise TestFailed
, 'getattr'
218 if not hasattr(sys
, 'stdout'): raise TestFailed
, 'hasattr'
222 if not hash(1) == hash(1L) == hash(1.0): raise TestFailed
, 'numeric hash()'
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)'
243 id({'spam': 1, 'eggs': 2, 'ham': 3})
245 # Test input() later, together with raw_input
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
268 (' \t\t 314 \t\t ', 314),
269 (`sys
.maxint`
, sys
.maxint
),
272 (' \t\t ', ValueError),
275 for sign
in "", "+", "-":
276 for prefix
in "", " ", "\t", " \t\t ":
277 ss
= prefix
+ sign
+ s
279 if sign
== "-" and v
is not ValueError:
283 raise TestFailed
, "int(%s)" % `ss`
286 except ValueError, e
:
287 raise TestFailed
, "int(%s) raised ValueError: %s" % (`ss`
, e
)
289 if int(s
)+1 != -sys
.maxint
:
290 raise TestFailed
, "int(%s)" % `s`
296 raise TestFailed
, "int(%s)" % `s
[1:]`
+ " should raise ValueError"
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)'
315 raise TestFailed
, 'isinstance(E, "foo")'
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)'
325 raise TestFailed
, 'issubclass("foo", E)'
330 raise TestFailed
, 'issubclass(E, "foo")'
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})'
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)
354 ('1' + '0'*20, 10L**20),
355 ('1' + '0'*100, 10L**100),
358 for sign
in "", "+", "-":
359 for prefix
in "", " ", "\t", " \t\t ":
360 ss
= prefix
+ sign
+ s
362 if sign
== "-" and v
is not ValueError:
365 if long(ss
) != long(vv
):
366 raise TestFailed
, "int(%s)" % `ss`
369 except ValueError, e
:
370 raise TestFailed
, "int(%s) raised ValueError: %s" % (`ss`
, e
)
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))'
383 from math
import sqrt
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])'
393 for i
in v
: accu
= accu
+ i
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))'
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)'
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)'