blob: 56fb14fc8933e11df74a4d8403fbebe931817e5a [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 1, grammar.
2# This just tests whether the parser accepts them all.
3
4from test_support import *
5
6print '1. Parser'
7
8print '1.1 Tokens'
9
10print '1.1.1 Backslashes'
11
12# Backslash means line continuation:
13x = 1 \
14+ 1
15if x <> 2: raise TestFailed, 'backslash for line continuation'
16
17# Backslash does not means continuation in comments :\
18x = 0
19if x <> 0: raise TestFailed, 'backslash ending comment'
20
21print '1.1.2 Numeric literals'
22
23print '1.1.2.1 Plain integers'
24if 0xff <> 255: raise TestFailed, 'hex int'
25if 0377 <> 255: raise TestFailed, 'octal int'
26if 2147483647 != 017777777777: raise TestFailed, 'max positive int'
27if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
28# XXX -2147483648
29if 037777777777 != -1: raise TestFailed, 'oct -1'
30if 0xffffffff != -1: raise TestFailed, 'hex -1'
31for s in '2147483648', '040000000000', '0x100000000':
32 try:
33 x = eval(s)
34 except OverflowError:
35 continue
36 raise TestFailed, 'No OverflowError on huge integer literal ' + `s`
37
38print '1.1.2.2 Long integers'
39x = 0L
40x = 0l
41x = 0xffffffffffffffffL
42x = 0xffffffffffffffffl
43x = 077777777777777777L
44x = 077777777777777777l
45x = 123456789012345678901234567890L
46x = 123456789012345678901234567890l
47
48print '1.1.2.3 Floating point'
49x = 3.14
50x = 314.
51x = 0.314
52# XXX x = 000.314
53x = .314
54x = 3e14
55x = 3E14
56x = 3e-14
57x = 3e+14
58x = 3.e14
59x = .3e14
60x = 3.1e4
61
62print '1.2 Grammar'
63
64print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
65# XXX can't test in a script -- this rule is only used when interactive
66
67print 'file_input' # (NEWLINE | stmt)* ENDMARKER
68# Being tested as this very moment this very module
69
70print 'expr_input' # testlist NEWLINE
71# XXX Hard to test -- used only in calls to input()
72
73print 'eval_input' # testlist ENDMARKER
74x = eval('1, 0 or 1')
75
76print 'funcdef'
77### 'def' NAME parameters ':' suite
78### parameters: '(' [varargslist] ')'
Guido van Rossum33693ea1992-04-03 16:33:00 +000079### varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +000080### fpdef: NAME | '(' fplist ')'
81### fplist: fpdef (',' fpdef)* [',']
82def f1(): pass
83def f2(one_argument): pass
84def f3(two, arguments): pass
85def f4(two, (compound, (argument, list))): pass
86def a1(one_arg,): pass
87def a2(two, args,): pass
88def v0(*rest): pass
89def v1(a, *rest): pass
90def v2(a, b, *rest): pass
91def v3(a, (b, c), *rest): pass
92
93### stmt: simple_stmt | compound_stmt
94# Tested below
95
96### simple_stmt: small_stmt (';' small_stmt)* [';']
97print 'simple_stmt'
98x = 1; pass; del x
99
100### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
101# Tested below
102
103print 'expr_stmt' # (exprlist '=')* exprlist
1041
1051, 2, 3
106x = 1
107x = 1, 2, 3
108x = y = z = 1, 2, 3
109x, y, z = 1, 2, 3
110abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
111# NB these variables are deleted below
112
113print 'print_stmt' # 'print' (test ',')* [test]
114print 1, 2, 3
115print 1, 2, 3,
116print
117print 0 or 1, 0 or 1,
118print 0 or 1
119
120print 'del_stmt' # 'del' exprlist
121del abc
122del x, y, (z, xyz)
123
124print 'pass_stmt' # 'pass'
125pass
126
127print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
128# Tested below
129
130print 'break_stmt' # 'break'
131while 1: break
132
133print 'continue_stmt' # 'continue'
134i = 1
135while i: i = 0; continue
136
137print 'return_stmt' # 'return' [testlist]
138def g1(): return
139def g2(): return 1
140g1()
141x = g2()
142
143print 'raise_stmt' # 'raise' test [',' test]
144try: raise RuntimeError, 'just testing'
145except RuntimeError: pass
146try: raise KeyboardInterrupt
147except KeyboardInterrupt: pass
148
149print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
150[1]
151import sys
152[2]
153import time, math
154[3]
155from time import sleep
156[4]
157from sys import *
158[5]
159from math import sin, cos
160[6]
161
162print 'global_stmt' # 'global' NAME (',' NAME)*
163def f():
164 global a
165 global a, b
166 global one, two, three, four, five, six, seven, eight, nine, ten
167
168### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
169# Tested below
170
171print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
172if 1: pass
173if 1: pass
174else: pass
175if 0: pass
176elif 0: pass
177if 0: pass
178elif 0: pass
179elif 0: pass
180elif 0: pass
181else: pass
182
183print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
184while 0: pass
185while 0: pass
186else: pass
187
188print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
189[1]
190for i in 1, 2, 3: pass
191[2]
192for i, j, k in (): pass
193else: pass
194[3]
195
Guido van Rossum627efd91992-03-31 18:54:11 +0000196print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000197### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000198try:
199 1/0
200except ZeroDivisionError:
201 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000202try: 1/0
203except EOFError: pass
204except TypeError, msg: pass
205except RuntimeError, msg: pass
206except: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000207try: 1/0
208except (EOFError, TypeError, ZeroDivisionError): pass
209try: 1/0
210except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000211try: pass
212finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000213
214print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
215if 1: pass
216if 1:
217 pass
218if 1:
219 #
220 #
221 #
222 pass
223 pass
224 #
225 pass
226 #
227
228print 'test'
229### and_test ('or' and_test)*
230### and_test: not_test ('and' not_test)*
231### not_test: 'not' not_test | comparison
232if not 1: pass
233if 1 and 1: pass
234if 1 or 1: pass
235if not not not 1: pass
236if not 1 and 1 and 1: pass
237if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
238
239print 'comparison'
240### comparison: expr (comp_op expr)*
241### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
242if 1: pass
243x = (1 == 1)
244if 1 == 1: pass
245if 1 != 1: pass
246if 1 <> 1: pass
247if 1 < 1: pass
248if 1 > 1: pass
249if 1 <= 1: pass
250if 1 >= 1: pass
251if 1 is 1: pass
252if 1 is not 1: pass
253if 1 in (): pass
254if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000255if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000256
257print 'binary mask ops'
258x = 1 & 1
259x = 1 ^ 1
260x = 1 | 1
261
262print 'shift ops'
263x = 1 << 1
264x = 1 >> 1
265x = 1 << 1 >> 1
266
267print 'additive ops'
268x = 1
269x = 1 + 1
270x = 1 - 1 - 1
271x = 1 - 1 + 1 - 1 + 1
272
273print 'multiplicative ops'
274x = 1 * 1
275x = 1 / 1
276x = 1 % 1
277x = 1 / 1 * 1 % 1
278
279print 'unary ops'
280x = +1
281x = -1
282x = ~1
283x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
284x = -1*1/1 + 1*1 - ---1*1
285
286print 'selectors'
287### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
288### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000289f1()
290f2(1)
291f2(1,)
292f3(1, 2)
293f3(1, 2,)
294f4(1, (2, (3, 4)))
295v0()
296v0(1)
297v0(1,)
298v0(1,2)
299v0(1,2,3,4,5,6,7,8,9,0)
300v1(1)
301v1(1,)
302v1(1,2)
303v1(1,2,3)
304v1(1,2,3,4,5,6,7,8,9,0)
305v2(1,2)
306v2(1,2,3)
307v2(1,2,3,4)
308v2(1,2,3,4,5,6,7,8,9,0)
309v3(1,(2,3))
310v3(1,(2,3),4)
311v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000312import sys, time
313c = sys.path[0]
314x = time.time()
315x = sys.modules['time'].time()
316a = '01234'
317c = a[0]
318c = a[-1]
319s = a[0:5]
320s = a[:5]
321s = a[0:]
322s = a[:]
323s = a[-5:]
324s = a[:-1]
325s = a[-4:-3]
326
327print 'atoms'
328### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
329### dictmaker: test ':' test (',' test ':' test)* [',']
330
331x = (1)
332x = (1 or 2 or 3)
333x = (1 or 2 or 3, 2, 3)
334
335x = []
336x = [1]
337x = [1 or 2 or 3]
338x = [1 or 2 or 3, 2, 3]
339x = []
340
341x = {}
342x = {'one': 1}
343x = {'one': 1,}
344x = {'one' or 'two': 1 or 2}
345x = {'one': 1, 'two': 2}
346x = {'one': 1, 'two': 2,}
347x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
348
349x = `x`
350x = `1 or 2 or 3`
351x = x
352x = 'x'
353x = 123
354
355### exprlist: expr (',' expr)* [',']
356### testlist: test (',' test)* [',']
357# These have been exercised enough above
358
Guido van Rossum85f18201992-11-27 22:53:50 +0000359print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000360class B: pass
361class C1(B): pass
362class C2(B): pass
363class D(C1, C2, B): pass
364class C:
365 def meth1(self): pass
366 def meth2(self, arg): pass
367 def meth3(self, a1, a2): pass