blob: a385be1decb7d7439cc39445b69ac6658357f3f3 [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
179except RuntimeError: pass
Guido van Rossum76105991991-12-16 13:10:58 +0000180except ZeroDivisionError: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000181try: 1/0
182except EOFError: pass
183except TypeError, msg: pass
184except RuntimeError, msg: pass
Guido van Rossum76105991991-12-16 13:10:58 +0000185except ZeroDivisionError, msg: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000186except: pass
187try: pass
188finally: pass
189try: 1/0
190except: pass
191finally: pass
192
193print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
194if 1: pass
195if 1:
196 pass
197if 1:
198 #
199 #
200 #
201 pass
202 pass
203 #
204 pass
205 #
206
207print 'test' # and_test ('or' and_test)*
208### and_test: not_test ('and' not_test)*
209### not_test: 'not' not_test | comparison
210### comparison: expr (comp_op expr)*
211### comp_op: '<'|'>'|'='|'>' '='|'<' '='|'<' '>'|'in'|'not' 'in'|'is'|'is' 'not'
212if 1: pass
213if 1 = 1: pass
214if 1 < 1 > 1 = 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
215if not 1 = 1 = 1: pass
216if not 1 = 1 and 1 and 1: pass
217if 1 and 1 or 1 and 1 and 1 or not 1 = 1 = 1 and 1: pass
218
219print 'expr' # term (('+'|'-') term)*
220x = 1
221x = 1 + 1
222x = 1 - 1 - 1
223x = 1 - 1 + 1 - 1 + 1
224
225print 'term' # factor (('*'|'/'|'%') factor)*
226x = 1 * 1
227x = 1 / 1
228x = 1 % 1
229x = 1 / 1 * 1 % 1
230
231print 'factor' # ('+'|'-') factor | atom trailer*
232### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
233### subscript: expr | [expr] ':' [expr]
234x = +1
235x = -1
236x = 1
Guido van Rossum6179fe61991-04-07 13:42:52 +0000237c = sys.path[0]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000238x = time.time()
239x = sys.modules['time'].time()
240a = '01234'
241c = a[0]
242c = a[0:5]
243c = a[:5]
244c = a[0:]
245c = a[:]
246c = a[-5:]
247c = a[:-1]
248c = a[-4:-3]
249
250print 'atom' # '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
251x = (1)
252x = (1 or 2 or 3)
253x = (1 or 2 or 3, 2, 3)
254x = []
255x = [1]
256x = [1 or 2 or 3]
257x = [1 or 2 or 3, 2, 3]
258x = []
259x = {}
260x = `x`
261x = x
262x = 'x'
263x = 123
264
265### exprlist: expr (',' expr)* [',']
266### testlist: test (',' test)* [',']
Guido van Rossuma2325361991-08-16 13:29:25 +0000267# These have been exercised enough above
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000268
269print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
270### baselist: atom arguments (',' atom arguments)*
271### arguments: '(' [testlist] ')'
272class B(): pass
273class C1() = B(): pass
274class C2() = B(): pass
275class D() = C1(), C2(), B(): pass
276class C():
277 def meth1(self): pass
278 def meth2(self, arg): pass
279 def meth3(self, (a1, a2)): pass
280
281
282#########################################################
283# Part 2. Test all opcodes from "opcode.h"
284#########################################################
285
286print '2. Opcodes'
287print 'XXX Not yet fully implemented'
288
289print '2.1 try inside for loop'
290n = 0
291for i in range(10):
292 n = n+i
293 try: 1/0
294 except NameError: pass
295 except RuntimeError: pass
Guido van Rossum76105991991-12-16 13:10:58 +0000296 except ZeroDivisionError: pass
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000297 except TypeError: pass
298 finally: pass
299 try: pass
300 except: pass
301 try: pass
302 finally: pass
303 n = n+i
304if n <> 90:
305 raise TestFailed, 'try inside for'
306
307
308#########################################################
309# Part 3. Test all operations on all object types
310#########################################################
311
312print '3. Object types'
313print 'XXX Not yet implemented'
314
315
316#########################################################
317# Part 4. Test all built-in functions
318#########################################################
319
320print '4. Built-in functions'
321
322print 'abs'
323if abs(0) <> 0: raise TestFailed, 'abs(0)'
324if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
325if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000326#
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000327if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
328if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
329if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000330#
331if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
332if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
333if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
334
335print 'chr'
336if chr(32) <> ' ': raise TestFailed, 'chr(32)'
337if chr(65) <> 'A': raise TestFailed, 'chr(65)'
338if chr(97) <> 'a': raise TestFailed, 'chr(97)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000339
340print 'dir'
Guido van Rossuma2325361991-08-16 13:29:25 +0000341x = 1
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000342if 'x' not in dir(): raise TestFailed, 'dir()'
Guido van Rossuma2325361991-08-16 13:29:25 +0000343import sys
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000344if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
345
346print 'divmod'
347if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
348if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
349if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
350if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000351#
352if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
353if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
354if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
355if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
356#
357if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
358if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
359if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
360if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000361
362print 'eval'
363if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
364
365print 'exec'
Guido van Rossuma2325361991-08-16 13:29:25 +0000366z = 0
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000367exec('z=1+1\n')
368if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
369
370print 'float'
371if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
372if float(314) <> 314.0: raise TestFailed, 'float(314)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000373if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000374
375print 'input'
376# Can't test in a script
377
378print 'int'
Guido van Rossuma2325361991-08-16 13:29:25 +0000379if int(314) <> 314: raise TestFailed, 'int(314)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000380if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000381if int(314L) <> 314: raise TestFailed, 'int(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000382
383print 'len'
384if len('123') <> 3: raise TestFailed, 'len(\'123\')'
385if len(()) <> 0: raise TestFailed, 'len(())'
386if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
387if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
388if len({}) <> 0: raise TestFailed, 'len({})'
389
Guido van Rossuma2325361991-08-16 13:29:25 +0000390print 'long'
391if long(314) <> 314L: raise TestFailed, 'long(314)'
392if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
393if long(314L) <> 314L: raise TestFailed, 'long(314L)'
394
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000395print 'min'
396if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
397if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
398if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
399if 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 +0000400#
401if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
402if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
403if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000404
405print 'max'
406if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
407if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
408if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
409if 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 +0000410#
411if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
412if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
413if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000414
415print 'open'
416print 'NB! This test creates a file named "@test" in the current directory.'
417fp = open('@test', 'w')
418fp.write('The quick brown fox jumps over the lazy dog')
419fp.write('.\n')
420fp.write('Dear John\n')
421fp.write('XXX'*100)
422fp.write('YYY'*100)
423fp.close()
424del fp
425fp = open('@test', 'r')
426if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
427 raise TestFailed, 'readline()'
428if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
429if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
430if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
431if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
432fp.close()
433del fp
434
Guido van Rossuma2325361991-08-16 13:29:25 +0000435print 'ord'
436if ord(' ') <> 32: raise TestFailed, 'ord(\' \')'
437if ord('A') <> 65: raise TestFailed, 'ord(\'A\')'
438if ord('a') <> 97: raise TestFailed, 'ord(\'a\')'
439
440print 'pow'
441if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)'
442if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)'
443if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)'
444if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)'
445#
446if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)'
447if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)'
448if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)'
449if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)'
450#
451if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)'
452if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)'
453if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)'
454if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)'
455
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000456print 'range'
457if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
458if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
459if range(0) <> []: raise TestFailed, 'range(0)'
460if range(-3) <> []: raise TestFailed, 'range(-3)'
461if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
462if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
463
464print 'raw_input'
465savestdin = sys.stdin
466try:
467 sys.stdin = open('@test', 'r')
468 if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
469 raise TestFailed, 'raw_input()'
470 if raw_input('testing\n') <> 'Dear John':
471 raise TestFailed, 'raw_input(\'testing\\n\')'
472finally:
473 sys.stdin = savestdin
474
475print 'reload'
476import string
477reload(string)
478
479print 'type'
480if type('') <> type('123') or type('') = type(()):
481 raise TestFailed, 'type()'
482
483
484print 'Passed all tests.'
485
486try:
487 import mac
488 unlink = mac.unlink
489except NameError:
490 try:
491 import posix
492 unlink = posix.unlink
493 except NameError:
494 pass
495
496unlink('@test')
497print 'Unlinked @test'