blob: 742477f9b4c01171b8f0c51738eba35255a3580d [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'
Guido van Rossume65cce51993-11-08 15:05:21 +000027# Change the following line to "if 0:" if you have 64-bit integers
28if 1:
29 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
30 # XXX -2147483648
31 if 037777777777 != -1: raise TestFailed, 'oct -1'
32 if 0xffffffff != -1: raise TestFailed, 'hex -1'
33 for s in '2147483648', '040000000000', '0x100000000':
34 try:
35 x = eval(s)
36 except OverflowError:
37 continue
38 raise TestFailed, \
39 'No OverflowError on huge integer literal ' + `s`
Guido van Rossum3bead091992-01-27 17:00:37 +000040
41print '1.1.2.2 Long integers'
42x = 0L
43x = 0l
44x = 0xffffffffffffffffL
45x = 0xffffffffffffffffl
46x = 077777777777777777L
47x = 077777777777777777l
48x = 123456789012345678901234567890L
49x = 123456789012345678901234567890l
50
51print '1.1.2.3 Floating point'
52x = 3.14
53x = 314.
54x = 0.314
55# XXX x = 000.314
56x = .314
57x = 3e14
58x = 3E14
59x = 3e-14
60x = 3e+14
61x = 3.e14
62x = .3e14
63x = 3.1e4
64
Guido van Rossumb31c7f71993-11-11 10:31:23 +000065print '1.1.3 String literals'
66
67def assert(s):
68 if not s: raise TestFailed, 'see traceback'
69
70x = ''; y = ""; assert(len(x) == 0 and x == y)
71x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
72x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
73x = "doesn't \"shrink\" does it"
74y = 'doesn\'t "shrink" does it'
75assert(len(x) == 24 and x == y)
76x = "doesn \"shrink\" doesn't it"
77y = 'doesn "shrink" doesn\'t it'
78assert(len(x) == 25 and x == y)
79
80
Guido van Rossum3bead091992-01-27 17:00:37 +000081print '1.2 Grammar'
82
83print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
84# XXX can't test in a script -- this rule is only used when interactive
85
86print 'file_input' # (NEWLINE | stmt)* ENDMARKER
87# Being tested as this very moment this very module
88
89print 'expr_input' # testlist NEWLINE
90# XXX Hard to test -- used only in calls to input()
91
92print 'eval_input' # testlist ENDMARKER
93x = eval('1, 0 or 1')
94
95print 'funcdef'
96### 'def' NAME parameters ':' suite
97### parameters: '(' [varargslist] ')'
Guido van Rossum33693ea1992-04-03 16:33:00 +000098### varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +000099### fpdef: NAME | '(' fplist ')'
100### fplist: fpdef (',' fpdef)* [',']
101def f1(): pass
102def f2(one_argument): pass
103def f3(two, arguments): pass
104def f4(two, (compound, (argument, list))): pass
105def a1(one_arg,): pass
106def a2(two, args,): pass
107def v0(*rest): pass
108def v1(a, *rest): pass
109def v2(a, b, *rest): pass
110def v3(a, (b, c), *rest): pass
111
112### stmt: simple_stmt | compound_stmt
113# Tested below
114
115### simple_stmt: small_stmt (';' small_stmt)* [';']
116print 'simple_stmt'
117x = 1; pass; del x
118
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000119### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
Guido van Rossum3bead091992-01-27 17:00:37 +0000120# Tested below
121
122print 'expr_stmt' # (exprlist '=')* exprlist
1231
1241, 2, 3
125x = 1
126x = 1, 2, 3
127x = y = z = 1, 2, 3
128x, y, z = 1, 2, 3
129abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
130# NB these variables are deleted below
131
132print 'print_stmt' # 'print' (test ',')* [test]
133print 1, 2, 3
134print 1, 2, 3,
135print
136print 0 or 1, 0 or 1,
137print 0 or 1
138
139print 'del_stmt' # 'del' exprlist
140del abc
141del x, y, (z, xyz)
142
143print 'pass_stmt' # 'pass'
144pass
145
146print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
147# Tested below
148
149print 'break_stmt' # 'break'
150while 1: break
151
152print 'continue_stmt' # 'continue'
153i = 1
154while i: i = 0; continue
155
156print 'return_stmt' # 'return' [testlist]
157def g1(): return
158def g2(): return 1
159g1()
160x = g2()
161
162print 'raise_stmt' # 'raise' test [',' test]
163try: raise RuntimeError, 'just testing'
164except RuntimeError: pass
165try: raise KeyboardInterrupt
166except KeyboardInterrupt: pass
167
168print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
169[1]
170import sys
171[2]
172import time, math
173[3]
174from time import sleep
175[4]
176from sys import *
177[5]
178from math import sin, cos
179[6]
180
181print 'global_stmt' # 'global' NAME (',' NAME)*
182def f():
183 global a
184 global a, b
185 global one, two, three, four, five, six, seven, eight, nine, ten
186
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000187print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
188def f():
189 z = None
190 del z
191 exec 'z=1+1\n'
192 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
193 del z
194 exec 'z=1+1'
195 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
196f()
197g = {}
198exec 'z = 1' in g
199if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
200g = {}
201l = {}
202exec 'global a; a = 1; b = 2' in g, l
203if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
204
205
Guido van Rossum3bead091992-01-27 17:00:37 +0000206### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
207# Tested below
208
209print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
210if 1: pass
211if 1: pass
212else: pass
213if 0: pass
214elif 0: pass
215if 0: pass
216elif 0: pass
217elif 0: pass
218elif 0: pass
219else: pass
220
221print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
222while 0: pass
223while 0: pass
224else: pass
225
226print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
227[1]
228for i in 1, 2, 3: pass
229[2]
230for i, j, k in (): pass
231else: pass
232[3]
233
Guido van Rossum627efd91992-03-31 18:54:11 +0000234print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000235### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000236try:
237 1/0
238except ZeroDivisionError:
239 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000240try: 1/0
241except EOFError: pass
242except TypeError, msg: pass
243except RuntimeError, msg: pass
244except: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000245try: 1/0
246except (EOFError, TypeError, ZeroDivisionError): pass
247try: 1/0
248except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000249try: pass
250finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000251
252print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
253if 1: pass
254if 1:
255 pass
256if 1:
257 #
258 #
259 #
260 pass
261 pass
262 #
263 pass
264 #
265
266print 'test'
267### and_test ('or' and_test)*
268### and_test: not_test ('and' not_test)*
269### not_test: 'not' not_test | comparison
270if not 1: pass
271if 1 and 1: pass
272if 1 or 1: pass
273if not not not 1: pass
274if not 1 and 1 and 1: pass
275if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
276
277print 'comparison'
278### comparison: expr (comp_op expr)*
279### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
280if 1: pass
281x = (1 == 1)
282if 1 == 1: pass
283if 1 != 1: pass
284if 1 <> 1: pass
285if 1 < 1: pass
286if 1 > 1: pass
287if 1 <= 1: pass
288if 1 >= 1: pass
289if 1 is 1: pass
290if 1 is not 1: pass
291if 1 in (): pass
292if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000293if 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 +0000294
295print 'binary mask ops'
296x = 1 & 1
297x = 1 ^ 1
298x = 1 | 1
299
300print 'shift ops'
301x = 1 << 1
302x = 1 >> 1
303x = 1 << 1 >> 1
304
305print 'additive ops'
306x = 1
307x = 1 + 1
308x = 1 - 1 - 1
309x = 1 - 1 + 1 - 1 + 1
310
311print 'multiplicative ops'
312x = 1 * 1
313x = 1 / 1
314x = 1 % 1
315x = 1 / 1 * 1 % 1
316
317print 'unary ops'
318x = +1
319x = -1
320x = ~1
321x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
322x = -1*1/1 + 1*1 - ---1*1
323
324print 'selectors'
325### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
326### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000327f1()
328f2(1)
329f2(1,)
330f3(1, 2)
331f3(1, 2,)
332f4(1, (2, (3, 4)))
333v0()
334v0(1)
335v0(1,)
336v0(1,2)
337v0(1,2,3,4,5,6,7,8,9,0)
338v1(1)
339v1(1,)
340v1(1,2)
341v1(1,2,3)
342v1(1,2,3,4,5,6,7,8,9,0)
343v2(1,2)
344v2(1,2,3)
345v2(1,2,3,4)
346v2(1,2,3,4,5,6,7,8,9,0)
347v3(1,(2,3))
348v3(1,(2,3),4)
349v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000350import sys, time
351c = sys.path[0]
352x = time.time()
353x = sys.modules['time'].time()
354a = '01234'
355c = a[0]
356c = a[-1]
357s = a[0:5]
358s = a[:5]
359s = a[0:]
360s = a[:]
361s = a[-5:]
362s = a[:-1]
363s = a[-4:-3]
364
365print 'atoms'
366### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
367### dictmaker: test ':' test (',' test ':' test)* [',']
368
369x = (1)
370x = (1 or 2 or 3)
371x = (1 or 2 or 3, 2, 3)
372
373x = []
374x = [1]
375x = [1 or 2 or 3]
376x = [1 or 2 or 3, 2, 3]
377x = []
378
379x = {}
380x = {'one': 1}
381x = {'one': 1,}
382x = {'one' or 'two': 1 or 2}
383x = {'one': 1, 'two': 2}
384x = {'one': 1, 'two': 2,}
385x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
386
387x = `x`
388x = `1 or 2 or 3`
389x = x
390x = 'x'
391x = 123
392
393### exprlist: expr (',' expr)* [',']
394### testlist: test (',' test)* [',']
395# These have been exercised enough above
396
Guido van Rossum85f18201992-11-27 22:53:50 +0000397print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000398class B: pass
399class C1(B): pass
400class C2(B): pass
401class D(C1, C2, B): pass
402class C:
403 def meth1(self): pass
404 def meth2(self, arg): pass
405 def meth3(self, a1, a2): pass