blob: 396c5c2fa0adcedaefc4bf8414d325554be0407f [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
34print 'chr'
35if chr(32) <> ' ': raise TestFailed, 'chr(32)'
36if chr(65) <> 'A': raise TestFailed, 'chr(65)'
37if chr(97) <> 'a': raise TestFailed, 'chr(97)'
38
Guido van Rossum85f18201992-11-27 22:53:50 +000039print 'cmp'
40if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
41if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
42if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'
43
44print 'coerce'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000045if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000046if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
Guido van Rossum35fb82a1993-01-26 13:04:43 +000047if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
Guido van Rossum85f18201992-11-27 22:53:50 +000048
Guido van Rossum3bead091992-01-27 17:00:37 +000049print 'dir'
50x = 1
51if 'x' not in dir(): raise TestFailed, 'dir()'
52import sys
53if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
54
55print 'divmod'
56if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
57if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
58if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
59if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
60#
61if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
62if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
63if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
64if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
65#
66if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
67if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
68if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
69if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
70#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000071if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
72 raise TestFailed, 'divmod(3.25, 1.0)'
73if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
74 raise TestFailed, 'divmod(-3.25, 1.0)'
75if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
76 raise TestFailed, 'divmod(3.25, -1.0)'
77if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
78 raise TestFailed, 'divmod(-3.25, -1.0)'
Guido van Rossum3bead091992-01-27 17:00:37 +000079
80print 'eval'
81if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
Guido van Rossum85f18201992-11-27 22:53:50 +000082if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
Guido van Rossum3bead091992-01-27 17:00:37 +000083
Guido van Rossum85f18201992-11-27 22:53:50 +000084print 'execfile'
85z = 0
86f = open(TESTFN, 'w')
87f.write('z = z+1\n')
88f.write('z = z*2\n')
89f.close()
90execfile(TESTFN)
91unlink(TESTFN)
Guido van Rossum3bead091992-01-27 17:00:37 +000092
Guido van Rossume65cce51993-11-08 15:05:21 +000093print 'filter'
94if filter("c: 'a' <= c <= 'z'", 'Hello World') <> 'elloorld':
95 raise TestFailed, 'filter (filter a string)'
96if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
97 raise TestFailed, 'filter (remove false values)'
98if filter('x: x > 0', [1, -3, 9, 0, 2]) <> [1, 9, 2]:
99 raise TestFailed, 'filter (keep positives)'
100
Guido van Rossum3bead091992-01-27 17:00:37 +0000101print 'float'
102if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
103if float(314) <> 314.0: raise TestFailed, 'float(314)'
104if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
105
106print 'getattr'
107import sys
108if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
109
110print 'hex'
111if hex(16) != '0x10': raise TestFailed, 'hex(16)'
112if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
113if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
114if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
115
116# Test input() later, together with raw_input
117
118print 'int'
119if int(314) <> 314: raise TestFailed, 'int(314)'
120if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
121if int(314L) <> 314: raise TestFailed, 'int(314L)'
122
Guido van Rossume65cce51993-11-08 15:05:21 +0000123print 'lambda'
124binary_plus = lambda('x, y: x+y')
125if binary_plus(2, 10) <> 12:
126 raise TestFailed, 'binary_plus(2, 10)'
127
Guido van Rossum3bead091992-01-27 17:00:37 +0000128print 'len'
129if len('123') <> 3: raise TestFailed, 'len(\'123\')'
130if len(()) <> 0: raise TestFailed, 'len(())'
131if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
132if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
133if len({}) <> 0: raise TestFailed, 'len({})'
134if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
135
136print 'long'
137if long(314) <> 314L: raise TestFailed, 'long(314)'
138if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
139if long(314L) <> 314L: raise TestFailed, 'long(314L)'
140
Guido van Rossume65cce51993-11-08 15:05:21 +0000141print 'map'
142if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
143 raise TestFailed, 'map(None, \'hello world\')'
144if map(None, 'abcd', 'efg') <> \
145 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
146 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
147if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
148 raise TestFailed, 'map(None, range(10))'
149if map('x: x*x', range(1,4)) <> [1, 4, 9]:
150 raise TestFailed, 'map(\'x: x*x\', range(1,4))'
151from math import sqrt
152if map('x: map(sqrt,x)', [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
153 raise TestFailed, map('x: map(sqrt,x)', [[16, 4], [81, 9]])
154if map('x,y: x+y', [1,3,2], [9,1,4]) <> [10, 4, 6]:
155 raise TestFailed, 'map(\'x,y: x+y\', [1,3,2], [9,1,4])'
156def plus(*v):
157 accu = 0
158 for i in v: accu = accu + i
159 return accu
160if map(plus, [1, 3, 7]) <> [1, 3, 7]:
161 raise TestFailed, 'map(plus, [1, 3, 7])'
162if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
163 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
164if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
165 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
166
Guido van Rossum3bead091992-01-27 17:00:37 +0000167print 'max'
168if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
169if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
170if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
171if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
172#
173if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
174if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
175if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
176
177print 'min'
178if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
179if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
180if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
181if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
182#
183if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
184if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
185if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'