1# partial unit test for gmpy rand functionality 2# relies on Tim Peters' "doctest.py" test-driver 3 4r''' 5>>> r 6<built-in function rand> 7>>> 8''' 9 10import gmpy as _g, doctest,sys 11__test__={} 12r = _g.rand 13 14__test__['rand']=\ 15r''' 16>>> r('error',1,2,3) 17Traceback (most recent call last): 18 ... 19TypeError: function takes exactly 2 arguments (4 given) 20>>> r('save') 21Traceback (most recent call last): 22 ... 23RuntimeError: can't save before init 24>>> r('init',20) 25>>> r('unkn',99) 26Traceback (most recent call last): 27 ... 28ValueError: unknown option 'unkn' 29>>> r('seed',1234) 30>>> for i in range(5): 31... print(r('next',100)) 32... 3321 3475 3563 3628 3727 38>>> alis=list("proktelnu") 39>>> for i in range(10): 40... r('shuf',alis) 41... print(''.join(alis)) 42... 43rtoulpnke 44eoturlknp 45plnuetokr 46ekoprulnt 47kpoutnrel 48rutoneklp 49ukeptnorl 50onkrlpteu 51lknteropu 52enrkutlpo 53>>> sav=r('save') 54>>> print(sav) 55774447212137 56>>> for i in range(5): 57... r('shuf',alis) 58... print(''.join(alis)) 59... 60elnuortpk 61enutolpkr 62eropulntk 63plroutenk 64ekonrtplu 65>>> r('seed',sav) 66>>> for i in range(5): 67... r('shuf',alis) 68... print(''.join(alis)) 69... 70epkruotln 71ekrtuplno 72eoulrpktn 73lpourtekn 74enukotlpr 75>>> r('seed',sav) 76>>> for i in range(3): 77... print("%.12f" % float(r('floa'))) 78... 790.448332786560 800.547296524048 810.895370483398 82>>> r('seed',sav) 83>>> for i in range(3): 84... print(float(r('floa',6))) 85... 860.484375 870.90625 880.75 89>>> r('seed',sav) 90>>> for i in range(3): 91... print(_g.f2q(r('floa',6),-6)) 92... 9315/31 949/10 953/4 96>>> r('seed',sav) 97>>> for i in range(3): 98... print(_g.f2q(r('floa',6))) 99... 10031/64 10129/32 1023/4 103>>> r('seed',sav) 104>>> for i in range(5): 105... r('shuf',alis) 106... print(''.join(alis)) 107... 108elnorutpk 109enotrlpku 110eurpolntk 111plurotenk 112ekrnutplo 113>>> try: r('shuf','astring') 114... except TypeError as e: print(int("does not support item assignment" in str(e))) 1151 116>>> r('shuf',23) 117Traceback (most recent call last): 118 ... 119TypeError: 'shuf' needs mutable sequence 120>>> 121''' 122 123# adapt to python 2.3's slightly different error message in an exception 124import sys 125if sys.version<'2.4': 126 __test__['rand'] = __test__['rand'].replace("does not", "doesn't") 127 128def _test(chat=None): 129 if chat: 130 print("Unit tests for gmpy 1.17 (rand functionality)") 131 print(" running on Python %s" % sys.version) 132 print("") 133 if _g.gmp_version(): 134 print("Testing gmpy %s (GMP %s) with default caching (%s, %s)" % ( 135 (_g.version(), _g.gmp_version(), _g.get_cache()[0], 136 _g.get_cache()[1]))) 137 else: 138 print("Testing gmpy %s (MPIR %s) with default caching (%s, %s)" % ( 139 (_g.version(), _g.mpir_version(), _g.get_cache()[0], 140 _g.get_cache()[1]))) 141 142 thismod = sys.modules.get(__name__) 143 doctest.testmod(thismod, report=0) 144 145 if chat: 146 print("") 147 print("Overall results for rnd:") 148 return doctest.master.summarize(chat) 149 150 151if __name__=='__main__': 152 _test(1) 153 154