blob: 10c0bfb677deedaf3fbc6f5e4efe9b70d3a1f797 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 4b, built-in functions n-z
2
3from test_support import *
4
5print 'oct'
6if oct(100) != '0144': raise TestFailed, 'oct(100)'
7if oct(100L) != '0144L': raise TestFailed, 'oct(100L)'
8if oct(-100) != '-0144': raise TestFailed, 'oct(-100)'
9if oct(-100L) != '-0144L': raise TestFailed, 'oct(-100L)'
10
11print 'open'
12# NB the first 4 lines are also used to test input and raw_input, below
13fp = open(TESTFN, 'w')
14try:
15 fp.write('1+1\n')
16 fp.write('1+1\n')
17 fp.write('The quick brown fox jumps over the lazy dog')
18 fp.write('.\n')
19 fp.write('Dear John\n')
20 fp.write('XXX'*100)
21 fp.write('YYY'*100)
22finally:
23 fp.close()
24#
25fp = open(TESTFN, 'r')
26try:
27 if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact'
28 if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact'
29 if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
30 raise TestFailed, 'readline() # default'
31 if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
32 if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
33 if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
34 if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
35finally:
36 fp.close()
37
38print 'ord'
39if ord(' ') <> 32: raise TestFailed, 'ord(\' \')'
40if ord('A') <> 65: raise TestFailed, 'ord(\'A\')'
41if ord('a') <> 97: raise TestFailed, 'ord(\'a\')'
42
43print 'pow'
44if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)'
45if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)'
46if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)'
47if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)'
48#
49if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)'
50if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)'
51if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)'
52if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)'
53#
54if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)'
55if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)'
56if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)'
57if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)'
58#
59if pow(0L,0) <> 1: raise TestFailed, 'pow(0L,0)'
60if pow(0L,1) <> 0: raise TestFailed, 'pow(0L,1)'
61if pow(1L,0) <> 1: raise TestFailed, 'pow(1L,0)'
62if pow(1L,1) <> 1: raise TestFailed, 'pow(1L,1)'
63#
64if pow(2L,0) <> 1: raise TestFailed, 'pow(2L,0)'
65if pow(2L,10) <> 1024: raise TestFailed, 'pow(2L,10)'
66if pow(2L,20) <> 1024*1024: raise TestFailed, 'pow(2L,20)'
67if pow(2L,30) <> 1024*1024*1024: raise TestFailed, 'pow(2L,30)'
68#
69if pow(-2L,0) <> 1: raise TestFailed, 'pow(-2L,0)'
70if pow(-2L,1) <> -2: raise TestFailed, 'pow(-2L,1)'
71if pow(-2L,2) <> 4: raise TestFailed, 'pow(-2L,2)'
72if pow(-2L,3) <> -8: raise TestFailed, 'pow(-2L,3)'
73#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000074if fcmp(pow(0.,0), 1.): raise TestFailed, 'pow(0.,0)'
75if fcmp(pow(0.,1), 0.): raise TestFailed, 'pow(0.,1)'
76if fcmp(pow(1.,0), 1.): raise TestFailed, 'pow(1.,0)'
77if fcmp(pow(1.,1), 1.): raise TestFailed, 'pow(1.,1)'
Guido van Rossum3bead091992-01-27 17:00:37 +000078#
Guido van Rossum35fb82a1993-01-26 13:04:43 +000079if fcmp(pow(2.,0), 1.): raise TestFailed, 'pow(2.,0)'
80if fcmp(pow(2.,10), 1024.): raise TestFailed, 'pow(2.,10)'
81if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed, 'pow(2.,20)'
82if fcmp(pow(2.,30), 1024.*1024.*1024.): raise TestFailed, 'pow(2.,30)'
Guido van Rossum3bead091992-01-27 17:00:37 +000083#
84# XXX These don't work -- negative float to the float power...
Guido van Rossum35fb82a1993-01-26 13:04:43 +000085#if fcmp(pow(-2.,0), 1.): raise TestFailed, 'pow(-2.,0)'
86#if fcmp(pow(-2.,1), -2.): raise TestFailed, 'pow(-2.,1)'
87#if fcmp(pow(-2.,2), 4.): raise TestFailed, 'pow(-2.,2)'
88#if fcmp(pow(-2.,3), -8.): raise TestFailed, 'pow(-2.,3)'
Guido van Rossum3bead091992-01-27 17:00:37 +000089
90print 'range'
91if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
92if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
93if range(0) <> []: raise TestFailed, 'range(0)'
94if range(-3) <> []: raise TestFailed, 'range(-3)'
95if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
96if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
97
98print 'input and raw_input'
99import sys
100fp = open(TESTFN, 'r')
101savestdin = sys.stdin
102try:
103 sys.stdin = fp
104 if input() <> 2: raise TestFailed, 'input()'
105 if input('testing\n') <> 2: raise TestFailed, 'input()'
106 if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
107 raise TestFailed, 'raw_input()'
108 if raw_input('testing\n') <> 'Dear John':
109 raise TestFailed, 'raw_input(\'testing\\n\')'
110finally:
111 sys.stdin = savestdin
112 fp.close()
113
Guido van Rossume65cce51993-11-08 15:05:21 +0000114print 'reduce'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000115if reduce(lambda x, y: x+y, ['a', 'b', 'c'], '') <> 'abc':
Guido van Rossume65cce51993-11-08 15:05:21 +0000116 raise TestFailed, 'reduce(): implode a string'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000117if reduce(lambda x, y: x+y,
118 [['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']:
Guido van Rossume65cce51993-11-08 15:05:21 +0000119 raise TestFailed, 'reduce(): append'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000120if reduce(lambda x, y: x*y, range(2,8), 1) <> 5040:
Guido van Rossume65cce51993-11-08 15:05:21 +0000121 raise TestFailed, 'reduce(): compute 7!'
Guido van Rossumae3b3a31993-11-30 13:43:54 +0000122if reduce(lambda x, y: x*y, range(2,21), 1L) <> 2432902008176640000L:
Guido van Rossume65cce51993-11-08 15:05:21 +0000123 raise TestFailed, 'reduce(): compute 20!, use long'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124class Squares:
125 def __init__(self, max):
126 self.max = max
127 self.sofar = []
128 def __len__(self): return len(self.sofar)
129 def __getitem__(self, i):
130 if not 0 <= i < self.max: raise IndexError
131 n = len(self.sofar)
132 while n <= i:
133 self.sofar.append(n*n)
134 n = n+1
135 return self.sofar[i]
136if reduce(lambda x, y: x+y, Squares(10)) != 285:
137 raise TestFailed, 'reduce(<+>, Squares(10))'
138if reduce(lambda x, y: x+y, Squares(10), 0) != 285:
139 raise TestFailed, 'reduce(<+>, Squares(10), 0)'
140if reduce(lambda x, y: x+y, Squares(0), 0) != 0:
141 raise TestFailed, 'reduce(<+>, Squares(0), 0)'
142
Guido van Rossume65cce51993-11-08 15:05:21 +0000143
Guido van Rossum3bead091992-01-27 17:00:37 +0000144print 'reload'
145import string
146reload(string)
147
Guido van Rossum85f18201992-11-27 22:53:50 +0000148print 'repr'
149if repr('') <> '\'\'': raise TestFailed, 'repr(\'\')'
150if repr(0) <> '0': raise TestFailed, 'repr(0)'
151if repr(0L) <> '0L': raise TestFailed, 'repr(0L)'
152if repr(()) <> '()': raise TestFailed, 'repr(())'
153if repr([]) <> '[]': raise TestFailed, 'repr([])'
154if repr({}) <> '{}': raise TestFailed, 'repr({})'
155
Guido van Rossume7113b61993-03-29 11:30:50 +0000156print 'round'
157if round(0.0) <> 0.0: raise TestFailed, 'round(0.0)'
158if round(1.0) <> 1.0: raise TestFailed, 'round(1.0)'
159if round(10.0) <> 10.0: raise TestFailed, 'round(10.0)'
160if round(1000000000.0) <> 1000000000.0:
161 raise TestFailed, 'round(1000000000.0)'
162if round(1e20) <> 1e20: raise TestFailed, 'round(1e20)'
163
164if round(-1.0) <> -1.0: raise TestFailed, 'round(-1.0)'
165if round(-10.0) <> -10.0: raise TestFailed, 'round(-10.0)'
166if round(-1000000000.0) <> -1000000000.0:
167 raise TestFailed, 'round(-1000000000.0)'
168if round(-1e20) <> -1e20: raise TestFailed, 'round(-1e20)'
169
170if round(0.1) <> 0.0: raise TestFailed, 'round(0.0)'
171if round(1.1) <> 1.0: raise TestFailed, 'round(1.0)'
172if round(10.1) <> 10.0: raise TestFailed, 'round(10.0)'
173if round(1000000000.1) <> 1000000000.0:
174 raise TestFailed, 'round(1000000000.0)'
175
176if round(-1.1) <> -1.0: raise TestFailed, 'round(-1.0)'
177if round(-10.1) <> -10.0: raise TestFailed, 'round(-10.0)'
178if round(-1000000000.1) <> -1000000000.0:
179 raise TestFailed, 'round(-1000000000.0)'
180
181if round(0.9) <> 1.0: raise TestFailed, 'round(0.9)'
182if round(9.9) <> 10.0: raise TestFailed, 'round(9.9)'
183if round(999999999.9) <> 1000000000.0:
184 raise TestFailed, 'round(999999999.9)'
185
186if round(-0.9) <> -1.0: raise TestFailed, 'round(-0.9)'
187if round(-9.9) <> -10.0: raise TestFailed, 'round(-9.9)'
188if round(-999999999.9) <> -1000000000.0:
189 raise TestFailed, 'round(-999999999.9)'
190
Guido van Rossum3bead091992-01-27 17:00:37 +0000191print 'setattr'
192import sys
193setattr(sys, 'foobar', 1)
194if sys.foobar != 1: raise TestFailed, 'setattr(sys, \'foobar\', 1)'
195
Guido van Rossum85f18201992-11-27 22:53:50 +0000196print 'str'
197if str('') <> '': raise TestFailed, 'str(\'\')'
198if str(0) <> '0': raise TestFailed, 'str(0)'
199if str(0L) <> '0L': raise TestFailed, 'str(0L)'
200if str(()) <> '()': raise TestFailed, 'str(())'
201if str([]) <> '[]': raise TestFailed, 'str([])'
202if str({}) <> '{}': raise TestFailed, 'str({})'
203
Guido van Rossum3bead091992-01-27 17:00:37 +0000204print 'type'
205if type('') <> type('123') or type('') == type(()):
206 raise TestFailed, 'type()'
207
208
209# Epilogue -- unlink the temp file
210
211unlink(TESTFN)