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)'
27 except TypeError: pass
28 else: raise TestFailed
, 'abs("a")'
32 if args
!= (): raise TestFailed
, 'f0 called with ' + `args`
34 if a1
!= 1: raise TestFailed
, 'f1 called with ' + `a1`
36 if a1
!= 1 or a2
!= 2:
37 raise TestFailed
, 'f2 called with ' + `a1
, a2`
39 if a1
!= 1 or a2
!= 2 or a3
!= 3:
40 raise TestFailed
, 'f3 called with ' + `a1
, a2
, a3`
46 # A PyCFunction that takes only positional parameters should allow an
47 # empty keyword dictionary to pass without a complaint, but raise a
48 # TypeError if the dictionary is non-empty.
51 apply(id, (1,), {"foo": 1})
55 raise TestFailed
, 'expected TypeError; no exception raised'
58 if not callable(len):raise TestFailed
, 'callable(len)'
60 if not callable(f
): raise TestFailed
, 'callable(f)'
63 if not callable(C
): raise TestFailed
, 'callable(C)'
65 if not callable(x
.meth
): raise TestFailed
, 'callable(x.meth)'
66 if callable(x
): raise TestFailed
, 'callable(x)'
68 def __call__(self
): pass
70 if not callable(y
): raise TestFailed
, 'callable(y)'
74 if chr(32) != ' ': raise TestFailed
, 'chr(32)'
75 if chr(65) != 'A': raise TestFailed
, 'chr(65)'
76 if chr(97) != 'a': raise TestFailed
, 'chr(97)'
80 if cmp(-1, 1) != -1: raise TestFailed
, 'cmp(-1, 1)'
81 if cmp(1, -1) != 1: raise TestFailed
, 'cmp(1, -1)'
82 if cmp(1, 1) != 0: raise TestFailed
, 'cmp(1, 1)'
83 # verify that circular objects are handled
86 from UserList
import UserList
87 c
= UserList(); c
.append(c
)
88 if cmp(a
, b
) != 0: raise TestFailed
, "cmp(%s, %s)" % (a
, b
)
89 if cmp(b
, c
) != 0: raise TestFailed
, "cmp(%s, %s)" % (b
, c
)
90 if cmp(c
, a
) != 0: raise TestFailed
, "cmp(%s, %s)" % (c
, a
)
91 if cmp(a
, c
) != 0: raise TestFailed
, "cmp(%s, %s)" % (a
, c
)
92 # okay, now break the cycles
93 a
.pop(); b
.pop(); c
.pop()
96 if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed
, 'coerce(1, 1.1)'
97 if coerce(1, 1L) != (1L, 1L): raise TestFailed
, 'coerce(1, 1L)'
98 if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed
, 'coerce(1L, 1.1)'
101 compile('print 1\n', '', 'exec')
104 if complex(1,10) != 1+10j
: raise TestFailed
, 'complex(1,10)'
105 if complex(1,10L) != 1+10j
: raise TestFailed
, 'complex(1,10L)'
106 if complex(1,10.0) != 1+10j
: raise TestFailed
, 'complex(1,10.0)'
107 if complex(1L,10) != 1+10j
: raise TestFailed
, 'complex(1L,10)'
108 if complex(1L,10L) != 1+10j
: raise TestFailed
, 'complex(1L,10L)'
109 if complex(1L,10.0) != 1+10j
: raise TestFailed
, 'complex(1L,10.0)'
110 if complex(1.0,10) != 1+10j
: raise TestFailed
, 'complex(1.0,10)'
111 if complex(1.0,10L) != 1+10j
: raise TestFailed
, 'complex(1.0,10L)'
112 if complex(1.0,10.0) != 1+10j
: raise TestFailed
, 'complex(1.0,10.0)'
113 if complex(3.14+0j
) != 3.14+0j
: raise TestFailed
, 'complex(3.14)'
114 if complex(3.14) != 3.14+0j
: raise TestFailed
, 'complex(3.14)'
115 if complex(314) != 314.0+0j
: raise TestFailed
, 'complex(314)'
116 if complex(314L) != 314.0+0j
: raise TestFailed
, 'complex(314L)'
117 if complex(3.14+0j
, 0j
) != 3.14+0j
: raise TestFailed
, 'complex(3.14, 0j)'
118 if complex(3.14, 0.0) != 3.14+0j
: raise TestFailed
, 'complex(3.14, 0.0)'
119 if complex(314, 0) != 314.0+0j
: raise TestFailed
, 'complex(314, 0)'
120 if complex(314L, 0L) != 314.0+0j
: raise TestFailed
, 'complex(314L, 0L)'
121 if complex(0j
, 3.14j
) != -3.14+0j
: raise TestFailed
, 'complex(0j, 3.14j)'
122 if complex(0.0, 3.14j
) != -3.14+0j
: raise TestFailed
, 'complex(0.0, 3.14j)'
123 if complex(0j
, 3.14) != 3.14j
: raise TestFailed
, 'complex(0j, 3.14)'
124 if complex(0.0, 3.14) != 3.14j
: raise TestFailed
, 'complex(0.0, 3.14)'
125 if complex("1") != 1+0j
: raise TestFailed
, 'complex("1")'
126 if complex("1j") != 1j
: raise TestFailed
, 'complex("1j")'
127 try: complex("1", "1")
128 except TypeError: pass
129 else: raise TestFailed
, 'complex("1", "1")'
131 except TypeError: pass
132 else: raise TestFailed
, 'complex(1, "1")'
133 if complex(" 3.14+J ") != 3.14+1j
: raise TestFailed
, 'complex(" 3.14+J )"'
135 if complex(unicode(" 3.14+J ")) != 3.14+1j
:
136 raise TestFailed
, 'complex(u" 3.14+J )"'
138 def __complex__(self
): return 3.14j
140 if complex(z
) != 3.14j
: raise TestFailed
, 'complex(classinstance)'
149 if 'x' not in dir(): raise TestFailed
, 'dir()'
151 if 'modules' not in dir(sys
): raise TestFailed
, 'dir(sys)'
154 if divmod(12, 7) != (1, 5): raise TestFailed
, 'divmod(12, 7)'
155 if divmod(-12, 7) != (-2, 2): raise TestFailed
, 'divmod(-12, 7)'
156 if divmod(12, -7) != (-2, -2): raise TestFailed
, 'divmod(12, -7)'
157 if divmod(-12, -7) != (1, -5): raise TestFailed
, 'divmod(-12, -7)'
159 if divmod(12L, 7L) != (1L, 5L): raise TestFailed
, 'divmod(12L, 7L)'
160 if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed
, 'divmod(-12L, 7L)'
161 if divmod(12L, -7L) != (-2L, -2L): raise TestFailed
, 'divmod(12L, -7L)'
162 if divmod(-12L, -7L) != (1L, -5L): raise TestFailed
, 'divmod(-12L, -7L)'
164 if divmod(12, 7L) != (1, 5L): raise TestFailed
, 'divmod(12, 7L)'
165 if divmod(-12, 7L) != (-2, 2L): raise TestFailed
, 'divmod(-12, 7L)'
166 if divmod(12L, -7) != (-2L, -2): raise TestFailed
, 'divmod(12L, -7)'
167 if divmod(-12L, -7) != (1L, -5): raise TestFailed
, 'divmod(-12L, -7)'
169 if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
170 raise TestFailed
, 'divmod(3.25, 1.0)'
171 if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
172 raise TestFailed
, 'divmod(-3.25, 1.0)'
173 if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
174 raise TestFailed
, 'divmod(3.25, -1.0)'
175 if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
176 raise TestFailed
, 'divmod(-3.25, -1.0)'
179 if eval('1+1') != 2: raise TestFailed
, 'eval(\'1+1\')'
180 if eval(' 1+1\n') != 2: raise TestFailed
, 'eval(\' 1+1\\n\')'
181 globals = {'a': 1, 'b': 2}
182 locals = {'b': 200, 'c': 300}
183 if eval('a', globals) != 1:
184 raise TestFailed
, "eval(1) == %s" % eval('a', globals)
185 if eval('a', globals, locals) != 1:
186 raise TestFailed
, "eval(2)"
187 if eval('b', globals, locals) != 200:
188 raise TestFailed
, "eval(3)"
189 if eval('c', globals, locals) != 300:
190 raise TestFailed
, "eval(4)"
192 if eval(unicode('1+1')) != 2: raise TestFailed
, 'eval(u\'1+1\')'
193 if eval(unicode(' 1+1\n')) != 2: raise TestFailed
, 'eval(u\' 1+1\\n\')'
194 globals = {'a': 1, 'b': 2}
195 locals = {'b': 200, 'c': 300}
197 if eval(unicode('a'), globals) != 1:
198 raise TestFailed
, "eval(1) == %s" % eval(unicode('a'), globals)
199 if eval(unicode('a'), globals, locals) != 1:
200 raise TestFailed
, "eval(2)"
201 if eval(unicode('b'), globals, locals) != 200:
202 raise TestFailed
, "eval(3)"
203 if eval(unicode('c'), globals, locals) != 300:
204 raise TestFailed
, "eval(4)"
208 f
= open(TESTFN
, 'w')
213 if z
!= 2: raise TestFailed
, "execfile(1)"
215 execfile(TESTFN
, globals)
216 if globals['z'] != 2: raise TestFailed
, "execfile(1)"
218 execfile(TESTFN
, globals, locals)
219 if locals['z'] != 2: raise TestFailed
, "execfile(1)"
223 if filter(lambda c
: 'a' <= c
<= 'z', 'Hello World') != 'elloorld':
224 raise TestFailed
, 'filter (filter a string)'
225 if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
226 raise TestFailed
, 'filter (remove false values)'
227 if filter(lambda x
: x
> 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
228 raise TestFailed
, 'filter (keep positives)'
230 def __init__(self
, max):
233 def __len__(self
): return len(self
.sofar
)
234 def __getitem__(self
, i
):
235 if not 0 <= i
< self
.max: raise IndexError
238 self
.sofar
.append(n
*n
)
241 if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
242 raise TestFailed
, 'filter(None, Squares(10))'
243 if filter(lambda x
: x
%2, Squares(10)) != [1, 9, 25, 49, 81]:
244 raise TestFailed
, 'filter(oddp, Squares(10))'
246 def __init__(self
, max):
250 return len(self
.sofar
)
251 def __getitem__(self
, i
):
252 if not 0 <= i
< self
.max:
256 self
.sofar
.append(str(n
*n
))
261 filter(identity
, Squares(5))
264 if float(3.14) != 3.14: raise TestFailed
, 'float(3.14)'
265 if float(314) != 314.0: raise TestFailed
, 'float(314)'
266 if float(314L) != 314.0: raise TestFailed
, 'float(314L)'
267 if float(" 3.14 ") != 3.14: raise TestFailed
, 'float(" 3.14 ")'
269 if float(unicode(" 3.14 ")) != 3.14:
270 raise TestFailed
, 'float(u" 3.14 ")'
271 if float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')) != 3.14:
272 raise TestFailed
, 'float(u" \u0663.\u0661\u0664 ")'
276 if getattr(sys
, 'stdout') is not sys
.stdout
: raise TestFailed
, 'getattr'
282 raise TestFailed
, "getattr(sys, 1) should raise an exception"
284 getattr(sys
, 1, "foo")
288 raise TestFailed
, 'getattr(sys, 1, "foo") should raise an exception'
292 if not hasattr(sys
, 'stdout'): raise TestFailed
, 'hasattr'
298 raise TestFailed
, "hasattr(sys, 1) should raise an exception"
302 if not hash(1) == hash(1L) == hash(1.0): raise TestFailed
, 'numeric hash()'
307 except TypeError: pass
308 else: raise TestFailed
, "hash([]) should raise an exception"
310 except TypeError: pass
311 else: raise TestFailed
, "hash({}) should raise an exception"
314 if hex(16) != '0x10': raise TestFailed
, 'hex(16)'
315 if hex(16L) != '0x10L': raise TestFailed
, 'hex(16L)'
316 if len(hex(-1)) != len(hex(sys
.maxint
)): raise TestFailed
, 'len(hex(-1))'
317 if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
318 raise TestFailed
, 'hex(-16)'
319 if hex(-16L) != '-0x10L': raise TestFailed
, 'hex(-16L)'
329 id({'spam': 1, 'eggs': 2, 'ham': 3})
331 # Test input() later, together with raw_input
334 if int(314) != 314: raise TestFailed
, 'int(314)'
335 if int(3.14) != 3: raise TestFailed
, 'int(3.14)'
336 if int(314L) != 314: raise TestFailed
, 'int(314L)'
337 # Check that conversion from float truncates towards zero
338 if int(-3.14) != -3: raise TestFailed
, 'int(-3.14)'
339 if int(3.9) != 3: raise TestFailed
, 'int(3.9)'
340 if int(-3.9) != -3: raise TestFailed
, 'int(-3.9)'
341 if int(3.5) != 3: raise TestFailed
, 'int(3.5)'
342 if int(-3.5) != -3: raise TestFailed
, 'int(-3.5)'
344 if int("10",16) != 16L: raise TestFailed
, 'int("10",16)'
346 if int(unicode("10"),16) != 16L:
347 raise TestFailed
, 'int(u"10",16)'
348 # Test conversion from strings and various anomalies
359 (' \t\t 314 \t\t ', 314),
360 (`sys
.maxint`
, sys
.maxint
),
363 (' 1\02 ', ValueError),
366 (' \t\t ', ValueError)
375 (unicode('100'), 100),
376 (unicode('314'), 314),
377 (unicode(' 314'), 314),
378 (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
379 (unicode(' \t\t 314 \t\t '), 314),
380 (unicode(' 1x'), ValueError),
382 (unicode(' 1\02 '), ValueError),
383 (unicode(''), ValueError),
384 (unicode(' '), ValueError),
385 (unicode(' \t\t '), ValueError),
388 for sign
in "", "+", "-":
389 for prefix
in "", " ", "\t", " \t\t ":
390 ss
= prefix
+ sign
+ s
392 if sign
== "-" and v
is not ValueError:
396 raise TestFailed
, "int(%s)" % `ss`
399 except ValueError, e
:
400 raise TestFailed
, "int(%s) raised ValueError: %s" % (`ss`
, e
)
402 if int(s
)+1 != -sys
.maxint
:
403 raise TestFailed
, "int(%s)" % `s`
409 raise TestFailed
, "int(%s)" % `s
[1:]`
+ " should raise ValueError"
412 except OverflowError:
415 raise TestFailed("int(1e100) expected OverflowError")
418 except OverflowError:
421 raise TestFailed("int(-1e100) expected OverflowError")
424 # SF bug 434186: 0x80000000/2 != 0x80000000>>1.
425 # Worked by accident in Windows release build, but failed in debug build.
426 # Failed in all Linux builds.
429 raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
432 except ValueError: pass
433 else: raise TestFailed("int('123\0') didn't raise exception")
445 if not isinstance(c
, C
): raise TestFailed
, 'isinstance(c, C)'
446 if not isinstance(d
, C
): raise TestFailed
, 'isinstance(d, C)'
447 if isinstance(e
, C
): raise TestFailed
, 'isinstance(e, C)'
448 if isinstance(c
, D
): raise TestFailed
, 'isinstance(c, D)'
449 if isinstance('foo', E
): raise TestFailed
, 'isinstance("Foo", E)'
452 raise TestFailed
, 'isinstance(E, "foo")'
457 if not issubclass(D
, C
): raise TestFailed
, 'issubclass(D, C)'
458 if not issubclass(C
, C
): raise TestFailed
, 'issubclass(C, C)'
459 if issubclass(C
, D
): raise TestFailed
, 'issubclass(C, D)'
462 raise TestFailed
, 'issubclass("foo", E)'
467 raise TestFailed
, 'issubclass(E, "foo")'
472 if len('123') != 3: raise TestFailed
, 'len(\'123\')'
473 if len(()) != 0: raise TestFailed
, 'len(())'
474 if len((1, 2, 3, 4)) != 4: raise TestFailed
, 'len((1, 2, 3, 4))'
475 if len([1, 2, 3, 4]) != 4: raise TestFailed
, 'len([1, 2, 3, 4])'
476 if len({}) != 0: raise TestFailed
, 'len({})'
477 if len({'a':1, 'b': 2}) != 2: raise TestFailed
, 'len({\'a\':1, \'b\': 2})'
480 if list([]) != []: raise TestFailed
, 'list([])'
482 l0_3_bis
= list(l0_3
)
483 if l0_3
!= l0_3_bis
or l0_3
is l0_3_bis
: raise TestFailed
, 'list([0, 1, 2, 3])'
484 if list(()) != []: raise TestFailed
, 'list(())'
485 if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed
, 'list((0, 1, 2, 3))'
486 if list('') != []: raise TestFailed
, 'list('')'
487 if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed
, "list('spam')"
490 if long(314) != 314L: raise TestFailed
, 'long(314)'
491 if long(3.14) != 3L: raise TestFailed
, 'long(3.14)'
492 if long(314L) != 314L: raise TestFailed
, 'long(314L)'
493 # Check that conversion from float truncates towards zero
494 if long(-3.14) != -3L: raise TestFailed
, 'long(-3.14)'
495 if long(3.9) != 3L: raise TestFailed
, 'long(3.9)'
496 if long(-3.9) != -3L: raise TestFailed
, 'long(-3.9)'
497 if long(3.5) != 3L: raise TestFailed
, 'long(3.5)'
498 if long(-3.5) != -3L: raise TestFailed
, 'long(-3.5)'
499 if long("-3") != -3L: raise TestFailed
, 'long("-3")'
501 if long(unicode("-3")) != -3L:
502 raise TestFailed
, 'long(u"-3")'
504 if long("10",16) != 16L: raise TestFailed
, 'long("10",16)'
506 if long(unicode("10"),16) != 16L:
507 raise TestFailed
, 'long(u"10",16)'
508 # Check conversions from string (same test set as for int(), and then some)
510 ('1' + '0'*20, 10L**20),
511 ('1' + '0'*100, 10L**100)
515 (unicode('1') + unicode('0')*20, 10L**20),
516 (unicode('1') + unicode('0')*100, 10L**100),
519 for sign
in "", "+", "-":
520 for prefix
in "", " ", "\t", " \t\t ":
521 ss
= prefix
+ sign
+ s
523 if sign
== "-" and v
is not ValueError:
526 if long(ss
) != long(vv
):
527 raise TestFailed
, "long(%s)" % `ss`
530 except ValueError, e
:
531 raise TestFailed
, "long(%s) raised ValueError: %s" % (`ss`
, e
)
534 except ValueError: pass
535 else: raise TestFailed("long('123\0') didn't raise exception")
538 if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
539 raise TestFailed
, 'map(None, \'hello world\')'
540 if map(None, 'abcd', 'efg') != \
541 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
542 raise TestFailed
, 'map(None, \'abcd\', \'efg\')'
543 if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
544 raise TestFailed
, 'map(None, range(10))'
545 if map(lambda x
: x
*x
, range(1,4)) != [1, 4, 9]:
546 raise TestFailed
, 'map(lambda x: x*x, range(1,4))'
548 from math
import sqrt
552 if map(lambda x
: map(sqrt
,x
), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
553 raise TestFailed
, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
554 if map(lambda x
, y
: x
+y
, [1,3,2], [9,1,4]) != [10, 4, 6]:
555 raise TestFailed
, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
558 for i
in v
: accu
= accu
+ i
560 if map(plus
, [1, 3, 7]) != [1, 3, 7]:
561 raise TestFailed
, 'map(plus, [1, 3, 7])'
562 if map(plus
, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
563 raise TestFailed
, 'map(plus, [1, 3, 7], [4, 9, 2])'
564 if map(plus
, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
565 raise TestFailed
, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
566 if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
567 raise TestFailed
, 'map(None, Squares(10))'
568 if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
569 raise TestFailed
, 'map(int, Squares(10))'
570 if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
571 raise TestFailed
, 'map(None, Squares(3), Squares(2))'
572 if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
573 raise TestFailed
, 'map(max, Squares(3), Squares(2))'
576 if max('123123') != '3': raise TestFailed
, 'max(\'123123\')'
577 if max(1, 2, 3) != 3: raise TestFailed
, 'max(1, 2, 3)'
578 if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed
, 'max((1, 2, 3, 1, 2, 3))'
579 if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed
, 'max([1, 2, 3, 1, 2, 3])'
581 if max(1, 2L, 3.0) != 3.0: raise TestFailed
, 'max(1, 2L, 3.0)'
582 if max(1L, 2.0, 3) != 3: raise TestFailed
, 'max(1L, 2.0, 3)'
583 if max(1.0, 2, 3L) != 3L: raise TestFailed
, 'max(1.0, 2, 3L)'
586 if min('123123') != '1': raise TestFailed
, 'min(\'123123\')'
587 if min(1, 2, 3) != 1: raise TestFailed
, 'min(1, 2, 3)'
588 if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed
, 'min((1, 2, 3, 1, 2, 3))'
589 if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed
, 'min([1, 2, 3, 1, 2, 3])'
591 if min(1, 2L, 3.0) != 1: raise TestFailed
, 'min(1, 2L, 3.0)'
592 if min(1L, 2.0, 3) != 1L: raise TestFailed
, 'min(1L, 2.0, 3)'
593 if min(1.0, 2, 3L) != 1.0: raise TestFailed
, 'min(1.0, 2, 3L)'