Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 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, 'max positive int' |
| 27 | if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int' |
| 28 | # XXX -2147483648 |
| 29 | if 037777777777 != -1: raise TestFailed, 'oct -1' |
| 30 | if 0xffffffff != -1: raise TestFailed, 'hex -1' |
| 31 | for s in '2147483648', '040000000000', '0x100000000': |
| 32 | try: |
| 33 | x = eval(s) |
| 34 | except OverflowError: |
| 35 | continue |
| 36 | raise TestFailed, 'No OverflowError on huge integer literal ' + `s` |
| 37 | |
| 38 | print '1.1.2.2 Long integers' |
| 39 | x = 0L |
| 40 | x = 0l |
| 41 | x = 0xffffffffffffffffL |
| 42 | x = 0xffffffffffffffffl |
| 43 | x = 077777777777777777L |
| 44 | x = 077777777777777777l |
| 45 | x = 123456789012345678901234567890L |
| 46 | x = 123456789012345678901234567890l |
| 47 | |
| 48 | print '1.1.2.3 Floating point' |
| 49 | x = 3.14 |
| 50 | x = 314. |
| 51 | x = 0.314 |
| 52 | # XXX x = 000.314 |
| 53 | x = .314 |
| 54 | x = 3e14 |
| 55 | x = 3E14 |
| 56 | x = 3e-14 |
| 57 | x = 3e+14 |
| 58 | x = 3.e14 |
| 59 | x = .3e14 |
| 60 | x = 3.1e4 |
| 61 | |
| 62 | print '1.2 Grammar' |
| 63 | |
| 64 | print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 65 | # XXX can't test in a script -- this rule is only used when interactive |
| 66 | |
| 67 | print 'file_input' # (NEWLINE | stmt)* ENDMARKER |
| 68 | # Being tested as this very moment this very module |
| 69 | |
| 70 | print 'expr_input' # testlist NEWLINE |
| 71 | # XXX Hard to test -- used only in calls to input() |
| 72 | |
| 73 | print 'eval_input' # testlist ENDMARKER |
| 74 | x = eval('1, 0 or 1') |
| 75 | |
| 76 | print 'funcdef' |
| 77 | ### 'def' NAME parameters ':' suite |
| 78 | ### parameters: '(' [varargslist] ')' |
Guido van Rossum | 33693ea | 1992-04-03 16:33:00 +0000 | [diff] [blame^] | 79 | ### varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [','] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 80 | ### fpdef: NAME | '(' fplist ')' |
| 81 | ### fplist: fpdef (',' fpdef)* [','] |
| 82 | def f1(): pass |
| 83 | def f2(one_argument): pass |
| 84 | def f3(two, arguments): pass |
| 85 | def f4(two, (compound, (argument, list))): pass |
| 86 | def a1(one_arg,): pass |
| 87 | def a2(two, args,): pass |
| 88 | def v0(*rest): pass |
| 89 | def v1(a, *rest): pass |
| 90 | def v2(a, b, *rest): pass |
| 91 | def v3(a, (b, c), *rest): pass |
| 92 | |
| 93 | ### stmt: simple_stmt | compound_stmt |
| 94 | # Tested below |
| 95 | |
| 96 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 97 | print 'simple_stmt' |
| 98 | x = 1; pass; del x |
| 99 | |
| 100 | ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt |
| 101 | # Tested below |
| 102 | |
| 103 | print 'expr_stmt' # (exprlist '=')* exprlist |
| 104 | 1 |
| 105 | 1, 2, 3 |
| 106 | x = 1 |
| 107 | x = 1, 2, 3 |
| 108 | x = y = z = 1, 2, 3 |
| 109 | x, y, z = 1, 2, 3 |
| 110 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 111 | # NB these variables are deleted below |
| 112 | |
| 113 | print 'print_stmt' # 'print' (test ',')* [test] |
| 114 | print 1, 2, 3 |
| 115 | print 1, 2, 3, |
| 116 | print |
| 117 | print 0 or 1, 0 or 1, |
| 118 | print 0 or 1 |
| 119 | |
| 120 | print 'del_stmt' # 'del' exprlist |
| 121 | del abc |
| 122 | del x, y, (z, xyz) |
| 123 | |
| 124 | print 'pass_stmt' # 'pass' |
| 125 | pass |
| 126 | |
| 127 | print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt |
| 128 | # Tested below |
| 129 | |
| 130 | print 'break_stmt' # 'break' |
| 131 | while 1: break |
| 132 | |
| 133 | print 'continue_stmt' # 'continue' |
| 134 | i = 1 |
| 135 | while i: i = 0; continue |
| 136 | |
| 137 | print 'return_stmt' # 'return' [testlist] |
| 138 | def g1(): return |
| 139 | def g2(): return 1 |
| 140 | g1() |
| 141 | x = g2() |
| 142 | |
| 143 | print 'raise_stmt' # 'raise' test [',' test] |
| 144 | try: raise RuntimeError, 'just testing' |
| 145 | except RuntimeError: pass |
| 146 | try: raise KeyboardInterrupt |
| 147 | except KeyboardInterrupt: pass |
| 148 | |
| 149 | print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*) |
| 150 | [1] |
| 151 | import sys |
| 152 | [2] |
| 153 | import time, math |
| 154 | [3] |
| 155 | from time import sleep |
| 156 | [4] |
| 157 | from sys import * |
| 158 | [5] |
| 159 | from math import sin, cos |
| 160 | [6] |
| 161 | |
| 162 | print 'global_stmt' # 'global' NAME (',' NAME)* |
| 163 | def f(): |
| 164 | global a |
| 165 | global a, b |
| 166 | global one, two, three, four, five, six, seven, eight, nine, ten |
| 167 | |
| 168 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 169 | # Tested below |
| 170 | |
| 171 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 172 | if 1: pass |
| 173 | if 1: pass |
| 174 | else: pass |
| 175 | if 0: pass |
| 176 | elif 0: pass |
| 177 | if 0: pass |
| 178 | elif 0: pass |
| 179 | elif 0: pass |
| 180 | elif 0: pass |
| 181 | else: pass |
| 182 | |
| 183 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] |
| 184 | while 0: pass |
| 185 | while 0: pass |
| 186 | else: pass |
| 187 | |
| 188 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 189 | [1] |
| 190 | for i in 1, 2, 3: pass |
| 191 | [2] |
| 192 | for i, j, k in (): pass |
| 193 | else: pass |
| 194 | [3] |
| 195 | |
Guido van Rossum | 627efd9 | 1992-03-31 18:54:11 +0000 | [diff] [blame] | 196 | print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 197 | ### except_clause: 'except' [expr [',' expr]] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 198 | try: 1/0 |
| 199 | except ZeroDivisionError: pass |
| 200 | try: 1/0 |
| 201 | except EOFError: pass |
| 202 | except TypeError, msg: pass |
| 203 | except RuntimeError, msg: pass |
| 204 | except: pass |
| 205 | try: pass |
| 206 | finally: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 207 | |
| 208 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 209 | if 1: pass |
| 210 | if 1: |
| 211 | pass |
| 212 | if 1: |
| 213 | # |
| 214 | # |
| 215 | # |
| 216 | pass |
| 217 | pass |
| 218 | # |
| 219 | pass |
| 220 | # |
| 221 | |
| 222 | print 'test' |
| 223 | ### and_test ('or' and_test)* |
| 224 | ### and_test: not_test ('and' not_test)* |
| 225 | ### not_test: 'not' not_test | comparison |
| 226 | if not 1: pass |
| 227 | if 1 and 1: pass |
| 228 | if 1 or 1: pass |
| 229 | if not not not 1: pass |
| 230 | if not 1 and 1 and 1: pass |
| 231 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
| 232 | |
| 233 | print 'comparison' |
| 234 | ### comparison: expr (comp_op expr)* |
| 235 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 236 | if 1: pass |
| 237 | x = (1 == 1) |
| 238 | if 1 == 1: pass |
| 239 | if 1 != 1: pass |
| 240 | if 1 <> 1: pass |
| 241 | if 1 < 1: pass |
| 242 | if 1 > 1: pass |
| 243 | if 1 <= 1: pass |
| 244 | if 1 >= 1: pass |
| 245 | if 1 is 1: pass |
| 246 | if 1 is not 1: pass |
| 247 | if 1 in (): pass |
| 248 | if 1 not in (): pass |
| 249 | if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass |
| 250 | |
| 251 | print 'binary mask ops' |
| 252 | x = 1 & 1 |
| 253 | x = 1 ^ 1 |
| 254 | x = 1 | 1 |
| 255 | |
| 256 | print 'shift ops' |
| 257 | x = 1 << 1 |
| 258 | x = 1 >> 1 |
| 259 | x = 1 << 1 >> 1 |
| 260 | |
| 261 | print 'additive ops' |
| 262 | x = 1 |
| 263 | x = 1 + 1 |
| 264 | x = 1 - 1 - 1 |
| 265 | x = 1 - 1 + 1 - 1 + 1 |
| 266 | |
| 267 | print 'multiplicative ops' |
| 268 | x = 1 * 1 |
| 269 | x = 1 / 1 |
| 270 | x = 1 % 1 |
| 271 | x = 1 / 1 * 1 % 1 |
| 272 | |
| 273 | print 'unary ops' |
| 274 | x = +1 |
| 275 | x = -1 |
| 276 | x = ~1 |
| 277 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 278 | x = -1*1/1 + 1*1 - ---1*1 |
| 279 | |
| 280 | print 'selectors' |
| 281 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 282 | ### subscript: expr | [expr] ':' [expr] |
| 283 | import sys, time |
| 284 | c = sys.path[0] |
| 285 | x = time.time() |
| 286 | x = sys.modules['time'].time() |
| 287 | a = '01234' |
| 288 | c = a[0] |
| 289 | c = a[-1] |
| 290 | s = a[0:5] |
| 291 | s = a[:5] |
| 292 | s = a[0:] |
| 293 | s = a[:] |
| 294 | s = a[-5:] |
| 295 | s = a[:-1] |
| 296 | s = a[-4:-3] |
| 297 | |
| 298 | print 'atoms' |
| 299 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
| 300 | ### dictmaker: test ':' test (',' test ':' test)* [','] |
| 301 | |
| 302 | x = (1) |
| 303 | x = (1 or 2 or 3) |
| 304 | x = (1 or 2 or 3, 2, 3) |
| 305 | |
| 306 | x = [] |
| 307 | x = [1] |
| 308 | x = [1 or 2 or 3] |
| 309 | x = [1 or 2 or 3, 2, 3] |
| 310 | x = [] |
| 311 | |
| 312 | x = {} |
| 313 | x = {'one': 1} |
| 314 | x = {'one': 1,} |
| 315 | x = {'one' or 'two': 1 or 2} |
| 316 | x = {'one': 1, 'two': 2} |
| 317 | x = {'one': 1, 'two': 2,} |
| 318 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
| 319 | |
| 320 | x = `x` |
| 321 | x = `1 or 2 or 3` |
| 322 | x = x |
| 323 | x = 'x' |
| 324 | x = 123 |
| 325 | |
| 326 | ### exprlist: expr (',' expr)* [','] |
| 327 | ### testlist: test (',' test)* [','] |
| 328 | # These have been exercised enough above |
| 329 | |
| 330 | print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite |
| 331 | ### baselist: atom arguments (',' atom arguments)* |
| 332 | ### arguments: '(' [testlist] ')' |
| 333 | class B: pass |
| 334 | class C1(B): pass |
| 335 | class C2(B): pass |
| 336 | class D(C1, C2, B): pass |
| 337 | class C: |
| 338 | def meth1(self): pass |
| 339 | def meth2(self, arg): pass |
| 340 | def meth3(self, a1, a2): pass |