PageRenderTime 61ms CodeModel.GetById 45ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/runtests.py

http://gmpy.googlecode.com/
Python | 135 lines | 93 code | 18 blank | 24 comment | 7 complexity | 4fedfce1f891dc78d1cd8f000c7b1e55 MD5 | raw file
  1from __future__ import print_function, division
  2
  3import sys
  4import doctest
  5import gmpy2
  6
  7# *****************************************************************************
  8# Test strategy
  9# -------------
 10# Tests are divided into two different categories:
 11#
 12#   1) The 'txt' files contain doctest style tests. These tests should cover
 13#      basic functionality for all functions/types.
 14#   2) The 'py' files contain Python code that perform extensive tests, but
 15#      may not test every function.
 16#
 17# If run by a debug build of Python, the test suite can be repeated multiple
 18# times to search for memory leaks.
 19#
 20# NOTE: IF THE LAST TEST IN A BLOCK OF TESTS GENERATES AN EXCEPTION, THE
 21#       REFERENCE COUNTING IN A DEBUG BUILD GETS CONFUSED. ALWAYS ENSURE THAT
 22#       AT LEAST ONE VALID TEST IS PERFORMED AFTER AN EXCEPTION IS RAISED!
 23#
 24# *****************************************************************************
 25
 26# Check if this is a debug build of Python.
 27try:
 28    sys.gettotalrefcount()
 29    debug = True
 30except AttributeError:
 31    debug = False
 32
 33# Change repeat to the number of times to repeat each test. Combined with a
 34# debug build, this can help identify memory leaks.
 35if debug:
 36    try:
 37        repeat = abs(int(sys.argv[1]))
 38    except:
 39        repeat = 1
 40else:
 41    repeat = 1
 42
 43print()
 44print("Unit tests for gmpy2 {0} with Python {1}".format(gmpy2.version(), sys.version.split()[0]))
 45print("  Mutliple-precision library:     {0}".format(gmpy2.mp_version()))
 46print("  Floating-point library:         {0}".format(gmpy2.mpfr_version()))
 47print("  Complex library:                {0}".format(gmpy2.mpc_version()))
 48print("  Caching Values: (Cache size)    {0}".format(gmpy2.get_cache()[0]))
 49print("  Caching Values: (Size in limbs) {0}".format(gmpy2.get_cache()[1]))
 50print()
 51
 52if sys.version.startswith('3.1'):
 53    print("Due to differences in formatting of exceptions and Python 3.x, there")
 54    print("will be test failures for exception handling when the tests are run")
 55    print("with Python 3.1. The doctest module in Python 3.2 and later does not")
 56    print("have this issue.")
 57    print()
 58    input("Press ENTER to continue.. ")
 59    print()
 60
 61mpz_doctests = ["test_mpz_create.txt", "test_mpz.txt", "test_mpz_io.txt",
 62                "test_mpz_pack_unpack.txt", "test_mpz_to_from_binary.txt"]
 63
 64mpq_doctests = ["test_mpq.txt", "test_mpq_to_from_binary.txt"]
 65
 66mpfr_doctests = ["test_mpfr_create.txt", "test_mpfr.txt",
 67                 "test_mpfr_trig.txt", "test_mpfr_min_max.txt",
 68                 "test_mpfr_to_from_binary.txt", "test_context.txt",
 69                 "test_mpfr_subnormalize.txt"]
 70
 71mpc_doctests = ["test_mpc_create.txt", "test_mpc.txt",
 72                "test_mpc_to_from_binary.txt"]
 73
 74gmpy2_tests = ["test_misc.txt", "test_abs.txt"]
 75
 76# The following tests will only pass on Python 3.2+.
 77py32_doctests = ["test_py32_hash.txt"]
 78
 79failed = 0
 80attempted = 0
 81
 82all_doctests = gmpy2_tests + mpz_doctests + mpq_doctests
 83
 84# MPFR support is required.
 85all_doctests += mpfr_doctests
 86
 87# MPC support is required.
 88all_doctests += mpc_doctests
 89
 90if sys.version >= "3.2":
 91    all_doctests += py32_doctests
 92
 93for test in sorted(all_doctests):
 94    for r in range(repeat):
 95        result = doctest.testfile(test, globs=globals(),
 96                                  optionflags=doctest.IGNORE_EXCEPTION_DETAIL |
 97                                              doctest.NORMALIZE_WHITESPACE |
 98                                              doctest.REPORT_NDIFF)
 99        print("Results for:  {0:24}".format(test.split(".")[0]), end="")
100        print(" Attempted: {1:4d}   Failed: {0:4d}".format(*result), end="")
101        if debug:
102            print(" RefCount: {0:6d}".format(sys.gettotalrefcount()))
103        else:
104            print()
105        failed += result[0]
106        attempted += result[1]
107
108
109print()
110print("                             Summary - Attempted: {0:4d}   Failed: {1:4d}".format(attempted, failed))
111print()
112print("Running external test programs.")
113
114print("Running {0:29}  ".format("test_pack.py"), end="")
115import test_pack
116if test_pack.test():
117    print("successful")
118    attempted += 1
119else:
120    print("failed")
121    failed += 1
122
123print("Running {0:29}  ".format("test_mpz_args.py"), end="")
124import test_mpz_args
125if test_mpz_args.test():
126    print("successful")
127    attempted += 1
128else:
129    print("failed")
130    failed += 1
131
132if failed:
133    sys.exit(1)
134else:
135    sys.exit(0)