blob: ef7cb1368a04344058de65c7e53c9391d2141c30 [file] [log] [blame]
Guido van Rossum217a5fa1990-12-26 15:40:07 +00001# Module 'testall'
2#
3# Python test set, should exercise:
4# - all lexical and grammatical constructs
5# - all opcodes from "opcode.h"
6# - all operations on all object types
7# - all builtin functions
8# Ideally also:
9# - all possible exception situations (Thank God we've got 'try')
10# - all boundary cases
11
12
13TestFailed = 'testall -- test failed' # Exception
14
15
16#########################################################
17# Part 1. Test all lexical and grammatical constructs.
18# This just tests whether the parser accepts them all.
19#########################################################
20
21print '1. Parser'
22
23print '1.1 Tokens'
24
25print '1.1.1 Backslashes'
26
27# Backslash means line continuation:
28x = 1 \
29+ 1
30if x <> 2: raise TestFailed, 'backslash for line continuation'
31
32# Backslash does not means continuation in comments :\
33x = 0
34if x <> 0: raise TestFailed, 'backslash ending comment'
35
36print '1.1.2 Number formats'
37
38if 0xff <> 255: raise TestFailed, 'hex number'
39if 0377 <> 255: raise TestFailed, 'octal number'
40x = 3.14
41x = 0.314
42x = 3e14
43x = 3E14
44x = 3e-14
45
46print '1.2 Grammar'
47
48print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
49# XXX can't test in a script -- this rule is only used when interactive
50
51print 'file_input' # (NEWLINE | stmt)* ENDMARKER
52# Being tested as this very moment this very module
53
54print 'expr_input' # testlist NEWLINE
55# XXX Hard to test -- used only in calls to input()
56
57print 'eval_input' # testlist ENDMARKER
58x = eval('1, 0 or 1')
59
60print 'funcdef' # 'def' NAME parameters ':' suite
61### parameters: '(' [fplist] ')'
62### fplist: fpdef (',' fpdef)*
63### fpdef: NAME | '(' fplist ')'
64def f1(): pass
65def f2(one_argument): pass
66def f3(two, arguments): pass
67def f4(two, (compound, (arguments))): pass
68
69### stmt: simple_stmt | compound_stmt
70### simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
71# Tested below
72
73print 'expr_stmt' # (exprlist '=')* exprlist NEWLINE
741
751, 2, 3
76x = 1
77x = 1, 2, 3
78x = y = z = 1, 2, 3
79x, y, z = 1, 2, 3
80abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
81# NB these variables are deleted below
82
83print 'print_stmt' # 'print' (test ',')* [test] NEWLINE
84print 1, 2, 3
85print 1, 2, 3,
86print
87print 0 or 1, 0 or 1,
88print 0 or 1
89
90print 'del_stmt' # 'del' exprlist NEWLINE
91del abc
92del x, y, (z, xyz)
93
94print 'pass_stmt' # 'pass' NEWLINE
95pass
96
97print 'flow_stmt' # break_stmt | return_stmt | raise_stmt
98# Tested below
99
100print 'break_stmt' # 'break' NEWLINE
101while 1: break
102
103print 'return_stmt' # 'return' [testlist] NEWLINE
104def g1(): return
105def g2(): return 1
106g1()
107x = g2()
108
109print 'raise_stmt' # 'raise' expr [',' expr] NEWLINE
110try: raise RuntimeError, 'just testing'
111except RuntimeError: pass
112try: raise KeyboardInterrupt
113except KeyboardInterrupt: pass
114
115print 'import_stmt' # 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE
116[1]
117import sys
118[2]
119import time, math
120[3]
121from time import sleep
122[4]
123from math import *
124[5]
125from sys import modules, ps1, ps2
126[6]
127
128### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
129# Tested below
130
131print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
132if 1: pass
133if 1: pass
134else: pass
135if 0: pass
136elif 0: pass
137if 0: pass
138elif 0: pass
139elif 0: pass
140elif 0: pass
141else: pass
142
143print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
144while 0: pass
145while 0: pass
146else: pass
147
148print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
149[1]
150for i in 1, 2, 3: pass
151[2]
152for i, j, k in (): pass
153else: pass
154[3]
155
156print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
157### except_clause: 'except' [expr [',' expr]]
158try: pass
159try: 1/0
160except RuntimeError: pass
161try: 1/0
162except EOFError: pass
163except TypeError, msg: pass
164except RuntimeError, msg: pass
165except: pass
166try: pass
167finally: pass
168try: 1/0
169except: pass
170finally: pass
171
172print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
173if 1: pass
174if 1:
175 pass
176if 1:
177 #
178 #
179 #
180 pass
181 pass
182 #
183 pass
184 #
185
186print 'test' # and_test ('or' and_test)*
187### and_test: not_test ('and' not_test)*
188### not_test: 'not' not_test | comparison
189### comparison: expr (comp_op expr)*
190### comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
191if 1: pass
192if 1 = 1: pass
193if 1 < 1 > 1 = 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
194if not 1 = 1 = 1: pass
195if not 1 = 1 and 1 and 1: pass
196if 1 and 1 or 1 and 1 and 1 or not 1 = 1 = 1 and 1: pass
197
198print 'expr' # term (('+'|'-') term)*
199x = 1
200x = 1 + 1
201x = 1 - 1 - 1
202x = 1 - 1 + 1 - 1 + 1
203
204print 'term' # factor (('*'|'/'|'%') factor)*
205x = 1 * 1
206x = 1 / 1
207x = 1 % 1
208x = 1 / 1 * 1 % 1
209
210print 'factor' # ('+'|'-') factor | atom trailer*
211### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
212### subscript: expr | [expr] ':' [expr]
213x = +1
214x = -1
215x = 1
216c = sys.ps1[0]
217x = time.time()
218x = sys.modules['time'].time()
219a = '01234'
220c = a[0]
221c = a[0:5]
222c = a[:5]
223c = a[0:]
224c = a[:]
225c = a[-5:]
226c = a[:-1]
227c = a[-4:-3]
228
229print 'atom' # '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
230x = (1)
231x = (1 or 2 or 3)
232x = (1 or 2 or 3, 2, 3)
233x = []
234x = [1]
235x = [1 or 2 or 3]
236x = [1 or 2 or 3, 2, 3]
237x = []
238x = {}
239x = `x`
240x = x
241x = 'x'
242x = 123
243
244### exprlist: expr (',' expr)* [',']
245### testlist: test (',' test)* [',']
246# These have been exercised enough above
247
248print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
249### baselist: atom arguments (',' atom arguments)*
250### arguments: '(' [testlist] ')'
251class B(): pass
252class C1() = B(): pass
253class C2() = B(): pass
254class D() = C1(), C2(), B(): pass
255class C():
256 def meth1(self): pass
257 def meth2(self, arg): pass
258 def meth3(self, (a1, a2)): pass
259
260
261#########################################################
262# Part 2. Test all opcodes from "opcode.h"
263#########################################################
264
265print '2. Opcodes'
266print 'XXX Not yet fully implemented'
267
268print '2.1 try inside for loop'
269n = 0
270for i in range(10):
271 n = n+i
272 try: 1/0
273 except NameError: pass
274 except RuntimeError: pass
275 except TypeError: pass
276 finally: pass
277 try: pass
278 except: pass
279 try: pass
280 finally: pass
281 n = n+i
282if n <> 90:
283 raise TestFailed, 'try inside for'
284
285
286#########################################################
287# Part 3. Test all operations on all object types
288#########################################################
289
290print '3. Object types'
291print 'XXX Not yet implemented'
292
293
294#########################################################
295# Part 4. Test all built-in functions
296#########################################################
297
298print '4. Built-in functions'
299
300print 'abs'
301if abs(0) <> 0: raise TestFailed, 'abs(0)'
302if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
303if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
304if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
305if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
306if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
307
308print 'dir'
309if 'x' not in dir(): raise TestFailed, 'dir()'
310if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
311
312print 'divmod'
313if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
314if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
315if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
316if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
317
318print 'eval'
319if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
320
321print 'exec'
322exec('z=1+1\n')
323if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
324
325print 'float'
326if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
327if float(314) <> 314.0: raise TestFailed, 'float(314)'
328
329print 'input'
330# Can't test in a script
331
332print 'int'
333if int(100) <> 100: raise TestFailed, 'int(100)'
334if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
335
336print 'len'
337if len('123') <> 3: raise TestFailed, 'len(\'123\')'
338if len(()) <> 0: raise TestFailed, 'len(())'
339if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
340if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
341if len({}) <> 0: raise TestFailed, 'len({})'
342
343print 'min'
344if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
345if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
346if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
347if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
348
349print 'max'
350if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
351if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
352if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
353if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
354
355print 'open'
356print 'NB! This test creates a file named "@test" in the current directory.'
357fp = open('@test', 'w')
358fp.write('The quick brown fox jumps over the lazy dog')
359fp.write('.\n')
360fp.write('Dear John\n')
361fp.write('XXX'*100)
362fp.write('YYY'*100)
363fp.close()
364del fp
365fp = open('@test', 'r')
366if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
367 raise TestFailed, 'readline()'
368if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
369if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
370if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
371if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
372fp.close()
373del fp
374
375print 'range'
376if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
377if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
378if range(0) <> []: raise TestFailed, 'range(0)'
379if range(-3) <> []: raise TestFailed, 'range(-3)'
380if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
381if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
382
383print 'raw_input'
384savestdin = sys.stdin
385try:
386 sys.stdin = open('@test', 'r')
387 if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
388 raise TestFailed, 'raw_input()'
389 if raw_input('testing\n') <> 'Dear John':
390 raise TestFailed, 'raw_input(\'testing\\n\')'
391finally:
392 sys.stdin = savestdin
393
394print 'reload'
395import string
396reload(string)
397
398print 'type'
399if type('') <> type('123') or type('') = type(()):
400 raise TestFailed, 'type()'
401
402
403print 'Passed all tests.'
404
405try:
406 import mac
407 unlink = mac.unlink
408except NameError:
409 try:
410 import posix
411 unlink = posix.unlink
412 except NameError:
413 pass
414
415unlink('@test')
416print 'Unlinked @test'