blob: 434b3799245adb08778e1a20a53b3e5b1e497eff [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'
Guido van Rossumae3b3a31993-11-30 13:43:54 +000094if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
Guido van Rossume65cce51993-11-08 15:05:21 +000095 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)'
Guido van Rossumae3b3a31993-11-30 13:43:54 +000098if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
Guido van Rossume65cce51993-11-08 15:05:21 +000099 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
123print 'len'
124if len('123') <> 3: raise TestFailed, 'len(\'123\')'
125if len(()) <> 0: raise TestFailed, 'len(())'
126if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
127if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
128if len({}) <> 0: raise TestFailed, 'len({})'
129if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
130
131print 'long'
132if long(314) <> 314L: raise TestFailed, 'long(314)'
133if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
134if long(314L) <> 314L: raise TestFailed, 'long(314L)'
135
Guido van Rossume65cce51993-11-08 15:05:21 +0000136print 'map'
137if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
138 raise TestFailed, 'map(None, \'hello world\')'
139if map(None, 'abcd', 'efg') <> \
140 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
141 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
142if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
143 raise TestFailed, 'map(None, range(10))'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000144if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
145 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
Guido van Rossume65cce51993-11-08 15:05:21 +0000146from math import sqrt
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000147if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
148 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
149if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
150 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
Guido van Rossume65cce51993-11-08 15:05:21 +0000151def plus(*v):
152 accu = 0
153 for i in v: accu = accu + i
154 return accu
155if map(plus, [1, 3, 7]) <> [1, 3, 7]:
156 raise TestFailed, 'map(plus, [1, 3, 7])'
157if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
158 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
159if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
160 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
161
Guido van Rossum3bead091992-01-27 17:00:37 +0000162print 'max'
163if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
164if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
165if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
166if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
167#
168if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
169if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
170if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
171
172print 'min'
173if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
174if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
175if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
176if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
177#
178if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
179if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
180if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'