summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_b1.py
blob: 903cc56c186f566b76df1bf4340210d10785375f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Python test set -- part 4a, built-in functions a-m

from test_support import *

print '__import__'
__import__('sys')
__import__('strop')
__import__('string')
try: __import__('spamspam')
except ImportError: pass
else: raise TestFailed, "__import__('spamspam') should fail"

print 'abs'
if abs(0) <> 0: raise TestFailed, 'abs(0)'
if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
#
if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
#
if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'

print 'apply'
def f0(*args):
	if args != (): raise TestFailed, 'f0 called with ' + `args`
def f1(a1):
	if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
def f2(a1, a2):
	if a1 != 1 or a2 != 2:
		raise TestFailed, 'f2 called with ' + `a1, a2`
def f3(a1, a2, a3):
	if a1 != 1 or a2 != 2 or a3 != 3:
		raise TestFailed, 'f3 called with ' + `a1, a2, a3`
apply(f0, ())
apply(f1, (1,))
apply(f2, (1, 2))
apply(f3, (1, 2, 3))

print 'callable'
if not callable(len):raise TestFailed, 'callable(len)'
def f(): pass
if not callable(f): raise TestFailed, 'callable(f)'
class C:
	def meth(self): pass
if not callable(C): raise TestFailed, 'callable(C)'
x = C()
if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
if callable(x): raise TestFailed, 'callable(x)'
class D(C):
	def __call__(self): pass
y = D()
if not callable(y): raise TestFailed, 'callable(y)'

print 'chr'
if chr(32) <> ' ': raise TestFailed, 'chr(32)'
if chr(65) <> 'A': raise TestFailed, 'chr(65)'
if chr(97) <> 'a': raise TestFailed, 'chr(97)'

print 'cmp'
if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'

print 'coerce'
if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'

print 'compile'
compile('print 1\n', '', 'exec')

print 'delattr'
import sys
sys.spam = 1
delattr(sys, 'spam')

print 'dir'
x = 1
if 'x' not in dir(): raise TestFailed, 'dir()'
import sys
if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'

print 'divmod'
if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
#
if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
#
if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
#
if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
	raise TestFailed, 'divmod(3.25, 1.0)'
if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
	raise TestFailed, 'divmod(-3.25, 1.0)'
if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
	raise TestFailed, 'divmod(3.25, -1.0)'
if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
	raise TestFailed, 'divmod(-3.25, -1.0)'

print 'eval'
if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
globals = {'a': 1, 'b': 2}
locals = {'b': 200, 'c': 300}
if eval('a', globals) <> 1: raise TestFailed, "eval(1)"
if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)"
if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)"
if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)"

print 'execfile'
z = 0
f = open(TESTFN, 'w')
f.write('z = z+1\n')
f.write('z = z*2\n')
f.close()
execfile(TESTFN)
if z <> 2: raise TestFailed, "execfile(1)"
globals['z'] = 0
execfile(TESTFN, globals)
if globals['z'] <> 2: raise TestFailed, "execfile(1)"
locals['z'] = 0
execfile(TESTFN, globals, locals)
if locals['z'] <> 2: raise TestFailed, "execfile(1)"
unlink(TESTFN)

print 'filter'
if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
	raise TestFailed, 'filter (filter a string)'
if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
	raise TestFailed, 'filter (remove false values)'
if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
	raise TestFailed, 'filter (keep positives)'
class Squares:
	def __init__(self, max):
		self.max = max
		self.sofar = []
	def __len__(self): return len(self.sofar)
	def __getitem__(self, i):
		if not 0 <= i < self.max: raise IndexError
		n = len(self.sofar)
		while n <= i:
			self.sofar.append(n*n)
			n = n+1
		return self.sofar[i]
if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
	raise TestFailed, 'filter(None, Squares(10))'
if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
	raise TestFailed, 'filter(oddp, Squares(10))'

print 'float'
if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
if float(314) <> 314.0: raise TestFailed, 'float(314)'
if float(314L) <> 314.0: raise TestFailed, 'float(314L)'

print 'getattr'
import sys
if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'

print 'hasattr'
import sys
if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'

print 'hash'
hash(None)
if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
hash('spam')
hash((0,1,2,3))
def f(): pass

print 'hex'
if hex(16) != '0x10': raise TestFailed, 'hex(16)'
if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'

print 'id'
id(None)
id(1)
id(1L)
id(1.0)
id('spam')
id((0,1,2,3))
id([0,1,2,3])
id({'spam': 1, 'eggs': 2, 'ham': 3})

# Test input() later, together with raw_input

print 'int'
if int(314) <> 314: raise TestFailed, 'int(314)'
if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
if int(314L) <> 314: raise TestFailed, 'int(314L)'

print 'len'
if len('123') <> 3: raise TestFailed, 'len(\'123\')'
if len(()) <> 0: raise TestFailed, 'len(())'
if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
if len({}) <> 0: raise TestFailed, 'len({})'
if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'

print 'long'
if long(314) <> 314L: raise TestFailed, 'long(314)'
if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
if long(314L) <> 314L: raise TestFailed, 'long(314L)'

print 'map'
if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
	raise TestFailed, 'map(None, \'hello world\')'
if map(None, 'abcd', 'efg') <> \
	  [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
	raise TestFailed, 'map(None, \'abcd\', \'efg\')'
if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
	raise TestFailed, 'map(None, range(10))'
if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
	raise TestFailed, 'map(lambda x: x*x, range(1,4))'
try:
	from math import sqrt
except ImportError:
	def sqrt(x):
		return pow(x, 0.5)
if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
	raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
	raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
def plus(*v):
	accu = 0
	for i in v: accu = accu + i
	return accu
if map(plus, [1, 3, 7]) <> [1, 3, 7]:
	raise TestFailed, 'map(plus, [1, 3, 7])'
if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
	raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
	raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
	raise TestFailed, 'map(None, Squares(10))'
if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
	raise TestFailed, 'map(int, Squares(10))'
if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
	raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
	raise TestFailed, 'map(None: x, Squares(3), Squares(2))'

print 'max'
if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
#
if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'

print 'min'
if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
#
if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'