1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
# partial unit test for gmpy rand functionality
# relies on Tim Peters' "doctest.py" test-driver
r'''
>>> r
<built-in function rand>
>>>
'''
import gmpy as _g, doctest,sys
__test__={}
r = _g.rand
__test__['rand']=\
r'''
>>> r('error',1,2,3)
Traceback (most recent call last):
...
TypeError: function takes exactly 2 arguments (4 given)
>>> r('save')
Traceback (most recent call last):
...
RuntimeError: can't save before init
>>> r('init',20)
>>> r('unkn',99)
Traceback (most recent call last):
...
ValueError: unknown option 'unkn'
>>> r('seed',1234)
>>> for i in range(5):
... print(r('next',100))
...
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("%.12f" % float(r('floa')))
...
0.448332786560
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
>>> try: r('shuf','astring')
... except TypeError as e: print(int("does not support item assignment" in str(e)))
1
>>> r('shuf',23)
Traceback (most recent call last):
...
TypeError: 'shuf' needs mutable sequence
>>>
'''
# adapt to python 2.3's slightly different error message in an exception
import sys
if sys.version<'2.4':
__test__['rand'] = __test__['rand'].replace("does not", "doesn't")
def _test(chat=None):
if chat:
print("Unit tests for gmpy 1.15 (rand functionality)")
print(" running on Python %s" % sys.version)
print("")
if _g.gmp_version():
print("Testing gmpy %s (GMP %s) with default caching (%s, %s)" % (
(_g.version(), _g.gmp_version(), _g.get_cache()[0],
_g.get_cache()[1])))
else:
print("Testing gmpy %s (MPIR %s) with default caching (%s, %s)" % (
(_g.version(), _g.mpir_version(), _g.get_cache()[0],
_g.get_cache()[1])))
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)
|