PageRenderTime 93ms CodeModel.GetById 64ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test3/gmpy_test_mpz.py

http://gmpy.googlecode.com/
Python | 967 lines | 963 code | 0 blank | 4 comment | 0 complexity | 7bf893f396aded8043f1750ca43ed50a MD5 | raw file
  1# partial unit test for gmpy2 mpz functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3r'''
  4>>> list(filter(lambda x: not x.startswith('_'), dir(_g)))
  5['Default', 'DivisionByZeroError', 'ExponentOutOfBoundsError', 'InexactResultError', 'InvalidOperationError', 'OverflowResultError', 'RangeError', 'RoundAwayZero', 'RoundDown', 'RoundToNearest', 'RoundToZero', 'RoundUp', 'UnderflowResultError', 'acos', 'acosh', 'add', 'agm', 'ai', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'bincoef', 'bit_clear', 'bit_flip', 'bit_length', 'bit_mask', 'bit_scan0', 'bit_scan1', 'bit_set', 'bit_test', 'c_div', 'c_div_2exp', 'c_divmod', 'c_divmod_2exp', 'c_mod', 'c_mod_2exp', 'cbrt', 'ceil', 'check_range', 'comb', 'const_catalan', 'const_euler', 'const_log2', 'const_pi', 'context', 'copy_sign', 'cos', 'cosh', 'cot', 'coth', 'csc', 'csch', 'degrees', 'denom', 'digamma', 'digits', 'div', 'div_2exp', 'divexact', 'divm', 'eint', 'erf', 'erfc', 'exp', 'exp10', 'exp2', 'expm1', 'f2q', 'f_div', 'f_div_2exp', 'f_divmod', 'f_divmod_2exp', 'f_mod', 'f_mod_2exp', 'fac', 'factorial', 'fib', 'fib2', 'floor', 'fma', 'fmod', 'fms', 'frac', 'frexp', 'from_binary', 'fsum', 'gamma', 'gcd', 'gcdext', 'get_cache', 'get_context', 'get_emax_max', 'get_emin_min', 'get_exp', 'get_max_precision', 'hamdist', 'hypot', 'ieee', 'inf', 'invert', 'iroot', 'iroot_rem', 'is_bpsw_prp', 'is_euler_prp', 'is_even', 'is_extra_strong_lucas_prp', 'is_fermat_prp', 'is_fibonacci_prp', 'is_finite', 'is_inf', 'is_infinite', 'is_integer', 'is_lessgreater', 'is_lucas_prp', 'is_nan', 'is_number', 'is_odd', 'is_power', 'is_prime', 'is_regular', 'is_selfridge_prp', 'is_signed', 'is_square', 'is_strong_bpsw_prp', 'is_strong_lucas_prp', 'is_strong_prp', 'is_strong_selfridge_prp', 'is_unordered', 'is_zero', 'isqrt', 'isqrt_rem', 'j0', 'j1', 'jacobi', 'jn', 'kronecker', 'lcm', 'legendre', 'lgamma', 'li2', 'license', 'lngamma', 'local_context', 'log', 'log10', 'log1p', 'log2', 'lucas', 'lucas2', 'lucasu', 'lucasu_mod', 'lucasv', 'lucasv_mod', 'max2', 'maxnum', 'min2', 'minnum', 'modf', 'mp_limbsize', 'mp_version', 'mpc', 'mpc_random', 'mpc_version', 'mpfr', 'mpfr_from_old_binary', 'mpfr_grandom', 'mpfr_random', 'mpfr_version', 'mpq', 'mpq_from_old_binary', 'mpz', 'mpz_from_old_binary', 'mpz_random', 'mpz_rrandomb', 'mpz_urandomb', 'mul', 'mul_2exp', 'nan', 'next_above', 'next_below', 'next_prime', 'next_toward', 'norm', 'num_digits', 'numer', 'pack', 'phase', 'polar', 'popcount', 'powmod', 'proj', 'qdiv', 'radians', 'random_state', 'rec_sqrt', 'rect', 'reldiff', 'remainder', 'remove', 'remquo', 'rint', 'rint_ceil', 'rint_floor', 'rint_round', 'rint_trunc', 'root', 'round2', 'round_away', 'sec', 'sech', 'set_cache', 'set_context', 'set_exp', 'set_sign', 'sign', 'sin', 'sin_cos', 'sinh', 'sinh_cosh', 'sqrt', 'square', 'sub', 't_div', 't_div_2exp', 't_divmod', 't_divmod_2exp', 't_mod', 't_mod_2exp', 'tan', 'tanh', 'to_binary', 'trunc', 'unpack', 'version', 'xbit_mask', 'xmpz', 'y0', 'y1', 'yn', 'zero', 'zeta']
  6>>> list([x for x in dir(a) if x != '__dir__'])
  7['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__index__', '__init__', '__int__', '__invert__', '__ipow__', '__irshift__', '__isub__', '__le__', '__len__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_clear', 'bit_flip', 'bit_length', 'bit_scan0', 'bit_scan1', 'bit_set', 'bit_test', 'denominator', 'digits', 'num_digits', 'numerator']
  8>>>
  9'''
 10
 11import gmpy2 as _g, doctest, sys, operator, gc
 12__test__={}
 13a=_g.mpz(123)
 14b=_g.mpz(456)
 15
 16# Disable tests since they are not reliable with Python 3.1 but left behind
 17# in case it is needed in the future.
 18
 19if sys.platform in ('__DISABLE__linux2', '__DISABLE__darwin'):
 20  def _memsize():
 21    """ this function tries to return a measurement of how much memory
 22        this process is consuming (if it doesn't manage to, it returns 0).
 23    """
 24    import os
 25    try: x = int(os.popen('ps -p %d -o vsz|tail -1' % os.getpid()).read())
 26    except: x = 0
 27    return x
 28else:
 29  def _memsize():
 30    return 0
 31
 32def factorize(x):
 33    r'''
 34    >>> factorize(a)
 35    [3, 41]
 36    >>> factorize(b)
 37    [2, 2, 2, 3, 19]
 38    >>>
 39    '''
 40    savex=x
 41    prime=2
 42    x=_g.mpz(x)
 43    factors=[]
 44    while x>=prime:
 45        newx,mult=_g.remove(x,prime)
 46        if mult:
 47            factors.extend([int(prime)]*mult)
 48            x=newx
 49        prime=_g.next_prime(prime)
 50    for factor in factors: assert _g.is_prime(factor)
 51    from operator import mul
 52    from functools import reduce
 53    assert reduce(mul, factors)==savex
 54    return factors
 55
 56__test__['index']=\
 57r'''
 58>>> range(333)[a]
 59123
 60>>> range(333)[b]
 61Traceback (innermost last):
 62  ...
 63IndexError: range object index out of range
 64'''
 65
 66__test__['elemop']=\
 67r'''
 68>>> a+b
 69mpz(579)
 70>>> a-b
 71mpz(-333)
 72>>> a*b
 73mpz(56088)
 74>>> a//b
 75mpz(0)
 76>>> a/b
 77mpfr('0.26973684210526316')
 78>>> b//a
 79mpz(3)
 80>>> b/a
 81mpfr('3.7073170731707319')
 82>>> a%b
 83mpz(123)
 84>>> 0%b
 85mpz(0)
 86>>> b+a
 87mpz(579)
 88>>> b-a
 89mpz(333)
 90>>> b*a
 91mpz(56088)
 92>>> b%a
 93mpz(87)
 94>>> divmod(a,b)
 95(mpz(0), mpz(123))
 96>>> divmod(b,a)
 97(mpz(3), mpz(87))
 98>>> divmod(0,b)
 99(mpz(0), mpz(0))
100>>> -a
101mpz(-123)
102>>> a+1
103mpz(124)
104>>> a+(-1)
105mpz(122)
106>>> (-1)+a
107mpz(122)
108>>> 1+a
109mpz(124)
110>>> a-1
111mpz(122)
112>>> a-(-1)
113mpz(124)
114>>> 1-a
115mpz(-122)
116>>> (-1)-a
117mpz(-124)
118>>> a+True
119mpz(124)
120>>> a+False
121mpz(123)
122>>> a*False
123mpz(0)
124>>> a//True
125mpz(123)
126>>> abs(-a)==a
1271
128>>> pow(a,10)
129mpz(792594609605189126649)
130>>> pow(a,7,b)
131mpz(99)
132>>> _g.sign(b-a)
1331
134>>> _g.sign(b-b)
1350
136>>> _g.sign(a-b)
137-1
138>>> _g.sign(a)
1391
140>>> _g.sign(-a)
141-1
142>>> z=b-b; _g.sign(z)
1430
144>>> import pickle
145>>> pickle.loads(pickle.dumps(_g.mpz(12346789)))
146mpz(12346789)
147>>> s='12345678901234567890123456789'
148>>> int(s) == _g.mpz(s)
149True
150>>> _g.mpz(s) == int(s)
151True
152>>> del s
153>>> _g.is_even(a)
154False
155>>> _g.is_odd(a)
156True
157>>> _g.is_even(b)
158True
159>>> _g.is_odd(b)
160False
161>>> _g.is_even(2)
162True
163'''
164
165__test__['special'] = \
166r'''
167>>> _g.context().trap_divzero == False
168True
169>>> a == float('Inf')
170False
171>>> a != float('Inf')
172True
173>>> a > float('Inf')
174False
175>>> a >= float('Inf')
176False
177>>> a < float('Inf')
178True
179>>> a <= float('Inf')
180True
181>>> a == float('-Inf')
182False
183>>> a != float('-Inf')
184True
185>>> a > float('-Inf')
186True
187>>> a >= float('-Inf')
188True
189>>> a < float('-Inf')
190False
191>>> a <= float('-Inf')
192False
193>>> a == float('nan')
194False
195>>> a != float('nan')
196True
197>>> a > float('nan')
198False
199>>> a >= float('nan')
200False
201>>> a < float('nan')
202False
203>>> a <= float('nan')
204False
205>>> float('Inf') == a
206False
207>>> float('Inf') != a
208True
209>>> float('Inf') > a
210True
211>>> float('Inf') >= a
212True
213>>> float('Inf') < a
214False
215>>> float('Inf') <= a
216False
217>>> float('-Inf') == a
218False
219>>> float('-Inf') != a
220True
221>>> float('-Inf') > a
222False
223>>> float('-Inf') >= a
224False
225>>> float('-Inf') < a
226True
227>>> float('-Inf') <= a
228True
229>>> float('nan') == a
230False
231>>> float('nan') != a
232True
233>>> float('nan') > a
234False
235>>> float('nan') >= a
236False
237>>> float('nan') < a
238False
239>>> float('nan') <= a
240False
241>>> a + float('Inf')
242mpfr('inf')
243>>> float('Inf') + a
244mpfr('inf')
245>>> a + float('-Inf')
246mpfr('-inf')
247>>> float('-Inf') + a
248mpfr('-inf')
249>>> a + float('nan')
250mpfr('nan')
251>>> float('nan') + a
252mpfr('nan')
253>>> a - float('Inf')
254mpfr('-inf')
255>>> float('Inf') - a
256mpfr('inf')
257>>> a - float('-Inf')
258mpfr('inf')
259>>> float('-Inf') - a
260mpfr('-inf')
261>>> a - float('nan')
262mpfr('nan')
263>>> float('nan') - a
264mpfr('nan')
265>>> a * float('Inf')
266mpfr('inf')
267>>> float('Inf') * a
268mpfr('inf')
269>>> a * float('-Inf')
270mpfr('-inf')
271>>> float('-Inf') * a
272mpfr('-inf')
273>>> -a * float('Inf')
274mpfr('-inf')
275>>> float('Inf') * -a
276mpfr('-inf')
277>>> -a * float('-Inf')
278mpfr('inf')
279>>> float('-Inf') * -a
280mpfr('inf')
281>>> a * float('nan')
282mpfr('nan')
283>>> float('nan') * a
284mpfr('nan')
285>>> _g.mpz(0) * float('Inf')
286mpfr('nan')
287>>> _g.mpz(0) * float('-Inf')
288mpfr('nan')
289>>> float('Inf') * _g.mpz(0)
290mpfr('nan')
291>>> float('-Inf') * _g.mpz(0)
292mpfr('nan')
293>>> a / float('Inf')
294mpfr('0.0')
295>>> -a / float('Inf')
296mpfr('-0.0')
297>>> float('Inf') / a
298mpfr('inf')
299>>> float('Inf') / -a
300mpfr('-inf')
301>>> a / float('-Inf')
302mpfr('-0.0')
303>>> -a / float('-Inf')
304mpfr('0.0')
305>>> float('-Inf') / a
306mpfr('-inf')
307>>> float('-Inf') / -a
308mpfr('inf')
309>>> a / float('nan')
310mpfr('nan')
311>>> float('nan') / a
312mpfr('nan')
313>>> float('nan') / _g.mpz(0)
314mpfr('nan')
315>>> float('nan') / _g.mpz(0)
316mpfr('nan')
317>>> divmod(a, float('Inf'))
318(mpfr('0.0'), mpfr('123.0'))
319>>> divmod(a, float('-Inf'))
320(mpfr('-1.0'), mpfr('-inf'))
321>>> divmod(-a, float('Inf'))
322(mpfr('-1.0'), mpfr('inf'))
323>>> divmod(-a, float('-Inf'))
324(mpfr('0.0'), mpfr('-123.0'))
325>>> divmod(a, float('nan'))
326(mpfr('nan'), mpfr('nan'))
327>>> divmod(-a, float('nan'))
328(mpfr('nan'), mpfr('nan'))
329>>> divmod(_g.mpz(0), float('Inf'))
330(mpfr('0.0'), mpfr('0.0'))
331>>> divmod(_g.mpz(0), float('-Inf'))
332(mpfr('-0.0'), mpfr('-0.0'))
333>>> divmod(_g.mpz(0), float('nan'))
334(mpfr('nan'), mpfr('nan'))
335>>> divmod(float('Inf'), a)
336(mpfr('nan'), mpfr('nan'))
337>>> divmod(float('-Inf'), a)
338(mpfr('nan'), mpfr('nan'))
339>>> divmod(float('Inf'), -a)
340(mpfr('nan'), mpfr('nan'))
341>>> divmod(float('-Inf'), -a)
342(mpfr('nan'), mpfr('nan'))
343>>> divmod(float('nan'), a)
344(mpfr('nan'), mpfr('nan'))
345>>> divmod(float('nan'), -a)
346(mpfr('nan'), mpfr('nan'))
347>>> divmod(float('Inf'), _g.mpz(0))
348(mpfr('nan'), mpfr('nan'))
349>>> divmod(float('-Inf'), _g.mpz(0))
350(mpfr('nan'), mpfr('nan'))
351>>> divmod(float('nan'), _g.mpz(0))
352(mpfr('nan'), mpfr('nan'))
353>>> divmod(_g.mpfr('Inf'), _g.mpz(0))
354(mpfr('nan'), mpfr('nan'))
355>>> divmod(_g.mpfr('-Inf'), _g.mpz(0))
356(mpfr('nan'), mpfr('nan'))
357>>> divmod(_g.mpfr('nan'), _g.mpz(0))
358(mpfr('nan'), mpfr('nan'))
359'''
360
361__test__['divexact']=\
362r'''
363>>> a=_g.mpz('1234567912345678912345679')
364>>> b=_g.mpz('789789789789789789789789')
365>>> c=a*b
366>>> _g.divexact(c,a)
367mpz(789789789789789789789789)
368>>>
369'''
370
371__test__['divmod']=\
372r'''
373>>> _g.c_divmod(17,5)
374(mpz(4), mpz(-3))
375>>> _g.c_divmod(-17,5)
376(mpz(-3), mpz(-2))
377>>> _g.c_divmod(17,-5)
378(mpz(-3), mpz(2))
379>>> _g.c_divmod(-17,-5)
380(mpz(4), mpz(3))
381>>> _g.f_divmod(17,5)
382(mpz(3), mpz(2))
383>>> _g.f_divmod(-17,5)
384(mpz(-4), mpz(3))
385>>> _g.f_divmod(17,-5)
386(mpz(-4), mpz(-3))
387>>> _g.f_divmod(-17,-5)
388(mpz(3), mpz(-2))
389>>> _g.t_divmod(17,5)
390(mpz(3), mpz(2))
391>>> _g.t_divmod(-17,5)
392(mpz(-3), mpz(-2))
393>>> _g.t_divmod(17,-5)
394(mpz(-3), mpz(2))
395>>> _g.t_divmod(-17,-5)
396(mpz(3), mpz(-2))
397'''
398
399__test__['cmpr']=\
400r'''
401>>> c=_g.mpz(a)
402>>> c is a
4031
404>>> c==a
4051
406>>> c>a
4070
408>>> c<a
4090
410>>> a>b
4110
412>>> a<b
4131
414>>> not _g.mpz(0)
4151
416>>> not a
4170
418>>> _g.mpz(1) == None
419False
420>>> _g.mpz(1) == '1'
421False
422>>> _g.mpz(1) == 'abc'
423False
424>>> [_g.mpz(23), None].count(None)
4251
426>>> _g.mpz(3.14)
427mpz(3)
428>>> _g.mpz(_g.mpq(17,3))
429mpz(5)
430>>> _g.mpz(23)
431mpz(23)
432>>> _g.mpz(-23)
433mpz(-23)
434>>> x=1000*1000*1000*1000*1000*1000*1000
435>>> _g.mpz(x)
436mpz(1000000000000000000000)
437>>> a == float('Inf')
438False
439>>> a != float('Inf')
440True
441>>> a > float('Inf')
442False
443>>> a >= float('Inf')
444False
445>>> a < float('Inf')
446True
447>>> a <= float('Inf')
448True
449>>> a == float('-Inf')
450False
451>>> a != float('-Inf')
452True
453>>> a > float('-Inf')
454True
455>>> a >= float('-Inf')
456True
457>>> a < float('-Inf')
458False
459>>> a <= float('-Inf')
460False
461>>> a == float('nan')
462False
463>>> a != float('nan')
464True
465>>> a > float('nan')
466False
467>>> a >= float('nan')
468False
469>>> a < float('nan')
470False
471>>> a <= float('nan')
472False
473'''
474
475__test__['bitops']=\
476r'''
477>>> ~a
478mpz(-124)
479>>> a&b
480mpz(72)
481>>> a|b
482mpz(507)
483>>> a^b
484mpz(435)
485>>> a<<1
486mpz(246)
487>>> a>>1
488mpz(61)
489>>> a<<-1
490Traceback (innermost last):
491  ...
492ValueError: negative shift count
493>>> a>>-2
494Traceback (innermost last):
495  ...
496ValueError: negative shift count
497>>> a<<0
498mpz(123)
499>>> a>>0
500mpz(123)
501>>> _g.popcount(a)
5026
503>>> _g.popcount(b)
5044
505>>> _g.popcount(-7)
506-1
507>>> _g.popcount(0)
5080
509>>> _g.hamdist(a,b)
5106
511>>> _g.hamdist(3)
512Traceback (innermost last):
513  ...
514TypeError: hamdist() requires 'mpz','mpz' arguments
515>>> _g.hamdist(a)
516Traceback (innermost last):
517  ...
518TypeError: hamdist() requires 'mpz','mpz' arguments
519>>> _g.hamdist(a, 3, 4)
520Traceback (innermost last):
521  ...
522TypeError: hamdist() requires 'mpz','mpz' arguments
523>>> _g.f_mod_2exp(a,5)
524mpz(27)
525>>> _g.f_mod_2exp(b,5)
526mpz(8)
527>>> _g.f_mod_2exp(b,5)==(b%32)
5281
529>>> _g.f_mod_2exp(a,5)==(a%32)
5301
531>>> a.bit_set(20)
532mpz(1048699)
533>>> a.bit_clear(0)
534mpz(122)
535>>> for i in range(8):
536...     print(a.bit_test(i))
537...
538True
539True
540False
541True
542True
543True
544True
545False
546>>> for i in range(10):
547...     print(b.bit_test(i))
548...
549False
550False
551False
552True
553False
554False
555True
556True
557True
558False
559>>> [a.bit_scan0(j) for j in range(33)]
560[2, 2, 2, 7, 7, 7, 7, 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]
561>>> [a.bit_scan1(j) for j in range(10)]
562[0, 1, 3, 3, 4, 5, 6, None, None, None]
563>>> n=_g.mpz(-(7+6*16+5*256+7*4092))
564>>> [n.bit_scan0(j) for j in range(18)]
565[1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
566>>> [n.bit_scan1(j) for j in range(33)]
567[0, 2, 2, 4, 4, 5, 7, 7, 9, 9, 11, 11, 15, 15, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
568>>> _g.mpz(0).bit_length()
5690
570>>> _g.mpz(12345).bit_length()
57114
572>>> _g.bit_mask(9)
573mpz(511)
574'''
575
576__test__['format']=\
577r'''
578>>> str(a)
579'123'
580>>> repr(a)
581'mpz(123)'
582>>> hex(a)
583'0x7b'
584>>> oct(a)
585'0o173'
586>>> _g.mpz('123')
587mpz(123)
588>>> _g.mpz('1001001011',2)
589mpz(587)
590>>> _g.mpz('1001001011',2).digits(2)
591'1001001011'
592>>> for i in range(2,63):
593...     print(a.digits(i))
594...
5951111011
59611120
5971323
598443
599323
600234
601173
602146
603123
604102
605a3
60696
6078b
60883
6097b
61074
6116f
61269
61363
6145i
6155d
61658
61753
6184n
6194j
6204f
6214b
62247
62343
6243u
6253r
6263o
6273l
6283i
6293f
6303C
63139
63236
63333
63430
6352d
6362b
6372Z
6382X
6392V
6402T
6412R
6422P
6432N
6442L
6452J
6462H
6472F
6482D
6492B
65029
65127
65225
65323
65421
6551z
656>>> print(a.digits(63))
657Traceback (innermost last):
658  ...
659ValueError: base must be in the interval 2 ... 62
660>>> _g.mpz('43')
661mpz(43)
662>>> _g.mpz('043')
663mpz(43)
664>>> _g.mpz('43',0)
665mpz(43)
666>>> _g.mpz('0o43')
667mpz(35)
668>>> _g.mpz('0x43')
669mpz(67)
670>>> _g.mpz('0x43',10)
671Traceback (innermost last):
672  File "<pyshell#181>", line 1, in ?
673    _g.mpz('0x43')
674ValueError: invalid digits
675>>>
676'''
677
678__test__['binio']=\
679r'''
680>>> a == _g.from_binary(_g.to_binary(a))
681True
682>>> -a == _g.from_binary(_g.to_binary(-a))
683True
684>>> z = _g.mpz(573406620562849222387053)
685>>> for i in range(2,37):
686...    print(i,z.num_digits(i))
687...
6882 79
6893 50
6904 40
6915 35
6926 31
6937 29
6948 27
6959 25
69610 24
69711 23
69812 23
69913 22
70014 21
70115 21
70216 20
70317 20
70418 19
70519 19
70620 19
70721 18
70822 18
70923 18
71024 18
71125 18
71226 17
71327 17
71428 17
71529 17
71630 17
71731 16
71832 16
71933 16
72034 16
72135 16
72236 16
723>>> _g.num_digits(23)
7242
725>>> _g.num_digits(23,2)
7265
727>>> _g.num_digits(23,99)
728Traceback (most recent call last):
729  File "<string>", line 1, in ?
730ValueError: base must be in the interval 2 ... 62
731>>> hash(a)
732123
733>>> hash(b)
734456
735>>> hash(z) == hash(int(z))
736True
737>>> int(_g.mpz(-3))
738-3
739'''
740
741__test__['number']=\
742r'''
743>>> _g.iroot_rem(a,2)
744(mpz(11), mpz(2))
745>>> _g.iroot_rem(a,3)
746(mpz(4), mpz(59))
747>>> _g.iroot_rem(a*a)
748Traceback (most recent call last):
749  File "<stdin>", line 1, in <module>
750TypeError: iroot_rem() requires 'mpz','int' arguments
751>>> _g.iroot_rem(a*a,2)
752(mpz(123), mpz(0))
753>>> print(_g.isqrt(a))
75411
755>>> print(_g.isqrt(b))
75621
757>>> print(_g.isqrt_rem(a))
758(mpz(11), mpz(2))
759>>> print(_g.isqrt_rem(b))
760(mpz(21), mpz(15))
761>>> for i in range(5):
762...    print(_g.iroot(a,i+1),_g.iroot(b,i+1))
763...
764(mpz(123), True) (mpz(456), True)
765(mpz(11), False) (mpz(21), False)
766(mpz(4), False) (mpz(7), False)
767(mpz(3), False) (mpz(4), False)
768(mpz(2), False) (mpz(3), False)
769>>> _g.is_square(a)
7700
771>>> _g.is_power(a)
7720
773>>> _g.is_square(99*99)
7741
775>>> _g.is_square(99*99*99)
7760
777>>> _g.is_square(0)
7781
779>>> _g.is_square(-1)
7800
781>>> _g.is_power(99*99*99)
7821
783>>> _g.gcd(a,b)
784mpz(3)
785>>> temp=_g.gcdext(a,b)
786>>> temp[0]==a*temp[1]+b*temp[2]
787True
788>>> _g.lcm(a,b)
789mpz(18696)
790>>> _g.fac(7)
791mpz(5040)
792>>> _g.fib(17)
793mpz(1597)
794>>> for i in range(10):
795...     print(_g.bincoef(10,i))
796...
7971
79810
79945
800120
801210
802252
803210
804120
80545
80610
807>>> _g.divm(b,a,20)
808mpz(12)
809>>> _g.divm(a,b,100)
810Traceback (innermost last):
811  File "<pyshell#184>", line 1, in ?
812    _g.divm(a,b,100)
813ZeroDivisionError: not invertible
814>>> _g.divm(6,12,14)
815mpz(4)
816>>> _g.divm(0,1,2)
817mpz(0)
818>>> # guard against regression of an ancient gmpy bug: divm w/non-coprime parms
819>>> _g.divm(4,8,20)
820mpz(3)
821>>> _g.divm(4,8,20)
822mpz(3)
823>>> _g.mpz(20)
824mpz(20)
825>>> _g.mpz(8)
826mpz(8)
827>>> _g.mpz(4)
828mpz(4)
829>>> # guard against regression of a memory leak in divm
830>>> __ = gc.collect()
831>>> _siz = 87654
832>>> _siz = _memsize()
833>>> for x in range(45678):
834...     _xx=_g.divm(b,a,20)
835>>> del _xx
836>>> __ = gc.collect()
837>>> (_memsize()-_siz) <= 32
838True
839>>> _g.invert(a,100)
840mpz(87)
841>>> _g.invert(b,100)
842Traceback (most recent call last):
843  ...
844ZeroDivisionError: invert() no inverse exists
845>>> _g.invert(3)
846Traceback (innermost last):
847  ...
848TypeError: invert() requires 'mpz','mpz' arguments
849>>> _g.invert()
850Traceback (innermost last):
851  ...
852TypeError: invert() requires 'mpz','mpz' arguments
853>>> _g.comb(3,-1)
854Traceback (most recent call last):
855  File "<stdin>", line 1, in ?
856ValueError: binomial coefficient with negative k
857>>> _g.isqrt(-1)
858Traceback (most recent call last):
859  File "<stdin>", line 1, in ?
860ValueError: isqrt() of negative number
861>>> _g.isqrt_rem(-1)
862Traceback (most recent call last):
863  File "<stdin>", line 1, in ?
864ValueError: isqrt_rem() of negative number
865>>> _g.remove(3,-1)
866Traceback (most recent call last):
867  File "<stdin>", line 1, in ?
868ValueError: factor must be > 1
869>>> _g.remove(3)
870Traceback (innermost last):
871  ...
872TypeError: remove() requires 'mpz','mpz' arguments
873>>> _g.remove()
874Traceback (innermost last):
875  ...
876TypeError: remove() requires 'mpz','mpz' arguments
877>>> _g.is_prime(3,-3)
878Traceback (most recent call last):
879  File "<stdin>", line 1, in ?
880ValueError: repetition count for is_prime() must be positive
881>>> _g.jacobi(10,3)
8821
883>>> _g.jacobi(10,-3)
884Traceback (most recent call last):
885  File "<stdin>", line 1, in ?
886ValueError: y must be odd and >0
887>>> _g.jacobi(3)
888Traceback (innermost last):
889  ...
890TypeError: jacobi() requires 'mpz','mpz' arguments
891>>> _g.jacobi()
892Traceback (innermost last):
893  ...
894TypeError: jacobi() requires 'mpz','mpz' arguments
895>>> _g.legendre(10,3)
8961
897>>> _g.legendre(10,-3)
898Traceback (most recent call last):
899  File "<stdin>", line 1, in ?
900ValueError: y must be odd and >0
901>>> _g.legendre(3)
902Traceback (innermost last):
903  ...
904TypeError: legendre() requires 'mpz','mpz' arguments
905>>> _g.legendre()
906Traceback (innermost last):
907  ...
908TypeError: legendre() requires 'mpz','mpz' arguments
909>>> _g.kronecker(10,3)
9101
911>>> _g.kronecker(10,-3)
9121
913>>> _g.kronecker(3)
914Traceback (innermost last):
915  ...
916TypeError: kronecker() requires 'mpz','mpz' arguments
917>>> _g.kronecker()
918Traceback (innermost last):
919  ...
920TypeError: kronecker() requires 'mpz','mpz' arguments
921>>> a=10**20
922>>> b=a+39
923>>> _g.jacobi(a,b)
9241
925>>> _g.legendre(a,b)
9261
927>>> _g.kronecker(a,b)
9281
929'''
930
931def _test(chat=None):
932    if chat:
933        print("Unit tests for gmpy2 (mpz functionality)")
934        print("    on Python %s" % sys.version)
935        print("Testing gmpy2 {0}".format(_g.version()))
936        print("  Mutliple-precision library:   {0}".format(_g.mp_version()))
937        print("  Floating-point library:       {0}".format(_g.mpfr_version()))
938        print("  Complex library:              {0}".format(_g.mpc_version()))
939        print("  Caching Values: (Number)      {0}".format(_g.get_cache()[0]))
940        print("  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
941    thismod = sys.modules.get(__name__)
942    doctest.testmod(thismod, report=0)
943
944    if chat: print("Repeating tests, with caching disabled")
945    _g.set_cache(0,128)
946
947    sav = sys.stdout
948    class _Dummy:
949        encoding = None
950        def write(self,*whatever):
951            pass
952    try:
953        sys.stdout = _Dummy()
954        doctest.testmod(thismod, report=0)
955    finally:
956        sys.stdout = sav
957
958    if chat:
959        print()
960        print("Overall results for mpz:")
961    return doctest.master.summarize(chat)
962
963
964if __name__=='__main__':
965    _test(1)
966
967