PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test2/gmpy_test_mpz.py

http://gmpy.googlecode.com/
Python | 849 lines | 845 code | 0 blank | 4 comment | 0 complexity | fb8de7d5e89b132168d104e61447fcfd MD5 | raw file
  1# partial unit test for gmpy2 mpz functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3r'''
  4>>> 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>>> filter(lambda x: not x.startswith('__'), dir(a))
  7['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    import gmpy2 as _g
 41    savex=x
 42    prime=2
 43    x=_g.mpz(x)
 44    factors=[]
 45    while x>=prime:
 46        newx,mult=_g.remove(x,prime)
 47        if mult:
 48            factors.extend([int(prime)]*mult)
 49            x=newx
 50        prime=_g.next_prime(prime)
 51    for factor in factors: assert _g.is_prime(factor)
 52    from operator import mul
 53    assert reduce(mul, factors)==savex
 54    return factors
 55
 56if sys.version[:3] >= '2.5':
 57  __test__['index']=\
 58r'''
 59>>> range(333)[a]
 60123
 61>>> range(333)[b]
 62Traceback (innermost last):
 63  ...
 64IndexError: list index out of range
 65>>> ix = operator.index
 66>>> ix(_g.mpz(sys.maxint)) == sys.maxint
 67True
 68>>> type(ix(_g.mpz(sys.maxint))) is int
 69True
 70>>> ix(_g.mpz(sys.maxint+1)) == sys.maxint+1
 71True
 72>>> type(ix(_g.mpz(sys.maxint+1))) is long
 73True
 74'''
 75
 76__test__['elemop']=\
 77r'''
 78>>> a+b
 79mpz(579)
 80>>> a-b
 81mpz(-333)
 82>>> a*b
 83mpz(56088)
 84>>> a/b
 85mpz(0)
 86>>> a%b
 87mpz(123)
 88>>> 0%b
 89mpz(0)
 90>>> b+a
 91mpz(579)
 92>>> b-a
 93mpz(333)
 94>>> b*a
 95mpz(56088)
 96>>> b%a
 97mpz(87)
 98>>> divmod(a,b)
 99(mpz(0), mpz(123))
100>>> divmod(b,a)
101(mpz(3), mpz(87))
102>>> divmod(0,b)
103(mpz(0), mpz(0))
104>>> -a
105mpz(-123)
106>>> a+1
107mpz(124)
108>>> a+(-1)
109mpz(122)
110>>> (-1)+a
111mpz(122)
112>>> 1+a
113mpz(124)
114>>> a-1
115mpz(122)
116>>> a-(-1)
117mpz(124)
118>>> 1-a
119mpz(-122)
120>>> (-1)-a
121mpz(-124)
122>>> a+True
123mpz(124)
124>>> a+False
125mpz(123)
126>>> a*False
127mpz(0)
128>>> a//True
129mpz(123)
130>>> abs(-a)==a
1311
132>>> pow(a,10)
133mpz(792594609605189126649L)
134>>> pow(a,7,b)
135mpz(99)
136>>> _g.sign(b-a)
1371
138>>> _g.sign(b-b)
1390
140>>> _g.sign(a-b)
141-1
142>>> _g.sign(a)
1431
144>>> _g.sign(-a)
145-1
146>>> z=b-b; _g.sign(z)
1470
148>>> _g.mpz(4)**(0.5)
149mpfr('2.0')
150>>> import pickle
151>>> pickle.loads(pickle.dumps(_g.mpz(12346789)))
152mpz(12346789)
153>>> s='12345678901234567890123456789'
154>>> int(s) == _g.mpz(s)
155True
156>>> _g.mpz(s) == int(s)
157True
158>>> del s
159>>> _g.is_even(a)
160False
161>>> _g.is_odd(a)
162True
163>>> _g.is_even(b)
164True
165>>> _g.is_odd(b)
166False
167>>> _g.is_even(2)
168True
169'''
170
171from gmpy_truediv import truediv
172__test__['newdiv']=\
173r'''
174>>> a/b
175mpz(0)
176>>> a//b
177mpz(0)
178>>> truediv(a,b)
179mpfr('0.26973684210526316')
180>>> b/a
181mpz(3)
182>>> b//a
183mpz(3)
184>>> truediv(b,a)
185mpfr('3.7073170731707319')
186>>>
187'''
188
189__test__['divexact']=\
190r'''
191>>> a=_g.mpz('1234567912345678912345679')
192>>> b=_g.mpz('789789789789789789789789')
193>>> c=a*b
194>>> _g.divexact(c,a)
195mpz(789789789789789789789789L)
196>>>
197'''
198
199__test__['divmod']=\
200r'''
201>>> _g.c_divmod(17,5)
202(mpz(4), mpz(-3))
203>>> _g.c_divmod(-17,5)
204(mpz(-3), mpz(-2))
205>>> _g.c_divmod(17,-5)
206(mpz(-3), mpz(2))
207>>> _g.c_divmod(-17,-5)
208(mpz(4), mpz(3))
209>>> _g.f_divmod(17,5)
210(mpz(3), mpz(2))
211>>> _g.f_divmod(-17,5)
212(mpz(-4), mpz(3))
213>>> _g.f_divmod(17,-5)
214(mpz(-4), mpz(-3))
215>>> _g.f_divmod(-17,-5)
216(mpz(3), mpz(-2))
217>>> _g.t_divmod(17,5)
218(mpz(3), mpz(2))
219>>> _g.t_divmod(-17,5)
220(mpz(-3), mpz(-2))
221>>> _g.t_divmod(17,-5)
222(mpz(-3), mpz(2))
223>>> _g.t_divmod(-17,-5)
224(mpz(3), mpz(-2))
225'''
226
227__test__['cmpr']=\
228r'''
229>>> c=_g.mpz(a)
230>>> c is a
2311
232>>> c==a
2331
234>>> c>a
2350
236>>> c<a
2370
238>>> cmp(a,c)
2390
240>>> cmp(a,b)
241-1
242>>> a>b
2430
244>>> a<b
2451
246>>> not _g.mpz(0)
2471
248>>> not a
2490
250>>> _g.mpz(1) == None
251False
252>>> _g.mpz(1) == '1'
253False
254>>> _g.mpz(1) == 'abc'
255False
256>>> [_g.mpz(23), None].count(None)
2571
258>>> _g.mpz(3.14)
259mpz(3)
260>>> _g.mpz(_g.mpq(17,3))
261mpz(5)
262>>> _g.mpz(23L)
263mpz(23)
264>>> _g.mpz(-23L)
265mpz(-23)
266>>> x=1000L*1000*1000*1000*1000*1000*1000
267>>> _g.mpz(x)
268mpz(1000000000000000000000L)
269>>> try: print cmp(_g.mpz(1), _g.mpz(-1))
270... except: print 'ouch!'
2711
272'''
273
274__test__['special'] = \
275r'''
276>>> _g.context().trap_divzero == False
277True
278>>> a == float('Inf')
279False
280>>> a != float('Inf')
281True
282>>> a > float('Inf')
283False
284>>> a >= float('Inf')
285False
286>>> a < float('Inf')
287True
288>>> a <= float('Inf')
289True
290>>> a == float('-Inf')
291False
292>>> a != float('-Inf')
293True
294>>> a > float('-Inf')
295True
296>>> a >= float('-Inf')
297True
298>>> a < float('-Inf')
299False
300>>> a <= float('-Inf')
301False
302>>> a == float('nan')
303False
304>>> a != float('nan')
305True
306>>> a > float('nan')
307False
308>>> a >= float('nan')
309False
310>>> a < float('nan')
311False
312>>> a <= float('nan')
313False
314>>> float('Inf') == a
315False
316>>> float('Inf') != a
317True
318>>> float('Inf') > a
319True
320>>> float('Inf') >= a
321True
322>>> float('Inf') < a
323False
324>>> float('Inf') <= a
325False
326>>> float('-Inf') == a
327False
328>>> float('-Inf') != a
329True
330>>> float('-Inf') > a
331False
332>>> float('-Inf') >= a
333False
334>>> float('-Inf') < a
335True
336>>> float('-Inf') <= a
337True
338>>> float('nan') == a
339False
340>>> float('nan') != a
341True
342>>> float('nan') > a
343False
344>>> float('nan') >= a
345False
346>>> float('nan') < a
347False
348>>> float('nan') <= a
349False
350>>> a + float('Inf')
351mpfr('inf')
352>>> float('Inf') + a
353mpfr('inf')
354>>> a + float('-Inf')
355mpfr('-inf')
356>>> float('-Inf') + a
357mpfr('-inf')
358>>> a + float('nan')
359mpfr('nan')
360>>> float('nan') + a
361mpfr('nan')
362>>> a - float('Inf')
363mpfr('-inf')
364>>> float('Inf') - a
365mpfr('inf')
366>>> a - float('-Inf')
367mpfr('inf')
368>>> float('-Inf') - a
369mpfr('-inf')
370>>> a - float('nan')
371mpfr('nan')
372>>> float('nan') - a
373mpfr('nan')
374>>> a * float('Inf')
375mpfr('inf')
376>>> float('Inf') * a
377mpfr('inf')
378>>> a * float('-Inf')
379mpfr('-inf')
380>>> float('-Inf') * a
381mpfr('-inf')
382>>> -a * float('Inf')
383mpfr('-inf')
384>>> float('Inf') * -a
385mpfr('-inf')
386>>> -a * float('-Inf')
387mpfr('inf')
388>>> float('-Inf') * -a
389mpfr('inf')
390>>> a * float('nan')
391mpfr('nan')
392>>> float('nan') * a
393mpfr('nan')
394>>> _g.mpz(0) * float('Inf')
395mpfr('nan')
396>>> _g.mpz(0) * float('-Inf')
397mpfr('nan')
398>>> float('Inf') * _g.mpz(0)
399mpfr('nan')
400>>> float('-Inf') * _g.mpz(0)
401mpfr('nan')
402>>> a / float('Inf')
403mpfr('0.0')
404>>> -a / float('Inf')
405mpfr('-0.0')
406>>> float('Inf') / a
407mpfr('inf')
408>>> float('Inf') / -a
409mpfr('-inf')
410>>> a / float('-Inf')
411mpfr('-0.0')
412>>> -a / float('-Inf')
413mpfr('0.0')
414>>> float('-Inf') / a
415mpfr('-inf')
416>>> float('-Inf') / -a
417mpfr('inf')
418>>> a / float('nan')
419mpfr('nan')
420>>> float('nan') / a
421mpfr('nan')
422>>> float('nan') / _g.mpz(0)
423mpfr('nan')
424>>> float('nan') / _g.mpz(0)
425mpfr('nan')
426>>> divmod(a, float('Inf'))
427(mpfr('0.0'), mpfr('123.0'))
428>>> divmod(a, float('-Inf'))
429(mpfr('-1.0'), mpfr('-inf'))
430>>> divmod(-a, float('Inf'))
431(mpfr('-1.0'), mpfr('inf'))
432>>> divmod(-a, float('-Inf'))
433(mpfr('0.0'), mpfr('-123.0'))
434>>> divmod(a, float('nan'))
435(mpfr('nan'), mpfr('nan'))
436>>> divmod(-a, float('nan'))
437(mpfr('nan'), mpfr('nan'))
438>>> divmod(_g.mpz(0), float('Inf'))
439(mpfr('0.0'), mpfr('0.0'))
440>>> divmod(_g.mpz(0), float('-Inf'))
441(mpfr('-0.0'), mpfr('-0.0'))
442>>> divmod(_g.mpz(0), float('nan'))
443(mpfr('nan'), mpfr('nan'))
444>>> divmod(float('Inf'), a)
445(mpfr('nan'), mpfr('nan'))
446>>> divmod(float('-Inf'), a)
447(mpfr('nan'), mpfr('nan'))
448>>> divmod(float('Inf'), -a)
449(mpfr('nan'), mpfr('nan'))
450>>> divmod(float('-Inf'), -a)
451(mpfr('nan'), mpfr('nan'))
452>>> divmod(float('nan'), a)
453(mpfr('nan'), mpfr('nan'))
454>>> divmod(float('nan'), -a)
455(mpfr('nan'), mpfr('nan'))
456>>> divmod(float('Inf'), _g.mpz(0))
457(mpfr('nan'), mpfr('nan'))
458>>> divmod(float('-Inf'), _g.mpz(0))
459(mpfr('nan'), mpfr('nan'))
460>>> divmod(float('nan'), _g.mpz(0))
461(mpfr('nan'), mpfr('nan'))
462>>> divmod(_g.mpfr('Inf'), _g.mpz(0))
463(mpfr('nan'), mpfr('nan'))
464>>> divmod(_g.mpfr('-Inf'), _g.mpz(0))
465(mpfr('nan'), mpfr('nan'))
466>>> divmod(_g.mpfr('nan'), _g.mpz(0))
467(mpfr('nan'), mpfr('nan'))
468'''
469
470__test__['bitops']=\
471r'''
472>>> ~a
473mpz(-124)
474>>> a&b
475mpz(72)
476>>> a|b
477mpz(507)
478>>> a^b
479mpz(435)
480>>> a<<1
481mpz(246)
482>>> a>>1
483mpz(61)
484>>> a<<-1
485Traceback (innermost last):
486  ...
487ValueError: negative shift count
488>>> a>>-2
489Traceback (innermost last):
490  ...
491ValueError: negative shift count
492>>> a<<0
493mpz(123)
494>>> a>>0
495mpz(123)
496>>> _g.popcount(a)
4976
498>>> _g.popcount(b)
4994
500>>> _g.popcount(-7)
501-1
502>>> _g.popcount(0)
5030
504>>> _g.hamdist(a,b)
5056
506>>> _g.hamdist(3)
507Traceback (innermost last):
508  ...
509TypeError: hamdist() requires 'mpz','mpz' arguments
510>>> _g.hamdist()
511Traceback (innermost last):
512  ...
513TypeError: hamdist() requires 'mpz','mpz' arguments
514>>> _g.f_mod_2exp(a,5)
515mpz(27)
516>>> _g.f_mod_2exp(b,5)
517mpz(8)
518>>> _g.f_mod_2exp(b,5)==(b%32)
5191
520>>> _g.f_mod_2exp(a,5)==(a%32)
5211
522>>> a.bit_set(20)
523mpz(1048699)
524>>> a.bit_clear(0)
525mpz(122)
526>>> for i in range(8):
527...     print a.bit_test(i),
528...     if i==7: print
529...
530True True False True True True True False
531>>> for i in range(10):
532...     print b.bit_test(i),
533...     if i==9: print
534...
535False False False True False False True True True False
536>>> [a.bit_scan0(j) for j in range(33)]
537[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]
538>>> [a.bit_scan1(j) for j in range(10)]
539[0, 1, 3, 3, 4, 5, 6, None, None, None]
540>>> n=_g.mpz(-(7+6*16+5*256+7*4092))
541>>> [n.bit_scan0(j) for j in range(18)]
542[1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
543>>> [n.bit_scan1(j) for j in range(33)]
544[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]
545>>> _g.mpz(0).bit_length()
5460
547>>> _g.mpz(12345).bit_length()
54814
549'''
550
551__test__['format']=\
552r'''
553>>> str(a)
554'123'
555>>> repr(a)
556'mpz(123)'
557>>> hex(a)
558'0x7b'
559>>> oct(a)
560'0173'
561>>> _g.mpz('123')
562mpz(123)
563>>> _g.mpz('1001001011',2)
564mpz(587)
565>>> _g.mpz('1001001011',2).digits(2)
566'1001001011'
567>>> for i in range(2,63):
568...     print a.digits(i),
569...     if i%6==0: print
570...
5711111011 11120 1323 443 323
572234 173 146 123 102 a3
57396 8b 83 7b 74 6f
57469 63 5i 5d 58 53
5754n 4j 4f 4b 47 43
5763u 3r 3o 3l 3i 3f
5773C 39 36 33 30 2d
5782b 2Z 2X 2V 2T 2R
5792P 2N 2L 2J 2H 2F
5802D 2B 29 27 25 23
58121 1z
582>>> print a.digits(63)
583Traceback (innermost last):
584  ...
585ValueError: base must be in the interval 2 ... 62
586>>> _g.mpz('43')
587mpz(43)
588>>> _g.mpz('043')
589mpz(43)
590>>> _g.mpz('43',0)
591mpz(43)
592>>> _g.mpz('0o43')
593mpz(35)
594>>> _g.mpz('0x43')
595mpz(67)
596>>> _g.mpz('0x43',10)
597Traceback (innermost last):
598  File "<pyshell#181>", line 1, in ?
599    _g.mpz('0x43')
600ValueError: invalid digits
601>>>
602'''
603
604__test__['binio']=\
605r'''
606>>> a== _g.from_binary(_g.to_binary(a))
607True
608>>> -a== _g.from_binary(_g.to_binary(-a))
609True
610>>> z=_g.mpz(573406620562849222387053L)
611>>> divmod(z,a)
612(mpz(4661842443600400182008L), mpz(69))
613>>> for i in range(2,37):
614...    print i,z.num_digits(i),
615...    if i%6==0: print
616...
6172 79 3 50 4 40 5 35 6 31
6187 29 8 27 9 25 10 24 11 23 12 23
61913 22 14 21 15 21 16 20 17 20 18 19
62019 19 20 19 21 18 22 18 23 18 24 18
62125 18 26 17 27 17 28 17 29 17 30 17
62231 16 32 16 33 16 34 16 35 16 36 16
623>>> _g.num_digits(23)
6242
625>>> _g.num_digits(23,2)
6265
627>>> _g.num_digits(23,99)
628Traceback (most recent call last):
629  File "<string>", line 1, in ?
630ValueError: base must be in the interval 2 ... 62
631>>> hash(a)
632123
633>>> hash(b)
634456
635>>> hash(z) == hash(long(z))
636True
637>>> long(_g.mpz(-3))
638-3L
639>>>
640'''
641
642__test__['number']=\
643r'''
644>>> _g.iroot_rem(a,2)
645(mpz(11), mpz(2))
646>>> _g.iroot_rem(a,3)
647(mpz(4), mpz(59))
648>>> _g.iroot_rem(a*a)
649Traceback (most recent call last):
650  File "<stdin>", line 1, in <module>
651TypeError: iroot_rem() requires 'mpz','int' arguments
652>>> _g.iroot_rem(a*a,2)
653(mpz(123), mpz(0))
654>>> print _g.isqrt(a), _g.isqrt(b)
65511 21
656>>> print _g.isqrt_rem(a), _g.isqrt_rem(b)
657(mpz(11), mpz(2)) (mpz(21), mpz(15))
658>>> for i in range(5):
659...    print _g.iroot(a,i+1),_g.iroot(b,i+1)
660...
661(mpz(123), True) (mpz(456), True)
662(mpz(11), False) (mpz(21), False)
663(mpz(4), False) (mpz(7), False)
664(mpz(3), False) (mpz(4), False)
665(mpz(2), False) (mpz(3), False)
666>>> _g.is_square(a)
6670
668>>> _g.is_power(a)
6690
670>>> _g.is_square(99*99)
6711
672>>> _g.is_square(99*99*99)
6730
674>>> _g.is_square(0)
6751
676>>> _g.is_square(-1)
6770
678>>> _g.is_power(99*99*99)
6791
680>>> _g.gcd(a,b)
681mpz(3)
682>>> temp=_g.gcdext(a,b)
683>>> temp[0]==a*temp[1]+b*temp[2]
684True
685>>> _g.lcm(a,b)
686mpz(18696)
687>>> _g.fac(7)
688mpz(5040)
689>>> _g.fib(17)
690mpz(1597)
691>>> for i in range(10):
692...     print _g.bincoef(10,i)
693...
6941
69510
69645
697120
698210
699252
700210
701120
70245
70310
704>>> _g.divm(b,a,20)
705mpz(12)
706>>> _g.divm(a,b,100)
707Traceback (innermost last):
708  File "<pyshell#184>", line 1, in ?
709    _g.divm(a,b,100)
710ZeroDivisionError: not invertible
711>>> _g.divm(6,12,14)
712mpz(4)
713>>> _g.divm(0,1,2)
714mpz(0)
715>>> # guard against regression of an ancient gmpy bug: divm w/non-coprime parms
716>>> _g.divm(4,8,20)
717mpz(3)
718>>> _g.divm(4,8,20)
719mpz(3)
720>>> _g.mpz(20)
721mpz(20)
722>>> _g.mpz(8)
723mpz(8)
724>>> _g.mpz(4)
725mpz(4)
726>>> # guard against regression of a memory leak in divm
727>>> __ = gc.collect()
728>>> _siz = 87654
729>>> _siz = _memsize()
730>>> for x in xrange(45678):
731...     _xx=_g.divm(b,a,20)
732>>> del _xx
733>>> __ = gc.collect()
734>>> (_memsize()-_siz) <= 16
735True
736>>> _g.invert(a,100)
737mpz(87)
738>>> _g.invert(b,100)
739Traceback (most recent call last):
740  ...
741ZeroDivisionError: invert() no inverse exists
742>>> _g.invert(3)
743Traceback (innermost last):
744  ...
745TypeError: invert() requires 'mpz','mpz' arguments
746>>> _g.comb(3,-1)
747Traceback (most recent call last):
748  File "<stdin>", line 1, in ?
749ValueError: binomial coefficient with negative k
750>>> _g.isqrt(-1)
751Traceback (most recent call last):
752  File "<stdin>", line 1, in ?
753ValueError: isqrt() of negative number
754>>> _g.isqrt_rem(-1)
755Traceback (most recent call last):
756  File "<stdin>", line 1, in ?
757ValueError: isqrt_rem() of negative number
758>>> _g.remove(3,-1)
759Traceback (most recent call last):
760  File "<stdin>", line 1, in ?
761ValueError: factor must be > 1
762>>> _g.remove(3)
763Traceback (innermost last):
764  ...
765TypeError: remove() requires 'mpz','mpz' arguments
766>>> _g.is_prime(3,-3)
767Traceback (most recent call last):
768  File "<stdin>", line 1, in ?
769ValueError: repetition count for is_prime() must be positive
770>>> _g.jacobi(10,3)
7711
772>>> _g.jacobi(10,-3)
773Traceback (most recent call last):
774  File "<stdin>", line 1, in ?
775ValueError: y must be odd and >0
776>>> _g.jacobi(3)
777Traceback (innermost last):
778  ...
779TypeError: jacobi() requires 'mpz','mpz' arguments
780>>> _g.legendre(10,3)
7811
782>>> _g.legendre(10,-3)
783Traceback (most recent call last):
784  File "<stdin>", line 1, in ?
785ValueError: y must be odd and >0
786>>> _g.legendre(3)
787Traceback (innermost last):
788  ...
789TypeError: legendre() requires 'mpz','mpz' arguments
790>>> _g.kronecker(10,3)
7911
792>>> _g.kronecker(10,-3)
7931
794>>> _g.kronecker(3)
795Traceback (innermost last):
796  ...
797TypeError: kronecker() requires 'mpz','mpz' arguments
798>>> a=10L**20
799>>> b=a+39
800>>> _g.jacobi(a,b)
8011
802>>> _g.legendre(a,b)
8031
804>>> _g.kronecker(a,b)
8051
806'''
807
808def _test(chat=None):
809    try:
810        float('nan')
811    except ValueError:
812        if "special" in __test__:
813            del(__test__["special"])
814
815    if chat:
816        print "Unit tests for gmpy2 (mpz functionality)"
817        print "    on Python %s" % sys.version
818        print "Testing gmpy2 {0}".format(_g.version())
819        print "  Mutliple-precision library:   {0}".format(_g.mp_version())
820        print "  Floating-point library:       {0}".format(_g.mpfr_version())
821        print "  Complex library:              {0}".format(_g.mpc_version())
822        print "  Caching Values: (Number)      {0}".format(_g.get_cache()[0])
823        print "  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1])
824
825    thismod = sys.modules.get(__name__)
826    doctest.testmod(thismod, report=0)
827
828    if chat: print "Repeating tests, with caching disabled"
829    _g.set_cache(0,128)
830
831    sav = sys.stdout
832    class _Dummy:
833        def write(self,*whatever):
834            pass
835    try:
836        sys.stdout = _Dummy()
837        doctest.testmod(thismod, report=0)
838    finally:
839        sys.stdout = sav
840
841    if chat:
842        print
843        print "Overall results for mpz:"
844    return doctest.master.summarize(chat)
845
846
847if __name__=='__main__':
848    _test(1)
849