1 # Python test set -- part 4b, built-in functions n-z
3 from test_support
import *
6 if oct(100) != '0144': raise TestFailed
, 'oct(100)'
7 if oct(100L) != '0144L': raise TestFailed
, 'oct(100L)'
8 if oct(-100) != '-0144': raise TestFailed
, 'oct(-100)'
9 if oct(-100L) != '-0144L': raise TestFailed
, 'oct(-100L)'
12 # NB the first 4 lines are also used to test input and raw_input, below
13 fp
= open(TESTFN
, 'w')
17 fp
.write('The quick brown fox jumps over the lazy dog')
19 fp
.write('Dear John\n')
25 fp
= open(TESTFN
, 'r')
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'
39 if ord(' ') <> 32: raise TestFailed
, 'ord(\' \')'
40 if ord('A') <> 65: raise TestFailed
, 'ord(\'A\')'
41 if ord('a') <> 97: raise TestFailed
, 'ord(\'a\')'
44 if pow(0,0) <> 1: raise TestFailed
, 'pow(0,0)'
45 if pow(0,1) <> 0: raise TestFailed
, 'pow(0,1)'
46 if pow(1,0) <> 1: raise TestFailed
, 'pow(1,0)'
47 if pow(1,1) <> 1: raise TestFailed
, 'pow(1,1)'
49 if pow(2,0) <> 1: raise TestFailed
, 'pow(2,0)'
50 if pow(2,10) <> 1024: raise TestFailed
, 'pow(2,10)'
51 if pow(2,20) <> 1024*1024: raise TestFailed
, 'pow(2,20)'
52 if pow(2,30) <> 1024*1024*1024: raise TestFailed
, 'pow(2,30)'
54 if pow(-2,0) <> 1: raise TestFailed
, 'pow(-2,0)'
55 if pow(-2,1) <> -2: raise TestFailed
, 'pow(-2,1)'
56 if pow(-2,2) <> 4: raise TestFailed
, 'pow(-2,2)'
57 if pow(-2,3) <> -8: raise TestFailed
, 'pow(-2,3)'
59 if pow(0L,0) <> 1: raise TestFailed
, 'pow(0L,0)'
60 if pow(0L,1) <> 0: raise TestFailed
, 'pow(0L,1)'
61 if pow(1L,0) <> 1: raise TestFailed
, 'pow(1L,0)'
62 if pow(1L,1) <> 1: raise TestFailed
, 'pow(1L,1)'
64 if pow(2L,0) <> 1: raise TestFailed
, 'pow(2L,0)'
65 if pow(2L,10) <> 1024: raise TestFailed
, 'pow(2L,10)'
66 if pow(2L,20) <> 1024*1024: raise TestFailed
, 'pow(2L,20)'
67 if pow(2L,30) <> 1024*1024*1024: raise TestFailed
, 'pow(2L,30)'
69 if pow(-2L,0) <> 1: raise TestFailed
, 'pow(-2L,0)'
70 if pow(-2L,1) <> -2: raise TestFailed
, 'pow(-2L,1)'
71 if pow(-2L,2) <> 4: raise TestFailed
, 'pow(-2L,2)'
72 if pow(-2L,3) <> -8: raise TestFailed
, 'pow(-2L,3)'
74 if fcmp(pow(0.,0), 1.): raise TestFailed
, 'pow(0.,0)'
75 if fcmp(pow(0.,1), 0.): raise TestFailed
, 'pow(0.,1)'
76 if fcmp(pow(1.,0), 1.): raise TestFailed
, 'pow(1.,0)'
77 if fcmp(pow(1.,1), 1.): raise TestFailed
, 'pow(1.,1)'
79 if fcmp(pow(2.,0), 1.): raise TestFailed
, 'pow(2.,0)'
80 if fcmp(pow(2.,10), 1024.): raise TestFailed
, 'pow(2.,10)'
81 if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed
, 'pow(2.,20)'
82 if fcmp(pow(2.,30), 1024.*1024.*1024.): raise TestFailed
, 'pow(2.,30)'
84 # XXX These don't work -- negative float to the float power...
85 #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)'
91 for y
in 10, 10L, 10.0:
92 for z
in 1000, 1000L, 1000.0:
93 if fcmp(pow(x
, y
, z
), 24.0):
94 raise TestFailed
, 'pow(%s, %s, %s)' % (x
, y
, z
)
97 if range(3) <> [0, 1, 2]: raise TestFailed
, 'range(3)'
98 if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed
, 'range(1, 5)'
99 if range(0) <> []: raise TestFailed
, 'range(0)'
100 if range(-3) <> []: raise TestFailed
, 'range(-3)'
101 if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed
, 'range(1, 10, 3)'
102 if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed
, 'range(5, -5, -3)'
104 print 'input and raw_input'
106 fp
= open(TESTFN
, 'r')
107 savestdin
= sys
.stdin
110 if input() <> 2: raise TestFailed
, 'input()'
111 if input('testing\n') <> 2: raise TestFailed
, 'input()'
112 if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
113 raise TestFailed
, 'raw_input()'
114 if raw_input('testing\n') <> 'Dear John':
115 raise TestFailed
, 'raw_input(\'testing\\n\')'
117 sys
.stdin
= savestdin
121 if reduce(lambda x
, y
: x
+y
, ['a', 'b', 'c'], '') <> 'abc':
122 raise TestFailed
, 'reduce(): implode a string'
123 if reduce(lambda x
, y
: x
+y
,
124 [['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']:
125 raise TestFailed
, 'reduce(): append'
126 if reduce(lambda x
, y
: x
*y
, range(2,8), 1) <> 5040:
127 raise TestFailed
, 'reduce(): compute 7!'
128 if reduce(lambda x
, y
: x
*y
, range(2,21), 1L) <> 2432902008176640000L:
129 raise TestFailed
, 'reduce(): compute 20!, use long'
131 def __init__(self
, max):
134 def __len__(self
): return len(self
.sofar
)
135 def __getitem__(self
, i
):
136 if not 0 <= i
< self
.max: raise IndexError
139 self
.sofar
.append(n
*n
)
142 if reduce(lambda x
, y
: x
+y
, Squares(10)) != 285:
143 raise TestFailed
, 'reduce(<+>, Squares(10))'
144 if reduce(lambda x
, y
: x
+y
, Squares(10), 0) != 285:
145 raise TestFailed
, 'reduce(<+>, Squares(10), 0)'
146 if reduce(lambda x
, y
: x
+y
, Squares(0), 0) != 0:
147 raise TestFailed
, 'reduce(<+>, Squares(0), 0)'
157 except ImportError: pass
158 else: raise TestFailed
, 'reload(sys) should fail'
161 if repr('') <> '\'\'': raise TestFailed
, 'repr(\'\')'
162 if repr(0) <> '0': raise TestFailed
, 'repr(0)'
163 if repr(0L) <> '0L': raise TestFailed
, 'repr(0L)'
164 if repr(()) <> '()': raise TestFailed
, 'repr(())'
165 if repr([]) <> '[]': raise TestFailed
, 'repr([])'
166 if repr({}) <> '{}': raise TestFailed
, 'repr({})'
169 if round(0.0) <> 0.0: raise TestFailed
, 'round(0.0)'
170 if round(1.0) <> 1.0: raise TestFailed
, 'round(1.0)'
171 if round(10.0) <> 10.0: raise TestFailed
, 'round(10.0)'
172 if round(1000000000.0) <> 1000000000.0:
173 raise TestFailed
, 'round(1000000000.0)'
174 if round(1e20
) <> 1e20
: raise TestFailed
, 'round(1e20)'
176 if round(-1.0) <> -1.0: raise TestFailed
, 'round(-1.0)'
177 if round(-10.0) <> -10.0: raise TestFailed
, 'round(-10.0)'
178 if round(-1000000000.0) <> -1000000000.0:
179 raise TestFailed
, 'round(-1000000000.0)'
180 if round(-1e20
) <> -1e20
: raise TestFailed
, 'round(-1e20)'
182 if round(0.1) <> 0.0: raise TestFailed
, 'round(0.0)'
183 if round(1.1) <> 1.0: raise TestFailed
, 'round(1.0)'
184 if round(10.1) <> 10.0: raise TestFailed
, 'round(10.0)'
185 if round(1000000000.1) <> 1000000000.0:
186 raise TestFailed
, 'round(1000000000.0)'
188 if round(-1.1) <> -1.0: raise TestFailed
, 'round(-1.0)'
189 if round(-10.1) <> -10.0: raise TestFailed
, 'round(-10.0)'
190 if round(-1000000000.1) <> -1000000000.0:
191 raise TestFailed
, 'round(-1000000000.0)'
193 if round(0.9) <> 1.0: raise TestFailed
, 'round(0.9)'
194 if round(9.9) <> 10.0: raise TestFailed
, 'round(9.9)'
195 if round(999999999.9) <> 1000000000.0:
196 raise TestFailed
, 'round(999999999.9)'
198 if round(-0.9) <> -1.0: raise TestFailed
, 'round(-0.9)'
199 if round(-9.9) <> -10.0: raise TestFailed
, 'round(-9.9)'
200 if round(-999999999.9) <> -1000000000.0:
201 raise TestFailed
, 'round(-999999999.9)'
205 setattr(sys
, 'spam', 1)
206 if sys
.spam
!= 1: raise TestFailed
, 'setattr(sys, \'spam\', 1)'
209 if str('') <> '': raise TestFailed
, 'str(\'\')'
210 if str(0) <> '0': raise TestFailed
, 'str(0)'
211 if str(0L) <> '0L': raise TestFailed
, 'str(0L)'
212 if str(()) <> '()': raise TestFailed
, 'str(())'
213 if str([]) <> '[]': raise TestFailed
, 'str([])'
214 if str({}) <> '{}': raise TestFailed
, 'str({})'
217 if tuple(()) <> (): raise TestFailed
, 'tuple(())'
218 if tuple((0, 1, 2, 3)) <> (0, 1, 2, 3): raise TestFailed
, 'tuple((0, 1, 2, 3))'
219 if tuple([]) <> (): raise TestFailed
, 'tuple([])'
220 if tuple([0, 1, 2, 3]) <> (0, 1, 2, 3): raise TestFailed
, 'tuple([0, 1, 2, 3])'
221 if tuple('') <> (): raise TestFailed
, 'tuple('')'
222 if tuple('spam') <> ('s', 'p', 'a', 'm'): raise TestFailed
, "tuple('spam')"
225 if type('') <> type('123') or type('') == type(()):
226 raise TestFailed
, 'type()'
234 if a
<> b
: raise TestFailed
, 'vars()'
240 if a
<> b
: raise TestFailed
, 'vars(sys)'
242 if vars() != {}: raise TestFailed
, 'vars() in f0()'
248 if vars() != {'a': a
, 'b': b
}: raise TestFailed
, 'vars() in f2()'
252 if tuple(xrange(10)) <> tuple(range(10)): raise TestFailed
, 'xrange(10)'
253 if tuple(xrange(5,10)) <> tuple(range(5,10)): raise TestFailed
, 'xrange(5,10)'
254 if tuple(xrange(0,10,2)) <> tuple(range(0,10,2)):
255 raise TestFailed
, 'xrange(0,10,2)'
258 # Epilogue -- unlink the temp file