PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test3/gmpy_test_dec.py

http://gmpy.googlecode.com/
Python | 99 lines | 97 code | 0 blank | 2 comment | 0 complexity | 39bf45051eefab58f41860bf84b4865e MD5 | raw file
 1# partial unit test for gmpy/decimal interoperability
 2# relies on Tim Peters' "doctest.py" test-driver
 3r'''
 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'''
 8try: import decimal as _d
 9except ImportError: _d = None
10
11import gmpy2 as _g, doctest, sys
12__test__={}
13f=_g.mpfr('123.456')
14q=_g.mpq('789123/1000')
15z=_g.mpz('234')
16if _d:
17    d=_d.Decimal('12.34')
18    fd=_d.Decimal('123.456')
19    qd=_d.Decimal('789.123')
20    zd=_d.Decimal('234')
21
22__test__['compat']=\
23r'''
24>>> f == fd
25False
26>>> fd == f
27False
28>>> q == qd
29True
30>>> qd == q
31True
32>>> z == zd
33True
34>>> zd == z
35True
36>>> f > d
37True
38>>> d > f
39False
40'''
41
42
43__test__['elemop']=\
44r'''
45>>> print(_g.mpz(23) == _d.Decimal(23))
46True
47>>> print(_g.mpz(d))
4812
49>>> print(_g.mpq(d))
50617/50
51>>> print(_g.mpfr(d))
5212.34
53>>> print(f+d)
54135.79599999999999
55>>> print(d+f)
56135.79599999999999
57>>> print(q+d)
58801.46300000000008
59>>> print(d+q)
60801.46300000000008
61>>> print(z+d)
62246.34
63>>> print(d+z)
64246.34
65>>> print(_g.ceil(d))
6613.0
67>>> print(_g.floor(d))
6812.0
69>>> print(_g.trunc(d))
7012.0
71>>> _g.mpfr(d).precision
7253
73>>>
74'''
75
76def _test(chat=None):
77    if chat:
78        print("Unit tests for gmpy2 (decimal interoperation)")
79        print("    on Python %s" % sys.version)
80        print("Testing gmpy2 {0}".format(_g.version()))
81        print("  Mutliple-precision library:   {0}".format(_g.mp_version()))
82        print("  Floating-point library:       {0}".format(_g.mpfr_version()))
83        print("  Complex library:              {0}".format(_g.mpc_version()))
84        print("  Caching Values: (Number)      {0}".format(_g.get_cache()[0]))
85        print("  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
86    if not _d:
87        if chat:
88            print("Can't test, since can't import decimal")
89        return 0, 0
90    thismod = sys.modules.get(__name__)
91    doctest.testmod(thismod, report=0)
92
93    if chat:
94        print()
95        print("Overall results for dec:")
96    return doctest.master.summarize(chat)
97
98if __name__=='__main__':
99    _test(1)