test_grammar.py 11.2 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# Python test set -- part 1, grammar.
# This just tests whether the parser accepts them all.

from test_support import *

print '1. Parser'

print '1.1 Tokens'

print '1.1.1 Backslashes'

# Backslash means line continuation:
x = 1 \
+ 1
if x <> 2: raise TestFailed, 'backslash for line continuation'

# Backslash does not means continuation in comments :\
x = 0
if x <> 0: raise TestFailed, 'backslash ending comment'

print '1.1.2 Numeric literals'

print '1.1.2.1 Plain integers'
if 0xff <> 255: raise TestFailed, 'hex int'
if 0377 <> 255: raise TestFailed, 'octal int'
Guido van Rossum's avatar
Guido van Rossum committed
26 27 28 29 30 31
if  2147483647   != 017777777777: raise TestFailed, 'large positive int'
try:
	from sys import maxint
except ImportError:
	maxint = 2147483647
if maxint == 2147483647:
32 33 34 35 36 37 38 39 40
	if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
	# XXX -2147483648
	if 037777777777 != -1: raise TestFailed, 'oct -1'
	if 0xffffffff != -1: raise TestFailed, 'hex -1'
	for s in '2147483648', '040000000000', '0x100000000':
		try:
			x = eval(s)
		except OverflowError:
			continue
41 42
##		raise TestFailed, \
		print \
43
			  'No OverflowError on huge integer literal ' + `s`
Guido van Rossum's avatar
Guido van Rossum committed
44
elif eval('maxint == 9223372036854775807'):
45
	if eval('-9223372036854775807-1 != 01000000000000000000000'):
Guido van Rossum's avatar
Guido van Rossum committed
46 47 48 49 50 51 52 53 54 55 56 57 58
		raise TestFailed, 'max negative int'
	if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
	if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
	for s in '9223372036854775808', '02000000000000000000000', \
		 '0x10000000000000000':
		try:
			x = eval(s)
		except OverflowError:
			continue
		raise TestFailed, \
			  'No OverflowError on huge integer literal ' + `s`
else:
	print 'Weird maxint value', maxint
Guido van Rossum's avatar
Guido van Rossum committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

print '1.1.2.2 Long integers'
x = 0L
x = 0l
x = 0xffffffffffffffffL
x = 0xffffffffffffffffl
x = 077777777777777777L
x = 077777777777777777l
x = 123456789012345678901234567890L
x = 123456789012345678901234567890l

print '1.1.2.3 Floating point'
x = 3.14
x = 314.
x = 0.314
# XXX x = 000.314
x = .314
x = 3e14
x = 3E14
x = 3e-14
x = 3e+14
x = 3.e14
x = .3e14
x = 3.1e4

84 85
print '1.1.3 String literals'

86 87
##def assert(s):
##	if not s: raise TestFailed, 'see traceback'
88 89 90 91 92 93 94

x = ''; y = ""; assert(len(x) == 0 and x == y)
x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
x = "doesn't \"shrink\" does it"
y = 'doesn\'t "shrink" does it'
assert(len(x) == 24 and x == y)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
x = "does \"shrink\" doesn't it"
y = 'does "shrink" doesn\'t it'
assert(len(x) == 24 and x == y)
x = """
The "quick"
brown fox
jumps over
the 'lazy' dog.
"""
y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
assert(x == y)
y = '''
The "quick"
brown fox
jumps over
the 'lazy' dog.
'''; assert(x == y)
y = "\n\
The \"quick\"\n\
brown fox\n\
jumps over\n\
the 'lazy' dog.\n\
"; assert(x == y)
y = '\n\
The \"quick\"\n\
brown fox\n\
jumps over\n\
the \'lazy\' dog.\n\
'; assert(x == y)
124 125


Guido van Rossum's avatar
Guido van Rossum committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
print '1.2 Grammar'

print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
# XXX can't test in a script -- this rule is only used when interactive

print 'file_input' # (NEWLINE | stmt)* ENDMARKER
# Being tested as this very moment this very module

print 'expr_input' # testlist NEWLINE
# XXX Hard to test -- used only in calls to input()

print 'eval_input' # testlist ENDMARKER
x = eval('1, 0 or 1')

print 'funcdef'
### 'def' NAME parameters ':' suite
### parameters: '(' [varargslist] ')'
143 144 145
### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
###            | ('**'|'*' '*') NAME)
###            | fpdef ['=' test] (',' fpdef ['=' test])* [',']  
Guido van Rossum's avatar
Guido van Rossum committed
146 147
### fpdef: NAME | '(' fplist ')'
### fplist: fpdef (',' fpdef)* [',']
148 149
### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
### argument: [test '='] test	# Really [keyword '='] test
Guido van Rossum's avatar
Guido van Rossum committed
150
def f1(): pass
151 152 153
f1()
f1(*())
f1(*(), **{})
Guido van Rossum's avatar
Guido van Rossum committed
154 155 156 157 158 159 160 161 162
def f2(one_argument): pass
def f3(two, arguments): pass
def f4(two, (compound, (argument, list))): pass
def a1(one_arg,): pass
def a2(two, args,): pass
def v0(*rest): pass
def v1(a, *rest): pass
def v2(a, b, *rest): pass
def v3(a, (b, c), *rest): pass
163 164 165
def d01(a=1): pass
d01()
d01(1)
166 167
d01(*(1,))
d01(**{'a':2})
168 169 170
def d11(a, b=1): pass
d11(1)
d11(1, 2)
171
d11(1, **{'b':2})
172 173 174
def d21(a, b, c=1): pass
d21(1, 2)
d21(1, 2, 3)
175 176 177 178
d21(*(1, 2, 3))
d21(1, *(2, 3))
d21(1, 2, *(3,))
d21(1, 2, **{'c':3})
179 180 181 182
def d02(a=1, b=2): pass
d02()
d02(1)
d02(1, 2)
183 184 185 186
d02(*(1, 2))
d02(1, *(2,))
d02(1, **{'b':2})
d02(**{'a': 1, 'b': 2})
187 188 189 190 191 192 193 194 195 196 197 198
def d12(a, b=1, c=2): pass
d12(1)
d12(1, 2)
d12(1, 2, 3)
def d22(a, b, c=1, d=2): pass
d22(1, 2)
d22(1, 2, 3)
d22(1, 2, 3, 4)
def d01v(a=1, *rest): pass
d01v()
d01v(1)
d01v(1, 2)
199 200 201
d01v(*(1, 2, 3, 4))
d01v(*(1,))
d01v(**{'a':2})
202 203 204 205 206 207 208 209
def d11v(a, b=1, *rest): pass
d11v(1)
d11v(1, 2)
d11v(1, 2, 3)
def d21v(a, b, c=1, *rest): pass
d21v(1, 2)
d21v(1, 2, 3)
d21v(1, 2, 3, 4)
210 211
d21v(*(1, 2, 3, 4))
d21v(1, 2, **{'c': 3})
212 213 214 215 216
def d02v(a=1, b=2, *rest): pass
d02v()
d02v(1)
d02v(1, 2)
d02v(1, 2, 3)
217 218
d02v(1, *(2, 3, 4))
d02v(**{'a': 1, 'b': 2})
219 220 221 222 223
def d12v(a, b=1, c=2, *rest): pass
d12v(1)
d12v(1, 2)
d12v(1, 2, 3)
d12v(1, 2, 3, 4)
224 225 226
d12v(*(1, 2, 3, 4))
d12v(1, 2, *(3, 4, 5))
d12v(1, *(2,), **{'c': 3})
227 228 229 230 231
def d22v(a, b, c=1, d=2, *rest): pass
d22v(1, 2)
d22v(1, 2, 3)
d22v(1, 2, 3, 4)
d22v(1, 2, 3, 4, 5)
232 233 234
d22v(*(1, 2, 3, 4))
d22v(1, 2, *(3, 4, 5))
d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum's avatar
Guido van Rossum committed
235 236 237 238 239 240 241 242

### stmt: simple_stmt | compound_stmt
# Tested below

### simple_stmt: small_stmt (';' small_stmt)* [';']
print 'simple_stmt'
x = 1; pass; del x

243
### small_stmt: expr_stmt | print_stmt  | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
Guido van Rossum's avatar
Guido van Rossum committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
# Tested below

print 'expr_stmt' # (exprlist '=')* exprlist
1
1, 2, 3
x = 1
x = 1, 2, 3
x = y = z = 1, 2, 3
x, y, z = 1, 2, 3
abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
# NB these variables are deleted below

print 'print_stmt' # 'print' (test ',')* [test]
print 1, 2, 3
print 1, 2, 3,
print
print 0 or 1, 0 or 1,
print 0 or 1

print 'del_stmt' # 'del' exprlist
del abc
del x, y, (z, xyz)

print 'pass_stmt' # 'pass'
pass

print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
# Tested below

print 'break_stmt' # 'break'
while 1: break

print 'continue_stmt' # 'continue'
i = 1
while i: i = 0; continue

print 'return_stmt' # 'return' [testlist]
def g1(): return
def g2(): return 1
g1()
x = g2()

print 'raise_stmt' # 'raise' test [',' test]
try: raise RuntimeError, 'just testing'
except RuntimeError: pass
try: raise KeyboardInterrupt
except KeyboardInterrupt: pass

print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
import sys
294
import time, sys
295
from time import time
Guido van Rossum's avatar
Guido van Rossum committed
296
from sys import *
297
from sys import path, argv
Guido van Rossum's avatar
Guido van Rossum committed
298 299 300 301 302 303 304

print 'global_stmt' # 'global' NAME (',' NAME)*
def f():
	global a
	global a, b
	global one, two, three, four, five, six, seven, eight, nine, ten

305 306 307 308 309 310 311 312 313 314 315 316
print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
def f():
	z = None
	del z
	exec 'z=1+1\n'
	if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
	del z
	exec 'z=1+1'
	if z <> 2: raise TestFailed, 'exec \'z=1+1\''
f()
g = {}
exec 'z = 1' in g
317
if g.has_key('__builtins__'): del g['__builtins__']
318 319 320 321
if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
g = {}
l = {}
exec 'global a; a = 1; b = 2' in g, l
322 323
if g.has_key('__builtins__'): del g['__builtins__']
if l.has_key('__builtins__'): del l['__builtins__']
324 325 326
if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'


Guido van Rossum's avatar
Guido van Rossum committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
# Tested below

print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
if 1: pass
if 1: pass
else: pass
if 0: pass
elif 0: pass
if 0: pass
elif 0: pass
elif 0: pass
elif 0: pass
else: pass

print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
while 0: pass
while 0: pass
else: pass

print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
for i in 1, 2, 3: pass
for i, j, k in (): pass
else: pass
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
class Squares:
	def __init__(self, max):
		self.max = max
		self.sofar = []
	def __len__(self): return len(self.sofar)
	def __getitem__(self, i):
		if not 0 <= i < self.max: raise IndexError
		n = len(self.sofar)
		while n <= i:
			self.sofar.append(n*n)
			n = n+1
		return self.sofar[i]
n = 0
for x in Squares(10): n = n+x
if n != 285: raise TestFailed, 'for over growing sequence'

print 'try_stmt'
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
###         | 'try' ':' suite 'finally' ':' suite
Guido van Rossum's avatar
Guido van Rossum committed
370
### except_clause: 'except' [expr [',' expr]]
371 372 373 374
try:
	1/0
except ZeroDivisionError:
	pass
375 376
else:
	pass
Guido van Rossum's avatar
Guido van Rossum committed
377 378 379 380 381
try: 1/0
except EOFError: pass
except TypeError, msg: pass
except RuntimeError, msg: pass
except: pass
382
else: pass
383 384 385 386
try: 1/0
except (EOFError, TypeError, ZeroDivisionError): pass
try: 1/0
except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum's avatar
Guido van Rossum committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
try: pass
finally: pass

print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
if 1: pass
if 1:
	pass
if 1:
	#
	#
	#
	pass
	pass
	#
	pass
	#

print 'test'
### and_test ('or' and_test)*
### and_test: not_test ('and' not_test)*
### not_test: 'not' not_test | comparison
if not 1: pass
if 1 and 1: pass
if 1 or 1: pass
if not not not 1: pass
if not 1 and 1 and 1: pass
if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass

print 'comparison'
### comparison: expr (comp_op expr)*
### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
if 1: pass
x = (1 == 1)
if 1 == 1: pass
if 1 != 1: pass
if 1 <> 1: pass
if 1 < 1: pass
if 1 > 1: pass
if 1 <= 1: pass
if 1 >= 1: pass
if 1 is 1: pass
if 1 is not 1: pass
if 1 in (): pass
if 1 not in (): pass
431
if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
Guido van Rossum's avatar
Guido van Rossum committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

print 'binary mask ops'
x = 1 & 1
x = 1 ^ 1
x = 1 | 1

print 'shift ops'
x = 1 << 1
x = 1 >> 1
x = 1 << 1 >> 1

print 'additive ops'
x = 1
x = 1 + 1
x = 1 - 1 - 1
x = 1 - 1 + 1 - 1 + 1

print 'multiplicative ops'
x = 1 * 1
x = 1 / 1
x = 1 % 1
x = 1 / 1 * 1 % 1

print 'unary ops'
x = +1
x = -1
x = ~1
x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
x = -1*1/1 + 1*1 - ---1*1

print 'selectors'
### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
### subscript: expr | [expr] ':' [expr]
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
f1()
f2(1)
f2(1,)
f3(1, 2)
f3(1, 2,)
f4(1, (2, (3, 4)))
v0()
v0(1)
v0(1,)
v0(1,2)
v0(1,2,3,4,5,6,7,8,9,0)
v1(1)
v1(1,)
v1(1,2)
v1(1,2,3)
v1(1,2,3,4,5,6,7,8,9,0)
v2(1,2)
v2(1,2,3)
v2(1,2,3,4)
v2(1,2,3,4,5,6,7,8,9,0)
v3(1,(2,3))
v3(1,(2,3),4)
v3(1,(2,3),4,5,6,7,8,9,0)
488
print
Guido van Rossum's avatar
Guido van Rossum committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
import sys, time
c = sys.path[0]
x = time.time()
x = sys.modules['time'].time()
a = '01234'
c = a[0]
c = a[-1]
s = a[0:5]
s = a[:5]
s = a[0:]
s = a[:]
s = a[-5:]
s = a[:-1]
s = a[-4:-3]

print 'atoms'
### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
### dictmaker: test ':' test (',' test ':' test)* [',']

x = (1)
x = (1 or 2 or 3)
x = (1 or 2 or 3, 2, 3)

x = []
x = [1]
x = [1 or 2 or 3]
x = [1 or 2 or 3, 2, 3]
x = []

x = {}
x = {'one': 1}
x = {'one': 1,}
x = {'one' or 'two': 1 or 2}
x = {'one': 1, 'two': 2}
x = {'one': 1, 'two': 2,}
x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}

x = `x`
x = `1 or 2 or 3`
x = x
x = 'x'
x = 123

### exprlist: expr (',' expr)* [',']
### testlist: test (',' test)* [',']
# These have been exercised enough above

536
print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum's avatar
Guido van Rossum committed
537 538 539 540 541 542 543 544
class B: pass
class C1(B): pass
class C2(B): pass
class D(C1, C2, B): pass
class C:
	def meth1(self): pass
	def meth2(self, arg): pass
	def meth3(self, a1, a2): pass