PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/test3/gmpy_test_mpq.py

http://gmpy.googlecode.com/
Python | 327 lines | 325 code | 0 blank | 2 comment | 0 complexity | 998f5884b3f3cc992682f214e091e366 MD5 | raw file
  1# partial unit test for gmpy2 mpq functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3r'''
  4>>> list([x for x in dir(a) 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__', 'denominator', 'digits', 'numerator']
  6>>>
  7'''
  8
  9import gmpy2 as _g, doctest,sys
 10import fractions
 11F=fractions.Fraction
 12
 13__test__={}
 14a=_g.mpq('123/456')
 15b=_g.mpq('789/123')
 16af=F(123,456)
 17bf=F(789,123)
 18
 19__test__['compat']=\
 20r'''
 21>>> a==af
 22True
 23>>> af==a
 24True
 25>>> a < af
 26False
 27>>> a <= af
 28True
 29>>> a > af
 30False
 31>>> a >= af
 32True
 33>>> af < a
 34False
 35>>> af <= a
 36True
 37>>> af > a
 38False
 39>>> af >= a
 40True
 41>>> a+bf
 42mpq(41657,6232)
 43>>> divmod(123*a, b) == divmod(123*af, bf)
 44True
 45>>> divmod(-23*a, b) == divmod(-23*af, bf)
 46True
 47>>> divmod(a+17, b-23) == divmod(af+17, bf-23)
 48True
 49>>> divmod(-a, -b) == divmod(-af, -bf)
 50True
 51'''
 52
 53__test__['elemop']=\
 54r'''
 55>>> a+b
 56mpq(41657,6232)
 57>>> a-b
 58mpq(-38295,6232)
 59>>> a*b
 60mpq(263,152)
 61>>> a/b
 62mpq(1681,39976)
 63>>> a//b
 64mpz(0)
 65>>> a//-b
 66mpz(-1)
 67>>> -a//b
 68mpz(-1)
 69>>> -a//-b
 70mpz(0)
 71>>> b+a
 72mpq(41657,6232)
 73>>> b-a
 74mpq(38295,6232)
 75>>> b*a
 76mpq(263,152)
 77>>> b/a
 78mpq(39976,1681)
 79>>> b//a
 80mpz(23)
 81>>> a+1
 82mpq(193,152)
 83>>> 1+a
 84mpq(193,152)
 85>>> a-1
 86mpq(-111,152)
 87>>> 1-a
 88mpq(111,152)
 89>>> a*1
 90mpq(41,152)
 91>>> 1*a
 92mpq(41,152)
 93>>> a/1
 94mpq(41,152)
 95>>> 1/a
 96mpq(152,41)
 97>>> a % b
 98mpq(41,152)
 99>>> a % -b
100mpq(-38295,6232)
101>>> 2*a % 7*b
102mpq(263,76)
103>>> -a
104mpq(-41,152)
105>>> abs(-a)
106mpq(41,152)
107>>> _g.sign(b-a)
1081
109>>> _g.sign(b-b)
1100
111>>> _g.sign(a-b)
112-1
113>>> _g.sign(a)
1141
115>>> _g.sign(-a)
116-1
117>>> z=b-b; _g.sign(z)
1180
119>>> _g.numer(a) == a.numerator
120True
121>>> _g.denom(a) == a.denominator
122True
123>>> an=_g.numer(a); ad=_g.denom(a);
124>>> an==0 or 1==a*_g.mpq(ad,an)
1251
126>>> bn=_g.numer(b); bd=_g.denom(b);
127>>> bn==0 or 1==b*_g.mpq(bd,bn)
1281
129>>> zn=_g.numer(z); zd=_g.denom(z);
130>>> zn==0 or 1==z*_g.mpq(zd,zn)
1311
132>>> (a+b) == _g.mpq(an*bd+ad*bn,ad*bd)
1331
134>>> (a+z) == _g.mpq(an*zd+ad*zn,ad*zd)
1351
136>>> (a+a) == _g.mpq(an*ad+ad*an,ad*ad)
1371
138>>> import pickle
139>>> pickle.loads(pickle.dumps(_g.mpq(1234,6789)))
140mpq(1234,6789)
141>>>
142'''
143
144__test__['cmpr']=\
145r'''
146>>> c=_g.mpq(a)
147>>> c is a
1481
149>>> c==a
1501
151>>> c>a
1520
153>>> c<a
1540
155>>> a>b
1560
157>>> a<b
1581
159>>> not _g.mpq(0)
1601
161>>> not a
1620
163>>> a>1
1640
165>>> a>1.0
1660
167>>> a<1
1681
169>>> a<1.0
1701
171>>> a==1
1720
173>>> a==1.0
1740
175>>> int(1/a)
1763
177>>> int(-1/a)
178-3
179>>>
180'''
181
182__test__['format']=\
183r'''
184>>> str(a)
185'41/152'
186>>> repr(a)
187'mpq(41,152)'
188>>> a==eval(repr(a),_g.__dict__)
1891
190>>> str(-a)
191'-41/152'
192>>> repr(-a)
193'mpq(-41,152)'
194>>> (-a)==eval(repr(-a),_g.__dict__)
1951
196>>> for i in range(1,7):
197...    for j in range(3,10):
198...       if _g.mpq(i,j) != _g.mpq("%d/%d"%(i,j)):
199...          print('er1:',i,j); break
200...       aa=_g.mpq(i,j); ai=_g.numer(aa); aj=_g.denom(aa)
201...       if aj!=1 and str(aa) != ("%d/%d"%(ai,aj)):
202...          print('er2:',i,j,str(aa),("%d/%d"%(ai,aj))); break
203...       if aj==1 and str(aa) != ("%d"%ai):
204...          print('er3:',i,j,str(aa),"%d"%ai); break
205...       if aj!=1 and repr(aa) != ("mpq(%d,%d)"%(ai,aj)):
206...          print('er4:',i,j,repr(aa),("mpq(%d,%d)"%(ai,aj))); break
207...       if aj==1 and repr(aa) != ("mpq(%d,%d)"%(ai,aj)):
208...          print('er5:',i,j,repr(aa),"mpq(%d,%d)"%(ai,aj)); break
209>>> fmo='_g.mpq('+hex(a.numerator)+','+hex(a.denominator)+')'
210>>> fmo
211'_g.mpq(0x29,0x98)'
212>>> eval(fmo)==a
2131
214>>> fmo='_g.mpq("'+_g.numer(a).digits(30)+'/'+_g.denom(a).digits(30)+'",30)'
215>>> fmo
216'_g.mpq("1b/52",30)'
217>>> eval(fmo)==a
2181
219>>> _g.digits(a,30)
220'1b/52'
221>>> a.digits(30)
222'1b/52'
223>>> _g.mpq(1000*1000*1000*1000*1000*1000*1000,23)
224mpq(1000000000000000000000,23)
225>>> _g.mpq(23,1000*1000*1000*1000*1000*1000*1000)
226mpq(23,1000000000000000000000)
227>>> _g.mpq(23**15,1000**7)
228mpq(266635235464391245607,1000000000000000000000)
229>>> x=_g.mpq('234/567')
230>>> del x
231>>> _g.mpq('7788')
232mpq(7788,1)
233>>> _g.mpq('12.34')
234mpq(617,50)
235'''
236
237__test__['binio']=\
238r'''
239>>> a == _g.from_binary(_g.to_binary(a))
240True
241>>> -a == _g.from_binary(_g.to_binary(-a))
242True
243'''
244
245__test__['power']=\
246r'''
247>>> _g.mpq(2,3)**3
248mpq(8,27)
249>>> _g.mpq(8,27)**_g.mpq('2/3')
250mpfr('0.44444444444444448')
251>>> _g.mpq(2,3)**-3
252mpq(27,8)
253>>> _g.mpq(8,27)**_g.mpq('-2/3')
254mpfr('2.25')
255>>> _g.mpq(3)**3 == _g.mpz(3)**3
256True
257>>> (a**-7) == 1/(a**7)
258True
259>>> (b**5) == 1/(b**-5)
260True
261>>>
262'''
263
264__test__['qdiv']=\
265r'''
266>>> _g.qdiv(12,2)
267mpz(6)
268>>> _g.qdiv(12,5)
269mpq(12,5)
270>>> a is _g.qdiv(a)
2711
272>>> a is _g.qdiv(a,1)
2731
274>>> a is _g.qdiv(a,2)
2750
276>>> x=_g.numer(a)
277>>> x is _g.qdiv(x)
2781
279>>> x is _g.qdiv(x,1)
2801
281>>> x is _g.qdiv(x,2)
2820
283>>> y=_g.mpq(4,1)
284>>> y is _g.qdiv(y)
2850
286>>> y == _g.qdiv(y)
2871
288>>>
289'''
290
291def _test(chat=None):
292    if chat:
293        print("Unit tests for gmpy2 (mpq functionality)")
294        print("    on Python %s" % sys.version)
295        print("Testing gmpy2 {0}".format(_g.version()))
296        print("  Mutliple-precision library:   {0}".format(_g.mp_version()))
297        print("  Floating-point library:       {0}".format(_g.mpfr_version()))
298        print("  Complex library:              {0}".format(_g.mpc_version()))
299        print("  Caching Values: (Number)      {0}".format(_g.get_cache()[0]))
300        print("  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
301
302    thismod = sys.modules.get(__name__)
303    doctest.testmod(thismod, report=0)
304
305    if chat: print("Repeating tests, with caching disabled")
306    _g.set_cache(0,128)
307
308    sav = sys.stdout
309    class _Dummy:
310        encoding = None
311        def write(self,*whatever):
312            pass
313    try:
314        sys.stdout = _Dummy()
315        doctest.testmod(thismod, report=0)
316    finally:
317        sys.stdout = sav
318
319    if chat:
320        print()
321        print("Overall results for mpq:")
322    return doctest.master.summarize(chat)
323
324
325if __name__=='__main__':
326    _test(1)
327