blob: 3fab517c0ff1470a9792381697a15ffdb838871a [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
5print 'abs'
6if abs(0) <> 0: raise TestFailed, 'abs(0)'
7if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
8if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
9#
10if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
11if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
12if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
13#
14if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
15if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
16if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
17
18print 'apply'
19def f0(*args):
20 if args != (): raise TestFailed, 'f0 called with ' + `args`
21def f1(a1):
22 if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
23def f2(a1, a2):
24 if a1 != 1 or a2 != 2:
25 raise TestFailed, 'f2 called with ' + `a1, a2`
26def f3(a1, a2, a3):
27 if a1 != 1 or a2 != 2 or a3 != 3:
Guido van Rossumb3b09c91993-10-22 14:24:22 +000028 raise TestFailed, 'f3 called with ' + `a1, a2, a3`
Guido van Rossum3bead091992-01-27 17:00:37 +000029apply(f0, ())
30apply(f1, (1,))
31apply(f2, (1, 2))
32apply(f3, (1, 2, 3))
33
Guido van Rossume23b62f1994-11-10 22:25:26 +000034print 'callable'
35if not callable(len):raise TestFailed, 'callable(len)'
36def f(): pass
37if not callable(f): raise TestFailed, 'callable(f)'
38class C:
39 def meth(self): pass
40if not callable(C): raise TestFailed, 'callable(C)'
41x = C()
42if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
43if callable(x): raise TestFailed, 'callable(x)'
44class D(C):
45 def __call__(self): pass
46y = D()
47if not callable(y): raise TestFailed, 'callable(y)'
48
Guido van Rossum3bead091992-01-27 17:00:37 +000049print 'chr'
50if chr(32) <> ' ': raise TestFailed, 'chr(32)'
51if chr(65) <> 'A': raise TestFailed, 'chr(65)'
52if chr(97) <> 'a': raise TestFailed, 'chr(97)'
53
Guido van Rossum85f18201992-11-27 22:53:50 +000054print 'cmp'
55if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
56if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
57if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'
58
59print 'coerce'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000060if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000061if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000062if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000063
Guido van Rossume23b62f1994-11-10 22:25:26 +000064print 'compile'
65compile('print 1\n', '', 'exec')
66
67print 'delattr'
68import sys
69sys.spam = 1
70delattr(sys, 'spam')
71
Guido van Rossum3bead091992-01-27 17:00:37 +000072print 'dir'
73x = 1
74if 'x' not in dir(): raise TestFailed, 'dir()'
75import sys
76if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
77
78print 'divmod'
79if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
80if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
81if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
82if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
83#
84if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
85if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
86if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
87if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
88#
89if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
90if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
91if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
92if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
93#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000094if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
95 raise TestFailed, 'divmod(3.25, 1.0)'
96if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
97 raise TestFailed, 'divmod(-3.25, 1.0)'
98if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
99 raise TestFailed, 'divmod(3.25, -1.0)'
100if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
101 raise TestFailed, 'divmod(-3.25, -1.0)'
Guido van Rossum3bead091992-01-27 17:00:37 +0000102
103print 'eval'
104if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
Guido van Rossum85f18201992-11-27 22:53:50 +0000105if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
Guido van Rossum3bead091992-01-27 17:00:37 +0000106
Guido van Rossum85f18201992-11-27 22:53:50 +0000107print 'execfile'
108z = 0
109f = open(TESTFN, 'w')
110f.write('z = z+1\n')
111f.write('z = z*2\n')
112f.close()
113execfile(TESTFN)
114unlink(TESTFN)
Guido van Rossum3bead091992-01-27 17:00:37 +0000115
Guido van Rossume65cce51993-11-08 15:05:21 +0000116print 'filter'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000117if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
Guido van Rossume65cce51993-11-08 15:05:21 +0000118 raise TestFailed, 'filter (filter a string)'
119if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
120 raise TestFailed, 'filter (remove false values)'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000121if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
Guido van Rossume65cce51993-11-08 15:05:21 +0000122 raise TestFailed, 'filter (keep positives)'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123class Squares:
124 def __init__(self, max):
125 self.max = max
126 self.sofar = []
127 def __len__(self): return len(self.sofar)
128 def __getitem__(self, i):
129 if not 0 <= i < self.max: raise IndexError
130 n = len(self.sofar)
131 while n <= i:
132 self.sofar.append(n*n)
133 n = n+1
134 return self.sofar[i]
135if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
136 raise TestFailed, 'filter(None, Squares(10))'
137if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
138 raise TestFailed, 'filter(oddp, Squares(10))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000139
Guido van Rossum3bead091992-01-27 17:00:37 +0000140print 'float'
141if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
142if float(314) <> 314.0: raise TestFailed, 'float(314)'
143if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
144
145print 'getattr'
146import sys
147if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
148
Guido van Rossume23b62f1994-11-10 22:25:26 +0000149print 'hasattr'
150import sys
151if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
152
153print 'hash'
154hash(None)
155if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
156hash('spam')
157hash((0,1,2,3))
158def f(): pass
159
Guido van Rossum3bead091992-01-27 17:00:37 +0000160print 'hex'
161if hex(16) != '0x10': raise TestFailed, 'hex(16)'
162if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
163if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
164if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
165
Guido van Rossume23b62f1994-11-10 22:25:26 +0000166print 'id'
167id(None)
168id(1)
169id(1L)
170id(1.0)
171id('spam')
172id((0,1,2,3))
173id([0,1,2,3])
174id({'spam': 1, 'eggs': 2, 'ham': 3})
175
Guido van Rossum3bead091992-01-27 17:00:37 +0000176# Test input() later, together with raw_input
177
178print 'int'
179if int(314) <> 314: raise TestFailed, 'int(314)'
180if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
181if int(314L) <> 314: raise TestFailed, 'int(314L)'
182
183print 'len'
184if len('123') <> 3: raise TestFailed, 'len(\'123\')'
185if len(()) <> 0: raise TestFailed, 'len(())'
186if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
187if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
188if len({}) <> 0: raise TestFailed, 'len({})'
189if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
190
191print 'long'
192if long(314) <> 314L: raise TestFailed, 'long(314)'
193if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
194if long(314L) <> 314L: raise TestFailed, 'long(314L)'
195
Guido van Rossume65cce51993-11-08 15:05:21 +0000196print 'map'
197if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
198 raise TestFailed, 'map(None, \'hello world\')'
199if map(None, 'abcd', 'efg') <> \
200 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
201 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
202if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
203 raise TestFailed, 'map(None, range(10))'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000204if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
205 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000206from math import sqrt
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000207if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
208 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
209if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
210 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
Guido van Rossume65cce51993-11-08 15:05:21 +0000211def plus(*v):
212 accu = 0
213 for i in v: accu = accu + i
214 return accu
215if map(plus, [1, 3, 7]) <> [1, 3, 7]:
216 raise TestFailed, 'map(plus, [1, 3, 7])'
217if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
218 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
219if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
220 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
222 raise TestFailed, 'map(None, Squares(10))'
223if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
224 raise TestFailed, 'map(int, Squares(10))'
225if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
226 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
227if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
228 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000229
Guido van Rossum3bead091992-01-27 17:00:37 +0000230print 'max'
231if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
232if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
233if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
234if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
235#
236if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
237if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
238if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
239
240print 'min'
241if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
242if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
243if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
244if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
245#
246if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
247if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
248if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'