PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/test2/gmpy_test_mpq.py

http://gmpy.googlecode.com/
Python | 305 lines | 303 code | 0 blank | 2 comment | 0 complexity | b50d52b5a51d92dc0b10a592d4ec5d6c MD5 | raw file
  1# partial unit test for gmpy2 mpq functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3r'''
  4>>> filter(lambda x: not x.startswith('__'), dir(a))
  5['denominator', 'digits', 'numerator']
  6>>>
  7'''
  8
  9import gmpy2 as _g, doctest,sys
 10__test__={}
 11a=_g.mpq('123/456')
 12b=_g.mpq('789/123')
 13
 14__test__['elemop']=\
 15r'''
 16>>> a+b
 17mpq(41657,6232)
 18>>> a-b
 19mpq(-38295,6232)
 20>>> a*b
 21mpq(263,152)
 22>>> a/b
 23mpq(1681,39976)
 24>>> b+a
 25mpq(41657,6232)
 26>>> b-a
 27mpq(38295,6232)
 28>>> b*a
 29mpq(263,152)
 30>>> b/a
 31mpq(39976,1681)
 32>>> a+1
 33mpq(193,152)
 34>>> 1+a
 35mpq(193,152)
 36>>> a-1
 37mpq(-111,152)
 38>>> 1-a
 39mpq(111,152)
 40>>> a*1
 41mpq(41,152)
 42>>> 1*a
 43mpq(41,152)
 44>>> a/1
 45mpq(41,152)
 46>>> 1/a
 47mpq(152,41)
 48>>> -a
 49mpq(-41,152)
 50>>> abs(-a)
 51mpq(41,152)
 52>>> _g.sign(b-a)
 531
 54>>> _g.sign(b-b)
 550
 56>>> _g.sign(a-b)
 57-1
 58>>> _g.sign(a)
 591
 60>>> _g.sign(-a)
 61-1
 62>>> z=b-b; _g.sign(z)
 630
 64>>> _g.numer(a) == a.numerator
 65True
 66>>> _g.denom(a) == a.denominator
 67True
 68>>> an=_g.numer(a); ad=_g.denom(a);
 69>>> an==0 or 1==a*_g.mpq(ad,an)
 701
 71>>> bn=_g.numer(b); bd=_g.denom(b);
 72>>> bn==0 or 1==b*_g.mpq(bd,bn)
 731
 74>>> zn=_g.numer(z); zd=_g.denom(z);
 75>>> zn==0 or 1==z*_g.mpq(zd,zn)
 761
 77>>> (a+b) == _g.mpq(an*bd+ad*bn,ad*bd)
 781
 79>>> (a+z) == _g.mpq(an*zd+ad*zn,ad*zd)
 801
 81>>> (a+a) == _g.mpq(an*ad+ad*an,ad*ad)
 821
 83>>> import pickle
 84>>> pickle.loads(pickle.dumps(_g.mpq(1234,6789)))
 85mpq(1234,6789)
 86>>>
 87'''
 88
 89from gmpy_truediv import truediv
 90__test__['newdiv']=\
 91r'''
 92>>> a/b
 93mpq(1681,39976)
 94>>> a//b
 95mpz(0)
 96>>> truediv(a,b)
 97mpq(1681,39976)
 98>>> b/a
 99mpq(39976,1681)
100>>> b//a
101mpz(23)
102>>> truediv(b,a)
103mpq(39976,1681)
104>>>
105'''
106
107__test__['cmpr']=\
108r'''
109>>> c=_g.mpq(a)
110>>> c is a
1111
112>>> c==a
1131
114>>> c>a
1150
116>>> c<a
1170
118>>> cmp(a,c)
1190
120>>> cmp(a,b)
121-1
122>>> a>b
1230
124>>> a<b
1251
126>>> not _g.mpq(0)
1271
128>>> not a
1290
130>>> a>1
1310
132>>> a>1.0
1330
134>>> a<1
1351
136>>> a<1.0
1371
138>>> a==1
1390
140>>> a==1.0
1410
142>>> cmp(a,1)
143-1
144>>> cmp(1.0,a)
1451
146>>> long(1/a)
1473L
148>>> long(-1/a)
149-3L
150>>> int(1/a)
1513
152>>> int(-1/a)
153-3
154>>>
155'''
156
157__test__['format']=\
158r'''
159>>> str(a)
160'41/152'
161>>> repr(a)
162'mpq(41,152)'
163>>> a==eval(repr(a),_g.__dict__)
1641
165>>> str(-a)
166'-41/152'
167>>> repr(-a)
168'mpq(-41,152)'
169>>> (-a)==eval(repr(-a),_g.__dict__)
1701
171>>> for i in range(1,7):
172...    for j in range(3,10):
173...       if _g.mpq(i,j) != _g.mpq("%d/%d"%(i,j)):
174...          print 'er1:',i,j; break
175...       aa=_g.mpq(i,j); ai=_g.numer(aa); aj=_g.denom(aa)
176...       if aj!=1 and str(aa) != ("%d/%d"%(ai,aj)):
177...          print 'er2:',i,j,str(aa),("%d/%d"%(ai,aj)); break
178...       if aj==1 and str(aa) != ("%d"%ai):
179...          print 'er3:',i,j,str(aa),"%d"%ai; break
180...       if aj!=1 and repr(aa) != ("mpq(%d,%d)"%(ai,aj)):
181...          print 'er4:',i,j,repr(aa),("mpq(%d,%d)"%(ai,aj)); break
182...       if aj==1 and repr(aa) != ("mpq(%d,%d)"%(ai,aj)):
183...          print 'er5:',i,j,repr(aa),"mpq(%d,%d)"%(ai,aj); break
184>>> fmo='_g.mpq('+hex(a.numerator)+','+hex(a.denominator)+')'
185>>> fmo
186'_g.mpq(0x29,0x98)'
187>>> eval(fmo)==a
1881
189>>> fmo='_g.mpq("'+_g.numer(a).digits(30)+'/'+_g.denom(a).digits(30)+'",30)'
190>>> fmo
191'_g.mpq("1b/52",30)'
192>>> eval(fmo)==a
1931
194>>> _g.digits(a,30)
195'1b/52'
196>>> a.digits(30)
197'1b/52'
198>>> _g.mpq(1000L*1000*1000*1000*1000*1000*1000,23)
199mpq(1000000000000000000000L,23)
200>>> _g.mpq(23,1000L*1000*1000*1000*1000*1000*1000)
201mpq(23,1000000000000000000000L)
202>>> _g.mpq(23L**15L,1000L**7L)
203mpq(266635235464391245607L,1000000000000000000000L)
204>>> x=_g.mpq('234/567')
205>>> del x
206>>> _g.mpq('7788')
207mpq(7788,1)
208>>> _g.mpq('12.34')
209mpq(617,50)
210'''
211
212__test__['binio']=\
213r'''
214>>> a == _g.from_binary(_g.to_binary(a))
215True
216>>> -a == _g.from_binary(_g.to_binary(-a))
217True
218'''
219
220__test__['power']=\
221r'''
222>>> _g.mpq(2,3)**3
223mpq(8,27)
224>>> _g.mpq(8,27)**_g.mpq('2/3')
225mpfr('0.44444444444444448')
226>>> _g.mpq(2,3)**-3
227mpq(27,8)
228>>> _g.mpq(8,27)**_g.mpq('-2/3')
229mpfr('2.25')
230>>> print float(_g.mpfr('0.2')**2)
2310.04
232>>> print float(_g.mpfr('0.2')**-2)
23325.0
234>>> _g.mpq(3)**3 == _g.mpz(3)**3
2351
236>>> (a**-7) == 1/(a**7)
2371
238>>> (b**5) == 1/(b**-5)
2391
240>>>
241'''
242
243__test__['qdiv']=\
244r'''
245>>> _g.qdiv(12,2)
246mpz(6)
247>>> _g.qdiv(12,5)
248mpq(12,5)
249>>> a is _g.qdiv(a)
2501
251>>> a is _g.qdiv(a,1)
2521
253>>> a is _g.qdiv(a,2)
2540
255>>> x=_g.numer(a)
256>>> x is _g.qdiv(x)
2571
258>>> x is _g.qdiv(x,1)
2591
260>>> x is _g.qdiv(x,2)
2610
262>>> y=_g.mpq(4,1)
263>>> y is _g.qdiv(y)
2640
265>>> y == _g.qdiv(y)
2661
267>>>
268'''
269
270def _test(chat=None):
271    if chat:
272        print "Unit tests for gmpy2 (mpq functionality)"
273        print "    on Python %s" % sys.version
274        print "Testing gmpy2 {0}".format(_g.version())
275        print "  Mutliple-precision library:   {0}".format(_g.mp_version())
276        print "  Floating-point library:       {0}".format(_g.mpfr_version())
277        print "  Complex library:              {0}".format(_g.mpc_version())
278        print "  Caching Values: (Number)      {0}".format(_g.get_cache()[0])
279        print "  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1])
280
281    thismod = sys.modules.get(__name__)
282    doctest.testmod(thismod, report=0)
283
284    if chat: print "Repeating tests, with caching disabled"
285    _g.set_cache(0,128)
286
287    sav = sys.stdout
288    class _Dummy:
289        def write(self,*whatever):
290            pass
291    try:
292        sys.stdout = _Dummy()
293        doctest.testmod(thismod, report=0)
294    finally:
295        sys.stdout = sav
296
297    if chat:
298        print
299        print "Overall results for mpq:"
300    return doctest.master.summarize(chat)
301
302
303if __name__=='__main__':
304    _test(1)
305