From: Gustavo N. <nie...@us...> - 2001-12-11 10:30:04
|
Update of /cvsroot/gmpy/gmpy/test In directory usw-pr-cvs1:/tmp/cvs-serv30377/test Added Files: doctest.py gmpy_test_08.py gmpy_test_08_cvr.py gmpy_test_08_mpf.py gmpy_test_08_mpq.py gmpy_test_08_mpz.py gmpy_test_08_rnd.py timing1.py timing2.py timing3.py Log Message: Added files to repository. --- NEW FILE: doctest.py --- # Module doctest version 0.9.4 # Released to the public domain 27-Mar-1999, # by Tim Peters (ti...@em...). # Provided as-is; use at your own risk; no warranty; no promises; enjoy! """Module doctest -- a framework for running examples in docstrings. NORMAL USAGE In normal use, end each module M with: def _test(): import doctest, M # replace M with your module's name return doctest.testmod(M) # ditto if __name__ == "__main__": _test() [...1074 lines suppressed...] -42 """ return self.val __test__ = {"_TestClass": _TestClass, "string": r""" Example of a string object, searched as-is. >>> x = 1; y = 2 >>> x + y, x * y (3, 2) """ } def _test(): import doctest return doctest.testmod(doctest) if __name__ == "__main__": _test() --- NEW FILE: gmpy_test_08.py --- r''' >>> gmpy.version() '0.8.0d' >>> ''' import sys import doctest import gmpy if sys.argv[-1] == 'debug': gmpy.set_debug(1) import gmpy_test_08_cvr import gmpy_test_08_rnd import gmpy_test_08_mpf import gmpy_test_08_mpq import gmpy_test_08_mpz test_modules = (gmpy_test_08_cvr, gmpy_test_08_rnd, gmpy_test_08_mpf, gmpy_test_08_mpq, gmpy_test_08_mpz ) _g = gmpy print "Unit tests for gmpy 0.8 pre-alpha" print " on Python",sys.version print "Testing gmpy %s (GMP %s), default caching (%s, %s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache(), _g.get_qcache(), ) + _g.get_zconst()) pf, pt = 0, 0 for x in test_modules: print x.__name__, failures, tests = x._test() print "%3d tests, %d failures" % (tests-pt, failures-pf) pf, pt = failures, tests doctest.master.summarize(1) --- NEW FILE: gmpy_test_08_cvr.py --- # partial unit test for gmpy 0.9 extra cover # relies on Tim Peters' "doctest.py" test-driver # test-version 0.9 r''' >>> _g.gmp_version() '3.1.1' >>> _g.version() '0.9.0b' ''' import gmpy as _g, doctest, sys __test__={} r = _g.rand __test__['misc_stuff']=\ r''' >>> junk=_g.set_debug(0) >>> knuj=_g.set_debug(junk) >>> junk==_g.set_debug(junk) 1 >>> _g.set_fcoform(None) >>> _g.set_fcoform() >>> print _g.mpf(3.0) 3.0 >>> ''' __test__['user_errors']=\ r''' >>> _g.version(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 0 arguments (1 given) >>> _g.gmp_version(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 0 arguments (1 given) >>> _g.get_zcache(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 0 arguments (1 given) >>> _g.set_zcache(2000) Traceback (most recent call last): File "<string>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.set_zcache(-23) Traceback (most recent call last): File "<string>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.get_qcache(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 0 arguments (1 given) >>> _g.set_qcache(2000) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.set_qcache(-42) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.get_zconst(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 0 arguments (1 given) >>> _g.set_zconst() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 2 arguments (0 given) >>> _g.set_zconst(23) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 2 arguments (1 given) >>> _g.set_zconst(23,45,67) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 2 arguments (3 given) >>> _g.set_zconst(23,'blub') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.set_zconst(-1000,1000) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.set_zconst(100,-100) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: cache must between 0 and 1000 >>> _g.set_debug() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 1 argument (0 given) >>> _g.set_debug(2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes exactly 1 argument (2 given) >>> _g.set_debug('boh') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.set_minprec(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: minimum precision must be >= 0 >>> _g.set_fcoform(33) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: number of digits n must be 0<n<=30 >>> _g.set_fcoform([]) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: set_fcoform argument must be int, string, or None >>> _g.mpz('12'+chr(0)+'34') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: string without NULL characters expected >>> _g.mpf('12'+chr(0)+'34') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: string without NULL characters expected >>> _g.mpq('12'+chr(0)+'34') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: string without NULL characters expected >>> _g.mpq('bo',256) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid mpq binary (too short) >>> _g.mpq('bologna',256) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid mpq binary (num len) >>> _g.mpq('\001\000\000\000\003\002',256) mpq(3,2) >>> _g.mpq('\002\000\000\000\003\377\002',256) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid mpq binary (num sgn) >>> _g.mpq('\001\000\000\000\003\002\377',256) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid mpq binary (den sgn) >>> _g.mpq('ba/bo') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid digits >>> print 'ba/bo' ba/bo >>> _g.mpq('1/bo') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid digits >>> print '1/bo' 1/bo >>> _g.mpq('1/0') Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: mpq: zero denominator >>> _g.mpf([]) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpf() expects numeric or string argument >>> _g.mpf('bo',0,256) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: string too short to be a gmpy.mpf binary encoding >>> _g.mpf('bo') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid digits >>> int(_g.mpz(1000*1000*1000*1000L)) Traceback (most recent call last): File "<stdin>", line 1, in ? OverflowError: mpz too large for int >>> _g.scan0(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: starting bit must be >= 0 >>> _g.scan1(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: starting bit must be >= 0 >>> _g.lowbits(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: nbits must be > 0 >>> _g.getbit(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: bit_index must be >= 0 >>> _g.setbit(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: bit_index must be >= 0 >>> _g.mpz(23).setbit(12,1,2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes at most 2 arguments (4 given) >>> _g.setbit(12,1,2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes at most 3 arguments (4 given) >>> _g.root(12,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: n must be > 0 >>> _g.root(12,0) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: n must be > 0 >>> _g.root(-12,2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: root of negative number >>> _g.digits(3.14,'peep') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.fdigits(3.14,'peep') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.qdigits(3.14,'peep') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.mpz(3).digits('bu') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.mpf(3).digits('bu') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: an integer is required >>> _g.qdiv(3,_g.mpq(1)) mpz(3) >>> _g.qdiv(3,_g.mpz(1)) mpz(3) >>> _g.qdiv(3,_g.mpf(1)) mpz(3) >>> _g.qdiv(3,1.0) mpz(3) >>> _g.qdiv(3,1L) mpz(3) >>> _g.qdiv(3) mpz(3) >>> _g.qdiv(3,'bu') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: second argument to qdiv not a number >>> _g.mpq(2).qdiv(3,4) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes at most 1 argument (2 given) >>> _g.qdiv(3,4,5) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: function takes at most 2 arguments (3 given) >>> _g.qdiv('bu') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: first argument to qdiv not a number >>> _g.qdiv(1.0,1) mpz(1) >>> _g.qdiv(1.0,0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: qdiv: zero divisor >>> _g.f2q(-1.0) mpz(-1) >>> _g.mpz(1,2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpz() requires 1 or 2 arguments >>> _g.mpz('bi','bo') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpz(): base must be an integer >>> _g.mpz('bi',99) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: base for gmpy.mpz must be 0, 256, or in the interval 2 ... 36 . >>> _g.mpz(1,2) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpz() with numeric argument needs exactly 1 argument >>> _g.mpz(None) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpz() expects numeric or string argument >>> _g.mpq(1,2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpq() requires 1 or 2 arguments >>> _g.mpq('bi','bo') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpq(): base must be an integer >>> _g.mpq('bi',99) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: base for gmpy.mpq() must be 0, 256, or in the interval 2 ... 36 . >>> _g.mpq(None) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpq() expects numeric or string argument >>> _g.mpq(1,None) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: argument can not be converted to mpq >>> _g.mpq(1,0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: mpq: zero denominator >>> _g.mpf() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpf() requires 1 to 3 arguments >>> _g.mpf(1,'bo') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpf(): bits must be an integer >>> _g.mpf(1,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: bits for gmpy.mpf must be >= 0 >>> _g.mpf('ba',0,'bu') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpf(): base must be an integer >>> _g.mpf('ba',0,99) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: base for gmpy.mpf must be 0, 256, or in the interval 2 ... 36 . >>> _g.mpf(1,2,3) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: gmpy.mpf() with numeric 1st argument needs 1 or 2 arguments >>> +_g.mpz(1) mpz(1) >>> +_g.mpf(1) mpf('1.e0') >>> +_g.mpq(1) mpq(1) >>> _g.mpz(2)**-2 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpz.pow with negative power >>> _g.mpz(2)**_g.mpz(1000000*1000000L) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpz.pow outrageous exponent >>> pow(_g.mpz(2),3,0) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpz.pow divide by zero >>> pow(_g.mpz(2),3,-5) mpz(-2) >>> pow(_g.mpq(2),3,-5) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpq.pow no modulo allowed >>> a=1000000L**2 >>> _g.mpq(2)**a Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpq.pow outrageous exp num >>> _g.mpq(2)**_g.mpq(1,a) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpq.pow outrageous exp den >>> _g.mpq(2)**0 mpq(1) >>> _g.mpq(2)**-1 mpq(1,2) >>> _g.mpq(2)**_g.mpq(1,2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpq.pow fractional exponent, inexact-root >>> _g.mpq(0)**_g.mpq(1,2) mpq(0) >>> _g.mpq(0)**-1 Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: mpq.pow 0 base to <0 exponent >>> _g.mpq(-1)**-1 mpq(-1) >>> _g.mpf(9,100)**2 mpf('8.1e1',100) >>> _g.mpf(9,100)**0.5 mpf('3.e0',100) >>> _g.mpf(9,100)**_g.mpf(0.5) mpf('3.e0') >>> pow(_g.mpf(2),3,-5) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: mpf.pow no modulo allowed >>> _g.mpz(1)+'bu' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: coercion to gmpy.mpz type failed >>> _g.mpz(1)+_g.mpf(1) mpf('2.e0') >>> _g.mpz(1)+_g.mpq(1) mpq(2) >>> _g.mpq(1)+'bu' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: coercion to gmpy.mpq type failed >>> _g.mpf(1)+'bu' Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: coercion to gmpy.mpf type failed >>> _g.mpf(1)+_g.mpq(2) mpq(3) >>> divmod(_g.mpz(3),0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: mpz.divmod by zero >>> divmod(_g.mpz(0),3) (mpz(0), mpz(0)) >>> _g.divm(1,2,0) Traceback (most recent call last): File "<stdin>", line 1, in ? ZeroDivisionError: not invertible >>> abs(_g.mpq(0)) mpq(0) >>> _g.mpz(0)**2 mpz(0) >>> _g.mpq(-2)**0 mpq(1) >>> _g.fac(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: factorial of negative number >>> _g.fib(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Fibonacci of negative number >>> _g.comb(7,-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: binomial coefficient with negative k >>> _g.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: sqrt of negative number >>> _g.sqrtrem(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: sqrt of negative number >>> _g.fsqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: sqrt of negative number ''' def _test(chat=None): if chat: print "Unit tests for gmpy 0.9 pre-alpha (extra cover)" print " running on Python",sys.version print print "Testing gmpy %s (GMP %s) with default caching" \ "(%s, %s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache(), _g.get_qcache(), ) + _g.get_zconst()) thismod = sys.modules.get(__name__) doctest.testmod(thismod, report=0) if chat: print "Repeating tests, with empty callbacks" def _zd(where,*args): if where=='qdiv': raise ZeroDivisionError, "qdiv: zero divisor" elif where=='mpz_divmod': raise ZeroDivisionError, "mpz.divmod by zero" elif where=='mpq_pow': raise ZeroDivisionError, "mpq.pow 0 base to <0 exponent" elif where=='divm': raise ZeroDivisionError, "not invertible" else: raise ZeroDivisionError, "mpq: zero denominator" def _zm(*args): return None def _at(self,name): return name.upper() def _er(where, what, *args): raise ValueError, what _g.set_callback('ZM',_zm) _g.set_callback('ZD',_zd) _g.set_callback('ER',_er) _g.set_callback('AT',_at) class _Fakenum: def __gmpy__(self, where, args, other, extype, exvalue): raise extype, exvalue _fn = _Fakenum() try: x = _g.getbit(_fn,17) except TypeError: pass try: x = _g.divm(23,45,_fn) except TypeError: pass assert(_g.mpz(23).foop=='FOOP') assert(_g.mpq(23).foop=='FOOP') assert(_g.mpf(23).foop=='FOOP') # avoid merge-reports for the second run sav = sys.stdout class _Dummy: def write(self,*whatever): pass try: sys.stdout = _Dummy() doctest.testmod(thismod, report=0) finally: sys.stdout = sav # just generate a little extra coverage...! _g.set_callback('ZM') _g.set_callback('ZD',None) _g.set_callback('ER') _g.set_callback('AT') try: _g.set_callback('ZD',23,45) except: pass try: _g.set_callback('ZD',23) except: pass try: _g.set_callback('ZZ') except: pass if chat: print print "Overall results for cvr:" return doctest.master.summarize(chat) if __name__=='__main__': _test(1) --- NEW FILE: gmpy_test_08_mpf.py --- # partial unit test for gmpy 0.8 mpf functionality # relies on Tim Peters' "doctest.py" test-driver # test-version 0.8 r''' >>> dir(a) ['_copy', 'binary', 'ceil', 'digits', 'f2q', 'floor', 'getprec', 'getrprec', 'qdiv', 'reldiff', 'setprec', 'sign', 'sqrt', 'trunc'] >>> ''' import gmpy as _g, doctest, sys __test__={} a=_g.mpf('123.456') b=_g.mpf('789.123') __test__['elemop']=\ r''' >>> print a+b 912.579 >>> print a-b -665.667 >>> print a*b 97421.969088 >>> print a/b 0.156447093799065544915 >>> print b+a 912.579 >>> print b-a 665.667 >>> print b*a 97421.969088 >>> print b/a 6.39193720839813374806 >>> print -a -123.456 >>> print abs(-a) 123.456 >>> _g.fsign(b-a) 1 >>> _g.fsign(b-b) 0 >>> _g.fsign(a-b) -1 >>> a.sign() 1 >>> (-a).sign() -1 >>> z=b-b; z.sign() 0 >>> import math >>> math.ceil(a) 124.0 >>> print a.ceil() 124.0 >>> print _g.ceil(a) 124.0 >>> math.floor(a) 123.0 >>> print a.floor() 123.0 >>> print _g.floor(a) 123.0 >>> print a.trunc() 123.0 >>> print _g.trunc(a) 123.0 >>> x=-a >>> math.floor(x) -124.0 >>> print x.floor() -124. >>> print _g.floor(x) -124. >>> print x.ceil() -123. >>> math.ceil(x) -123.0 >>> print _g.ceil(x) -123. >>> print x.trunc() -123. >>> print _g.trunc(x) -123. >>> _g.ceil(12.3)==math.ceil(12.3) 1 >>> _g.floor(12.3)==math.floor(12.3) 1 >>> _g.ceil(-12.3)==math.ceil(-12.3) 1 >>> _g.floor(-12.3)==math.floor(-12.3) 1 >>> (a**2).reldiff(float(a)**2) < (2.0**-(a.getrprec()-1)) 1 >>> (a**2).reldiff(a*a) < (2.0**-(a.getprec()-1)) 1 >>> (b**2).reldiff(float(b)**2) < (2.0**-(b.getrprec()-1)) 1 >>> (b**2).reldiff(b*b) < (2.0**-(b.getprec()-1)) 1 >>> a.sqrt() mpf('1.11110755554986664846e1') >>> _g.fsqrt(a) mpf('1.11110755554986664846e1') >>> _g.fsqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? File "a.py", line 9, in _er raise ValueError, what ValueError: sqrt of negative number >>> ''' __test__['cmpr']=\ r''' >>> c=_g.mpf(a) >>> c is a 1 >>> c==a 1 >>> c>a 0 >>> c<a 0 >>> d=a._copy() >>> a is d 0 >>> a == d 1 >>> cmp(a,c) 0 >>> cmp(a,b) -1 >>> a>b 0 >>> a<b 1 >>> not _g.mpf(0) 1 >>> not a 0 >>> coerce(a,1) (mpf('1.23456e2'), mpf('1.e0')) >>> coerce(1,a) (mpf('1.e0'), mpf('1.23456e2')) >>> coerce(a,1.0) (mpf('1.23456e2'), mpf('1.e0')) >>> a.f2q(0.1) mpz(123) >>> a.f2q(0.01) mpz(123) >>> a.f2q(0.001) mpq(247,2) >>> a.f2q(0.0001) mpq(1358,11) >>> a.f2q(0.00001) mpq(7037,57) >>> a.f2q(0.000001) mpq(15432,125) >>> a.f2q(0.0000001) mpq(15432,125) >>> a.f2q() mpq(15432,125) >>> print _g.mpf(_g.mpz(1234)) 1234.0 >>> x=1000*1000*1000*1000L >>> _g.mpf(x) mpf('1.e12') >>> c=_g.mpf(a) >>> a is c 1 >>> c=_g.mpf(a,99) >>> a is c 0 >>> a==c 1 >>> ''' __test__['format']=\ r''' >>> str(a) '123.456' >>> repr(a) "mpf('1.23456e2')" >>> _g.set_tagoff(0) 1 >>> a gmpy.mpf('1.23456e2') >>> _g.set_tagoff(1) 0 >>> a.digits(10,0) '1.23456e2' >>> a.digits(10,1) '1.e2' >>> a.digits(10,2) '1.2e2' >>> a.digits(10,3) '1.23e2' >>> a.digits(10,4) '1.235e2' >>> a.digits(10,5) '1.2346e2' >>> a.digits(10,6) '1.23456e2' >>> a.digits(10,7) '1.23456e2' >>> a.digits(10,8) '1.23456e2' >>> junk=_g.set_fcoform(14) >>> frmt=_g.set_fcoform(14) >>> frmt '%.14e' >>> ofmt=_g.set_fcoform(frmt) >>> ofmt '%.14e' >>> _g.mpf(3.4) mpf('3.4e0') >>> print _g.mpf(3.4) 3.4 >>> for i in range(11,99): ... assert str(_g.mpf(i/10.0))==str(i/10.0) ... >>> a.digits(1) Traceback (most recent call last): File "<string>", line 1, in ? ValueError: base must be either 0 or in the interval 2 ... 36 >>> a.digits(2,-1) Traceback (most recent call last): File "<string>", line 1, in ? ValueError: digits must be >= 0 >>> a.digits(10,0,0,-1,2) ('123456', 3, 53) >>> saveprec=a.getrprec() >>> a.setprec(33) >>> a mpf('1.23456e2',33) >>> a.setprec(saveprec) >>> a.getrprec()==saveprec 1 >>> _g.fdigits(2.2e5, 0, 6, -10, 10) '220000.0' >>> _g.fdigits(2.2e-5, 0, 6, -10, 10) '0.000022' >>> _g.digits(_g.mpf(23.45)) '23' >>> _g.fbinary('pep') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: argument can not be converted to mpf >>> ''' __test__['binio']=\ r''' >>> epsilon=_g.mpf(2)**-(a.getrprec()) >>> ba=a.binary() >>> a.reldiff(_g.mpf(ba,0,256)) <= epsilon 1 >>> len(ba) 18 >>> for i in range(len(ba)): ... print ord(ba[i]), ... if i==len(ba)-1: print ... 8 53 0 0 0 1 0 0 0 123 116 188 106 126 249 219 34 209 >>> na=(-a).binary() >>> (-a).reldiff(_g.mpf(na,0,256)) <= epsilon 1 >>> na[0] == chr(ord(ba[0])|1) 1 >>> for bd,nd in zip(ba[1:],na[1:]): ... assert bd==nd >>> ia=(1/a).binary() >>> (1/a).reldiff(_g.mpf(ia,0,256)) <= epsilon 1 >>> _g.fbinary(0) '\x085\x00\x00\x00\x00\x00\x00\x00' >>> _g.fbinary(0.5) '\x085\x00\x00\x00\x00\x00\x00\x00\x80' >>> _g.fbinary(-0.5) '\t5\x00\x00\x00\x00\x00\x00\x00\x80' >>> _g.fbinary(-2.0) '\t5\x00\x00\x00\x01\x00\x00\x00\x02' >>> _g.fbinary(2.0) '\x085\x00\x00\x00\x01\x00\x00\x00\x02' >>> prec=_g.set_minprec(0) >>> junk=_g.set_minprec(prec) >>> a.getrprec()==prec 1 >>> b.getrprec()==prec 1 >>> _g.mpf(1.0).getrprec()==prec 1 >>> hash(_g.mpf(23.0))==hash(23) 1 >>> print _g.mpf('\004',0,256) 0.0 >>> long(a) 123L >>> long(-a) -123L >>> int(a) 123 >>> int(-a) -123 >>> ''' def _test(chat=None): if chat: print "Unit tests for gmpy 0.8 pre-alpha (mpf functionality)" print " running on Python",sys.version print print "Testing gmpy %s (GMP %s) with default caching (%s, %s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache(), _g.get_qcache(), ) + _g.get_zconst()) thismod = sys.modules.get(__name__) doctest.testmod(thismod, report=0) if chat: print print "Overall results for mpf:" return doctest.master.summarize(chat) if __name__=='__main__': _test(1) --- NEW FILE: gmpy_test_08_mpq.py --- # partial unit test for gmpy 0.8 mpq functionality # relies on Tim Peters' "doctest.py" test-driver # test-version 0.8 r''' >>> dir(a) ['_copy', 'binary', 'denom', 'digits', 'numer', 'qdiv', 'sign'] >>> ''' import gmpy as _g, doctest,sys __test__={} a=_g.mpq('123/456') b=_g.mpq('789/123') __test__['elemop']=\ r''' >>> a+b mpq(41657,6232) >>> a-b mpq(-38295,6232) >>> a*b mpq(263,152) >>> a/b mpq(1681,39976) >>> b+a mpq(41657,6232) >>> b-a mpq(38295,6232) >>> b*a mpq(263,152) >>> b/a mpq(39976,1681) >>> a+1 mpq(193,152) >>> 1+a mpq(193,152) >>> a-1 mpq(-111,152) >>> 1-a mpq(111,152) >>> a*1 mpq(41,152) >>> 1*a mpq(41,152) >>> a/1 mpq(41,152) >>> 1/a mpq(152,41) >>> -a mpq(-41,152) >>> abs(-a) mpq(41,152) >>> _g.qsign(b-a) 1 >>> _g.qsign(b-b) 0 >>> _g.qsign(a-b) -1 >>> a.sign() 1 >>> (-a).sign() -1 >>> z=b-b; z.sign() 0 >>> an=a.numer(); ad=a.denom(); >>> an==0 or 1==a*_g.mpq(ad,an) 1 >>> bn=b.numer(); bd=b.denom(); >>> bn==0 or 1==b*_g.mpq(bd,bn) 1 >>> zn=z.numer(); zd=z.denom(); >>> zn==0 or 1==z*_g.mpq(zd,zn) 1 >>> (a+b) == _g.mpq(an*bd+ad*bn,ad*bd) 1 >>> (a+z) == _g.mpq(an*zd+ad*zn,ad*zd) 1 >>> (a+a) == _g.mpq(an*ad+ad*an,ad*ad) 1 >>> ''' __test__['cmpr']=\ r''' >>> c=_g.mpq(a) >>> c is a 1 >>> c==a 1 >>> c>a 0 >>> c<a 0 >>> d=a._copy() >>> a is d 0 >>> a == d 1 >>> cmp(a,c) 0 >>> cmp(a,b) -1 >>> a>b 0 >>> a<b 1 >>> not _g.mpq(0) 1 >>> not a 0 >>> coerce(a,1) (mpq(41,152), mpq(1)) >>> coerce(1,a) (mpq(1), mpq(41,152)) >>> coerce(a,1.0) (mpq(41,152), mpq(1)) >>> coerce(a,_g.mpz(1)) (mpq(41,152), mpq(1)) >>> coerce(a,_g.mpf(1)) (mpq(41,152), mpq(1)) >>> a>1 0 >>> a>1.0 0 >>> a<1 1 >>> a<1.0 1 >>> a==1 0 >>> a==1.0 0 >>> cmp(a,1) -1 >>> cmp(1.0,a) 1 >>> long(1/a) 3L >>> long(-1/a) -3L >>> int(1/a) 3 >>> int(-1/a) -3 >>> ''' __test__['format']=\ r''' >>> str(a) '41/152' >>> repr(a) 'mpq(41,152)' >>> a==eval(repr(a),_g.__dict__) 1 >>> str(-a) '-41/152' >>> repr(-a) 'mpq(-41,152)' >>> (-a)==eval(repr(-a),_g.__dict__) 1 >>> _g.set_tagoff(0) 1 >>> a gmpy.mpq(41,152) >>> _g.set_tagoff(1) 0 >>> for i in range(1,7): ... for j in range(3,10): ... if _g.mpq(i,j) != _g.mpq("%d/%d"%(i,j)): ... print 'er1:',i,j; break ... aa=_g.mpq(i,j); ai=aa.numer(); aj=aa.denom() ... if aj!=1 and str(aa) != ("%d/%d"%(ai,aj)): ... print 'er2:',i,j,str(aa),("%d/%d"%(ai,aj)); break ... if aj==1 and str(aa) != ("%d"%ai): ... print 'er3:',i,j,str(aa),"%d"%ai; break ... if aj!=1 and repr(aa) != ("mpq(%d,%d)"%(ai,aj)): ... print 'er4:',i,j,repr(aa),("mpq(%d,%d)"%(ai,aj)); break ... if aj==1 and repr(aa) != ("mpq(%d)"%ai): ... print 'er5:',i,j,repr(aa),"mpq(%d)"%ai; break >>> fmo='_g.mpq('+a.numer().digits(16)+','+a.denom().digits(16)+')' >>> fmo '_g.mpq(0x29,0x98)' >>> eval(fmo)==a 1 >>> fmo='_g.mpq("'+a.numer().digits(30)+'/'+a.denom().digits(30)+'",30)' >>> fmo '_g.mpq("1b/52",30)' >>> eval(fmo)==a 1 >>> _g.qdigits(a,30) '1b/52' >>> a.digits(30) '1b/52' >>> _g.mpq(1000*1000*1000*1000L,23) mpq(1000000000000L,23) >>> _g.mpq(23,1000*1000*1000*1000L) mpq(23,1000000000000L) >>> _g.mpq(23L**10L,1000L**4L) mpq(41426511213649L,1000000000000L) >>> _g.qbinary('pep') Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: argument can not be converted to mpq >>> ''' __test__['binio']=\ r''' >>> ba=a.binary() >>> len(ba) 6 >>> for i in range(len(ba)): ... print ord(ba[i]), ... if i==len(ba)-1: print ... 1 0 0 0 41 152 >>> _g.mpq(ba,256)==a 1 >>> ba == _g.qbinary(a) 1 >>> ba=(-a).binary() >>> len(ba) 6 >>> for i in range(len(ba)): ... print ord(ba[i]), ... if i==len(ba)-1: print ... 1 0 0 128 41 152 >>> _g.mpq(ba,256)==-a 1 >>> hash(a)==hash(_g.mpf(a))==hash(float(a)) 1 >>> hash(b)==hash(_g.mpf(b))==hash(float(b)) 1 >>> hash(-a)==hash(-_g.mpf(a))==hash(-float(a)) 1 >>> hash(-b)==hash(-_g.mpf(b))==hash(-float(b)) 1 >>> ''' __test__['power']=\ r''' >>> _g.mpq(2,3)**3 mpq(8,27) >>> _g.mpq(8,27)**_g.mpq('2/3') mpq(4,9) >>> _g.mpq(2,3)**-3 mpq(27,8) >>> _g.mpq(8,27)**_g.mpq('-2/3') mpq(9,4) >>> print float(_g.mpf('0.2')**2) 0.04 >>> print float(_g.mpf('0.2')**-2) 25.0 >>> _g.mpq(3)**3 == _g.mpz(3)**3 1 >>> (a**-7) == 1/(a**7) 1 >>> (b**5) == 1/(b**-5) 1 >>> ''' __test__['qdiv']=\ r''' >>> _g.qdiv(12,2) mpz(6) >>> _g.qdiv(12,5) mpq(12,5) >>> a is a.qdiv() 1 >>> a is a.qdiv(1) 1 >>> a is a.qdiv(2) 0 >>> x=a.numer() >>> x is _g.qdiv(x) 1 >>> x is _g.qdiv(x,1) 1 >>> x is _g.qdiv(x,2) 0 >>> y=_g.mpq(4,1) >>> y is y.qdiv() 0 >>> y == y.qdiv() 1 >>> ''' def _test(chat=None): if chat: print "Unit tests for gmpy 0.8 pre-alpha (mpq functionality)" print " running on Python",sys.version print print "Testing gmpy %s (GMP %s) with default caching (%s, %s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache(), _g.get_qcache(), ) + _g.get_zconst()) thismod = sys.modules.get(__name__) doctest.testmod(thismod, report=0) if chat: print "Repeating tests, with caching disabled" _g.set_qcache(0) sav = sys.stdout class _Dummy: def write(self,*whatever): pass try: sys.stdout = _Dummy() doctest.testmod(thismod, report=0) finally: sys.stdout = sav if chat: print print "Overall results for cvr:" return doctest.master.summarize(chat) if __name__=='__main__': _test(1) --- NEW FILE: gmpy_test_08_mpz.py --- # partial unit test for gmpy 0.8 mpz functionality # relies on Tim Peters' "doctest.py" test-driver # test-version 0.8 r''' >>> filter(lambda x: not x.startswith('_'), dir(_g)) ['binary', 'bincoef', 'ceil', 'comb', 'denom', 'digits', 'divm', 'f2q', 'fac', 'fbinary', 'fdigits', 'fib', 'floor', 'fsign', 'fsqrt', 'gcd', 'gcdext', 'get_qcache', 'get_zcache', 'get_zconst', 'getbit', 'getprec', 'getrprec', 'gmp_version', 'hamdist', 'invert', 'is_power', 'is_prime', 'is_square', 'jacobi', 'kronecker', 'lcm', 'legendre', 'lowbits', 'mpf', 'mpq', 'mpz', 'next_prime', 'numdigits', 'numer', 'popcount', 'qbinary', 'qdigits', 'qdiv', 'qsign', 'rand', 'reldiff', 'remove', 'root', 'scan0', 'scan1', 'set_callback', 'set_debug', 'set_fcoform', 'set_minprec', 'set_qcache', 'set_tagoff', 'set_zcache', 'set_zconst', 'setbit', 'sign', 'sqrt', 'sqrtrem', 'trunc', 'version'] >>> dir(a) ['_copy', 'binary', 'bincoef', 'comb', 'digits', 'getbit', 'hamdist', 'invert', 'is_power', 'is_prime', 'is_square', 'jacobi', 'kronecker', 'legendre', 'lowbits', 'next_prime', 'numdigits', 'popcount', 'qdiv', 'remove', 'root', 'scan0', 'scan1', 'setbit', 'sign', 'sqrt', 'sqrtrem'] >>> ''' import gmpy as _g, doctest,sys __test__={} a=_g.mpz(123) b=_g.mpz(456) def factorize(x): r''' >>> factorize(a) [3, 41] >>> factorize(b) [2, 2, 2, 3, 19] >>> ''' import gmpy as _g savex=x prime=2 x=_g.mpz(x) factors=[] while x>=prime: newx,mult=x.remove(prime) if mult: factors.extend([int(prime)]*mult) x=newx prime=_g.next_prime(prime) for factor in factors: assert _g.is_prime(factor) from operator import mul assert reduce(mul, factors)==savex return factors __test__['elemop']=\ r''' >>> a+b mpz(579) >>> a-b mpz(-333) >>> a*b mpz(56088) >>> a/b mpz(0) >>> a%b mpz(123) >>> b+a mpz(579) >>> b-a mpz(333) >>> b*a mpz(56088) >>> b%a mpz(87) >>> divmod(a,b) (mpz(0), mpz(123)) >>> divmod(b,a) (mpz(3), mpz(87)) >>> -a mpz(-123) >>> abs(-a)==a 1 >>> pow(a,7) mpz(425927596977747L) >>> pow(a,7,b) mpz(99) >>> _g.sign(b-a) 1 >>> _g.sign(b-b) 0 >>> _g.sign(a-b) -1 >>> a.sign() 1 >>> (-a).sign() -1 >>> z=b-b; z.sign() 0 >>> ''' __test__['cmpr']=\ r''' >>> c=_g.mpz(a) >>> c is a 1 >>> c==a 1 >>> c>a 0 >>> c<a 0 >>> d=a._copy() >>> a is d 0 >>> a == d 1 >>> cmp(a,c) 0 >>> cmp(a,b) -1 >>> a>b 0 >>> a<b 1 >>> not _g.mpz(0) 1 >>> not a 0 >>> coerce(a,1) (mpz(123), mpz(1)) >>> coerce(1,a) (mpz(1), mpz(123)) >>> coerce(a,1.0) (123.0, 1.0) >>> _g.mpz(3.14) mpz(3) >>> _g.mpz(_g.mpq(17,3)) mpz(5) >>> _g.mpz(23L) mpz(23) >>> _g.mpz(-23L) mpz(-23) >>> x=1000*1000*1000*1000L >>> _g.mpz(x) mpz(1000000000000L) >>> ''' __test__['bitops']=\ r''' >>> ~a mpz(-124) >>> a&b mpz(72) >>> a|b mpz(507) >>> a^b mpz(435) >>> a<<1 mpz(246) >>> a>>1 mpz(61) >>> a<<-1 Traceback (innermost last): File "<pyshell#42>", line 1, in ? a<<-1 ValueError: Pympz_lshift negative shift count >>> a>>-2 Traceback (innermost last): File "<pyshell#43>", line 1, in ? a>>-2 ValueError: Pympz_rshift negative shift count >>> a<<0 mpz(123) >>> a>>0 mpz(123) >>> a.popcount() 6 >>> _g.popcount(b) 4 >>> a.hamdist(b) 6 >>> a.lowbits(5) mpz(27) >>> b.lowbits(5) mpz(8) >>> b.lowbits(5)==(b%32) 1 >>> a.lowbits(5)==(a%32) 1 >>> a.setbit(20) mpz(1048699) >>> a.setbit(0,0) mpz(122) >>> for i in range(8): ... print a.getbit(i), ... if i==7: print ... 1 1 0 1 1 1 1 0 >>> for i in range(10): ... print b.getbit(i), ... if i==9: print ... 0 0 0 1 0 0 1 1 1 0 >>> i=0 >>> while 1: ... j = a.scan0(i) ... print j, ... if j is None or j>31: ... print ... break ... else: i = j + 1 ... 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 >>> i=0 >>> while 1: ... j = a.scan1(i) ... print j, ... if j is None or j>31: ... print ... break ... else: i = j + 1 ... 0 1 3 4 5 6 None >>> n=_g.mpz(-(7+6*16+5*256+7*4092)) >>> i=0 >>> while 1: ... j = n.scan0(i) ... print j, ... if j is None or j>31: ... print ... break ... else: i = j + 1 ... 2 4 5 7 9 11 15 None >>> i=0 >>> while 1: ... j = n.scan1(i) ... print j, ... if j is None or j>31: ... print ... break ... else: i = j + 1 ... 0 1 3 6 8 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 >>> ''' __test__['format']=\ r''' >>> str(a) '123' >>> repr(a) 'mpz(123)' >>> hex(a) '0x7b' >>> oct(a) '0173' >>> _g.mpz('123') mpz(123) >>> _g.mpz('1001001011',2) mpz(587) >>> _g.mpz('1001001011',2).digits(2) '1001001011' >>> for i in range(2,37): ... print a.digits(i), ... if i%6==0: print ... 1111011 11120 1323 443 323 234 0173 146 123 102 a3 96 8b 83 0x7b 74 6f 69 63 5i 5d 58 53 4n 4j 4f 4b 47 43 3u 3r 3o 3l 3i 3f >>> print a.digits(37) Traceback (innermost last): File "<stdin>", line 1, in ? print a.digits(37) ValueError: base must be either 0 or in the interval 2 ... 36 >>> _g.set_tagoff(0) 1 >>> a gmpy.mpz(123) >>> _g.set_tagoff(1) 0 >>> _g.mpz('43') mpz(43) >>> _g.mpz('043') mpz(43) >>> _g.mpz('43',0) mpz(43) >>> _g.mpz('043',0) mpz(35) >>> _g.mpz('0x43',0) mpz(67) >>> _g.mpz('0x43') Traceback (innermost last): File "<pyshell#181>", line 1, in ? _g.mpz('0x43') ValueError: invalid digits >>> ''' __test__['binio']=\ r''' >>> ba=a.binary() >>> ba '{' >>> _g.mpz(ba,256) mpz(123) >>> _g.mpz(ba,256)==a 1 >>> _g.binary(123) '{' >>> z=_g.mpz('melancholy',256) >>> z mpz(573406620562849222387053L) >>> divmod(z,a) (mpz(4661842443600400182008L), mpz(69)) >>> for i in range(2,37): ... print i,z.numdigits(i), ... if i%6==0: print ... 2 79 3 50 4 40 5 35 6 31 7 29 8 27 9 25 10 24 11 23 12 23 13 22 14 21 15 21 16 20 17 20 18 19 19 19 20 19 21 18 22 18 23 18 24 18 25 18 26 17 27 17 28 17 29 17 30 17 31 16 32 16 33 16 34 16 35 16 36 16 >>> _g.numdigits(23) 2 >>> _g.numdigits(23,2) 5 >>> _g.numdigits(23,99) Traceback (most recent call last): File "<string>", line 1, in ? ValueError: base must be either 0 or in the interval 2 ... 36 >>> hash(a) 123 >>> hash(b) 456 >>> hash(z) -791330233 >>> _g.mpz(_g.binary(-123),256) mpz(-123) >>> long(_g.mpz(-3)) -3L >>> ''' __test__['number']=\ r''' >>> print a.sqrt(), b.sqrt() 11 21 >>> print a.sqrtrem(), b.sqrtrem() (mpz(11), mpz(2)) (mpz(21), mpz(15)) >>> for i in range(5): ... print a.root(i+1),b.root(i+1) ... (mpz(123), 1) (mpz(456), 1) (mpz(11), 0) (mpz(21), 0) (mpz(4), 0) (mpz(7), 0) (mpz(3), 0) (mpz(4), 0) (mpz(2), 0) (mpz(3), 0) >>> a.is_square() 0 >>> a.is_power() 0 >>> _g.is_square(99*99) 1 >>> _g.is_square(99*99*99) 0 >>> _g.is_power(99*99*99) 1 >>> _g.gcd(a,b) mpz(3) >>> _g.gcdext(a,b) (mpz(3), mpz(-63), mpz(17)) >>> _g.lcm(a,b) mpz(18696) >>> _g.fac(7) mpz(5040) >>> _g.fib(17) mpz(1597) >>> for i in range(10): ... print _g.bincoef(10,i) ... 1 10 45 120 210 252 210 120 45 10 >>> _g.divm(b,a,20) mpz(12) >>> _g.divm(a,b,100) Traceback (innermost last): File "<pyshell#184>", line 1, in ? _g.divm(a,b,100) ZeroDivisionError: not invertible >>> _g.divm(0,1,2) mpz(0) >>> a.invert(100) mpz(87) >>> b.invert(100) mpz(0) >>> _g.comb(3,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: binomial coefficient with negative k >>> _g.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: sqrt of negative number >>> _g.sqrtrem(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: sqrt of negative number >>> _g.remove(3,-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: factor must be > 0 >>> _g.is_prime(3,-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: repetition count for is_prime must be positive >>> _g.jacobi(10,3) 1 >>> _g.jacobi(10,-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: jacobi's y must be odd prime > 0 >>> _g.legendre(10,3) 1 >>> _g.legendre(10,-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: legendres's y must be odd and > 0 >>> _g.kronecker(10,3) 1 >>> _g.kronecker(10,-3) 1 >>> a=1000000L**2 >>> b=a+1 >>> _g.jacobi(a,b) 1 >>> _g.legendre(a,b) 1 >>> _g.kronecker(a,b) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Either arg in Kronecker must fit in an int >>> f=_g.mpf(3.3) >>> f.setprec(-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: n must be >=0 >>> _g.rand('init',-1) >>> _g.rand('init',-7) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: size must be in 1..128 >>> _g.rand('init',200) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: size must be in 1..128 >>> _g.rand('qual') 32 >>> _g.rand('floa',-7) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: 'floa' needs arg>=0 ''' def _test(chat=None): if chat: print "Unit tests for gmpy 0.8 pre-alpha (mpz functionality)" print " running on Python",sys.version print print "Testing gmpy %s (GMP %s) with default caching (%s, %s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache(), _g.get_qcache(), ) + _g.get_zconst()) thismod = sys.modules.get(__name__) doctest.testmod(thismod, report=0) if chat: print "Repeating tests, with caching disabled" _g.set_zcache(0) _g.set_zconst(0,0) sav = sys.stdout class _Dummy: def write(self,*whatever): pass try: sys.stdout = _Dummy() doctest.testmod(thismod, report=0) finally: sys.stdout = sav if chat: print print "Overall results for cvr:" return doctest.master.summarize(chat) if __name__=='__main__': _test(1) --- NEW FILE: gmpy_test_08_rnd.py --- # partial unit test for gmpy 0.8 rand functionality # relies on Tim Peters' "doctest.py" test-driver # test-version 0.8 r''' >>> r <built-in function rand> >>> ''' import gmpy as _g, doctest,sys __test__={} r = _g.rand __test__['rand']=\ r''' >>> r('init',20) >>> r('seed',1234) >>> for i in range(5): ... print r('next',100), ... if i==4: print ... 21 75 63 28 27 >>> alis=list("proktelnu") >>> for i in range(10): ... r('shuf',alis) ... print ''.join(alis) ... rtoulpnke eoturlknp plnuetokr ekoprulnt kpoutnrel rutoneklp ukeptnorl onkrlpteu lknteropu enrkutlpo >>> sav=r('save') >>> print sav 774447212137 >>> for i in range(5): ... r('shuf',alis) ... print ''.join(alis) ... elnuortpk enutolpkr eropulntk plroutenk ekonrtplu >>> r('seed',sav) >>> for i in range(5): ... r('shuf',alis) ... print ''.join(alis) ... epkruotln ekrtuplno eoulrpktn lpourtekn enukotlpr >>> r('seed',sav) >>> for i in range(3): ... print float(r('floa')) ... 0.44833278656 0.547296524048 0.895370483398 >>> r('seed',sav) >>> for i in range(3): ... print float(r('floa',6)) ... 0.484375 0.90625 0.75 >>> r('seed',sav) >>> for i in range(3): ... print _g.f2q(r('floa',6),-6) ... 15/31 9/10 3/4 >>> r('seed',sav) >>> for i in range(3): ... print _g.f2q(r('floa',6)) ... 31/64 29/32 3/4 >>> r('seed',sav) >>> for i in range(5): ... r('shuf',alis) ... print ''.join(alis) ... elnorutpk enotrlpku eurpolntk plurotenk ekrnutplo >>> ''' def _test(chat=None): if chat: print "Unit tests for gmpy 0.8 pre-alpha (rand functionality)" print " running on Python",sys.version print print "Testing gmpy %s (GMP %s) with default caching (%s, %s..%s)" % ( (_g.version(), _g.gmp_version(), _g.get_zcache() ) + _g.get_zconst()) thismod = sys.modules.get(__name__) doctest.testmod(thismod, report=0) if chat: print print "Overall results for rnd:" return doctest.master.summarize(chat) if __name__=='__main__': _test(1) --- NEW FILE: timing1.py --- import gmpy, time def timedsum(n, zero): start=time.clock() tot=zero for i in range(n): tot+=i stend=time.clock() return type(zero), stend-start, tot def timedmul(n, one): start=time.clock() tot=one for i in range(n): tot*=(i+1) stend=time.clock() return type(one), stend-start, tot def test(n=10*1000): print "Sum of %d items of various types:" % n for z in 0L, 0.0, gmpy.mpz(0), gmpy.mpf(0): tip, tim, tot = timedsum(n, z) print " %5.3f %.0f %s" % (tim, float(tot), tip) print "Mul of %d items of various types:" % n for z in 1L, 1.0, gmpy.mpz(1), gmpy.mpf(1): tip, tim, tot = timedmul(n, z) print " %5.3f %s" % (tim, tip) if __name__=='__main__': test() --- NEW FILE: timing2.py --- import gmpy, time def timedfib(n, zero): start=time.clock() a=zero; b=a+1 for i in range(n): a,b=b,a+b stend=time.clock() return type(zero), stend-start, a def timedfibsp(n, one): start=time.clock() result=gmpy.fib(n) stend=time.clock() return type(one), stend-start, result def test(n=10*1000): print "%dth Fibonacci number of various types:" % n for z in 0L, gmpy.mpz(0), gmpy.mpf(0): tip, tim, tot = timedfib(n, z) print " %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), tip) tip, tim, tot = timedfibsp(n, 1) print " %5.3f %s %s" % (tim, gmpy.fdigits(tot,10,6), "gmpy.fib") if __name__=='__main__': test() --- NEW FILE: timing3.py --- import gmpy as _g import time print "Typical expected results",""" D:\PySym>python timing.py Factorial of 10 took 9.97333181359e-005 (7 digits) Fibonacci of 10 took 9.88952230255e-005 (2 digits) Factorial of 100 took 0.000121523791006 (159 digits) Fibonacci of 100 took 9.05142719216e-005 (21 digits) Factorial of 1000 took 0.00173569497361 (2568 digits) Fibonacci of 1000 took 0.000138285693214 (209 digits) Factorial of 10000 took 0.0619989238859 (35660 digits) Fibonacci of 10000 took 0.000744228458022 (2090 digits) Factorial of 100000 took 4.44311764676 (456574 digits) Fibonacci of 100000 took 0.022344453738 (20899 digits) Factorial of 1000000 took 152.151135367 (5565709 digits) Fibonacci of 1000000 took 0.670207059778 (208988 digits) """ for i in (10,100,1000,10000,100000,1000000): start=time.clock() x=_g.fac(i) stend=time.clock() print "Factorial of %d took %s (%d digits)" % ( i, stend-start, x.numdigits()) start=time.clock() x=_g.fib(i) stend=time.clock() print "Fibonacci of %d took %s (%d digits)" % ( i, stend-start, x.numdigits()) |