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