/test/runtests.py

http://gmpy.googlecode.com/ · Python · 135 lines · 88 code · 23 blank · 24 comment · 17 complexity · 4fedfce1f891dc78d1cd8f000c7b1e55 MD5 · raw file

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