blob: 8d47abc73706c69ac911fcb2aba6629f70e2db94 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 4a, built-in functions a-m
2
3from test_support import *
4
Guido van Rossumeecf0351994-12-30 17:17:46 +00005print '__import__'
6__import__('sys')
Barry Warsaw3a9d0612000-09-01 06:53:52 +00007__import__('time')
Guido van Rossumeecf0351994-12-30 17:17:46 +00008__import__('string')
9try: __import__('spamspam')
10except ImportError: pass
11else: raise TestFailed, "__import__('spamspam') should fail"
12
Guido van Rossum3bead091992-01-27 17:00:37 +000013print 'abs'
Fred Drake132dce22000-12-12 23:11:42 +000014if abs(0) != 0: raise TestFailed, 'abs(0)'
15if abs(1234) != 1234: raise TestFailed, 'abs(1234)'
16if abs(-1234) != 1234: raise TestFailed, 'abs(-1234)'
Guido van Rossum3bead091992-01-27 17:00:37 +000017#
Fred Drake132dce22000-12-12 23:11:42 +000018if abs(0.0) != 0.0: raise TestFailed, 'abs(0.0)'
19if abs(3.14) != 3.14: raise TestFailed, 'abs(3.14)'
20if abs(-3.14) != 3.14: raise TestFailed, 'abs(-3.14)'
Guido van Rossum3bead091992-01-27 17:00:37 +000021#
Fred Drake132dce22000-12-12 23:11:42 +000022if abs(0L) != 0L: raise TestFailed, 'abs(0L)'
23if abs(1234L) != 1234L: raise TestFailed, 'abs(1234L)'
24if abs(-1234L) != 1234L: raise TestFailed, 'abs(-1234L)'
Guido van Rossum3bead091992-01-27 17:00:37 +000025
Guido van Rossum29d26062001-12-11 04:37:34 +000026try: abs('a')
27except TypeError: pass
28else: raise TestFailed, 'abs("a")'
29
Guido van Rossum3bead091992-01-27 17:00:37 +000030print 'apply'
31def f0(*args):
Guido van Rossum27d445f1998-08-10 20:12:34 +000032 if args != (): raise TestFailed, 'f0 called with ' + `args`
Guido van Rossum3bead091992-01-27 17:00:37 +000033def f1(a1):
Guido van Rossum27d445f1998-08-10 20:12:34 +000034 if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
Guido van Rossum3bead091992-01-27 17:00:37 +000035def f2(a1, a2):
Guido van Rossum27d445f1998-08-10 20:12:34 +000036 if a1 != 1 or a2 != 2:
37 raise TestFailed, 'f2 called with ' + `a1, a2`
Guido van Rossum3bead091992-01-27 17:00:37 +000038def f3(a1, a2, a3):
Guido van Rossum27d445f1998-08-10 20:12:34 +000039 if a1 != 1 or a2 != 2 or a3 != 3:
40 raise TestFailed, 'f3 called with ' + `a1, a2, a3`
Guido van Rossum3bead091992-01-27 17:00:37 +000041apply(f0, ())
42apply(f1, (1,))
43apply(f2, (1, 2))
44apply(f3, (1, 2, 3))
45
Fred Drake1a7aab72001-01-04 22:33:02 +000046# 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.
49apply(id, (1,), {})
50try:
51 apply(id, (1,), {"foo": 1})
52except TypeError:
53 pass
54else:
55 raise TestFailed, 'expected TypeError; no exception raised'
56
Guido van Rossume23b62f1994-11-10 22:25:26 +000057print 'callable'
58if not callable(len):raise TestFailed, 'callable(len)'
59def f(): pass
60if not callable(f): raise TestFailed, 'callable(f)'
61class C:
Guido van Rossum27d445f1998-08-10 20:12:34 +000062 def meth(self): pass
Guido van Rossume23b62f1994-11-10 22:25:26 +000063if not callable(C): raise TestFailed, 'callable(C)'
64x = C()
65if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
66if callable(x): raise TestFailed, 'callable(x)'
67class D(C):
Guido van Rossum27d445f1998-08-10 20:12:34 +000068 def __call__(self): pass
Guido van Rossume23b62f1994-11-10 22:25:26 +000069y = D()
70if not callable(y): raise TestFailed, 'callable(y)'
Jeremy Hylton5ab36612001-01-03 23:53:31 +000071y()
Guido van Rossume23b62f1994-11-10 22:25:26 +000072
Guido van Rossum3bead091992-01-27 17:00:37 +000073print 'chr'
Fred Drake132dce22000-12-12 23:11:42 +000074if chr(32) != ' ': raise TestFailed, 'chr(32)'
75if chr(65) != 'A': raise TestFailed, 'chr(65)'
76if chr(97) != 'a': raise TestFailed, 'chr(97)'
Guido van Rossum3bead091992-01-27 17:00:37 +000077
Neil Schemenauer38f02232001-01-04 01:33:41 +000078# cmp
Guido van Rossum85f18201992-11-27 22:53:50 +000079print 'cmp'
Fred Drake132dce22000-12-12 23:11:42 +000080if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)'
81if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)'
82if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)'
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +000083# verify that circular objects are handled
84a = []; a.append(a)
85b = []; b.append(b)
86from UserList import UserList
87c = UserList(); c.append(c)
88if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b)
89if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c)
90if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a)
91if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c)
Vladimir Marangozovd57f5cf2000-07-14 04:32:09 +000092# okay, now break the cycles
93a.pop(); b.pop(); c.pop()
Guido van Rossum85f18201992-11-27 22:53:50 +000094
95print 'coerce'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000096if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
Fred Drake132dce22000-12-12 23:11:42 +000097if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000098if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000099
Guido van Rossume23b62f1994-11-10 22:25:26 +0000100print 'compile'
101compile('print 1\n', '', 'exec')
102
Guido van Rossumdddf7a61997-03-31 17:13:17 +0000103print 'complex'
Fred Drake132dce22000-12-12 23:11:42 +0000104if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)'
105if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)'
106if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)'
107if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)'
108if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)'
109if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)'
110if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)'
111if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)'
112if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)'
113if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)'
114if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)'
115if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)'
116if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)'
117if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)'
118if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)'
119if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)'
120if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)'
121if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)'
122if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)'
123if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)'
124if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
Fred Drake526c7a02001-12-13 19:52:22 +0000125if complex("1") != 1+0j: raise TestFailed, 'complex("1")'
126if complex("1j") != 1j: raise TestFailed, 'complex("1j")'
127try: complex("1", "1")
128except TypeError: pass
129else: raise TestFailed, 'complex("1", "1")'
130try: complex(1, "1")
131except TypeError: pass
132else: raise TestFailed, 'complex(1, "1")'
Fred Drake132dce22000-12-12 23:11:42 +0000133if complex(" 3.14+J ") != 3.14+1j: raise TestFailed, 'complex(" 3.14+J )"'
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000134if have_unicode:
135 if complex(unicode(" 3.14+J ")) != 3.14+1j:
136 raise TestFailed, 'complex(u" 3.14+J )"'
Guido van Rossumdddf7a61997-03-31 17:13:17 +0000137class Z:
138 def __complex__(self): return 3.14j
139z = Z()
Fred Drake132dce22000-12-12 23:11:42 +0000140if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)'
Guido van Rossumdddf7a61997-03-31 17:13:17 +0000141
Guido van Rossume23b62f1994-11-10 22:25:26 +0000142print 'delattr'
143import sys
144sys.spam = 1
145delattr(sys, 'spam')
146
Guido van Rossum3bead091992-01-27 17:00:37 +0000147print 'dir'
148x = 1
149if 'x' not in dir(): raise TestFailed, 'dir()'
150import sys
151if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
152
153print 'divmod'
Fred Drake132dce22000-12-12 23:11:42 +0000154if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)'
155if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)'
156if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)'
157if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000158#
Fred Drake132dce22000-12-12 23:11:42 +0000159if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
160if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
161if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
162if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000163#
Fred Drake132dce22000-12-12 23:11:42 +0000164if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)'
165if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
166if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
167if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000168#
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000169if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
Guido van Rossum27d445f1998-08-10 20:12:34 +0000170 raise TestFailed, 'divmod(3.25, 1.0)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000171if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
Guido van Rossum27d445f1998-08-10 20:12:34 +0000172 raise TestFailed, 'divmod(-3.25, 1.0)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000173if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
Guido van Rossum27d445f1998-08-10 20:12:34 +0000174 raise TestFailed, 'divmod(3.25, -1.0)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000175if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
Guido van Rossum27d445f1998-08-10 20:12:34 +0000176 raise TestFailed, 'divmod(-3.25, -1.0)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000177
178print 'eval'
Fred Drake132dce22000-12-12 23:11:42 +0000179if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')'
180if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')'
Guido van Rossum824de251995-01-02 18:38:42 +0000181globals = {'a': 1, 'b': 2}
182locals = {'b': 200, 'c': 300}
Fred Drake132dce22000-12-12 23:11:42 +0000183if eval('a', globals) != 1:
Jeremy Hyltoncf29bb22000-08-23 21:11:05 +0000184 raise TestFailed, "eval(1) == %s" % eval('a', globals)
Fred Drake132dce22000-12-12 23:11:42 +0000185if eval('a', globals, locals) != 1:
Jeremy Hyltoncf29bb22000-08-23 21:11:05 +0000186 raise TestFailed, "eval(2)"
Fred Drake132dce22000-12-12 23:11:42 +0000187if eval('b', globals, locals) != 200:
Jeremy Hyltoncf29bb22000-08-23 21:11:05 +0000188 raise TestFailed, "eval(3)"
Fred Drake132dce22000-12-12 23:11:42 +0000189if eval('c', globals, locals) != 300:
Jeremy Hyltoncf29bb22000-08-23 21:11:05 +0000190 raise TestFailed, "eval(4)"
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000191if have_unicode:
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\')'
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000194globals = {'a': 1, 'b': 2}
195locals = {'b': 200, 'c': 300}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000196if have_unicode:
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)"
Guido van Rossum3bead091992-01-27 17:00:37 +0000205
Guido van Rossum85f18201992-11-27 22:53:50 +0000206print 'execfile'
207z = 0
208f = open(TESTFN, 'w')
209f.write('z = z+1\n')
210f.write('z = z*2\n')
211f.close()
212execfile(TESTFN)
Fred Drake132dce22000-12-12 23:11:42 +0000213if z != 2: raise TestFailed, "execfile(1)"
Guido van Rossum824de251995-01-02 18:38:42 +0000214globals['z'] = 0
215execfile(TESTFN, globals)
Fred Drake132dce22000-12-12 23:11:42 +0000216if globals['z'] != 2: raise TestFailed, "execfile(1)"
Guido van Rossum824de251995-01-02 18:38:42 +0000217locals['z'] = 0
218execfile(TESTFN, globals, locals)
Fred Drake132dce22000-12-12 23:11:42 +0000219if locals['z'] != 2: raise TestFailed, "execfile(1)"
Guido van Rossum85f18201992-11-27 22:53:50 +0000220unlink(TESTFN)
Guido van Rossum3bead091992-01-27 17:00:37 +0000221
Guido van Rossume65cce51993-11-08 15:05:21 +0000222print 'filter'
Fred Drake132dce22000-12-12 23:11:42 +0000223if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld':
Guido van Rossum27d445f1998-08-10 20:12:34 +0000224 raise TestFailed, 'filter (filter a string)'
Fred Drake132dce22000-12-12 23:11:42 +0000225if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000226 raise TestFailed, 'filter (remove false values)'
Fred Drake132dce22000-12-12 23:11:42 +0000227if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000228 raise TestFailed, 'filter (keep positives)'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229class Squares:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000230 def __init__(self, max):
231 self.max = max
232 self.sofar = []
233 def __len__(self): return len(self.sofar)
234 def __getitem__(self, i):
235 if not 0 <= i < self.max: raise IndexError
236 n = len(self.sofar)
237 while n <= i:
238 self.sofar.append(n*n)
239 n = n+1
240 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000242 raise TestFailed, 'filter(None, Squares(10))'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000244 raise TestFailed, 'filter(oddp, Squares(10))'
Barry Warsawab11f601999-01-28 19:44:06 +0000245class StrSquares:
246 def __init__(self, max):
247 self.max = max
248 self.sofar = []
249 def __len__(self):
250 return len(self.sofar)
251 def __getitem__(self, i):
252 if not 0 <= i < self.max:
253 raise IndexError
254 n = len(self.sofar)
255 while n <= i:
256 self.sofar.append(str(n*n))
257 n = n+1
258 return self.sofar[i]
259def identity(item):
260 return 1
261filter(identity, Squares(5))
Guido van Rossume65cce51993-11-08 15:05:21 +0000262
Guido van Rossum3bead091992-01-27 17:00:37 +0000263print 'float'
Fred Drake132dce22000-12-12 23:11:42 +0000264if float(3.14) != 3.14: raise TestFailed, 'float(3.14)'
265if float(314) != 314.0: raise TestFailed, 'float(314)'
266if float(314L) != 314.0: raise TestFailed, 'float(314L)'
267if float(" 3.14 ") != 3.14: raise TestFailed, 'float(" 3.14 ")'
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000268if have_unicode:
269 if float(unicode(" 3.14 ")) != 3.14:
270 raise TestFailed, 'float(u" 3.14 ")'
Martin v. Löwis10d72552001-08-17 22:08:34 +0000271 if float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')) != 3.14:
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000272 raise TestFailed, 'float(u" \u0663.\u0661\u0664 ")'
Guido van Rossum3bead091992-01-27 17:00:37 +0000273
274print 'getattr'
275import sys
276if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
Jeremy Hyltonb7a77312001-07-30 22:49:11 +0000277try:
278 getattr(sys, 1)
279except TypeError:
280 pass
281else:
282 raise TestFailed, "getattr(sys, 1) should raise an exception"
283try:
284 getattr(sys, 1, "foo")
285except TypeError:
286 pass
287else:
288 raise TestFailed, 'getattr(sys, 1, "foo") should raise an exception'
Guido van Rossum3bead091992-01-27 17:00:37 +0000289
Guido van Rossume23b62f1994-11-10 22:25:26 +0000290print 'hasattr'
291import sys
292if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
Jeremy Hyltonb7a77312001-07-30 22:49:11 +0000293try:
294 hasattr(sys, 1)
295except TypeError:
296 pass
297else:
298 raise TestFailed, "hasattr(sys, 1) should raise an exception"
Guido van Rossume23b62f1994-11-10 22:25:26 +0000299
300print 'hash'
301hash(None)
302if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
303hash('spam')
304hash((0,1,2,3))
305def f(): pass
Guido van Rossum0c6614c2001-01-18 23:36:14 +0000306try: hash([])
307except TypeError: pass
308else: raise TestFailed, "hash([]) should raise an exception"
309try: hash({})
310except TypeError: pass
311else: raise TestFailed, "hash({}) should raise an exception"
Guido van Rossume23b62f1994-11-10 22:25:26 +0000312
Guido van Rossum3bead091992-01-27 17:00:37 +0000313print 'hex'
314if hex(16) != '0x10': raise TestFailed, 'hex(16)'
315if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
Guido van Rossumc1c96d11997-05-14 21:37:23 +0000316if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
317if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
318 raise TestFailed, 'hex(-16)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000319if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
320
Guido van Rossume23b62f1994-11-10 22:25:26 +0000321print 'id'
322id(None)
323id(1)
324id(1L)
325id(1.0)
326id('spam')
327id((0,1,2,3))
328id([0,1,2,3])
329id({'spam': 1, 'eggs': 2, 'ham': 3})
330
Guido van Rossum3bead091992-01-27 17:00:37 +0000331# Test input() later, together with raw_input
332
333print 'int'
Fred Drake132dce22000-12-12 23:11:42 +0000334if int(314) != 314: raise TestFailed, 'int(314)'
335if int(3.14) != 3: raise TestFailed, 'int(3.14)'
336if int(314L) != 314: raise TestFailed, 'int(314L)'
Guido van Rossumdddf7a61997-03-31 17:13:17 +0000337# Check that conversion from float truncates towards zero
Fred Drake132dce22000-12-12 23:11:42 +0000338if int(-3.14) != -3: raise TestFailed, 'int(-3.14)'
339if int(3.9) != 3: raise TestFailed, 'int(3.9)'
340if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'
341if int(3.5) != 3: raise TestFailed, 'int(3.5)'
342if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'
Guido van Rossum9e896b32000-04-05 20:11:21 +0000343# Different base:
Fred Drake132dce22000-12-12 23:11:42 +0000344if int("10",16) != 16L: raise TestFailed, 'int("10",16)'
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000345if have_unicode:
346 if int(unicode("10"),16) != 16L:
347 raise TestFailed, 'int(u"10",16)'
Jeremy Hyltona05e2932000-06-28 14:48:01 +0000348# Test conversion from strings and various anomalies
Guido van Rossum70115041998-06-30 17:02:20 +0000349L = [
Guido van Rossum27d445f1998-08-10 20:12:34 +0000350 ('0', 0),
351 ('1', 1),
352 ('9', 9),
353 ('10', 10),
354 ('99', 99),
355 ('100', 100),
356 ('314', 314),
357 (' 314', 314),
358 ('314 ', 314),
359 (' \t\t 314 \t\t ', 314),
360 (`sys.maxint`, sys.maxint),
Guido van Rossum9e896b32000-04-05 20:11:21 +0000361 (' 1x', ValueError),
362 (' 1 ', 1),
363 (' 1\02 ', ValueError),
Guido van Rossum27d445f1998-08-10 20:12:34 +0000364 ('', ValueError),
365 (' ', ValueError),
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000366 (' \t\t ', ValueError)
367]
368if have_unicode:
369 L += [
370 (unicode('0'), 0),
371 (unicode('1'), 1),
372 (unicode('9'), 9),
373 (unicode('10'), 10),
374 (unicode('99'), 99),
375 (unicode('100'), 100),
376 (unicode('314'), 314),
377 (unicode(' 314'), 314),
Martin v. Löwis10d72552001-08-17 22:08:34 +0000378 (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000379 (unicode(' \t\t 314 \t\t '), 314),
380 (unicode(' 1x'), ValueError),
381 (unicode(' 1 '), 1),
382 (unicode(' 1\02 '), ValueError),
383 (unicode(''), ValueError),
384 (unicode(' '), ValueError),
385 (unicode(' \t\t '), ValueError),
Guido van Rossum70115041998-06-30 17:02:20 +0000386]
387for s, v in L:
388 for sign in "", "+", "-":
Guido van Rossum27d445f1998-08-10 20:12:34 +0000389 for prefix in "", " ", "\t", " \t\t ":
390 ss = prefix + sign + s
391 vv = v
392 if sign == "-" and v is not ValueError:
393 vv = -v
394 try:
395 if int(ss) != vv:
396 raise TestFailed, "int(%s)" % `ss`
397 except v:
398 pass
399 except ValueError, e:
400 raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
Guido van Rossum70115041998-06-30 17:02:20 +0000401s = `-1-sys.maxint`
402if int(s)+1 != -sys.maxint:
403 raise TestFailed, "int(%s)" % `s`
404try:
405 int(s[1:])
406except ValueError:
407 pass
408else:
409 raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
Tim Peters7321ec42001-07-26 20:02:17 +0000410try:
411 int(1e100)
412except OverflowError:
413 pass
414else:
415 raise TestFailed("int(1e100) expected OverflowError")
416try:
417 int(-1e100)
418except OverflowError:
419 pass
420else:
421 raise TestFailed("int(-1e100) expected OverflowError")
422
Guido van Rossum3bead091992-01-27 17:00:37 +0000423
Tim Peters1dad6a82001-06-18 19:21:11 +0000424# 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.
427x = -1-sys.maxint
Guido van Rossum54e54c62001-09-04 19:14:14 +0000428if x >> 1 != x//2:
Tim Peters1dad6a82001-06-18 19:21:11 +0000429 raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
430
Guido van Rossum29d26062001-12-11 04:37:34 +0000431try: int('123\0')
432except ValueError: pass
433else: raise TestFailed("int('123\0') didn't raise exception")
434
Neal Norwitz32f41532001-12-29 00:35:20 +0000435try: int('53', 40)
436except ValueError: pass
437else: raise TestFailed("int('53', 40) didn't raise ValueError")
438
439try: int('1' * 512)
440except ValueError: pass
441else: raise TestFailed("int('1' * 512) didn't raise ValueError")
442
443try: int(1, 12)
444except TypeError: pass
445else: raise TestFailed("int(1, 12) didn't raise TypeError")
446
447if int('0123', 0) != 83:
448 raise TestFailed("int('0123', 0) != 83")
449
Barry Warsawd5430771997-08-22 21:27:03 +0000450print 'isinstance'
451class C:
452 pass
453class D(C):
454 pass
455class E:
456 pass
457c = C()
458d = D()
459e = E()
460if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
461if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
462if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
463if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
464if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
465try:
466 isinstance(E, 'foo')
467 raise TestFailed, 'isinstance(E, "foo")'
468except TypeError:
469 pass
470
471print 'issubclass'
472if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
473if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
474if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
475try:
476 issubclass('foo', E)
477 raise TestFailed, 'issubclass("foo", E)'
478except TypeError:
479 pass
480try:
481 issubclass(E, 'foo')
482 raise TestFailed, 'issubclass(E, "foo")'
483except TypeError:
484 pass
485
Guido van Rossum3bead091992-01-27 17:00:37 +0000486print 'len'
Fred Drake132dce22000-12-12 23:11:42 +0000487if len('123') != 3: raise TestFailed, 'len(\'123\')'
488if len(()) != 0: raise TestFailed, 'len(())'
489if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'
490if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'
491if len({}) != 0: raise TestFailed, 'len({})'
492if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
Guido van Rossum3bead091992-01-27 17:00:37 +0000493
Guido van Rossum06ee2532002-02-26 22:39:23 +0000494print 'list'
495if list([]) != []: raise TestFailed, 'list([])'
496l0_3 = [0, 1, 2, 3]
497l0_3_bis = list(l0_3)
498if l0_3 != l0_3_bis or l0_3 is l0_3_bis: raise TestFailed, 'list([0, 1, 2, 3])'
499if list(()) != []: raise TestFailed, 'list(())'
500if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed, 'list((0, 1, 2, 3))'
501if list('') != []: raise TestFailed, 'list('')'
502if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed, "list('spam')"
503
Guido van Rossum3bead091992-01-27 17:00:37 +0000504print 'long'
Fred Drake132dce22000-12-12 23:11:42 +0000505if long(314) != 314L: raise TestFailed, 'long(314)'
506if long(3.14) != 3L: raise TestFailed, 'long(3.14)'
507if long(314L) != 314L: raise TestFailed, 'long(314L)'
Guido van Rossumdddf7a61997-03-31 17:13:17 +0000508# Check that conversion from float truncates towards zero
Fred Drake132dce22000-12-12 23:11:42 +0000509if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'
510if long(3.9) != 3L: raise TestFailed, 'long(3.9)'
511if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'
512if long(3.5) != 3L: raise TestFailed, 'long(3.5)'
513if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'
514if long("-3") != -3L: raise TestFailed, 'long("-3")'
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000515if have_unicode:
516 if long(unicode("-3")) != -3L:
517 raise TestFailed, 'long(u"-3")'
Guido van Rossum9e896b32000-04-05 20:11:21 +0000518# Different base:
Fred Drake132dce22000-12-12 23:11:42 +0000519if long("10",16) != 16L: raise TestFailed, 'long("10",16)'
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000520if have_unicode:
521 if long(unicode("10"),16) != 16L:
522 raise TestFailed, 'long(u"10",16)'
Guido van Rossum70115041998-06-30 17:02:20 +0000523# Check conversions from string (same test set as for int(), and then some)
524LL = [
Guido van Rossum27d445f1998-08-10 20:12:34 +0000525 ('1' + '0'*20, 10L**20),
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000526 ('1' + '0'*100, 10L**100)
527]
528if have_unicode:
529 L+=[
530 (unicode('1') + unicode('0')*20, 10L**20),
531 (unicode('1') + unicode('0')*100, 10L**100),
Guido van Rossum70115041998-06-30 17:02:20 +0000532]
533for s, v in L + LL:
534 for sign in "", "+", "-":
Guido van Rossum27d445f1998-08-10 20:12:34 +0000535 for prefix in "", " ", "\t", " \t\t ":
536 ss = prefix + sign + s
537 vv = v
538 if sign == "-" and v is not ValueError:
539 vv = -v
540 try:
541 if long(ss) != long(vv):
Guido van Rossum9e896b32000-04-05 20:11:21 +0000542 raise TestFailed, "long(%s)" % `ss`
Guido van Rossum27d445f1998-08-10 20:12:34 +0000543 except v:
544 pass
545 except ValueError, e:
Guido van Rossum9e896b32000-04-05 20:11:21 +0000546 raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
Guido van Rossum3bead091992-01-27 17:00:37 +0000547
Guido van Rossum29d26062001-12-11 04:37:34 +0000548try: long('123\0')
549except ValueError: pass
550else: raise TestFailed("long('123\0') didn't raise exception")
551
Neal Norwitz32f41532001-12-29 00:35:20 +0000552try: long('53', 40)
553except ValueError: pass
554else: raise TestFailed("long('53', 40) didn't raise ValueError")
555
556try: long(1, 12)
557except TypeError: pass
558else: raise TestFailed("long(1, 12) didn't raise TypeError")
559
Guido van Rossume65cce51993-11-08 15:05:21 +0000560print 'map'
Fred Drake132dce22000-12-12 23:11:42 +0000561if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000562 raise TestFailed, 'map(None, \'hello world\')'
Fred Drake132dce22000-12-12 23:11:42 +0000563if map(None, 'abcd', 'efg') != \
Guido van Rossum27d445f1998-08-10 20:12:34 +0000564 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
565 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
Fred Drake132dce22000-12-12 23:11:42 +0000566if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000567 raise TestFailed, 'map(None, range(10))'
Fred Drake132dce22000-12-12 23:11:42 +0000568if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000569 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000570try:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000571 from math import sqrt
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000572except ImportError:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000573 def sqrt(x):
574 return pow(x, 0.5)
Fred Drake132dce22000-12-12 23:11:42 +0000575if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000576 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
Fred Drake132dce22000-12-12 23:11:42 +0000577if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000578 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
Guido van Rossume65cce51993-11-08 15:05:21 +0000579def plus(*v):
Guido van Rossum27d445f1998-08-10 20:12:34 +0000580 accu = 0
581 for i in v: accu = accu + i
582 return accu
Fred Drake132dce22000-12-12 23:11:42 +0000583if map(plus, [1, 3, 7]) != [1, 3, 7]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000584 raise TestFailed, 'map(plus, [1, 3, 7])'
Fred Drake132dce22000-12-12 23:11:42 +0000585if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000586 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
Fred Drake132dce22000-12-12 23:11:42 +0000587if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000588 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000589if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000590 raise TestFailed, 'map(None, Squares(10))'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000591if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000592 raise TestFailed, 'map(int, Squares(10))'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000593if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000594 raise TestFailed, 'map(None, Squares(3), Squares(2))'
Guido van Rossum7f9b5e02001-01-22 19:30:07 +0000595if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
Guido van Rossum27d445f1998-08-10 20:12:34 +0000596 raise TestFailed, 'map(max, Squares(3), Squares(2))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000597
Guido van Rossum3bead091992-01-27 17:00:37 +0000598print 'max'
Fred Drake132dce22000-12-12 23:11:42 +0000599if max('123123') != '3': raise TestFailed, 'max(\'123123\')'
600if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'
601if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
602if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
Guido van Rossum3bead091992-01-27 17:00:37 +0000603#
Fred Drake132dce22000-12-12 23:11:42 +0000604if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
605if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'
606if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000607
608print 'min'
Fred Drake132dce22000-12-12 23:11:42 +0000609if min('123123') != '1': raise TestFailed, 'min(\'123123\')'
610if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'
611if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
612if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
Guido van Rossum3bead091992-01-27 17:00:37 +0000613#
Fred Drake132dce22000-12-12 23:11:42 +0000614if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'
615if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'
616if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'