blob: 356edd2813ba905d745c07121ab415db589ca3ae [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')
7__import__('strop')
8__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'
14if abs(0) <> 0: raise TestFailed, 'abs(0)'
15if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
16if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
17#
18if 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)'
21#
22if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
23if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
24if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
25
26print 'apply'
27def f0(*args):
28 if args != (): raise TestFailed, 'f0 called with ' + `args`
29def f1(a1):
30 if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
31def f2(a1, a2):
32 if a1 != 1 or a2 != 2:
33 raise TestFailed, 'f2 called with ' + `a1, a2`
34def f3(a1, a2, a3):
35 if a1 != 1 or a2 != 2 or a3 != 3:
Guido van Rossumb3b09c91993-10-22 14:24:22 +000036 raise TestFailed, 'f3 called with ' + `a1, a2, a3`
Guido van Rossum3bead091992-01-27 17:00:37 +000037apply(f0, ())
38apply(f1, (1,))
39apply(f2, (1, 2))
40apply(f3, (1, 2, 3))
41
Guido van Rossume23b62f1994-11-10 22:25:26 +000042print 'callable'
43if not callable(len):raise TestFailed, 'callable(len)'
44def f(): pass
45if not callable(f): raise TestFailed, 'callable(f)'
46class C:
47 def meth(self): pass
48if not callable(C): raise TestFailed, 'callable(C)'
49x = C()
50if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
51if callable(x): raise TestFailed, 'callable(x)'
52class D(C):
53 def __call__(self): pass
54y = D()
55if not callable(y): raise TestFailed, 'callable(y)'
56
Guido van Rossum3bead091992-01-27 17:00:37 +000057print 'chr'
58if chr(32) <> ' ': raise TestFailed, 'chr(32)'
59if chr(65) <> 'A': raise TestFailed, 'chr(65)'
60if chr(97) <> 'a': raise TestFailed, 'chr(97)'
61
Guido van Rossum85f18201992-11-27 22:53:50 +000062print 'cmp'
63if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
64if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
65if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'
66
67print 'coerce'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000068if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000069if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000070if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000071
Guido van Rossume23b62f1994-11-10 22:25:26 +000072print 'compile'
73compile('print 1\n', '', 'exec')
74
75print 'delattr'
76import sys
77sys.spam = 1
78delattr(sys, 'spam')
79
Guido van Rossum3bead091992-01-27 17:00:37 +000080print 'dir'
81x = 1
82if 'x' not in dir(): raise TestFailed, 'dir()'
83import sys
84if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
85
86print 'divmod'
87if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
88if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
89if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
90if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
91#
92if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
93if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
94if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
95if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
96#
97if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
98if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
99if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
100if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
101#
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000102if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
103 raise TestFailed, 'divmod(3.25, 1.0)'
104if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
105 raise TestFailed, 'divmod(-3.25, 1.0)'
106if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
107 raise TestFailed, 'divmod(3.25, -1.0)'
108if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
109 raise TestFailed, 'divmod(-3.25, -1.0)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000110
111print 'eval'
112if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
Guido van Rossum85f18201992-11-27 22:53:50 +0000113if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
Guido van Rossum3bead091992-01-27 17:00:37 +0000114
Guido van Rossum85f18201992-11-27 22:53:50 +0000115print 'execfile'
116z = 0
117f = open(TESTFN, 'w')
118f.write('z = z+1\n')
119f.write('z = z*2\n')
120f.close()
121execfile(TESTFN)
122unlink(TESTFN)
Guido van Rossum3bead091992-01-27 17:00:37 +0000123
Guido van Rossume65cce51993-11-08 15:05:21 +0000124print 'filter'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000125if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
Guido van Rossume65cce51993-11-08 15:05:21 +0000126 raise TestFailed, 'filter (filter a string)'
127if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
128 raise TestFailed, 'filter (remove false values)'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000129if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
Guido van Rossume65cce51993-11-08 15:05:21 +0000130 raise TestFailed, 'filter (keep positives)'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131class Squares:
132 def __init__(self, max):
133 self.max = max
134 self.sofar = []
135 def __len__(self): return len(self.sofar)
136 def __getitem__(self, i):
137 if not 0 <= i < self.max: raise IndexError
138 n = len(self.sofar)
139 while n <= i:
140 self.sofar.append(n*n)
141 n = n+1
142 return self.sofar[i]
143if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
144 raise TestFailed, 'filter(None, Squares(10))'
145if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
146 raise TestFailed, 'filter(oddp, Squares(10))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000147
Guido van Rossum3bead091992-01-27 17:00:37 +0000148print 'float'
149if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
150if float(314) <> 314.0: raise TestFailed, 'float(314)'
151if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
152
153print 'getattr'
154import sys
155if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
156
Guido van Rossume23b62f1994-11-10 22:25:26 +0000157print 'hasattr'
158import sys
159if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
160
161print 'hash'
162hash(None)
163if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
164hash('spam')
165hash((0,1,2,3))
166def f(): pass
167
Guido van Rossum3bead091992-01-27 17:00:37 +0000168print 'hex'
169if hex(16) != '0x10': raise TestFailed, 'hex(16)'
170if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
171if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
172if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
173
Guido van Rossume23b62f1994-11-10 22:25:26 +0000174print 'id'
175id(None)
176id(1)
177id(1L)
178id(1.0)
179id('spam')
180id((0,1,2,3))
181id([0,1,2,3])
182id({'spam': 1, 'eggs': 2, 'ham': 3})
183
Guido van Rossum3bead091992-01-27 17:00:37 +0000184# Test input() later, together with raw_input
185
186print 'int'
187if int(314) <> 314: raise TestFailed, 'int(314)'
188if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
189if int(314L) <> 314: raise TestFailed, 'int(314L)'
190
191print 'len'
192if len('123') <> 3: raise TestFailed, 'len(\'123\')'
193if len(()) <> 0: raise TestFailed, 'len(())'
194if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
195if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
196if len({}) <> 0: raise TestFailed, 'len({})'
197if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
198
199print 'long'
200if long(314) <> 314L: raise TestFailed, 'long(314)'
201if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
202if long(314L) <> 314L: raise TestFailed, 'long(314L)'
203
Guido van Rossume65cce51993-11-08 15:05:21 +0000204print 'map'
205if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
206 raise TestFailed, 'map(None, \'hello world\')'
207if map(None, 'abcd', 'efg') <> \
208 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
209 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
210if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
211 raise TestFailed, 'map(None, range(10))'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000212if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
213 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000214from math import sqrt
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000215if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
216 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
217if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
218 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
Guido van Rossume65cce51993-11-08 15:05:21 +0000219def plus(*v):
220 accu = 0
221 for i in v: accu = accu + i
222 return accu
223if map(plus, [1, 3, 7]) <> [1, 3, 7]:
224 raise TestFailed, 'map(plus, [1, 3, 7])'
225if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
226 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
227if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
228 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
230 raise TestFailed, 'map(None, Squares(10))'
231if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
232 raise TestFailed, 'map(int, Squares(10))'
233if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
234 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
235if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
236 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000237
Guido van Rossum3bead091992-01-27 17:00:37 +0000238print 'max'
239if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
240if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
241if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
242if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
243#
244if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
245if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
246if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
247
248print 'min'
249if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
250if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
251if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
252if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
253#
254if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
255if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
256if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'