📄 test_gra.py
字号:
# 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 \+ 1if x <> 2: raise TestFailed, 'backslash for line continuation'# Backslash does not means continuation in comments :\x = 0if 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'if 2147483647 != 017777777777: raise TestFailed, 'large positive int'try: from sys import maxintexcept ImportError: maxint = 2147483647if maxint == 2147483647: 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## raise TestFailed, \ print \ 'No OverflowError on huge integer literal ' + `s`elif eval('maxint == 9223372036854775807'): if eval('-9223372036854775807-1 != 01000000000000000000000'): 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', maxintprint '1.1.2.2 Long integers'x = 0Lx = 0lx = 0xffffffffffffffffLx = 0xfffffffffffffffflx = 077777777777777777Lx = 077777777777777777lx = 123456789012345678901234567890Lx = 123456789012345678901234567890lprint '1.1.2.3 Floating point'x = 3.14x = 314.x = 0.314# XXX x = 000.314x = .314x = 3e14x = 3E14x = 3e-14x = 3e+14x = 3.e14x = .3e14x = 3.1e4print '1.1.3 String literals'##def assert(s):## if not s: raise TestFailed, 'see traceback'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)x = "does \"shrink\" doesn't it"y = 'does "shrink" doesn\'t it'assert(len(x) == 24 and x == y)x = """The "quick"brown foxjumps overthe 'lazy' dog."""y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'assert(x == y)y = '''The "quick"brown foxjumps overthe '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)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 interactiveprint 'file_input' # (NEWLINE | stmt)* ENDMARKER# Being tested as this very moment this very moduleprint 'expr_input' # testlist NEWLINE# XXX Hard to test -- used only in calls to input()print 'eval_input' # testlist ENDMARKERx = eval('1, 0 or 1')print 'funcdef'### 'def' NAME parameters ':' suite### parameters: '(' [varargslist] ')'### varargslist: (fpdef ['=' test] ',')* '*' NAME### | fpdef ['=' test] (',' fpdef ['=' test])* [',']### fpdef: NAME | '(' fplist ')'### fplist: fpdef (',' fpdef)* [',']def f1(): passdef f2(one_argument): passdef f3(two, arguments): passdef f4(two, (compound, (argument, list))): passdef a1(one_arg,): passdef a2(two, args,): passdef v0(*rest): passdef v1(a, *rest): passdef v2(a, b, *rest): passdef v3(a, (b, c), *rest): passdef d01(a=1): passd01()d01(1)def d11(a, b=1): passd11(1)d11(1, 2)def d21(a, b, c=1): passd21(1, 2)d21(1, 2, 3)def d02(a=1, b=2): passd02()d02(1)d02(1, 2)def d12(a, b=1, c=2): passd12(1)d12(1, 2)d12(1, 2, 3)def d22(a, b, c=1, d=2): passd22(1, 2)d22(1, 2, 3)d22(1, 2, 3, 4)def d01v(a=1, *rest): passd01v()d01v(1)d01v(1, 2)def d11v(a, b=1, *rest): passd11v(1)d11v(1, 2)d11v(1, 2, 3)def d21v(a, b, c=1, *rest): passd21v(1, 2)d21v(1, 2, 3)d21v(1, 2, 3, 4)def d02v(a=1, b=2, *rest): passd02v()d02v(1)d02v(1, 2)d02v(1, 2, 3)def d12v(a, b=1, c=2, *rest): passd12v(1)d12v(1, 2)d12v(1, 2, 3)d12v(1, 2, 3, 4)def d22v(a, b, c=1, d=2, *rest): passd22v(1, 2)d22v(1, 2, 3)d22v(1, 2, 3, 4)d22v(1, 2, 3, 4, 5)### stmt: simple_stmt | compound_stmt# Tested below### simple_stmt: small_stmt (';' small_stmt)* [';']print 'simple_stmt'x = 1; pass; del x### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt# Tested belowprint 'expr_stmt' # (exprlist '=')* exprlist11, 2, 3x = 1x = 1, 2, 3x = y = z = 1, 2, 3x, y, z = 1, 2, 3abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)# NB these variables are deleted belowprint 'print_stmt' # 'print' (test ',')* [test]print 1, 2, 3print 1, 2, 3,printprint 0 or 1, 0 or 1,print 0 or 1print 'del_stmt' # 'del' exprlistdel abcdel x, y, (z, xyz)print 'pass_stmt' # 'pass'passprint 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt# Tested belowprint 'break_stmt' # 'break'while 1: breakprint 'continue_stmt' # 'continue'i = 1while i: i = 0; continueprint 'return_stmt' # 'return' [testlist]def g1(): returndef g2(): return 1g1()x = g2()print 'raise_stmt' # 'raise' test [',' test]try: raise RuntimeError, 'just testing'except RuntimeError: passtry: raise KeyboardInterruptexcept KeyboardInterrupt: passprint 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)import sysimport time, sysfrom time import timefrom sys import *from sys import path, argvprint 'global_stmt' # 'global' NAME (',' NAME)*def f(): global a global a, b global one, two, three, four, five, six, seven, eight, nine, tenprint '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 gif g.has_key('__builtins__'): del g['__builtins__']if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'g = {}l = {}exec 'global a; a = 1; b = 2' in g, lif g.has_key('__builtins__'): del g['__builtins__']if l.has_key('__builtins__'): del l['__builtins__']if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef# Tested belowprint 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]if 1: passif 1: passelse: passif 0: passelif 0: passif 0: passelif 0: passelif 0: passelif 0: passelse: passprint 'while_stmt' # 'while' test ':' suite ['else' ':' suite]while 0: passwhile 0: passelse: passprint 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]for i in 1, 2, 3: passfor i, j, k in (): passelse: passclass 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 = 0for x in Squares(10): n = n+xif n != 285: raise TestFailed, 'for over growing sequence'print 'try_stmt'### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]### | 'try' ':' suite 'finally' ':' suite### except_clause: 'except' [expr [',' expr]]try: 1/0except ZeroDivisionError: passelse: passtry: 1/0except EOFError: passexcept TypeError, msg: passexcept RuntimeError, msg: passexcept: passelse: passtry: 1/0except (EOFError, TypeError, ZeroDivisionError): passtry: 1/0except (EOFError, TypeError, ZeroDivisionError), msg: passtry: passfinally: passprint 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENTif 1: passif 1: passif 1: # # # pass pass # pass #print 'test'### and_test ('or' and_test)*### and_test: not_test ('and' not_test)*### not_test: 'not' not_test | comparisonif not 1: passif 1 and 1: passif 1 or 1: passif not not not 1: passif not 1 and 1 and 1: passif 1 and 1 or 1 and 1 and 1 or not 1 and 1: passprint 'comparison'### comparison: expr (comp_op expr)*### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'if 1: passx = (1 == 1)if 1 == 1: passif 1 != 1: passif 1 <> 1: passif 1 < 1: passif 1 > 1: passif 1 <= 1: passif 1 >= 1: passif 1 is 1: passif 1 is not 1: passif 1 in (): passif 1 not in (): passif 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: passprint 'binary mask ops'x = 1 & 1x = 1 ^ 1x = 1 | 1print 'shift ops'x = 1 << 1x = 1 >> 1x = 1 << 1 >> 1print 'additive ops'x = 1x = 1 + 1x = 1 - 1 - 1x = 1 - 1 + 1 - 1 + 1print 'multiplicative ops'x = 1 * 1x = 1 / 1x = 1 % 1x = 1 / 1 * 1 % 1print 'unary ops'x = +1x = -1x = ~1x = ~1 ^ 1 & 1 | 1 & 1 ^ -1x = -1*1/1 + 1*1 - ---1*1print 'selectors'### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME### subscript: expr | [expr] ':' [expr]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)import sys, timec = 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 = xx = 'x'x = 123### exprlist: expr (',' expr)* [',']### testlist: test (',' test)* [',']# These have been exercised enough aboveprint 'classdef' # 'class' NAME ['(' testlist ')'] ':' suiteclass B: passclass C1(B): passclass C2(B): passclass D(C1, C2, B): passclass C: def meth1(self): pass def meth2(self, arg): pass def meth3(self, a1, a2): pass
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -