blob: 3f1447f682546c7e0f8f15416e19d88cda3ec92f [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] ')'
79### varargslist: (fpdef ',')* ('+'|'*') NAME | fpdef (',' fpdef)* [',']
80### 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
196print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite]
197### except_clause: 'except' [expr [',' expr]]
198try: pass
199try: 1/0
200except ZeroDivisionError: pass
201try: 1/0
202except EOFError: pass
203except TypeError, msg: pass
204except RuntimeError, msg: pass
205except: pass
206try: pass
207finally: pass
208try: 1/0
209except: pass
210finally: pass
211
212print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
213if 1: pass
214if 1:
215 pass
216if 1:
217 #
218 #
219 #
220 pass
221 pass
222 #
223 pass
224 #
225
226print 'test'
227### and_test ('or' and_test)*
228### and_test: not_test ('and' not_test)*
229### not_test: 'not' not_test | comparison
230if not 1: pass
231if 1 and 1: pass
232if 1 or 1: pass
233if not not not 1: pass
234if not 1 and 1 and 1: pass
235if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
236
237print 'comparison'
238### comparison: expr (comp_op expr)*
239### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
240if 1: pass
241x = (1 == 1)
242if 1 == 1: pass
243if 1 != 1: pass
244if 1 <> 1: pass
245if 1 < 1: pass
246if 1 > 1: pass
247if 1 <= 1: pass
248if 1 >= 1: pass
249if 1 is 1: pass
250if 1 is not 1: pass
251if 1 in (): pass
252if 1 not in (): pass
253if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
254
255print 'binary mask ops'
256x = 1 & 1
257x = 1 ^ 1
258x = 1 | 1
259
260print 'shift ops'
261x = 1 << 1
262x = 1 >> 1
263x = 1 << 1 >> 1
264
265print 'additive ops'
266x = 1
267x = 1 + 1
268x = 1 - 1 - 1
269x = 1 - 1 + 1 - 1 + 1
270
271print 'multiplicative ops'
272x = 1 * 1
273x = 1 / 1
274x = 1 % 1
275x = 1 / 1 * 1 % 1
276
277print 'unary ops'
278x = +1
279x = -1
280x = ~1
281x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
282x = -1*1/1 + 1*1 - ---1*1
283
284print 'selectors'
285### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
286### subscript: expr | [expr] ':' [expr]
287import sys, time
288c = sys.path[0]
289x = time.time()
290x = sys.modules['time'].time()
291a = '01234'
292c = a[0]
293c = a[-1]
294s = a[0:5]
295s = a[:5]
296s = a[0:]
297s = a[:]
298s = a[-5:]
299s = a[:-1]
300s = a[-4:-3]
301
302print 'atoms'
303### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
304### dictmaker: test ':' test (',' test ':' test)* [',']
305
306x = (1)
307x = (1 or 2 or 3)
308x = (1 or 2 or 3, 2, 3)
309
310x = []
311x = [1]
312x = [1 or 2 or 3]
313x = [1 or 2 or 3, 2, 3]
314x = []
315
316x = {}
317x = {'one': 1}
318x = {'one': 1,}
319x = {'one' or 'two': 1 or 2}
320x = {'one': 1, 'two': 2}
321x = {'one': 1, 'two': 2,}
322x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
323
324x = `x`
325x = `1 or 2 or 3`
326x = x
327x = 'x'
328x = 123
329
330### exprlist: expr (',' expr)* [',']
331### testlist: test (',' test)* [',']
332# These have been exercised enough above
333
334print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
335### baselist: atom arguments (',' atom arguments)*
336### arguments: '(' [testlist] ')'
337class B: pass
338class C1(B): pass
339class C2(B): pass
340class D(C1, C2, B): pass
341class C:
342 def meth1(self): pass
343 def meth2(self, arg): pass
344 def meth3(self, a1, a2): pass