blob: 21012d2d8e8793fa3627d811ad44fab76294c5da [file] [log] [blame]
Guido van Rossum5c971671996-07-22 15:23:25 +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, 'large positive int'
27try:
28 from sys import maxint
29except ImportError:
30 maxint = 2147483647
31if maxint == 2147483647:
32 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 print \
43 'No OverflowError on huge integer literal ' + `s`
44elif eval('maxint == 9223372036854775807'):
45 if eval('-9223372036854775807-1 != 01000000000000000000000'):
46 raise TestFailed, 'max negative int'
47 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
48 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
49 for s in '9223372036854775808', '02000000000000000000000', \
50 '0x10000000000000000':
51 try:
52 x = eval(s)
53 except OverflowError:
54 continue
55 raise TestFailed, \
56 'No OverflowError on huge integer literal ' + `s`
57else:
58 print 'Weird maxint value', maxint
59
60print '1.1.2.2 Long integers'
61x = 0L
62x = 0l
63x = 0xffffffffffffffffL
64x = 0xffffffffffffffffl
65x = 077777777777777777L
66x = 077777777777777777l
67x = 123456789012345678901234567890L
68x = 123456789012345678901234567890l
69
70print '1.1.2.3 Floating point'
71x = 3.14
72x = 314.
73x = 0.314
74# XXX x = 000.314
75x = .314
76x = 3e14
77x = 3E14
78x = 3e-14
79x = 3e+14
80x = 3.e14
81x = .3e14
82x = 3.1e4
83
84print '1.1.3 String literals'
85
Guido van Rossumde554ec1997-05-08 23:14:57 +000086##def assert(s):
87## if not s: raise TestFailed, 'see traceback'
Guido van Rossum5c971671996-07-22 15:23:25 +000088
89x = ''; y = ""; assert(len(x) == 0 and x == y)
90x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
91x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
92x = "doesn't \"shrink\" does it"
93y = 'doesn\'t "shrink" does it'
94assert(len(x) == 24 and x == y)
95x = "does \"shrink\" doesn't it"
96y = 'does "shrink" doesn\'t it'
97assert(len(x) == 24 and x == y)
98x = """
99The "quick"
100brown fox
101jumps over
102the 'lazy' dog.
103"""
104y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
105assert(x == y)
106y = '''
107The "quick"
108brown fox
109jumps over
110the 'lazy' dog.
111'''; assert(x == y)
112y = "\n\
113The \"quick\"\n\
114brown fox\n\
115jumps over\n\
116the 'lazy' dog.\n\
117"; assert(x == y)
118y = '\n\
119The \"quick\"\n\
120brown fox\n\
121jumps over\n\
122the \'lazy\' dog.\n\
123'; assert(x == y)
124
125
126print '1.2 Grammar'
127
128print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
129# XXX can't test in a script -- this rule is only used when interactive
130
131print 'file_input' # (NEWLINE | stmt)* ENDMARKER
132# Being tested as this very moment this very module
133
134print 'expr_input' # testlist NEWLINE
135# XXX Hard to test -- used only in calls to input()
136
137print 'eval_input' # testlist ENDMARKER
138x = eval('1, 0 or 1')
139
140print 'funcdef'
141### 'def' NAME parameters ':' suite
142### parameters: '(' [varargslist] ')'
143### varargslist: (fpdef ['=' test] ',')* '*' NAME
144### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
145### fpdef: NAME | '(' fplist ')'
146### fplist: fpdef (',' fpdef)* [',']
147def f1(): pass
148def f2(one_argument): pass
149def f3(two, arguments): pass
150def f4(two, (compound, (argument, list))): pass
151def a1(one_arg,): pass
152def a2(two, args,): pass
153def v0(*rest): pass
154def v1(a, *rest): pass
155def v2(a, b, *rest): pass
156def v3(a, (b, c), *rest): pass
157def d01(a=1): pass
158d01()
159d01(1)
160def d11(a, b=1): pass
161d11(1)
162d11(1, 2)
163def d21(a, b, c=1): pass
164d21(1, 2)
165d21(1, 2, 3)
166def d02(a=1, b=2): pass
167d02()
168d02(1)
169d02(1, 2)
170def d12(a, b=1, c=2): pass
171d12(1)
172d12(1, 2)
173d12(1, 2, 3)
174def d22(a, b, c=1, d=2): pass
175d22(1, 2)
176d22(1, 2, 3)
177d22(1, 2, 3, 4)
178def d01v(a=1, *rest): pass
179d01v()
180d01v(1)
181d01v(1, 2)
182def d11v(a, b=1, *rest): pass
183d11v(1)
184d11v(1, 2)
185d11v(1, 2, 3)
186def d21v(a, b, c=1, *rest): pass
187d21v(1, 2)
188d21v(1, 2, 3)
189d21v(1, 2, 3, 4)
190def d02v(a=1, b=2, *rest): pass
191d02v()
192d02v(1)
193d02v(1, 2)
194d02v(1, 2, 3)
195def d12v(a, b=1, c=2, *rest): pass
196d12v(1)
197d12v(1, 2)
198d12v(1, 2, 3)
199d12v(1, 2, 3, 4)
200def d22v(a, b, c=1, d=2, *rest): pass
201d22v(1, 2)
202d22v(1, 2, 3)
203d22v(1, 2, 3, 4)
204d22v(1, 2, 3, 4, 5)
205
206### stmt: simple_stmt | compound_stmt
207# Tested below
208
209### simple_stmt: small_stmt (';' small_stmt)* [';']
210print 'simple_stmt'
211x = 1; pass; del x
212
213### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
214# Tested below
215
216print 'expr_stmt' # (exprlist '=')* exprlist
2171
2181, 2, 3
219x = 1
220x = 1, 2, 3
221x = y = z = 1, 2, 3
222x, y, z = 1, 2, 3
223abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
224# NB these variables are deleted below
225
226print 'print_stmt' # 'print' (test ',')* [test]
227print 1, 2, 3
228print 1, 2, 3,
229print
230print 0 or 1, 0 or 1,
231print 0 or 1
232
233print 'del_stmt' # 'del' exprlist
234del abc
235del x, y, (z, xyz)
236
237print 'pass_stmt' # 'pass'
238pass
239
240print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
241# Tested below
242
243print 'break_stmt' # 'break'
244while 1: break
245
246print 'continue_stmt' # 'continue'
247i = 1
248while i: i = 0; continue
249
250print 'return_stmt' # 'return' [testlist]
251def g1(): return
252def g2(): return 1
253g1()
254x = g2()
255
256print 'raise_stmt' # 'raise' test [',' test]
257try: raise RuntimeError, 'just testing'
258except RuntimeError: pass
259try: raise KeyboardInterrupt
260except KeyboardInterrupt: pass
261
262print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
263import sys
264import time, sys
265from time import time
266from sys import *
267from sys import path, argv
268
269print 'global_stmt' # 'global' NAME (',' NAME)*
270def f():
271 global a
272 global a, b
273 global one, two, three, four, five, six, seven, eight, nine, ten
274
275print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
276def f():
277 z = None
278 del z
279 exec 'z=1+1\n'
280 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
281 del z
282 exec 'z=1+1'
283 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
284f()
285g = {}
286exec 'z = 1' in g
287if g.has_key('__builtins__'): del g['__builtins__']
288if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
289g = {}
290l = {}
291exec 'global a; a = 1; b = 2' in g, l
292if g.has_key('__builtins__'): del g['__builtins__']
293if l.has_key('__builtins__'): del l['__builtins__']
294if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
295
296
297### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
298# Tested below
299
300print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
301if 1: pass
302if 1: pass
303else: pass
304if 0: pass
305elif 0: pass
306if 0: pass
307elif 0: pass
308elif 0: pass
309elif 0: pass
310else: pass
311
312print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
313while 0: pass
314while 0: pass
315else: pass
316
317print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
318for i in 1, 2, 3: pass
319for i, j, k in (): pass
320else: pass
321class Squares:
322 def __init__(self, max):
323 self.max = max
324 self.sofar = []
325 def __len__(self): return len(self.sofar)
326 def __getitem__(self, i):
327 if not 0 <= i < self.max: raise IndexError
328 n = len(self.sofar)
329 while n <= i:
330 self.sofar.append(n*n)
331 n = n+1
332 return self.sofar[i]
333n = 0
334for x in Squares(10): n = n+x
335if n != 285: raise TestFailed, 'for over growing sequence'
336
337print 'try_stmt'
338### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
339### | 'try' ':' suite 'finally' ':' suite
340### except_clause: 'except' [expr [',' expr]]
341try:
342 1/0
343except ZeroDivisionError:
344 pass
345else:
346 pass
347try: 1/0
348except EOFError: pass
349except TypeError, msg: pass
350except RuntimeError, msg: pass
351except: pass
352else: pass
353try: 1/0
354except (EOFError, TypeError, ZeroDivisionError): pass
355try: 1/0
356except (EOFError, TypeError, ZeroDivisionError), msg: pass
357try: pass
358finally: pass
359
360print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
361if 1: pass
362if 1:
363 pass
364if 1:
365 #
366 #
367 #
368 pass
369 pass
370 #
371 pass
372 #
373
374print 'test'
375### and_test ('or' and_test)*
376### and_test: not_test ('and' not_test)*
377### not_test: 'not' not_test | comparison
378if not 1: pass
379if 1 and 1: pass
380if 1 or 1: pass
381if not not not 1: pass
382if not 1 and 1 and 1: pass
383if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
384
385print 'comparison'
386### comparison: expr (comp_op expr)*
387### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
388if 1: pass
389x = (1 == 1)
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 >= 1: pass
397if 1 is 1: pass
398if 1 is not 1: pass
399if 1 in (): pass
400if 1 not in (): pass
401if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
402
403print 'binary mask ops'
404x = 1 & 1
405x = 1 ^ 1
406x = 1 | 1
407
408print 'shift ops'
409x = 1 << 1
410x = 1 >> 1
411x = 1 << 1 >> 1
412
413print 'additive ops'
414x = 1
415x = 1 + 1
416x = 1 - 1 - 1
417x = 1 - 1 + 1 - 1 + 1
418
419print 'multiplicative ops'
420x = 1 * 1
421x = 1 / 1
422x = 1 % 1
423x = 1 / 1 * 1 % 1
424
425print 'unary ops'
426x = +1
427x = -1
428x = ~1
429x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
430x = -1*1/1 + 1*1 - ---1*1
431
432print 'selectors'
433### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
434### subscript: expr | [expr] ':' [expr]
435f1()
436f2(1)
437f2(1,)
438f3(1, 2)
439f3(1, 2,)
440f4(1, (2, (3, 4)))
441v0()
442v0(1)
443v0(1,)
444v0(1,2)
445v0(1,2,3,4,5,6,7,8,9,0)
446v1(1)
447v1(1,)
448v1(1,2)
449v1(1,2,3)
450v1(1,2,3,4,5,6,7,8,9,0)
451v2(1,2)
452v2(1,2,3)
453v2(1,2,3,4)
454v2(1,2,3,4,5,6,7,8,9,0)
455v3(1,(2,3))
456v3(1,(2,3),4)
457v3(1,(2,3),4,5,6,7,8,9,0)
458import sys, time
459c = sys.path[0]
460x = time.time()
461x = sys.modules['time'].time()
462a = '01234'
463c = a[0]
464c = a[-1]
465s = a[0:5]
466s = a[:5]
467s = a[0:]
468s = a[:]
469s = a[-5:]
470s = a[:-1]
471s = a[-4:-3]
472
473print 'atoms'
474### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
475### dictmaker: test ':' test (',' test ':' test)* [',']
476
477x = (1)
478x = (1 or 2 or 3)
479x = (1 or 2 or 3, 2, 3)
480
481x = []
482x = [1]
483x = [1 or 2 or 3]
484x = [1 or 2 or 3, 2, 3]
485x = []
486
487x = {}
488x = {'one': 1}
489x = {'one': 1,}
490x = {'one' or 'two': 1 or 2}
491x = {'one': 1, 'two': 2}
492x = {'one': 1, 'two': 2,}
493x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
494
495x = `x`
496x = `1 or 2 or 3`
497x = x
498x = 'x'
499x = 123
500
501### exprlist: expr (',' expr)* [',']
502### testlist: test (',' test)* [',']
503# These have been exercised enough above
504
505print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
506class B: pass
507class C1(B): pass
508class C2(B): pass
509class D(C1, C2, B): pass
510class C:
511 def meth1(self): pass
512 def meth2(self, arg): pass
513 def meth3(self, a1, a2): pass