home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_grammar.py < prev    next >
Text File  |  1997-04-15  |  11KB  |  514 lines

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