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