blob: 20d9ec821ab3dad94c96f13b45629977f4e6f720 [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
180try: 1/0
181except EOFError: pass
182except TypeError, msg: pass
183except RuntimeError, msg: pass
184except: pass
185try: pass
186finally: pass
187try: 1/0
188except: pass
189finally: pass
190
191print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
192if 1: pass
193if 1:
194 pass
195if 1:
196 #
197 #
198 #
199 pass
200 pass
201 #
202 pass
203 #
204
205print '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'
210if 1: pass
211if 1 = 1: pass
212if 1 < 1 > 1 = 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
213if not 1 = 1 = 1: pass
214if not 1 = 1 and 1 and 1: pass
215if 1 and 1 or 1 and 1 and 1 or not 1 = 1 = 1 and 1: pass
216
217print 'expr' # term (('+'|'-') term)*
218x = 1
219x = 1 + 1
220x = 1 - 1 - 1
221x = 1 - 1 + 1 - 1 + 1
222
223print 'term' # factor (('*'|'/'|'%') factor)*
224x = 1 * 1
225x = 1 / 1
226x = 1 % 1
227x = 1 / 1 * 1 % 1
228
229print 'factor' # ('+'|'-') factor | atom trailer*
230### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
231### subscript: expr | [expr] ':' [expr]
232x = +1
233x = -1
234x = 1
Guido van Rossum6179fe61991-04-07 13:42:52 +0000235c = sys.path[0]
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000236x = time.time()
237x = sys.modules['time'].time()
238a = '01234'
239c = a[0]
240c = a[0:5]
241c = a[:5]
242c = a[0:]
243c = a[:]
244c = a[-5:]
245c = a[:-1]
246c = a[-4:-3]
247
248print 'atom' # '(' [testlist] ')' | '[' [testlist] ']' | '{' '}' | '`' testlist '`' | NAME | NUMBER | STRING
249x = (1)
250x = (1 or 2 or 3)
251x = (1 or 2 or 3, 2, 3)
252x = []
253x = [1]
254x = [1 or 2 or 3]
255x = [1 or 2 or 3, 2, 3]
256x = []
257x = {}
258x = `x`
259x = x
260x = 'x'
261x = 123
262
263### exprlist: expr (',' expr)* [',']
264### testlist: test (',' test)* [',']
Guido van Rossuma2325361991-08-16 13:29:25 +0000265# These have been exercised enough above
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000266
267print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
268### baselist: atom arguments (',' atom arguments)*
269### arguments: '(' [testlist] ')'
270class B(): pass
271class C1() = B(): pass
272class C2() = B(): pass
273class D() = C1(), C2(), B(): pass
274class 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
284print '2. Opcodes'
285print 'XXX Not yet fully implemented'
286
287print '2.1 try inside for loop'
288n = 0
289for 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
301if n <> 90:
302 raise TestFailed, 'try inside for'
303
304
305#########################################################
306# Part 3. Test all operations on all object types
307#########################################################
308
309print '3. Object types'
310print 'XXX Not yet implemented'
311
312
313#########################################################
314# Part 4. Test all built-in functions
315#########################################################
316
317print '4. Built-in functions'
318
319print 'abs'
320if abs(0) <> 0: raise TestFailed, 'abs(0)'
321if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
322if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000323#
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000324if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
325if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
326if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000327#
328if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
329if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
330if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
331
332print 'chr'
333if chr(32) <> ' ': raise TestFailed, 'chr(32)'
334if chr(65) <> 'A': raise TestFailed, 'chr(65)'
335if chr(97) <> 'a': raise TestFailed, 'chr(97)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000336
337print 'dir'
Guido van Rossuma2325361991-08-16 13:29:25 +0000338x = 1
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000339if 'x' not in dir(): raise TestFailed, 'dir()'
Guido van Rossuma2325361991-08-16 13:29:25 +0000340import sys
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000341if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
342
343print 'divmod'
344if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
345if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
346if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
347if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000348#
349if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
350if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
351if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
352if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
353#
354if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
355if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
356if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
357if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000358
359print 'eval'
360if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
361
362print 'exec'
Guido van Rossuma2325361991-08-16 13:29:25 +0000363z = 0
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000364exec('z=1+1\n')
365if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)'
366
367print 'float'
368if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
369if float(314) <> 314.0: raise TestFailed, 'float(314)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000370if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000371
372print 'input'
373# Can't test in a script
374
375print 'int'
Guido van Rossuma2325361991-08-16 13:29:25 +0000376if int(314) <> 314: raise TestFailed, 'int(314)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000377if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
Guido van Rossuma2325361991-08-16 13:29:25 +0000378if int(314L) <> 314: raise TestFailed, 'int(314L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000379
380print 'len'
381if len('123') <> 3: raise TestFailed, 'len(\'123\')'
382if len(()) <> 0: raise TestFailed, 'len(())'
383if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
384if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
385if len({}) <> 0: raise TestFailed, 'len({})'
386
Guido van Rossuma2325361991-08-16 13:29:25 +0000387print 'long'
388if long(314) <> 314L: raise TestFailed, 'long(314)'
389if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
390if long(314L) <> 314L: raise TestFailed, 'long(314L)'
391
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000392print 'min'
393if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
394if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
395if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
396if 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 +0000397#
398if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
399if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
400if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000401
402print 'max'
403if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
404if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
405if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
406if 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 +0000407#
408if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
409if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
410if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000411
412print 'open'
413print 'NB! This test creates a file named "@test" in the current directory.'
414fp = open('@test', 'w')
415fp.write('The quick brown fox jumps over the lazy dog')
416fp.write('.\n')
417fp.write('Dear John\n')
418fp.write('XXX'*100)
419fp.write('YYY'*100)
420fp.close()
421del fp
422fp = open('@test', 'r')
423if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
424 raise TestFailed, 'readline()'
425if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
426if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
427if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
428if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
429fp.close()
430del fp
431
Guido van Rossuma2325361991-08-16 13:29:25 +0000432print 'ord'
433if ord(' ') <> 32: raise TestFailed, 'ord(\' \')'
434if ord('A') <> 65: raise TestFailed, 'ord(\'A\')'
435if ord('a') <> 97: raise TestFailed, 'ord(\'a\')'
436
437print 'pow'
438if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)'
439if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)'
440if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)'
441if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)'
442#
443if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)'
444if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)'
445if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)'
446if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)'
447#
448if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)'
449if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)'
450if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)'
451if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)'
452
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000453print 'range'
454if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
455if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
456if range(0) <> []: raise TestFailed, 'range(0)'
457if range(-3) <> []: raise TestFailed, 'range(-3)'
458if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
459if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
460
461print 'raw_input'
462savestdin = sys.stdin
463try:
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\')'
469finally:
470 sys.stdin = savestdin
471
472print 'reload'
473import string
474reload(string)
475
476print 'type'
477if type('') <> type('123') or type('') = type(()):
478 raise TestFailed, 'type()'
479
480
481print 'Passed all tests.'
482
483try:
484 import mac
485 unlink = mac.unlink
486except NameError:
487 try:
488 import posix
489 unlink = posix.unlink
490 except NameError:
491 pass
492
493unlink('@test')
494print 'Unlinked @test'