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

/test3/gmpy_test_thr.py

http://gmpy.googlecode.com/
Python | 126 lines | 120 code | 3 blank | 3 comment | 2 complexity | 0b6aeb6c200875864aefd96aed87ebc9 MD5 | raw file
  1# partial unit test for gmpy2 threaded mpz functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3
  4import gmpy2 as _g, doctest, sys, operator, gc, queue, threading
  5from functools import reduce
  6__test__={}
  7
  8def _tf(N=2, _K=1234**5678):
  9    """Takes about 100ms on a first-generation Macbook Pro"""
 10    for i in range(N): assert (_g.mpz(1234)**5678)==_K
 11a=_g.mpz(123)
 12b=_g.mpz(456)
 13c=_g.mpz(123456789123456789)
 14
 15def factorize(x=c):
 16    r'''
 17    (Takes about 25ms, on c, on a first-generation Macbook Pro)
 18    >>> factorize(a)
 19    [3, 41]
 20    >>> factorize(b)
 21    [2, 2, 2, 3, 19]
 22    >>>
 23    '''
 24    import gmpy2 as _g
 25    savex=x
 26    prime=2
 27    x=_g.mpz(x)
 28    factors=[]
 29    while x>=prime:
 30        newx,mult=x.remove(prime)
 31        if mult:
 32            factors.extend([int(prime)]*mult)
 33            x=newx
 34        prime=_g.next_prime(prime)
 35    for factor in factors: assert _g.is_prime(factor)
 36    from operator import mul
 37    assert reduce(mul, factors)==savex
 38    return factors
 39
 40def elemop(N=1000):
 41    r'''
 42    (Takes about 40ms on a first-generation Macbook Pro)
 43    '''
 44    for i in range(N):
 45        assert a+b == 579
 46        assert a-b == -333
 47        assert b*a == a*b == 56088
 48        assert b%a == 87
 49        assert divmod(a, b) == (0, 123)
 50        assert divmod(b, a) == (3, 87)
 51        assert -a == -123
 52        assert pow(a, 10) == 792594609605189126649
 53        assert pow(a, 7, b) == 99
 54        assert cmp(a, b) == -1
 55        assert '7' in str(c)
 56        assert '0' not in str(c)
 57        assert a.sqrt() == 11
 58        assert _g.lcm(a, b) == 18696
 59        assert _g.fac(7) == 5040
 60        assert _g.fib(17) == 1597
 61        assert _g.divm(b, a, 20) == 12
 62        assert _g.divm(4, 8, 20) == 3
 63        assert _g.divm(4, 8, 20) == 3
 64        assert _g.mpz(20) == 20
 65        assert _g.mpz(8) == 8
 66        assert _g.mpz(4) == 4
 67        assert a.invert(100) == 87
 68
 69def _test(chat=None):
 70    if chat:
 71        print("Unit tests for gmpy2 (threading)")
 72        print("    on Python %s" % sys.version)
 73        print("Testing gmpy2 {0}".format(_g.version()))
 74        print("  Mutliple-precision library:   {0}".format(_g.mp_version()))
 75        print("  Floating-point library:       {0}".format(_g.mpfr_version()))
 76        print("  Complex library:              {0}".format(_g.mpc_version()))
 77        print("  Caching Values: (Number)      {0}".format(_g.get_cache()[0]))
 78        print("  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
 79
 80    thismod = sys.modules.get(__name__)
 81    doctest.testmod(thismod, report=0)
 82
 83    if chat: print("Repeating tests, with caching disabled")
 84    _g.set_cache(0,128)
 85
 86    sav = sys.stdout
 87    class _Dummy:
 88        def write(self,*whatever):
 89            pass
 90    try:
 91        sys.stdout = _Dummy()
 92        doctest.testmod(thismod, report=0)
 93    finally:
 94        sys.stdout = sav
 95
 96    if chat:
 97        print()
 98        print("Overall results for thr:")
 99    return doctest.master.summarize(chat)
100
101class DoOne(threading.Thread):
102    def __init__(self, q):
103        threading.Thread.__init__(self)
104        self.q = q
105    def run(self):
106        while True:
107            task = self.q.get()
108            if task is None: break
109            task()
110
111def _test_thr(Ntasks=5, Nthreads=1):
112    q = queue.Queue()
113    funcs = (_tf, 1), (factorize, 4), (elemop, 2)
114    for i in range(Ntasks):
115        for f, n in funcs:
116            for x in range(n):
117                q.put(f)
118    for i in range(Nthreads):
119        q.put(None)
120    thrs = [DoOne(q) for i in range(Nthreads)]
121    for t in thrs: t.start()
122    for t in thrs: t.join()
123
124if __name__=='__main__':
125    _test(1)
126