blob: 5f4a9dc69ea208a7857632d8cb02c1e3d0cee789 [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:
Guido van Rossuma2325361991-08-16 13:29:25 +00009# - all builtin modules
Guido van Rossum217a5fa1990-12-26 15:40:07 +000010# - all possible exception situations (Thank God we've got 'try')
11# - all boundary cases
12
13
14TestFailed = 'testall -- test failed' # Exception
15
16
17#########################################################
18# Part 1. Test all lexical and grammatical constructs.
19# This just tests whether the parser accepts them all.
20#########################################################
21
22print '1. Parser'
23
24print '1.1 Tokens'
25
26print '1.1.1 Backslashes'
27
28# Backslash means line continuation:
29x = 1 \
30+ 1
31if x <> 2: raise TestFailed, 'backslash for line continuation'
32
33# Backslash does not means continuation in comments :\
34x = 0
35if x <> 0: raise TestFailed, 'backslash ending comment'
36
37print '1.1.2 Number formats'
38
39if 0xff <> 255: raise TestFailed, 'hex number'
40if 0377 <> 255: raise TestFailed, 'octal number'
41x = 3.14
42x = 0.314
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000043x = .14
44x = 3.
Guido van Rossum217a5fa1990-12-26 15:40:07 +000045x = 3e14
46x = 3E14
47x = 3e-14
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000048x = 3.e14
49x = .14e3
Guido van Rossuma2325361991-08-16 13:29:25 +000050x = 0L
51x = 0l
52x = 0xffffffffffffffffL
53x = 0xffffffffffffffffl
54x = 077777777777777777L
55x = 077777777777777777l
56x = 123456789012345678901234567890L
57x = 123456789012345678901234567890l
Guido van Rossum217a5fa1990-12-26 15:40:07 +000058
59print '1.2 Grammar'
60
61print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
62# XXX can't test in a script -- this rule is only used when interactive
63
64print 'file_input' # (NEWLINE | stmt)* ENDMARKER
65# Being tested as this very moment this very module
66
67print 'expr_input' # testlist NEWLINE
68# XXX Hard to test -- used only in calls to input()
69
70print 'eval_input' # testlist ENDMARKER
71x = eval('1, 0 or 1')
72
73print 'funcdef' # 'def' NAME parameters ':' suite
74### parameters: '(' [fplist] ')'
75### fplist: fpdef (',' fpdef)*
76### fpdef: NAME | '(' fplist ')'
77def f1(): pass
78def f2(one_argument): pass
79def f3(two, arguments): pass
80def f4(two, (compound, (arguments))): pass
81
82### stmt: simple_stmt | compound_stmt
Guido van Rossum217a5fa1990-12-26 15:40:07 +000083# Tested below
84
Guido van Rossuma2325361991-08-16 13:29:25 +000085### simple_stmt: small_stmt (';' small_stmt)* [';']
86print 'simple_stmt'
87x = 1; pass; del x
88
89### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
90# Tested below
91
92print 'expr_stmt' # (exprlist '=')* exprlist
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000931
941, 2, 3
95x = 1
96x = 1, 2, 3
97x = y = z = 1, 2, 3
98x, y, z = 1, 2, 3
99abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
100# NB these variables are deleted below
101
Guido van Rossuma2325361991-08-16 13:29:25 +0000102print 'print_stmt' # 'print' (test ',')* [test]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000103print 1, 2, 3
104print 1, 2, 3,
105print
106print 0 or 1, 0 or 1,
107print 0 or 1
108
Guido van Rossuma2325361991-08-16 13:29:25 +0000109print 'del_stmt' # 'del' exprlist
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000110del abc
111del x, y, (z, xyz)
112
Guido van Rossuma2325361991-08-16 13:29:25 +0000113print 'pass_stmt' # 'pass'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000114pass
115
Guido van Rossuma2325361991-08-16 13:29:25 +0000116print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000117# Tested below
118
Guido van Rossuma2325361991-08-16 13:29:25 +0000119print 'break_stmt' # 'break'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000120while 1: break
121
Guido van Rossuma2325361991-08-16 13:29:25 +0000122print 'continue_stmt' # 'continue'
123i = 1
124while i: i = 0; continue
125
126print 'return_stmt' # 'return' [testlist]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000127def g1(): return
128def g2(): return 1
129g1()
130x = g2()
131
Guido van Rossuma2325361991-08-16 13:29:25 +0000132print 'raise_stmt' # 'raise' test [',' test]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000133try: raise RuntimeError, 'just testing'
134except RuntimeError: pass
135try: raise KeyboardInterrupt
136except KeyboardInterrupt: pass
137
Guido van Rossuma2325361991-08-16 13:29:25 +0000138print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000139[1]
140import sys
141[2]
142import time, math
143[3]
144from time import sleep
145[4]
Guido van Rossuma2325361991-08-16 13:29:25 +0000146from sys import *
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000147[5]
Guido van Rossuma2325361991-08-16 13:29:25 +0000148from math import sin, cos
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000149[6]
150
151### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
152# Tested below
153
154print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
155if 1: pass
156if 1: pass
157else: pass
158if 0: pass
159elif 0: pass
160if 0: pass
161elif 0: pass
162elif 0: pass
163elif 0: pass
164else: pass
165
166print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
167while 0: pass
168while 0: pass
169else: pass
170
171print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
172[1]
173for i in 1, 2, 3: pass
174[2]
175for i, j, k in (): pass
176else: pass
177[3]
178
179print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
180### except_clause: 'except' [expr [',' expr]]
181try: pass
182try: 1/0
Guido van Rossum76105991991-12-16 13:10:58 +0000183except ZeroDivisionError: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000184try: 1/0
185except EOFError: pass
186except TypeError, msg: pass
187except RuntimeError, msg: pass
Guido van Rossum76105991991-12-16 13:10:58 +0000188except ZeroDivisionError, msg: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000189except: pass
190try: pass
191finally: pass
192try: 1/0
193except: pass
194finally: pass
195
196print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
197if 1: pass
198if 1:
199 pass
200if 1:
201 #
202 #
203 #
204 pass
205 pass
206 #
207 pass
208 #
209
210print 'test' # and_test ('or' and_test)*
211### and_test: not_test ('and' not_test)*
212### not_test: 'not' not_test | comparison
213### comparison: expr (comp_op expr)*
214### comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
215if 1: pass
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000216if 1 == 1: pass
217if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
218if not 1 == 1 == 1: pass
219if not 1 == 1 and 1 and 1: pass
220if 1 and 1 or 1 and 1 and 1 or not 1 == 1 == 1 and 1: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000221
222print 'expr' # term (('+'|'-') term)*
223x = 1
224x = 1 + 1
225x = 1 - 1 - 1
226x = 1 - 1 + 1 - 1 + 1
227
228print 'term' # factor (('*'|'/'|'%') factor)*
229x = 1 * 1
230x = 1 / 1
231x = 1 % 1
232x = 1 / 1 * 1 % 1
233
234print 'factor' # ('+'|'-') factor | atom trailer*
235### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
236### subscript: expr | [expr] ':' [expr]
237x = +1
238x = -1
239x = 1
Guido van Rossum6179fe61991-04-07 13:42:52 +0000240c = sys.path[0]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000241x = time.time()
242x = sys.modules['time'].time()
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000243for a in '01234', (0,1,2,3,4), [0,1,2,3,4]:
244 c = a[0]
245 c = a[-1]
246 c = a[0:5]
247 c = a[:5]
248 c = a[0:]
249 c = a[:]
250 c = a[-5:]
251 c = a[:-1]
252 c = a[-4:-3]
253a = [0,1,2,3,4]
254del a[0]
255a = [0,1,2,3,4]
256del a[-1]
257a = [0,1,2,3,4]
258del a[1:2]
259a = [0,1,2,3,4]
260del a[:1]
261a = [0,1,2,3,4]
262del a[-1:]
263a = [0,1,2,3,4]
264a[0] = 0
265a[-1] = 4
266a[1:2] = [1]
267a[1:4] = [0]
268a[1:-1] = [1,2,3]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000269
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000270print 'atom' # '(' [tetslist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000271x = (1)
272x = (1 or 2 or 3)
273x = (1 or 2 or 3, 2, 3)
274x = []
275x = [1]
276x = [1 or 2 or 3]
277x = [1 or 2 or 3, 2, 3]
278x = []
279x = {}
280x = `x`
281x = x
282x = 'x'
283x = 123
284
285### exprlist: expr (',' expr)* [',']
286### testlist: test (',' test)* [',']
Guido van Rossuma2325361991-08-16 13:29:25 +0000287# These have been exercised enough above
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000288
289print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
290### baselist: atom arguments (',' atom arguments)*
291### arguments: '(' [testlist] ')'
Guido van Rossumccfd6e11991-12-26 13:06:39 +0000292class B: pass
293class C1(B): pass
294class C2(B): pass
295class D(C1, C2, B): pass
296class C:
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000297 def meth1(self): pass
298 def meth2(self, arg): pass
299 def meth3(self, (a1, a2)): pass
300
301
302#########################################################
303# Part 2. Test all opcodes from "opcode.h"
304#########################################################
305
306print '2. Opcodes'
307print 'XXX Not yet fully implemented'
308
309print '2.1 try inside for loop'
310n = 0
311for i in range(10):
312 n = n+i
313 try: 1/0
314 except NameError: pass
315 except RuntimeError: pass
Guido van Rossum76105991991-12-16 13:10:58 +0000316 except ZeroDivisionError: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000317 except TypeError: pass
318 finally: pass
319 try: pass
320 except: pass
321 try: pass
322 finally: pass
323 n = n+i
324if n <> 90:
325 raise TestFailed, 'try inside for'
326
327
328#########################################################
329# Part 3. Test all operations on all object types
330#########################################################
331
332print '3. Object types'
333print 'XXX Not yet implemented'
334
335
336#########################################################
337# Part 4. Test all built-in functions
338#########################################################
339
340print '4. Built-in functions'
341
342print 'abs'
343if abs(0) <> 0: raise TestFailed, 'abs(0)'
344if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
345if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000346#
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000347if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
348if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
349if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000350#
351if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
352if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
353if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
354
355print 'chr'
356if chr(32) <> ' ': raise TestFailed, 'chr(32)'
357if chr(65) <> 'A': raise TestFailed, 'chr(65)'
358if chr(97) <> 'a': raise TestFailed, 'chr(97)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000359
360print 'dir'
Guido van Rossuma2325361991-08-16 13:29:25 +0000361x = 1
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000362if 'x' not in dir(): raise TestFailed, 'dir()'
Guido van Rossuma2325361991-08-16 13:29:25 +0000363import sys
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000364if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
365
366print 'divmod'
367if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
368if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
369if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
370if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000371#
372if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
373if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
374if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
375if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
376#
377if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
378if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
379if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
380if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000381
382print 'eval'
383if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
384
385print 'exec'
Guido van Rossuma2325361991-08-16 13:29:25 +0000386z = 0
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000387exec('z=1+1\n')
388if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
389
390print 'float'
391if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
392if float(314) <> 314.0: raise TestFailed, 'float(314)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000393if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000394
395print 'input'
396# Can't test in a script
397
398print 'int'
Guido van Rossuma2325361991-08-16 13:29:25 +0000399if int(314) <> 314: raise TestFailed, 'int(314)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000400if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000401if int(314L) <> 314: raise TestFailed, 'int(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000402
403print 'len'
404if len('123') <> 3: raise TestFailed, 'len(\'123\')'
405if len(()) <> 0: raise TestFailed, 'len(())'
406if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
407if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
408if len({}) <> 0: raise TestFailed, 'len({})'
409
Guido van Rossuma2325361991-08-16 13:29:25 +0000410print 'long'
411if long(314) <> 314L: raise TestFailed, 'long(314)'
412if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
413if long(314L) <> 314L: raise TestFailed, 'long(314L)'
414
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000415print 'min'
416if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
417if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
418if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
419if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
Guido van Rossuma2325361991-08-16 13:29:25 +0000420#
421if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
422if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
423if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000424
425print 'max'
426if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
427if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
428if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
429if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
Guido van Rossuma2325361991-08-16 13:29:25 +0000430#
431if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
432if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
433if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000434
435print 'open'
436print 'NB! This test creates a file named "@test" in the current directory.'
437fp = open('@test', 'w')
438fp.write('The quick brown fox jumps over the lazy dog')
439fp.write('.\n')
440fp.write('Dear John\n')
441fp.write('XXX'*100)
442fp.write('YYY'*100)
443fp.close()
444del fp
445fp = open('@test', 'r')
446if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
447 raise TestFailed, 'readline()'
448if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
449if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
450if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
451if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
452fp.close()
453del fp
454
Guido van Rossuma2325361991-08-16 13:29:25 +0000455print 'ord'
456if ord(' ') <> 32: raise TestFailed, 'ord(\' \')'
457if ord('A') <> 65: raise TestFailed, 'ord(\'A\')'
458if ord('a') <> 97: raise TestFailed, 'ord(\'a\')'
459
460print 'pow'
461if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)'
462if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)'
463if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)'
464if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)'
465#
466if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)'
467if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)'
468if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)'
469if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)'
470#
471if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)'
472if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)'
473if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)'
474if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)'
475
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000476print 'range'
477if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
478if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
479if range(0) <> []: raise TestFailed, 'range(0)'
480if range(-3) <> []: raise TestFailed, 'range(-3)'
481if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
482if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
483
484print 'raw_input'
485savestdin = sys.stdin
486try:
487 sys.stdin = open('@test', 'r')
488 if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
489 raise TestFailed, 'raw_input()'
490 if raw_input('testing\n') <> 'Dear John':
491 raise TestFailed, 'raw_input(\'testing\\n\')'
492finally:
493 sys.stdin = savestdin
494
495print 'reload'
496import string
497reload(string)
498
499print 'type'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000500if type('') <> type('123') or type('') == type(()):
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000501 raise TestFailed, 'type()'
502
503
504print 'Passed all tests.'
505
506try:
507 import mac
508 unlink = mac.unlink
Guido van Rossumccfd6e11991-12-26 13:06:39 +0000509except ImportError:
510 import posix
511 unlink = posix.unlink
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000512
513unlink('@test')
514print 'Unlinked @test'