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