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