Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # Module 'testall' |
| 2 | # |
| 3 | # Python test set, should exercise: |
| 4 | # - all lexical and grammatical constructs |
| 5 | # - all opcodes from "opcode.h" |
| 6 | # - all operations on all object types |
| 7 | # - all builtin functions |
| 8 | # Ideally also: |
| 9 | # - all possible exception situations (Thank God we've got 'try') |
| 10 | # - all boundary cases |
| 11 | |
| 12 | |
| 13 | TestFailed = 'testall -- test failed' # Exception |
| 14 | |
| 15 | |
| 16 | ######################################################### |
| 17 | # Part 1. Test all lexical and grammatical constructs. |
| 18 | # This just tests whether the parser accepts them all. |
| 19 | ######################################################### |
| 20 | |
| 21 | print '1. Parser' |
| 22 | |
| 23 | print '1.1 Tokens' |
| 24 | |
| 25 | print '1.1.1 Backslashes' |
| 26 | |
| 27 | # Backslash means line continuation: |
| 28 | x = 1 \ |
| 29 | + 1 |
| 30 | if x <> 2: raise TestFailed, 'backslash for line continuation' |
| 31 | |
| 32 | # Backslash does not means continuation in comments :\ |
| 33 | x = 0 |
| 34 | if x <> 0: raise TestFailed, 'backslash ending comment' |
| 35 | |
| 36 | print '1.1.2 Number formats' |
| 37 | |
| 38 | if 0xff <> 255: raise TestFailed, 'hex number' |
| 39 | if 0377 <> 255: raise TestFailed, 'octal number' |
| 40 | x = 3.14 |
| 41 | x = 0.314 |
| 42 | x = 3e14 |
| 43 | x = 3E14 |
| 44 | x = 3e-14 |
| 45 | |
| 46 | print '1.2 Grammar' |
| 47 | |
| 48 | print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 49 | # XXX can't test in a script -- this rule is only used when interactive |
| 50 | |
| 51 | print 'file_input' # (NEWLINE | stmt)* ENDMARKER |
| 52 | # Being tested as this very moment this very module |
| 53 | |
| 54 | print 'expr_input' # testlist NEWLINE |
| 55 | # XXX Hard to test -- used only in calls to input() |
| 56 | |
| 57 | print 'eval_input' # testlist ENDMARKER |
| 58 | x = eval('1, 0 or 1') |
| 59 | |
| 60 | print 'funcdef' # 'def' NAME parameters ':' suite |
| 61 | ### parameters: '(' [fplist] ')' |
| 62 | ### fplist: fpdef (',' fpdef)* |
| 63 | ### fpdef: NAME | '(' fplist ')' |
| 64 | def f1(): pass |
| 65 | def f2(one_argument): pass |
| 66 | def f3(two, arguments): pass |
| 67 | def f4(two, (compound, (arguments))): pass |
| 68 | |
| 69 | ### stmt: simple_stmt | compound_stmt |
| 70 | ### simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt |
| 71 | # Tested below |
| 72 | |
| 73 | print 'expr_stmt' # (exprlist '=')* exprlist NEWLINE |
| 74 | 1 |
| 75 | 1, 2, 3 |
| 76 | x = 1 |
| 77 | x = 1, 2, 3 |
| 78 | x = y = z = 1, 2, 3 |
| 79 | x, y, z = 1, 2, 3 |
| 80 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
| 81 | # NB these variables are deleted below |
| 82 | |
| 83 | print 'print_stmt' # 'print' (test ',')* [test] NEWLINE |
| 84 | print 1, 2, 3 |
| 85 | print 1, 2, 3, |
| 86 | print |
| 87 | print 0 or 1, 0 or 1, |
| 88 | print 0 or 1 |
| 89 | |
| 90 | print 'del_stmt' # 'del' exprlist NEWLINE |
| 91 | del abc |
| 92 | del x, y, (z, xyz) |
| 93 | |
| 94 | print 'pass_stmt' # 'pass' NEWLINE |
| 95 | pass |
| 96 | |
| 97 | print 'flow_stmt' # break_stmt | return_stmt | raise_stmt |
| 98 | # Tested below |
| 99 | |
| 100 | print 'break_stmt' # 'break' NEWLINE |
| 101 | while 1: break |
| 102 | |
| 103 | print 'return_stmt' # 'return' [testlist] NEWLINE |
| 104 | def g1(): return |
| 105 | def g2(): return 1 |
| 106 | g1() |
| 107 | x = g2() |
| 108 | |
| 109 | print 'raise_stmt' # 'raise' expr [',' expr] NEWLINE |
| 110 | try: raise RuntimeError, 'just testing' |
| 111 | except RuntimeError: pass |
| 112 | try: raise KeyboardInterrupt |
| 113 | except KeyboardInterrupt: pass |
| 114 | |
| 115 | print 'import_stmt' # 'import' NAME (',' NAME)* NEWLINE | 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE |
| 116 | [1] |
| 117 | import sys |
| 118 | [2] |
| 119 | import time, math |
| 120 | [3] |
| 121 | from time import sleep |
| 122 | [4] |
| 123 | from math import * |
| 124 | [5] |
| 125 | from sys import modules, ps1, ps2 |
| 126 | [6] |
| 127 | |
| 128 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 129 | # Tested below |
| 130 | |
| 131 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 132 | if 1: pass |
| 133 | if 1: pass |
| 134 | else: pass |
| 135 | if 0: pass |
| 136 | elif 0: pass |
| 137 | if 0: pass |
| 138 | elif 0: pass |
| 139 | elif 0: pass |
| 140 | elif 0: pass |
| 141 | else: pass |
| 142 | |
| 143 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] |
| 144 | while 0: pass |
| 145 | while 0: pass |
| 146 | else: pass |
| 147 | |
| 148 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 149 | [1] |
| 150 | for i in 1, 2, 3: pass |
| 151 | [2] |
| 152 | for i, j, k in (): pass |
| 153 | else: pass |
| 154 | [3] |
| 155 | |
| 156 | print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] |
| 157 | ### except_clause: 'except' [expr [',' expr]] |
| 158 | try: pass |
| 159 | try: 1/0 |
| 160 | except RuntimeError: pass |
| 161 | try: 1/0 |
| 162 | except EOFError: pass |
| 163 | except TypeError, msg: pass |
| 164 | except RuntimeError, msg: pass |
| 165 | except: pass |
| 166 | try: pass |
| 167 | finally: pass |
| 168 | try: 1/0 |
| 169 | except: pass |
| 170 | finally: pass |
| 171 | |
| 172 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 173 | if 1: pass |
| 174 | if 1: |
| 175 | pass |
| 176 | if 1: |
| 177 | # |
| 178 | # |
| 179 | # |
| 180 | pass |
| 181 | pass |
| 182 | # |
| 183 | pass |
| 184 | # |
| 185 | |
| 186 | print 'test' # and_test ('or' and_test)* |
| 187 | ### and_test: not_test ('and' not_test)* |
| 188 | ### not_test: 'not' not_test | comparison |
| 189 | ### comparison: expr (comp_op expr)* |
| 190 | ### comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not' |
| 191 | if 1: pass |
| 192 | if 1 = 1: pass |
| 193 | if 1 < 1 > 1 = 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass |
| 194 | if not 1 = 1 = 1: pass |
| 195 | if not 1 = 1 and 1 and 1: pass |
| 196 | if 1 and 1 or 1 and 1 and 1 or not 1 = 1 = 1 and 1: pass |
| 197 | |
| 198 | print 'expr' # term (('+'|'-') term)* |
| 199 | x = 1 |
| 200 | x = 1 + 1 |
| 201 | x = 1 - 1 - 1 |
| 202 | x = 1 - 1 + 1 - 1 + 1 |
| 203 | |
| 204 | print 'term' # factor (('*'|'/'|'%') factor)* |
| 205 | x = 1 * 1 |
| 206 | x = 1 / 1 |
| 207 | x = 1 % 1 |
| 208 | x = 1 / 1 * 1 % 1 |
| 209 | |
| 210 | print 'factor' # ('+'|'-') factor | atom trailer* |
| 211 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 212 | ### subscript: expr | [expr] ':' [expr] |
| 213 | x = +1 |
| 214 | x = -1 |
| 215 | x = 1 |
| 216 | c = sys.ps1[0] |
| 217 | x = time.time() |
| 218 | x = sys.modules['time'].time() |
| 219 | a = '01234' |
| 220 | c = a[0] |
| 221 | c = a[0:5] |
| 222 | c = a[:5] |
| 223 | c = a[0:] |
| 224 | c = a[:] |
| 225 | c = a[-5:] |
| 226 | c = a[:-1] |
| 227 | c = a[-4:-3] |
| 228 | |
| 229 | print 'atom' # '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING |
| 230 | x = (1) |
| 231 | x = (1 or 2 or 3) |
| 232 | x = (1 or 2 or 3, 2, 3) |
| 233 | x = [] |
| 234 | x = [1] |
| 235 | x = [1 or 2 or 3] |
| 236 | x = [1 or 2 or 3, 2, 3] |
| 237 | x = [] |
| 238 | x = {} |
| 239 | x = `x` |
| 240 | x = x |
| 241 | x = 'x' |
| 242 | x = 123 |
| 243 | |
| 244 | ### exprlist: expr (',' expr)* [','] |
| 245 | ### testlist: test (',' test)* [','] |
| 246 | # These have been exercised enough above |
| 247 | |
| 248 | print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite |
| 249 | ### baselist: atom arguments (',' atom arguments)* |
| 250 | ### arguments: '(' [testlist] ')' |
| 251 | class B(): pass |
| 252 | class C1() = B(): pass |
| 253 | class C2() = B(): pass |
| 254 | class D() = C1(), C2(), B(): pass |
| 255 | class C(): |
| 256 | def meth1(self): pass |
| 257 | def meth2(self, arg): pass |
| 258 | def meth3(self, (a1, a2)): pass |
| 259 | |
| 260 | |
| 261 | ######################################################### |
| 262 | # Part 2. Test all opcodes from "opcode.h" |
| 263 | ######################################################### |
| 264 | |
| 265 | print '2. Opcodes' |
| 266 | print 'XXX Not yet fully implemented' |
| 267 | |
| 268 | print '2.1 try inside for loop' |
| 269 | n = 0 |
| 270 | for i in range(10): |
| 271 | n = n+i |
| 272 | try: 1/0 |
| 273 | except NameError: pass |
| 274 | except RuntimeError: pass |
| 275 | except TypeError: pass |
| 276 | finally: pass |
| 277 | try: pass |
| 278 | except: pass |
| 279 | try: pass |
| 280 | finally: pass |
| 281 | n = n+i |
| 282 | if n <> 90: |
| 283 | raise TestFailed, 'try inside for' |
| 284 | |
| 285 | |
| 286 | ######################################################### |
| 287 | # Part 3. Test all operations on all object types |
| 288 | ######################################################### |
| 289 | |
| 290 | print '3. Object types' |
| 291 | print 'XXX Not yet implemented' |
| 292 | |
| 293 | |
| 294 | ######################################################### |
| 295 | # Part 4. Test all built-in functions |
| 296 | ######################################################### |
| 297 | |
| 298 | print '4. Built-in functions' |
| 299 | |
| 300 | print 'abs' |
| 301 | if abs(0) <> 0: raise TestFailed, 'abs(0)' |
| 302 | if abs(1234) <> 1234: raise TestFailed, 'abs(1234)' |
| 303 | if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)' |
| 304 | if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)' |
| 305 | if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)' |
| 306 | if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)' |
| 307 | |
| 308 | print 'dir' |
| 309 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 310 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 311 | |
| 312 | print 'divmod' |
| 313 | if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 314 | if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 315 | if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 316 | if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)' |
| 317 | |
| 318 | print 'eval' |
| 319 | if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')' |
| 320 | |
| 321 | print 'exec' |
| 322 | exec('z=1+1\n') |
| 323 | if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)' |
| 324 | |
| 325 | print 'float' |
| 326 | if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' |
| 327 | if float(314) <> 314.0: raise TestFailed, 'float(314)' |
| 328 | |
| 329 | print 'input' |
| 330 | # Can't test in a script |
| 331 | |
| 332 | print 'int' |
| 333 | if int(100) <> 100: raise TestFailed, 'int(100)' |
| 334 | if int(3.14) <> 3: raise TestFailed, 'int(3.14)' |
| 335 | |
| 336 | print 'len' |
| 337 | if len('123') <> 3: raise TestFailed, 'len(\'123\')' |
| 338 | if len(()) <> 0: raise TestFailed, 'len(())' |
| 339 | if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 340 | if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 341 | if len({}) <> 0: raise TestFailed, 'len({})' |
| 342 | |
| 343 | print 'min' |
| 344 | if min('123123') <> '1': raise TestFailed, 'min(\'123123\')' |
| 345 | if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)' |
| 346 | if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 347 | if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
| 348 | |
| 349 | print 'max' |
| 350 | if max('123123') <> '3': raise TestFailed, 'max(\'123123\')' |
| 351 | if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)' |
| 352 | if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 353 | if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
| 354 | |
| 355 | print 'open' |
| 356 | print 'NB! This test creates a file named "@test" in the current directory.' |
| 357 | fp = open('@test', 'w') |
| 358 | fp.write('The quick brown fox jumps over the lazy dog') |
| 359 | fp.write('.\n') |
| 360 | fp.write('Dear John\n') |
| 361 | fp.write('XXX'*100) |
| 362 | fp.write('YYY'*100) |
| 363 | fp.close() |
| 364 | del fp |
| 365 | fp = open('@test', 'r') |
| 366 | if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n': |
| 367 | raise TestFailed, 'readline()' |
| 368 | if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short' |
| 369 | if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)' |
| 370 | if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)' |
| 371 | if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate' |
| 372 | fp.close() |
| 373 | del fp |
| 374 | |
| 375 | print 'range' |
| 376 | if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)' |
| 377 | if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)' |
| 378 | if range(0) <> []: raise TestFailed, 'range(0)' |
| 379 | if range(-3) <> []: raise TestFailed, 'range(-3)' |
| 380 | if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)' |
| 381 | if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)' |
| 382 | |
| 383 | print 'raw_input' |
| 384 | savestdin = sys.stdin |
| 385 | try: |
| 386 | sys.stdin = open('@test', 'r') |
| 387 | if raw_input() <> 'The quick brown fox jumps over the lazy dog.': |
| 388 | raise TestFailed, 'raw_input()' |
| 389 | if raw_input('testing\n') <> 'Dear John': |
| 390 | raise TestFailed, 'raw_input(\'testing\\n\')' |
| 391 | finally: |
| 392 | sys.stdin = savestdin |
| 393 | |
| 394 | print 'reload' |
| 395 | import string |
| 396 | reload(string) |
| 397 | |
| 398 | print 'type' |
| 399 | if type('') <> type('123') or type('') = type(()): |
| 400 | raise TestFailed, 'type()' |
| 401 | |
| 402 | |
| 403 | print 'Passed all tests.' |
| 404 | |
| 405 | try: |
| 406 | import mac |
| 407 | unlink = mac.unlink |
| 408 | except NameError: |
| 409 | try: |
| 410 | import posix |
| 411 | unlink = posix.unlink |
| 412 | except NameError: |
| 413 | pass |
| 414 | |
| 415 | unlink('@test') |
| 416 | print 'Unlinked @test' |