blob: 115b5f95618b1cd89bc26e2b8970b00094f77171 [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'):
Guido van Rossumb6775db1994-08-01 11:34:53 +000044 if eval('-9223372036854775807-1 != 01000000000000000000000'):
Guido van Rossumdd8cb441993-12-29 15:33:08 +000045 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)
Guido van Rossumb6775db1994-08-01 11:34:53 +000094x = "does \"shrink\" doesn't it"
95y = 'does "shrink" doesn\'t it'
96assert(len(x) == 24 and x == y)
97x = """
98The "quick"
99brown fox
100jumps over
101the 'lazy' dog.
102"""
103y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
104assert(x == y)
105y = '''
106The "quick"
107brown fox
108jumps over
109the 'lazy' dog.
110'''; assert(x == y)
111y = "\n\
112The \"quick\"\n\
113brown fox\n\
114jumps over\n\
115the 'lazy' dog.\n\
116"; assert(x == y)
117y = '\n\
118The \"quick\"\n\
119brown fox\n\
120jumps over\n\
121the \'lazy\' dog.\n\
122'; assert(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000123
124
Guido van Rossum3bead091992-01-27 17:00:37 +0000125print '1.2 Grammar'
126
127print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
128# XXX can't test in a script -- this rule is only used when interactive
129
130print 'file_input' # (NEWLINE | stmt)* ENDMARKER
131# Being tested as this very moment this very module
132
133print 'expr_input' # testlist NEWLINE
134# XXX Hard to test -- used only in calls to input()
135
136print 'eval_input' # testlist ENDMARKER
137x = eval('1, 0 or 1')
138
139print 'funcdef'
140### 'def' NAME parameters ':' suite
141### parameters: '(' [varargslist] ')'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142### varargslist: (fpdef ['=' test] ',')* '*' NAME
143### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000144### fpdef: NAME | '(' fplist ')'
145### fplist: fpdef (',' fpdef)* [',']
146def f1(): pass
147def f2(one_argument): pass
148def f3(two, arguments): pass
149def f4(two, (compound, (argument, list))): pass
150def a1(one_arg,): pass
151def a2(two, args,): pass
152def v0(*rest): pass
153def v1(a, *rest): pass
154def v2(a, b, *rest): pass
155def v3(a, (b, c), *rest): pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156def d01(a=1): pass
157d01()
158d01(1)
159def d11(a, b=1): pass
160d11(1)
161d11(1, 2)
162def d21(a, b, c=1): pass
163d21(1, 2)
164d21(1, 2, 3)
165def d02(a=1, b=2): pass
166d02()
167d02(1)
168d02(1, 2)
169def d12(a, b=1, c=2): pass
170d12(1)
171d12(1, 2)
172d12(1, 2, 3)
173def d22(a, b, c=1, d=2): pass
174d22(1, 2)
175d22(1, 2, 3)
176d22(1, 2, 3, 4)
177def d01v(a=1, *rest): pass
178d01v()
179d01v(1)
180d01v(1, 2)
181def d11v(a, b=1, *rest): pass
182d11v(1)
183d11v(1, 2)
184d11v(1, 2, 3)
185def d21v(a, b, c=1, *rest): pass
186d21v(1, 2)
187d21v(1, 2, 3)
188d21v(1, 2, 3, 4)
189def d02v(a=1, b=2, *rest): pass
190d02v()
191d02v(1)
192d02v(1, 2)
193d02v(1, 2, 3)
194def d12v(a, b=1, c=2, *rest): pass
195d12v(1)
196d12v(1, 2)
197d12v(1, 2, 3)
198d12v(1, 2, 3, 4)
199def d22v(a, b, c=1, d=2, *rest): pass
200d22v(1, 2)
201d22v(1, 2, 3)
202d22v(1, 2, 3, 4)
203d22v(1, 2, 3, 4, 5)
Guido van Rossum3bead091992-01-27 17:00:37 +0000204
205### stmt: simple_stmt | compound_stmt
206# Tested below
207
208### simple_stmt: small_stmt (';' small_stmt)* [';']
209print 'simple_stmt'
210x = 1; pass; del x
211
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000212### 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 +0000213# Tested below
214
215print 'expr_stmt' # (exprlist '=')* exprlist
2161
2171, 2, 3
218x = 1
219x = 1, 2, 3
220x = y = z = 1, 2, 3
221x, y, z = 1, 2, 3
222abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
223# NB these variables are deleted below
224
225print 'print_stmt' # 'print' (test ',')* [test]
226print 1, 2, 3
227print 1, 2, 3,
228print
229print 0 or 1, 0 or 1,
230print 0 or 1
231
232print 'del_stmt' # 'del' exprlist
233del abc
234del x, y, (z, xyz)
235
236print 'pass_stmt' # 'pass'
237pass
238
239print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
240# Tested below
241
242print 'break_stmt' # 'break'
243while 1: break
244
245print 'continue_stmt' # 'continue'
246i = 1
247while i: i = 0; continue
248
249print 'return_stmt' # 'return' [testlist]
250def g1(): return
251def g2(): return 1
252g1()
253x = g2()
254
255print 'raise_stmt' # 'raise' test [',' test]
256try: raise RuntimeError, 'just testing'
257except RuntimeError: pass
258try: raise KeyboardInterrupt
259except KeyboardInterrupt: pass
260
261print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000262import sys
Guido van Rossum3bead091992-01-27 17:00:37 +0000263import time, math
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000265from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +0000266from math import sin, cos
Guido van Rossum3bead091992-01-27 17:00:37 +0000267
268print 'global_stmt' # 'global' NAME (',' NAME)*
269def f():
270 global a
271 global a, b
272 global one, two, three, four, five, six, seven, eight, nine, ten
273
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000274print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
275def f():
276 z = None
277 del z
278 exec 'z=1+1\n'
279 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
280 del z
281 exec 'z=1+1'
282 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
283f()
284g = {}
285exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000286if g.has_key('__builtins__'): del g['__builtins__']
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000287if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
288g = {}
289l = {}
290exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000291if g.has_key('__builtins__'): del g['__builtins__']
292if l.has_key('__builtins__'): del l['__builtins__']
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000293if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
294
295
Guido van Rossum3bead091992-01-27 17:00:37 +0000296### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
297# Tested below
298
299print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
300if 1: pass
301if 1: pass
302else: pass
303if 0: pass
304elif 0: pass
305if 0: pass
306elif 0: pass
307elif 0: pass
308elif 0: pass
309else: pass
310
311print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
312while 0: pass
313while 0: pass
314else: pass
315
316print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000317for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000318for i, j, k in (): pass
319else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320class Squares:
321 def __init__(self, max):
322 self.max = max
323 self.sofar = []
324 def __len__(self): return len(self.sofar)
325 def __getitem__(self, i):
326 if not 0 <= i < self.max: raise IndexError
327 n = len(self.sofar)
328 while n <= i:
329 self.sofar.append(n*n)
330 n = n+1
331 return self.sofar[i]
332n = 0
333for x in Squares(10): n = n+x
334if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000335
Guido van Rossumb6775db1994-08-01 11:34:53 +0000336print 'try_stmt'
337### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
338### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000339### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000340try:
341 1/0
342except ZeroDivisionError:
343 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000344else:
345 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000346try: 1/0
347except EOFError: pass
348except TypeError, msg: pass
349except RuntimeError, msg: pass
350except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000351else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000352try: 1/0
353except (EOFError, TypeError, ZeroDivisionError): pass
354try: 1/0
355except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000356try: pass
357finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000358
359print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
360if 1: pass
361if 1:
362 pass
363if 1:
364 #
365 #
366 #
367 pass
368 pass
369 #
370 pass
371 #
372
373print 'test'
374### and_test ('or' and_test)*
375### and_test: not_test ('and' not_test)*
376### not_test: 'not' not_test | comparison
377if not 1: pass
378if 1 and 1: pass
379if 1 or 1: pass
380if not not not 1: pass
381if not 1 and 1 and 1: pass
382if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
383
384print 'comparison'
385### comparison: expr (comp_op expr)*
386### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
387if 1: pass
388x = (1 == 1)
389if 1 == 1: pass
390if 1 != 1: pass
391if 1 <> 1: pass
392if 1 < 1: pass
393if 1 > 1: pass
394if 1 <= 1: pass
395if 1 >= 1: pass
396if 1 is 1: pass
397if 1 is not 1: pass
398if 1 in (): pass
399if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000400if 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 +0000401
402print 'binary mask ops'
403x = 1 & 1
404x = 1 ^ 1
405x = 1 | 1
406
407print 'shift ops'
408x = 1 << 1
409x = 1 >> 1
410x = 1 << 1 >> 1
411
412print 'additive ops'
413x = 1
414x = 1 + 1
415x = 1 - 1 - 1
416x = 1 - 1 + 1 - 1 + 1
417
418print 'multiplicative ops'
419x = 1 * 1
420x = 1 / 1
421x = 1 % 1
422x = 1 / 1 * 1 % 1
423
424print 'unary ops'
425x = +1
426x = -1
427x = ~1
428x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
429x = -1*1/1 + 1*1 - ---1*1
430
431print 'selectors'
432### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
433### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000434f1()
435f2(1)
436f2(1,)
437f3(1, 2)
438f3(1, 2,)
439f4(1, (2, (3, 4)))
440v0()
441v0(1)
442v0(1,)
443v0(1,2)
444v0(1,2,3,4,5,6,7,8,9,0)
445v1(1)
446v1(1,)
447v1(1,2)
448v1(1,2,3)
449v1(1,2,3,4,5,6,7,8,9,0)
450v2(1,2)
451v2(1,2,3)
452v2(1,2,3,4)
453v2(1,2,3,4,5,6,7,8,9,0)
454v3(1,(2,3))
455v3(1,(2,3),4)
456v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000457import sys, time
458c = sys.path[0]
459x = time.time()
460x = sys.modules['time'].time()
461a = '01234'
462c = a[0]
463c = a[-1]
464s = a[0:5]
465s = a[:5]
466s = a[0:]
467s = a[:]
468s = a[-5:]
469s = a[:-1]
470s = a[-4:-3]
471
472print 'atoms'
473### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
474### dictmaker: test ':' test (',' test ':' test)* [',']
475
476x = (1)
477x = (1 or 2 or 3)
478x = (1 or 2 or 3, 2, 3)
479
480x = []
481x = [1]
482x = [1 or 2 or 3]
483x = [1 or 2 or 3, 2, 3]
484x = []
485
486x = {}
487x = {'one': 1}
488x = {'one': 1,}
489x = {'one' or 'two': 1 or 2}
490x = {'one': 1, 'two': 2}
491x = {'one': 1, 'two': 2,}
492x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
493
494x = `x`
495x = `1 or 2 or 3`
496x = x
497x = 'x'
498x = 123
499
500### exprlist: expr (',' expr)* [',']
501### testlist: test (',' test)* [',']
502# These have been exercised enough above
503
Guido van Rossum85f18201992-11-27 22:53:50 +0000504print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000505class B: pass
506class C1(B): pass
507class C2(B): pass
508class D(C1, C2, B): pass
509class C:
510 def meth1(self): pass
511 def meth2(self, arg): pass
512 def meth3(self, a1, a2): pass