blob: b780c729edd1b3ab99188023f283f47a9f6100c0 [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:
28 raise TestFailed, 'f2 called with ' + `a1, a2, a3`
29apply(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'
45if coerce(1, 1.1) <> (1.0, 1.1): raise TestFailed, 'coerce(1, 1.1)'
46if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
47if coerce(1L, 1.1) <> (1.0, 1.1): raise TestFailed, 'coerce(1L, 1.1)'
48
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#
71if divmod(3.25, 1.0) <> (3.0, 0.25): raise TestFailed, 'divmod(3.25, 1.0)'
72if divmod(-3.25, 1.0) <> (-4.0, 0.75): raise TestFailed, 'divmod(-3.25, 1.0)'
73if divmod(3.25, -1.0) <> (-4.0, -0.75): raise TestFailed, 'divmod(3.25, -1.0)'
74if divmod(-3.25, -1.0) <> (3.0, -0.25): raise TestFailed, 'divmod(-3.25, -1.0)'
75
76print 'eval'
77if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
Guido van Rossum85f18201992-11-27 22:53:50 +000078if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
Guido van Rossum3bead091992-01-27 17:00:37 +000079
80print 'exec'
81z = 0
82exec('z=1+1\n')
83if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
Guido van Rossum85f18201992-11-27 22:53:50 +000084z = 0
85exec('z=1+1')
86if z <> 2: raise TestFailed, 'exec(\'z=1+1\')'
87
88print 'execfile'
89z = 0
90f = open(TESTFN, 'w')
91f.write('z = z+1\n')
92f.write('z = z*2\n')
93f.close()
94execfile(TESTFN)
95unlink(TESTFN)
Guido van Rossum3bead091992-01-27 17:00:37 +000096
97print 'float'
98if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
99if float(314) <> 314.0: raise TestFailed, 'float(314)'
100if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
101
102print 'getattr'
103import sys
104if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
105
106print 'hex'
107if hex(16) != '0x10': raise TestFailed, 'hex(16)'
108if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
109if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
110if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
111
112# Test input() later, together with raw_input
113
114print 'int'
115if int(314) <> 314: raise TestFailed, 'int(314)'
116if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
117if int(314L) <> 314: raise TestFailed, 'int(314L)'
118
119print 'len'
120if len('123') <> 3: raise TestFailed, 'len(\'123\')'
121if len(()) <> 0: raise TestFailed, 'len(())'
122if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
123if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
124if len({}) <> 0: raise TestFailed, 'len({})'
125if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
126
127print 'long'
128if long(314) <> 314L: raise TestFailed, 'long(314)'
129if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
130if long(314L) <> 314L: raise TestFailed, 'long(314L)'
131
132print 'max'
133if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
134if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
135if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
136if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
137#
138if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
139if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
140if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
141
142print 'min'
143if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
144if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
145if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
146if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
147#
148if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
149if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
150if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'