blob: 1fafc4f59f43a6e10dc92103777fe6d54d1d92f8 [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
39print 'dir'
40x = 1
41if 'x' not in dir(): raise TestFailed, 'dir()'
42import sys
43if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
44
45print 'divmod'
46if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
47if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
48if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
49if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
50#
51if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
52if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
53if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
54if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
55#
56if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
57if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
58if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
59if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
60#
61if divmod(3.25, 1.0) <> (3.0, 0.25): raise TestFailed, 'divmod(3.25, 1.0)'
62if divmod(-3.25, 1.0) <> (-4.0, 0.75): raise TestFailed, 'divmod(-3.25, 1.0)'
63if divmod(3.25, -1.0) <> (-4.0, -0.75): raise TestFailed, 'divmod(3.25, -1.0)'
64if divmod(-3.25, -1.0) <> (3.0, -0.25): raise TestFailed, 'divmod(-3.25, -1.0)'
65
66print 'eval'
67if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
68
69print 'exec'
70z = 0
71exec('z=1+1\n')
72if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
73
74print 'float'
75if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
76if float(314) <> 314.0: raise TestFailed, 'float(314)'
77if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
78
79print 'getattr'
80import sys
81if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
82
83print 'hex'
84if hex(16) != '0x10': raise TestFailed, 'hex(16)'
85if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
86if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
87if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
88
89# Test input() later, together with raw_input
90
91print 'int'
92if int(314) <> 314: raise TestFailed, 'int(314)'
93if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
94if int(314L) <> 314: raise TestFailed, 'int(314L)'
95
96print 'len'
97if len('123') <> 3: raise TestFailed, 'len(\'123\')'
98if len(()) <> 0: raise TestFailed, 'len(())'
99if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
100if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
101if len({}) <> 0: raise TestFailed, 'len({})'
102if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
103
104print 'long'
105if long(314) <> 314L: raise TestFailed, 'long(314)'
106if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
107if long(314L) <> 314L: raise TestFailed, 'long(314L)'
108
109print 'max'
110if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
111if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
112if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
113if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
114#
115if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
116if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
117if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
118
119print 'min'
120if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
121if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
122if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
123if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
124#
125if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
126if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
127if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'