From 85f1820ee15faec7961056ace378f2d444419c1c Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 27 Nov 1992 22:53:50 +0000 Subject: [PATCH] Added some new tests and two new files for testing: test_types.py (testing operations on built-in types) and autotest.py (automatic regression testing). --- Lib/test/autotest.py | 68 ++++++++++++++++++ Lib/test/test_b1.py | 23 ++++++ Lib/test/test_b2.py | 16 +++++ Lib/test/test_grammar.py | 39 ++++++++-- Lib/test/test_types.py | 151 +++++++++++++++++++++++++++++++++++++++ Lib/test/testall.py | 4 ++ 6 files changed, 295 insertions(+), 6 deletions(-) create mode 100644 Lib/test/autotest.py create mode 100644 Lib/test/test_types.py diff --git a/Lib/test/autotest.py b/Lib/test/autotest.py new file mode 100644 index 00000000000..57e3674865b --- /dev/null +++ b/Lib/test/autotest.py @@ -0,0 +1,68 @@ +# Automatic Python regression test. +# +# Some essential parts of the Python interpreter are tested by the module +# 'testall'. (Despite its name, it doesn't test everything -- that would +# be a truly Herculean task!) When a test fails, 'testall' raises an +# exception. When all tests succeed, it produces quite a lot of output. +# +# For a normal regression test, this output is never looked at unless +# something goes wrong. Thus, it would be wise to suppress the output +# normally. This module does that, but it doesn't just throw the output +# from 'testall' away -- it compares it with the output from a previous +# run. If a difference is noticed it raises an exception; if all is well, +# it prints nothing except 'All tests OK.' at the very end. +# +# The output from a previous run is supposed to be in a file 'testall.out' +# somewhere on the search path for modules (sys.path, initialized from +# $PYTHONPATH plus some default places). +# +# Of course, if the normal output of the tests is changed because the +# tests have been changed (rather than a test producing the wrong output), +# 'autotest' will fail as well. In this case, run 'testall' manually +# and direct its output to 'testall.out'. +# +# The comparison uses (and demonstrates!) a rather new Python feature: +# program output that normally goes to stdout (by 'print' statements +# or by writing directly to sys.stdout) can be redirected to an +# arbitrary class instance as long as it has a 'write' method. + +import os +import sys + +# Function to find a file somewhere on sys.path +def findfile(filename): + for dirname in sys.path: + fullname = os.path.join(dirname, filename) + if os.path.exists(fullname): + return fullname + return filename # Will cause exception later + +# Exception raised when the test failed (not the same as in test_support) +TestFailed = 'autotest.TestFailed' + +# Class substituted for sys.stdout, to compare it with the given file +class Compare: + def init(self, filename): + self.fp = open(filename, 'r') + return self + def write(self, data): + expected = self.fp.read(len(data)) + if data <> expected: + raise TestFailed, \ + 'Writing: '+`data`+', expected: '+`expected` + def close(self): + self.fp.close() + +# The main program +def main(): + import sys + filename = findfile('testall.out') + real_stdout = sys.stdout + try: + sys.stdout = Compare().init(filename) + import testall + finally: + sys.stdout = real_stdout + print 'All tests OK.' + +main() diff --git a/Lib/test/test_b1.py b/Lib/test/test_b1.py index 1fafc4f59f4..b780c729edd 100644 --- a/Lib/test/test_b1.py +++ b/Lib/test/test_b1.py @@ -36,6 +36,16 @@ if chr(32) <> ' ': raise TestFailed, 'chr(32)' if chr(65) <> 'A': raise TestFailed, 'chr(65)' if chr(97) <> 'a': raise TestFailed, 'chr(97)' +print 'cmp' +if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)' +if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)' +if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)' + +print 'coerce' +if coerce(1, 1.1) <> (1.0, 1.1): raise TestFailed, 'coerce(1, 1.1)' +if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)' +if coerce(1L, 1.1) <> (1.0, 1.1): raise TestFailed, 'coerce(1L, 1.1)' + print 'dir' x = 1 if 'x' not in dir(): raise TestFailed, 'dir()' @@ -65,11 +75,24 @@ if divmod(-3.25, -1.0) <> (3.0, -0.25): raise TestFailed, 'divmod(-3.25, -1.0)' print 'eval' if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')' +if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')' print 'exec' z = 0 exec('z=1+1\n') if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)' +z = 0 +exec('z=1+1') +if z <> 2: raise TestFailed, 'exec(\'z=1+1\')' + +print 'execfile' +z = 0 +f = open(TESTFN, 'w') +f.write('z = z+1\n') +f.write('z = z*2\n') +f.close() +execfile(TESTFN) +unlink(TESTFN) print 'float' if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' diff --git a/Lib/test/test_b2.py b/Lib/test/test_b2.py index a4bea24136d..568ed9724a6 100644 --- a/Lib/test/test_b2.py +++ b/Lib/test/test_b2.py @@ -115,11 +115,27 @@ print 'reload' import string reload(string) +print 'repr' +if repr('') <> '\'\'': raise TestFailed, 'repr(\'\')' +if repr(0) <> '0': raise TestFailed, 'repr(0)' +if repr(0L) <> '0L': raise TestFailed, 'repr(0L)' +if repr(()) <> '()': raise TestFailed, 'repr(())' +if repr([]) <> '[]': raise TestFailed, 'repr([])' +if repr({}) <> '{}': raise TestFailed, 'repr({})' + print 'setattr' import sys setattr(sys, 'foobar', 1) if sys.foobar != 1: raise TestFailed, 'setattr(sys, \'foobar\', 1)' +print 'str' +if str('') <> '': raise TestFailed, 'str(\'\')' +if str(0) <> '0': raise TestFailed, 'str(0)' +if str(0L) <> '0L': raise TestFailed, 'str(0L)' +if str(()) <> '()': raise TestFailed, 'str(())' +if str([]) <> '[]': raise TestFailed, 'str([])' +if str({}) <> '{}': raise TestFailed, 'str({})' + print 'type' if type('') <> type('123') or type('') == type(()): raise TestFailed, 'type()' diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 6cfda0fb816..56fb14fc893 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -195,13 +195,19 @@ else: pass print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite ### except_clause: 'except' [expr [',' expr]] -try: 1/0 -except ZeroDivisionError: pass +try: + 1/0 +except ZeroDivisionError: + pass try: 1/0 except EOFError: pass except TypeError, msg: pass except RuntimeError, msg: pass except: pass +try: 1/0 +except (EOFError, TypeError, ZeroDivisionError): pass +try: 1/0 +except (EOFError, TypeError, ZeroDivisionError), msg: pass try: pass finally: pass @@ -246,7 +252,7 @@ if 1 is 1: pass if 1 is not 1: pass if 1 in (): pass if 1 not in (): pass -if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass +if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass print 'binary mask ops' x = 1 & 1 @@ -280,6 +286,29 @@ x = -1*1/1 + 1*1 - ---1*1 print 'selectors' ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME ### subscript: expr | [expr] ':' [expr] +f1() +f2(1) +f2(1,) +f3(1, 2) +f3(1, 2,) +f4(1, (2, (3, 4))) +v0() +v0(1) +v0(1,) +v0(1,2) +v0(1,2,3,4,5,6,7,8,9,0) +v1(1) +v1(1,) +v1(1,2) +v1(1,2,3) +v1(1,2,3,4,5,6,7,8,9,0) +v2(1,2) +v2(1,2,3) +v2(1,2,3,4) +v2(1,2,3,4,5,6,7,8,9,0) +v3(1,(2,3)) +v3(1,(2,3),4) +v3(1,(2,3),4,5,6,7,8,9,0) import sys, time c = sys.path[0] x = time.time() @@ -327,9 +356,7 @@ x = 123 ### testlist: test (',' test)* [','] # These have been exercised enough above -print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite -### baselist: atom arguments (',' atom arguments)* -### arguments: '(' [testlist] ')' +print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite class B: pass class C1(B): pass class C2(B): pass diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py new file mode 100644 index 00000000000..fb6e9345c58 --- /dev/null +++ b/Lib/test/test_types.py @@ -0,0 +1,151 @@ +# Python test set -- part 6, built-in types + +from test_support import * + +print '6. Built-in types' + +print '6.1 Truth value testing' +if None: raise TestFailed, 'None is true instead of false' +if 0: raise TestFailed, '0 is true instead of false' +if 0L: raise TestFailed, '0L is true instead of false' +if 0.0: raise TestFailed, '0.0 is true instead of false' +if '': raise TestFailed, '\'\' is true instead of false' +if (): raise TestFailed, '() is true instead of false' +if []: raise TestFailed, '[] is true instead of false' +if {}: raise TestFailed, '{} is true instead of false' +if not 1: raise TestFailed, '1 is false instead of true' +if not 1L: raise TestFailed, '1L is false instead of true' +if not 1.0: raise TestFailed, '1.0 is false instead of true' +if not 'x': raise TestFailed, '\'x\' is false instead of true' +if not (1, 1): raise TestFailed, '(1, 1) is false instead of true' +if not [1]: raise TestFailed, '[1] is false instead of true' +if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' +def f(): pass +class C(): pass +import sys +x = C() +if not f: raise TestFailed, 'f is false instead of true' +if not C: raise TestFailed, 'C is false instead of true' +if not sys: raise TestFailed, 'sys is false instead of true' +if not x: raise TestFailed, 'x is false instead of true' + +print '6.2 Boolean operations' +if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' +if 1 and 1: pass +else: raise TestFailed, '1 and 1 is false instead of false' +if not 1: raise TestFailed, 'not 1 is true instead of false' + +print '6.3 Comparisons' +if 0 < 1 <= 1 == 1 >= 1 > 0 <> 1: pass +else: raise TestFailed, 'int comparisons failed' +if 0L < 1L <= 1L == 1L >= 1L > 0L <> 1L: pass +else: raise TestFailed, 'long int comparisons failed' +if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 <> 1.0: pass +else: raise TestFailed, 'float comparisons failed' +if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass +else: raise TestFailed, 'string comparisons failed' +if 0 in [0] and 0 not in [1]: pass +else: raise TestFailed, 'membership test failed' +if None is None and [] is not []: pass +else: raise TestFailed, 'identity test failed' + +print '6.4 Numeric types (mostly conversions)' +if 0 <> 0L or 0 <> 0.0 or 0L <> 0.0: raise TestFailed, 'mixed comparisons' +if 1 <> 1L or 1 <> 1.0 or 1L <> 1.0: raise TestFailed, 'mixed comparisons' +if -1 <> -1L or -1 <> -1.0 or -1L <> -1.0: + raise TestFailed, 'int/long/float value not equal' +if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass +else: raise TestFailed, 'int() does not round properly' +if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass +else: raise TestFailed, 'long() does not round properly' +if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass +else: raise TestFailed, 'float() does not work properly' + +print '6.5 Sequence types' + +print '6.5.1 Strings' +if len('') <> 0: raise TestFailed, 'len(\'\')' +if len('a') <> 1: raise TestFailed, 'len(\'a\')' +if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')' +if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation' +if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3' +if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*' +if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string' +if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass +else: raise TestFailed, 'in/not in string' + +print '6.5.2 Tuples' +if len(()) <> 0: raise TestFailed, 'len(())' +if len((1,)) <> 1: raise TestFailed, 'len((1,))' +if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))' +if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation' +if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3' +if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*' +if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple' +if 0 in (0,1,2) and 1 in (0,1,2) and 2 in (0,1,2) and 3 not in (0,1,2): pass +else: raise TestFailed, 'in/not in tuple' + +print '6.5.3 Lists' +if len([]) <> 0: raise TestFailed, 'len([])' +if len([1,]) <> 1: raise TestFailed, 'len([1,])' +if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])' +if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation' +if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3' +if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*' +if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list' +if 0 in [0,1,2] and 1 in [0,1,2] and 2 in [0,1,2] and 3 not in [0,1,2]: pass +else: raise TestFailed, 'in/not in list' + +print '6.5.3a Additional list operations' +a = [0,1,2,3,4] +a[0] = 5 +a[1] = 6 +a[2] = 7 +if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]' +a[-2] = 8 +a[-1] = 9 +if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]' +a[:2] = [0,4] +a[-3:] = [] +a[1:1] = [1,2,3] +if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment' +del a[1:4] +if a <> [0,4]: raise TestFailed, 'list slice deletion' +del a[0] +if a <> [4]: raise TestFailed, 'list item deletion [0]' +del a[-1] +if a <> []: raise TestFailed, 'list item deletion [-1]' +a.append(0) +a.append(1) +a.append(2) +if a <> [0,1,2]: raise TestFailed, 'list append' +a.insert(0, -2) +a.insert(1, -1) +a.insert(2,0) +if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert' +if a.count(0) <> 2: raise TestFailed, ' list count' +if a.index(0) <> 2: raise TestFailed, 'list index' +a.remove(0) +if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove' +a.reverse() +if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse' +a.sort() +if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort' + +print '6.6 Mappings == Dictionaries' +d = {} +if d.keys() <> []: raise TestFailed, '{}.keys()' +if len(d) <> 0: raise TestFailed, 'len({})' +d = {'a': 1, 'b': 2} +if len(d) <> 2: raise TestFailed, 'len(dict)' +k = d.keys() +k.sort() +if k <> ['a', 'b']: raise TestFailed, 'dict keys()' +if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass +else: raise TestFailed, 'dict keys()' +if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item' +d['c'] = 3 +d['a'] = 4 +if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment' +del d['b'] +if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' diff --git a/Lib/test/testall.py b/Lib/test/testall.py index 5f17be1dcfd..92e7b9fe3fb 100644 --- a/Lib/test/testall.py +++ b/Lib/test/testall.py @@ -25,4 +25,8 @@ print 'test_exceptions' unload('test_exceptions') import test_exceptions +print 'test_types' +unload('test_types') +import test_types + print 'Passed all tests.'