1992-01-27 13:00:37 -04:00
|
|
|
# Python test set -- part 4a, built-in functions a-m
|
|
|
|
|
|
|
|
from test_support import *
|
|
|
|
|
1994-12-30 13:17:46 -04:00
|
|
|
print '__import__'
|
|
|
|
__import__('sys')
|
2000-09-01 03:53:52 -03:00
|
|
|
__import__('time')
|
1994-12-30 13:17:46 -04:00
|
|
|
__import__('string')
|
|
|
|
try: __import__('spamspam')
|
|
|
|
except ImportError: pass
|
|
|
|
else: raise TestFailed, "__import__('spamspam') should fail"
|
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'abs'
|
2000-12-12 19:11:42 -04:00
|
|
|
if abs(0) != 0: raise TestFailed, 'abs(0)'
|
|
|
|
if abs(1234) != 1234: raise TestFailed, 'abs(1234)'
|
|
|
|
if abs(-1234) != 1234: raise TestFailed, 'abs(-1234)'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if abs(0.0) != 0.0: raise TestFailed, 'abs(0.0)'
|
|
|
|
if abs(3.14) != 3.14: raise TestFailed, 'abs(3.14)'
|
|
|
|
if abs(-3.14) != 3.14: raise TestFailed, 'abs(-3.14)'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if abs(0L) != 0L: raise TestFailed, 'abs(0L)'
|
|
|
|
if abs(1234L) != 1234L: raise TestFailed, 'abs(1234L)'
|
|
|
|
if abs(-1234L) != 1234L: raise TestFailed, 'abs(-1234L)'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
|
|
|
print 'apply'
|
|
|
|
def f0(*args):
|
1998-08-10 17:12:34 -03:00
|
|
|
if args != (): raise TestFailed, 'f0 called with ' + `args`
|
1992-01-27 13:00:37 -04:00
|
|
|
def f1(a1):
|
1998-08-10 17:12:34 -03:00
|
|
|
if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
|
1992-01-27 13:00:37 -04:00
|
|
|
def f2(a1, a2):
|
1998-08-10 17:12:34 -03:00
|
|
|
if a1 != 1 or a2 != 2:
|
|
|
|
raise TestFailed, 'f2 called with ' + `a1, a2`
|
1992-01-27 13:00:37 -04:00
|
|
|
def f3(a1, a2, a3):
|
1998-08-10 17:12:34 -03:00
|
|
|
if a1 != 1 or a2 != 2 or a3 != 3:
|
|
|
|
raise TestFailed, 'f3 called with ' + `a1, a2, a3`
|
1992-01-27 13:00:37 -04:00
|
|
|
apply(f0, ())
|
|
|
|
apply(f1, (1,))
|
|
|
|
apply(f2, (1, 2))
|
|
|
|
apply(f3, (1, 2, 3))
|
|
|
|
|
2001-01-04 18:33:02 -04:00
|
|
|
# A PyCFunction that takes only positional parameters should allow an
|
|
|
|
# empty keyword dictionary to pass without a complaint, but raise a
|
|
|
|
# TypeError if the dictionary is non-empty.
|
|
|
|
apply(id, (1,), {})
|
|
|
|
try:
|
|
|
|
apply(id, (1,), {"foo": 1})
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed, 'expected TypeError; no exception raised'
|
|
|
|
|
1994-11-10 18:25:26 -04:00
|
|
|
print 'callable'
|
|
|
|
if not callable(len):raise TestFailed, 'callable(len)'
|
|
|
|
def f(): pass
|
|
|
|
if not callable(f): raise TestFailed, 'callable(f)'
|
|
|
|
class C:
|
1998-08-10 17:12:34 -03:00
|
|
|
def meth(self): pass
|
1994-11-10 18:25:26 -04:00
|
|
|
if not callable(C): raise TestFailed, 'callable(C)'
|
|
|
|
x = C()
|
|
|
|
if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
|
|
|
|
if callable(x): raise TestFailed, 'callable(x)'
|
|
|
|
class D(C):
|
1998-08-10 17:12:34 -03:00
|
|
|
def __call__(self): pass
|
1994-11-10 18:25:26 -04:00
|
|
|
y = D()
|
|
|
|
if not callable(y): raise TestFailed, 'callable(y)'
|
2001-01-03 19:53:31 -04:00
|
|
|
y()
|
1994-11-10 18:25:26 -04:00
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'chr'
|
2000-12-12 19:11:42 -04:00
|
|
|
if chr(32) != ' ': raise TestFailed, 'chr(32)'
|
|
|
|
if chr(65) != 'A': raise TestFailed, 'chr(65)'
|
|
|
|
if chr(97) != 'a': raise TestFailed, 'chr(97)'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
2001-01-03 21:33:41 -04:00
|
|
|
# cmp
|
1992-11-27 18:53:50 -04:00
|
|
|
print 'cmp'
|
2000-12-12 19:11:42 -04:00
|
|
|
if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)'
|
|
|
|
if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)'
|
|
|
|
if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)'
|
2000-04-14 16:13:24 -03:00
|
|
|
# verify that circular objects are handled
|
|
|
|
a = []; a.append(a)
|
|
|
|
b = []; b.append(b)
|
|
|
|
from UserList import UserList
|
|
|
|
c = UserList(); c.append(c)
|
|
|
|
if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b)
|
|
|
|
if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c)
|
|
|
|
if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a)
|
|
|
|
if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c)
|
2000-07-14 01:32:09 -03:00
|
|
|
# okay, now break the cycles
|
|
|
|
a.pop(); b.pop(); c.pop()
|
1992-11-27 18:53:50 -04:00
|
|
|
|
|
|
|
print 'coerce'
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
|
2000-12-12 19:11:42 -04:00
|
|
|
if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
|
1992-11-27 18:53:50 -04:00
|
|
|
|
1994-11-10 18:25:26 -04:00
|
|
|
print 'compile'
|
|
|
|
compile('print 1\n', '', 'exec')
|
|
|
|
|
1997-03-31 13:13:17 -04:00
|
|
|
print 'complex'
|
2000-12-12 19:11:42 -04:00
|
|
|
if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)'
|
|
|
|
if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)'
|
|
|
|
if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)'
|
|
|
|
if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)'
|
|
|
|
if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)'
|
|
|
|
if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)'
|
|
|
|
if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)'
|
|
|
|
if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)'
|
|
|
|
if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)'
|
|
|
|
if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)'
|
|
|
|
if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)'
|
|
|
|
if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)'
|
|
|
|
if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)'
|
|
|
|
if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)'
|
|
|
|
if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)'
|
|
|
|
if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)'
|
|
|
|
if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)'
|
|
|
|
if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)'
|
|
|
|
if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)'
|
|
|
|
if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)'
|
|
|
|
if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
|
|
|
|
if complex(" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"'
|
|
|
|
if complex(u" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(u" 3.14+J )"'
|
1997-03-31 13:13:17 -04:00
|
|
|
class Z:
|
|
|
|
def __complex__(self): return 3.14j
|
|
|
|
z = Z()
|
2000-12-12 19:11:42 -04:00
|
|
|
if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)'
|
1997-03-31 13:13:17 -04:00
|
|
|
|
1994-11-10 18:25:26 -04:00
|
|
|
print 'delattr'
|
|
|
|
import sys
|
|
|
|
sys.spam = 1
|
|
|
|
delattr(sys, 'spam')
|
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'dir'
|
|
|
|
x = 1
|
|
|
|
if 'x' not in dir(): raise TestFailed, 'dir()'
|
|
|
|
import sys
|
|
|
|
if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
|
|
|
|
|
|
|
|
print 'divmod'
|
2000-12-12 19:11:42 -04:00
|
|
|
if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)'
|
|
|
|
if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)'
|
|
|
|
if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)'
|
|
|
|
if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
|
|
|
|
if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
|
|
|
|
if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
|
|
|
|
if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)'
|
|
|
|
if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
|
|
|
|
if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
|
|
|
|
if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'divmod(3.25, 1.0)'
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'divmod(-3.25, 1.0)'
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'divmod(3.25, -1.0)'
|
1993-01-26 09:04:43 -04:00
|
|
|
if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'divmod(-3.25, -1.0)'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
|
|
|
print 'eval'
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')'
|
|
|
|
if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')'
|
1995-01-02 14:38:42 -04:00
|
|
|
globals = {'a': 1, 'b': 2}
|
|
|
|
locals = {'b': 200, 'c': 300}
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval('a', globals) != 1:
|
2000-08-23 18:11:05 -03:00
|
|
|
raise TestFailed, "eval(1) == %s" % eval('a', globals)
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval('a', globals, locals) != 1:
|
2000-08-23 18:11:05 -03:00
|
|
|
raise TestFailed, "eval(2)"
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval('b', globals, locals) != 200:
|
2000-08-23 18:11:05 -03:00
|
|
|
raise TestFailed, "eval(3)"
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval('c', globals, locals) != 300:
|
2000-08-23 18:11:05 -03:00
|
|
|
raise TestFailed, "eval(4)"
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval(u'1+1') != 2: raise TestFailed, 'eval(u\'1+1\')'
|
|
|
|
if eval(u' 1+1\n') != 2: raise TestFailed, 'eval(u\' 1+1\\n\')'
|
2000-09-19 18:04:18 -03:00
|
|
|
globals = {'a': 1, 'b': 2}
|
|
|
|
locals = {'b': 200, 'c': 300}
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval(u'a', globals) != 1:
|
2000-09-19 18:04:18 -03:00
|
|
|
raise TestFailed, "eval(1) == %s" % eval(u'a', globals)
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval(u'a', globals, locals) != 1:
|
2000-09-19 18:04:18 -03:00
|
|
|
raise TestFailed, "eval(2)"
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval(u'b', globals, locals) != 200:
|
2000-09-19 18:04:18 -03:00
|
|
|
raise TestFailed, "eval(3)"
|
2000-12-12 19:11:42 -04:00
|
|
|
if eval(u'c', globals, locals) != 300:
|
2000-09-19 18:04:18 -03:00
|
|
|
raise TestFailed, "eval(4)"
|
1992-01-27 13:00:37 -04:00
|
|
|
|
1992-11-27 18:53:50 -04:00
|
|
|
print 'execfile'
|
|
|
|
z = 0
|
|
|
|
f = open(TESTFN, 'w')
|
|
|
|
f.write('z = z+1\n')
|
|
|
|
f.write('z = z*2\n')
|
|
|
|
f.close()
|
|
|
|
execfile(TESTFN)
|
2000-12-12 19:11:42 -04:00
|
|
|
if z != 2: raise TestFailed, "execfile(1)"
|
1995-01-02 14:38:42 -04:00
|
|
|
globals['z'] = 0
|
|
|
|
execfile(TESTFN, globals)
|
2000-12-12 19:11:42 -04:00
|
|
|
if globals['z'] != 2: raise TestFailed, "execfile(1)"
|
1995-01-02 14:38:42 -04:00
|
|
|
locals['z'] = 0
|
|
|
|
execfile(TESTFN, globals, locals)
|
2000-12-12 19:11:42 -04:00
|
|
|
if locals['z'] != 2: raise TestFailed, "execfile(1)"
|
1992-11-27 18:53:50 -04:00
|
|
|
unlink(TESTFN)
|
1992-01-27 13:00:37 -04:00
|
|
|
|
* string.py: added rindex(), rfind(); changed index() to interpret
negative start indices starting from the right.
* ftplib.py: debug() -> set_debuglevel(); change demo to use __init__().
* os.py: added execl, execlp, and execvp.
* lambda.py: removed (now that we have built-in map, reduce, bagof, lambda)
* test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce
* commands.py: use os, not posix
* test_grammar.py: make it easy to disable non-portable int overflow tests
* dis.py: don't abuse range()
1993-11-08 11:05:21 -04:00
|
|
|
print 'filter'
|
2000-12-12 19:11:42 -04:00
|
|
|
if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld':
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'filter (filter a string)'
|
2000-12-12 19:11:42 -04:00
|
|
|
if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'filter (remove false values)'
|
2000-12-12 19:11:42 -04:00
|
|
|
if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'filter (keep positives)'
|
1994-08-01 08:34:53 -03:00
|
|
|
class Squares:
|
1998-08-10 17:12:34 -03:00
|
|
|
def __init__(self, max):
|
|
|
|
self.max = max
|
|
|
|
self.sofar = []
|
|
|
|
def __len__(self): return len(self.sofar)
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if not 0 <= i < self.max: raise IndexError
|
|
|
|
n = len(self.sofar)
|
|
|
|
while n <= i:
|
|
|
|
self.sofar.append(n*n)
|
|
|
|
n = n+1
|
|
|
|
return self.sofar[i]
|
1994-08-01 08:34:53 -03:00
|
|
|
if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'filter(None, Squares(10))'
|
1994-08-01 08:34:53 -03:00
|
|
|
if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'filter(oddp, Squares(10))'
|
1999-01-28 15:44:06 -04:00
|
|
|
class StrSquares:
|
|
|
|
def __init__(self, max):
|
|
|
|
self.max = max
|
|
|
|
self.sofar = []
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.sofar)
|
|
|
|
def __getitem__(self, i):
|
|
|
|
if not 0 <= i < self.max:
|
|
|
|
raise IndexError
|
|
|
|
n = len(self.sofar)
|
|
|
|
while n <= i:
|
|
|
|
self.sofar.append(str(n*n))
|
|
|
|
n = n+1
|
|
|
|
return self.sofar[i]
|
|
|
|
def identity(item):
|
|
|
|
return 1
|
|
|
|
filter(identity, Squares(5))
|
* string.py: added rindex(), rfind(); changed index() to interpret
negative start indices starting from the right.
* ftplib.py: debug() -> set_debuglevel(); change demo to use __init__().
* os.py: added execl, execlp, and execvp.
* lambda.py: removed (now that we have built-in map, reduce, bagof, lambda)
* test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce
* commands.py: use os, not posix
* test_grammar.py: make it easy to disable non-portable int overflow tests
* dis.py: don't abuse range()
1993-11-08 11:05:21 -04:00
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'float'
|
2000-12-12 19:11:42 -04:00
|
|
|
if float(3.14) != 3.14: raise TestFailed, 'float(3.14)'
|
|
|
|
if float(314) != 314.0: raise TestFailed, 'float(314)'
|
|
|
|
if float(314L) != 314.0: raise TestFailed, 'float(314L)'
|
|
|
|
if float(" 3.14 ") != 3.14: raise TestFailed, 'float(" 3.14 ")'
|
|
|
|
if float(u" 3.14 ") != 3.14: raise TestFailed, 'float(u" 3.14 ")'
|
|
|
|
if float(u" \u0663.\u0661\u0664 ") != 3.14:
|
2000-04-05 17:11:21 -03:00
|
|
|
raise TestFailed, 'float(u" \u0663.\u0661\u0664 ")'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
|
|
|
print 'getattr'
|
|
|
|
import sys
|
|
|
|
if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
|
|
|
|
|
1994-11-10 18:25:26 -04:00
|
|
|
print 'hasattr'
|
|
|
|
import sys
|
|
|
|
if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
|
|
|
|
|
|
|
|
print 'hash'
|
|
|
|
hash(None)
|
|
|
|
if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
|
|
|
|
hash('spam')
|
|
|
|
hash((0,1,2,3))
|
|
|
|
def f(): pass
|
2001-01-18 19:36:14 -04:00
|
|
|
try: hash([])
|
|
|
|
except TypeError: pass
|
|
|
|
else: raise TestFailed, "hash([]) should raise an exception"
|
|
|
|
try: hash({})
|
|
|
|
except TypeError: pass
|
|
|
|
else: raise TestFailed, "hash({}) should raise an exception"
|
1994-11-10 18:25:26 -04:00
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'hex'
|
|
|
|
if hex(16) != '0x10': raise TestFailed, 'hex(16)'
|
|
|
|
if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
|
1997-05-14 18:37:23 -03:00
|
|
|
if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
|
|
|
|
if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
|
|
|
|
raise TestFailed, 'hex(-16)'
|
1992-01-27 13:00:37 -04:00
|
|
|
if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
|
|
|
|
|
1994-11-10 18:25:26 -04:00
|
|
|
print 'id'
|
|
|
|
id(None)
|
|
|
|
id(1)
|
|
|
|
id(1L)
|
|
|
|
id(1.0)
|
|
|
|
id('spam')
|
|
|
|
id((0,1,2,3))
|
|
|
|
id([0,1,2,3])
|
|
|
|
id({'spam': 1, 'eggs': 2, 'ham': 3})
|
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
# Test input() later, together with raw_input
|
|
|
|
|
|
|
|
print 'int'
|
2000-12-12 19:11:42 -04:00
|
|
|
if int(314) != 314: raise TestFailed, 'int(314)'
|
|
|
|
if int(3.14) != 3: raise TestFailed, 'int(3.14)'
|
|
|
|
if int(314L) != 314: raise TestFailed, 'int(314L)'
|
1997-03-31 13:13:17 -04:00
|
|
|
# Check that conversion from float truncates towards zero
|
2000-12-12 19:11:42 -04:00
|
|
|
if int(-3.14) != -3: raise TestFailed, 'int(-3.14)'
|
|
|
|
if int(3.9) != 3: raise TestFailed, 'int(3.9)'
|
|
|
|
if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'
|
|
|
|
if int(3.5) != 3: raise TestFailed, 'int(3.5)'
|
|
|
|
if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'
|
2000-04-05 17:11:21 -03:00
|
|
|
# Different base:
|
2000-12-12 19:11:42 -04:00
|
|
|
if int("10",16) != 16L: raise TestFailed, 'int("10",16)'
|
|
|
|
if int(u"10",16) != 16L: raise TestFailed, 'int(u"10",16)'
|
2000-06-28 11:48:01 -03:00
|
|
|
# Test conversion from strings and various anomalies
|
1998-06-30 14:02:20 -03:00
|
|
|
L = [
|
1998-08-10 17:12:34 -03:00
|
|
|
('0', 0),
|
|
|
|
('1', 1),
|
|
|
|
('9', 9),
|
|
|
|
('10', 10),
|
|
|
|
('99', 99),
|
|
|
|
('100', 100),
|
|
|
|
('314', 314),
|
|
|
|
(' 314', 314),
|
|
|
|
('314 ', 314),
|
|
|
|
(' \t\t 314 \t\t ', 314),
|
|
|
|
(`sys.maxint`, sys.maxint),
|
2000-04-05 17:11:21 -03:00
|
|
|
(' 1x', ValueError),
|
|
|
|
(' 1 ', 1),
|
|
|
|
(' 1\02 ', ValueError),
|
1998-08-10 17:12:34 -03:00
|
|
|
('', ValueError),
|
|
|
|
(' ', ValueError),
|
|
|
|
(' \t\t ', ValueError),
|
2000-04-05 17:11:21 -03:00
|
|
|
(u'0', 0),
|
|
|
|
(u'1', 1),
|
|
|
|
(u'9', 9),
|
|
|
|
(u'10', 10),
|
|
|
|
(u'99', 99),
|
|
|
|
(u'100', 100),
|
|
|
|
(u'314', 314),
|
|
|
|
(u' 314', 314),
|
|
|
|
(u'\u0663\u0661\u0664 ', 314),
|
|
|
|
(u' \t\t 314 \t\t ', 314),
|
|
|
|
(u' 1x', ValueError),
|
|
|
|
(u' 1 ', 1),
|
|
|
|
(u' 1\02 ', ValueError),
|
|
|
|
(u'', ValueError),
|
|
|
|
(u' ', ValueError),
|
|
|
|
(u' \t\t ', ValueError),
|
1998-06-30 14:02:20 -03:00
|
|
|
]
|
|
|
|
for s, v in L:
|
|
|
|
for sign in "", "+", "-":
|
1998-08-10 17:12:34 -03:00
|
|
|
for prefix in "", " ", "\t", " \t\t ":
|
|
|
|
ss = prefix + sign + s
|
|
|
|
vv = v
|
|
|
|
if sign == "-" and v is not ValueError:
|
|
|
|
vv = -v
|
|
|
|
try:
|
|
|
|
if int(ss) != vv:
|
|
|
|
raise TestFailed, "int(%s)" % `ss`
|
|
|
|
except v:
|
|
|
|
pass
|
|
|
|
except ValueError, e:
|
|
|
|
raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
|
1998-06-30 14:02:20 -03:00
|
|
|
s = `-1-sys.maxint`
|
|
|
|
if int(s)+1 != -sys.maxint:
|
|
|
|
raise TestFailed, "int(%s)" % `s`
|
|
|
|
try:
|
|
|
|
int(s[1:])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
|
1992-01-27 13:00:37 -04:00
|
|
|
|
2001-06-18 16:21:11 -03:00
|
|
|
# SF bug 434186: 0x80000000/2 != 0x80000000>>1.
|
|
|
|
# Worked by accident in Windows release build, but failed in debug build.
|
|
|
|
# Failed in all Linux builds.
|
|
|
|
x = -1-sys.maxint
|
|
|
|
if x >> 1 != x/2:
|
|
|
|
raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
|
|
|
|
|
1997-08-22 18:27:03 -03:00
|
|
|
print 'isinstance'
|
|
|
|
class C:
|
|
|
|
pass
|
|
|
|
class D(C):
|
|
|
|
pass
|
|
|
|
class E:
|
|
|
|
pass
|
|
|
|
c = C()
|
|
|
|
d = D()
|
|
|
|
e = E()
|
|
|
|
if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
|
|
|
|
if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
|
|
|
|
if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
|
|
|
|
if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
|
|
|
|
if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
|
|
|
|
try:
|
|
|
|
isinstance(E, 'foo')
|
|
|
|
raise TestFailed, 'isinstance(E, "foo")'
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
print 'issubclass'
|
|
|
|
if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
|
|
|
|
if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
|
|
|
|
if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
|
|
|
|
try:
|
|
|
|
issubclass('foo', E)
|
|
|
|
raise TestFailed, 'issubclass("foo", E)'
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
issubclass(E, 'foo')
|
|
|
|
raise TestFailed, 'issubclass(E, "foo")'
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'len'
|
2000-12-12 19:11:42 -04:00
|
|
|
if len('123') != 3: raise TestFailed, 'len(\'123\')'
|
|
|
|
if len(()) != 0: raise TestFailed, 'len(())'
|
|
|
|
if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'
|
|
|
|
if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'
|
|
|
|
if len({}) != 0: raise TestFailed, 'len({})'
|
|
|
|
if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
|
|
|
print 'long'
|
2000-12-12 19:11:42 -04:00
|
|
|
if long(314) != 314L: raise TestFailed, 'long(314)'
|
|
|
|
if long(3.14) != 3L: raise TestFailed, 'long(3.14)'
|
|
|
|
if long(314L) != 314L: raise TestFailed, 'long(314L)'
|
1997-03-31 13:13:17 -04:00
|
|
|
# Check that conversion from float truncates towards zero
|
2000-12-12 19:11:42 -04:00
|
|
|
if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'
|
|
|
|
if long(3.9) != 3L: raise TestFailed, 'long(3.9)'
|
|
|
|
if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'
|
|
|
|
if long(3.5) != 3L: raise TestFailed, 'long(3.5)'
|
|
|
|
if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'
|
|
|
|
if long("-3") != -3L: raise TestFailed, 'long("-3")'
|
|
|
|
if long(u"-3") != -3L: raise TestFailed, 'long(u"-3")'
|
2000-04-05 17:11:21 -03:00
|
|
|
# Different base:
|
2000-12-12 19:11:42 -04:00
|
|
|
if long("10",16) != 16L: raise TestFailed, 'long("10",16)'
|
|
|
|
if long(u"10",16) != 16L: raise TestFailed, 'long(u"10",16)'
|
1998-06-30 14:02:20 -03:00
|
|
|
# Check conversions from string (same test set as for int(), and then some)
|
|
|
|
LL = [
|
1998-08-10 17:12:34 -03:00
|
|
|
('1' + '0'*20, 10L**20),
|
|
|
|
('1' + '0'*100, 10L**100),
|
2000-04-05 17:11:21 -03:00
|
|
|
(u'1' + u'0'*20, 10L**20),
|
|
|
|
(u'1' + u'0'*100, 10L**100),
|
1998-06-30 14:02:20 -03:00
|
|
|
]
|
|
|
|
for s, v in L + LL:
|
|
|
|
for sign in "", "+", "-":
|
1998-08-10 17:12:34 -03:00
|
|
|
for prefix in "", " ", "\t", " \t\t ":
|
|
|
|
ss = prefix + sign + s
|
|
|
|
vv = v
|
|
|
|
if sign == "-" and v is not ValueError:
|
|
|
|
vv = -v
|
|
|
|
try:
|
|
|
|
if long(ss) != long(vv):
|
2000-04-05 17:11:21 -03:00
|
|
|
raise TestFailed, "long(%s)" % `ss`
|
1998-08-10 17:12:34 -03:00
|
|
|
except v:
|
|
|
|
pass
|
|
|
|
except ValueError, e:
|
2000-04-05 17:11:21 -03:00
|
|
|
raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
|
1992-01-27 13:00:37 -04:00
|
|
|
|
* string.py: added rindex(), rfind(); changed index() to interpret
negative start indices starting from the right.
* ftplib.py: debug() -> set_debuglevel(); change demo to use __init__().
* os.py: added execl, execlp, and execvp.
* lambda.py: removed (now that we have built-in map, reduce, bagof, lambda)
* test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce
* commands.py: use os, not posix
* test_grammar.py: make it easy to disable non-portable int overflow tests
* dis.py: don't abuse range()
1993-11-08 11:05:21 -04:00
|
|
|
print 'map'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(None, \'hello world\')'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(None, 'abcd', 'efg') != \
|
1998-08-10 17:12:34 -03:00
|
|
|
[('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
|
|
|
|
raise TestFailed, 'map(None, \'abcd\', \'efg\')'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(None, range(10))'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(lambda x: x*x, range(1,4))'
|
1995-03-04 18:30:54 -04:00
|
|
|
try:
|
1998-08-10 17:12:34 -03:00
|
|
|
from math import sqrt
|
1995-03-04 18:30:54 -04:00
|
|
|
except ImportError:
|
1998-08-10 17:12:34 -03:00
|
|
|
def sqrt(x):
|
|
|
|
return pow(x, 0.5)
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
|
* string.py: added rindex(), rfind(); changed index() to interpret
negative start indices starting from the right.
* ftplib.py: debug() -> set_debuglevel(); change demo to use __init__().
* os.py: added execl, execlp, and execvp.
* lambda.py: removed (now that we have built-in map, reduce, bagof, lambda)
* test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce
* commands.py: use os, not posix
* test_grammar.py: make it easy to disable non-portable int overflow tests
* dis.py: don't abuse range()
1993-11-08 11:05:21 -04:00
|
|
|
def plus(*v):
|
1998-08-10 17:12:34 -03:00
|
|
|
accu = 0
|
|
|
|
for i in v: accu = accu + i
|
|
|
|
return accu
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(plus, [1, 3, 7]) != [1, 3, 7]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(plus, [1, 3, 7])'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
|
2000-12-12 19:11:42 -04:00
|
|
|
if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
|
1994-08-01 08:34:53 -03:00
|
|
|
if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(None, Squares(10))'
|
1994-08-01 08:34:53 -03:00
|
|
|
if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(int, Squares(10))'
|
1994-08-01 08:34:53 -03:00
|
|
|
if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(None, Squares(3), Squares(2))'
|
2001-01-22 15:30:07 -04:00
|
|
|
if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
|
1998-08-10 17:12:34 -03:00
|
|
|
raise TestFailed, 'map(max, Squares(3), Squares(2))'
|
* string.py: added rindex(), rfind(); changed index() to interpret
negative start indices starting from the right.
* ftplib.py: debug() -> set_debuglevel(); change demo to use __init__().
* os.py: added execl, execlp, and execvp.
* lambda.py: removed (now that we have built-in map, reduce, bagof, lambda)
* test_b{1,2}.py, testall.out: added tests for bagof, lambda, map, reduce
* commands.py: use os, not posix
* test_grammar.py: make it easy to disable non-portable int overflow tests
* dis.py: don't abuse range()
1993-11-08 11:05:21 -04:00
|
|
|
|
1992-01-27 13:00:37 -04:00
|
|
|
print 'max'
|
2000-12-12 19:11:42 -04:00
|
|
|
if max('123123') != '3': raise TestFailed, 'max(\'123123\')'
|
|
|
|
if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'
|
|
|
|
if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
|
|
|
|
if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
|
|
|
|
if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'
|
|
|
|
if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'
|
1992-01-27 13:00:37 -04:00
|
|
|
|
|
|
|
print 'min'
|
2000-12-12 19:11:42 -04:00
|
|
|
if min('123123') != '1': raise TestFailed, 'min(\'123123\')'
|
|
|
|
if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'
|
|
|
|
if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
|
|
|
|
if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
|
1992-01-27 13:00:37 -04:00
|
|
|
#
|
2000-12-12 19:11:42 -04:00
|
|
|
if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'
|
|
|
|
if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'
|
|
|
|
if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
|