/test3/gmpy_test_dec.py

http://gmpy.googlecode.com/ · Python · 99 lines · 40 code · 7 blank · 52 comment · 8 complexity · 39bf45051eefab58f41860bf84b4865e MD5 · raw file

  1. # partial unit test for gmpy/decimal interoperability
  2. # relies on Tim Peters' "doctest.py" test-driver
  3. r'''
  4. >>> list([x for x in dir(f) if x != '__dir__'])
  5. ['__abs__', '__add__', '__bool__', '__ceil__', '__class__', '__delattr__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'as_mantissa_exp', 'as_simple_fraction', 'conjugate', 'digits', 'imag', 'is_integer', 'precision', 'rc', 'real']
  6. >>>
  7. '''
  8. try: import decimal as _d
  9. except ImportError: _d = None
  10. import gmpy2 as _g, doctest, sys
  11. __test__={}
  12. f=_g.mpfr('123.456')
  13. q=_g.mpq('789123/1000')
  14. z=_g.mpz('234')
  15. if _d:
  16. d=_d.Decimal('12.34')
  17. fd=_d.Decimal('123.456')
  18. qd=_d.Decimal('789.123')
  19. zd=_d.Decimal('234')
  20. __test__['compat']=\
  21. r'''
  22. >>> f == fd
  23. False
  24. >>> fd == f
  25. False
  26. >>> q == qd
  27. True
  28. >>> qd == q
  29. True
  30. >>> z == zd
  31. True
  32. >>> zd == z
  33. True
  34. >>> f > d
  35. True
  36. >>> d > f
  37. False
  38. '''
  39. __test__['elemop']=\
  40. r'''
  41. >>> print(_g.mpz(23) == _d.Decimal(23))
  42. True
  43. >>> print(_g.mpz(d))
  44. 12
  45. >>> print(_g.mpq(d))
  46. 617/50
  47. >>> print(_g.mpfr(d))
  48. 12.34
  49. >>> print(f+d)
  50. 135.79599999999999
  51. >>> print(d+f)
  52. 135.79599999999999
  53. >>> print(q+d)
  54. 801.46300000000008
  55. >>> print(d+q)
  56. 801.46300000000008
  57. >>> print(z+d)
  58. 246.34
  59. >>> print(d+z)
  60. 246.34
  61. >>> print(_g.ceil(d))
  62. 13.0
  63. >>> print(_g.floor(d))
  64. 12.0
  65. >>> print(_g.trunc(d))
  66. 12.0
  67. >>> _g.mpfr(d).precision
  68. 53
  69. >>>
  70. '''
  71. def _test(chat=None):
  72. if chat:
  73. print("Unit tests for gmpy2 (decimal interoperation)")
  74. print(" on Python %s" % sys.version)
  75. print("Testing gmpy2 {0}".format(_g.version()))
  76. print(" Mutliple-precision library: {0}".format(_g.mp_version()))
  77. print(" Floating-point library: {0}".format(_g.mpfr_version()))
  78. print(" Complex library: {0}".format(_g.mpc_version()))
  79. print(" Caching Values: (Number) {0}".format(_g.get_cache()[0]))
  80. print(" Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
  81. if not _d:
  82. if chat:
  83. print("Can't test, since can't import decimal")
  84. return 0, 0
  85. thismod = sys.modules.get(__name__)
  86. doctest.testmod(thismod, report=0)
  87. if chat:
  88. print()
  89. print("Overall results for dec:")
  90. return doctest.master.summarize(chat)
  91. if __name__=='__main__':
  92. _test(1)