PageRenderTime 39ms CodeModel.GetById 24ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/test3/gmpy_test_xmpz.py

http://gmpy.googlecode.com/
Python | 685 lines | 681 code | 2 blank | 2 comment | 0 complexity | 7fe07760ae0084967747f8d24683c892 MD5 | raw file
  1# partial unit test for gmpy2 xmpz functionality
  2# relies on Tim Peters' "doctest.py" test-driver
  3
  4import gmpy2 as _g, doctest, sys, operator, gc
  5__test__={}
  6a=_g.xmpz(123)
  7b=_g.xmpz(456)
  8aa=_g.mpz(123)
  9bb=_g.mpz(456)
 10
 11__test__['index']=\
 12r'''
 13>>> a=_g.xmpz(123)
 14>>> b=_g.xmpz(456)
 15>>> range(333)[a]
 16123
 17>>> range(333)[b]
 18Traceback (innermost last):
 19  ...
 20IndexError: range object index out of range
 21'''
 22
 23__test__['elemop']=\
 24r'''
 25>>> a=_g.xmpz(123)
 26>>> b=_g.xmpz(456)
 27>>> a+b
 28mpz(579)
 29>>> a-b
 30mpz(-333)
 31>>> a*b
 32mpz(56088)
 33>>> a//b
 34mpz(0)
 35>>> a/b
 36mpfr('0.26973684210526316')
 37>>> b//a
 38mpz(3)
 39>>> b/a
 40mpfr('3.7073170731707319')
 41>>> a%b
 42mpz(123)
 43>>> 0%b
 44mpz(0)
 45>>> b+a
 46mpz(579)
 47>>> b-a
 48mpz(333)
 49>>> b*a
 50mpz(56088)
 51>>> b%a
 52mpz(87)
 53>>> divmod(a,b)
 54(mpz(0), mpz(123))
 55>>> divmod(b,a)
 56(mpz(3), mpz(87))
 57>>> divmod(0,b)
 58(mpz(0), mpz(0))
 59>>> -a
 60>>> a
 61xmpz(-123)
 62>>> a=_g.xmpz(123)
 63>>> a+1
 64mpz(124)
 65>>> a+=1; a
 66xmpz(124)
 67>>> a=_g.xmpz(123)
 68>>> a+(-1)
 69mpz(122)
 70>>> a-=1; a
 71xmpz(122)
 72>>> a=_g.xmpz(123)
 73>>> (-1)+a
 74mpz(122)
 75>>> 1+a
 76mpz(124)
 77>>> a-1
 78mpz(122)
 79>>> a-(-1)
 80mpz(124)
 81>>> 1-a
 82mpz(-122)
 83>>> (-1)-a
 84mpz(-124)
 85>>> a+True
 86mpz(124)
 87>>> a+False
 88mpz(123)
 89>>> a*False
 90mpz(0)
 91>>> a//True
 92mpz(123)
 93>>> -a
 94>>> abs(a)
 95>>> a
 96xmpz(123)
 97>>> pow(a,10)
 98mpz(792594609605189126649)
 99>>> pow(a,7,b)
100mpz(99)
101>>> _g.sign(b-a)
1021
103>>> _g.sign(b-b)
1040
105>>> _g.sign(a-b)
106-1
107>>> _g.sign(a)
1081
109>>> -a
110>>> _g.sign(a)
111-1
112>>> a=_g.xmpz(123)
113>>> z=b-b; _g.sign(z)
1140
115>>> s='12345678901234567890123456789'
116>>> int(s) == _g.xmpz(s)
117True
118>>> _g.xmpz(s) == int(s)
119True
120>>> del s
121>>> _g.is_even(a)
122False
123>>> _g.is_odd(a)
124True
125>>> _g.is_even(b)
126True
127>>> _g.is_odd(b)
128False
129>>> _g.is_even(2)
130True
131'''
132
133__test__['special'] = \
134r'''
135>>> a=_g.xmpz(123)
136>>> b=_g.xmpz(456)
137>>> a == float('Inf')
138False
139>>> a != float('Inf')
140True
141>>> a > float('Inf')
142False
143>>> a >= float('Inf')
144False
145>>> a < float('Inf')
146True
147>>> a <= float('Inf')
148True
149>>> a == float('-Inf')
150False
151>>> a != float('-Inf')
152True
153>>> a > float('-Inf')
154True
155>>> a >= float('-Inf')
156True
157>>> a < float('-Inf')
158False
159>>> a <= float('-Inf')
160False
161>>> a == float('nan')
162False
163>>> a != float('nan')
164True
165>>> a > float('nan')
166False
167>>> a >= float('nan')
168False
169>>> a < float('nan')
170False
171>>> a <= float('nan')
172False
173>>> float('Inf') == a
174False
175>>> float('Inf') != a
176True
177>>> float('Inf') > a
178True
179>>> float('Inf') >= a
180True
181>>> float('Inf') < a
182False
183>>> float('Inf') <= a
184False
185>>> float('-Inf') == a
186False
187>>> float('-Inf') != a
188True
189>>> float('-Inf') > a
190False
191>>> float('-Inf') >= a
192False
193>>> float('-Inf') < a
194True
195>>> float('-Inf') <= a
196True
197>>> float('nan') == a
198False
199>>> float('nan') != a
200True
201>>> float('nan') > a
202False
203>>> float('nan') >= a
204False
205>>> float('nan') < a
206False
207>>> float('nan') <= a
208False
209>>> a + float('Inf')
210mpfr('inf')
211>>> float('Inf') + a
212mpfr('inf')
213>>> a + float('-Inf')
214mpfr('-inf')
215>>> float('-Inf') + a
216mpfr('-inf')
217>>> a + float('nan')
218mpfr('nan')
219>>> float('nan') + a
220mpfr('nan')
221>>> a - float('Inf')
222mpfr('-inf')
223>>> float('Inf') - a
224mpfr('inf')
225>>> a - float('-Inf')
226mpfr('inf')
227>>> float('-Inf') - a
228mpfr('-inf')
229>>> a - float('nan')
230mpfr('nan')
231>>> float('nan') - a
232mpfr('nan')
233>>> a * float('Inf')
234mpfr('inf')
235>>> float('Inf') * a
236mpfr('inf')
237>>> a * float('-Inf')
238mpfr('-inf')
239>>> float('-Inf') * a
240mpfr('-inf')
241>>> -a
242>>> a * float('Inf')
243mpfr('-inf')
244>>> float('Inf') * a
245mpfr('-inf')
246>>> a * float('-Inf')
247mpfr('inf')
248>>> float('-Inf') * a
249mpfr('inf')
250>>> -a
251>>> a * float('nan')
252mpfr('nan')
253>>> float('nan') * a
254mpfr('nan')
255>>> _g.xmpz(0) * float('Inf')
256mpfr('nan')
257>>> _g.xmpz(0) * float('-Inf')
258mpfr('nan')
259>>> float('Inf') * _g.xmpz(0)
260mpfr('nan')
261>>> float('-Inf') * _g.xmpz(0)
262mpfr('nan')
263>>> a / float('Inf')
264mpfr('0.0')
265>>> float('Inf') / a
266mpfr('inf')
267>>> a / float('-Inf')
268mpfr('-0.0')
269>>> float('-Inf') / a
270mpfr('-inf')
271>>> a / float('nan')
272mpfr('nan')
273>>> float('nan') / a
274mpfr('nan')
275>>> -a
276>>> a / float('Inf')
277mpfr('-0.0')
278>>> float('Inf') / a
279mpfr('-inf')
280>>> a / float('-Inf')
281mpfr('0.0')
282>>> float('-Inf') / a
283mpfr('inf')
284>>> -a
285'''
286
287__test__['divexact']=\
288r'''
289>>> a=_g.xmpz('1234567912345678912345679')
290>>> b=_g.xmpz('789789789789789789789789')
291>>> c=a*b
292>>> _g.divexact(c,a)
293mpz(789789789789789789789789)
294>>>
295'''
296
297__test__['divmod']=\
298r'''
299>>> _g.c_divmod(17,5)
300(mpz(4), mpz(-3))
301>>> _g.c_divmod(-17,5)
302(mpz(-3), mpz(-2))
303>>> _g.c_divmod(17,-5)
304(mpz(-3), mpz(2))
305>>> _g.c_divmod(-17,-5)
306(mpz(4), mpz(3))
307>>> _g.f_divmod(17,5)
308(mpz(3), mpz(2))
309>>> _g.f_divmod(-17,5)
310(mpz(-4), mpz(3))
311>>> _g.f_divmod(17,-5)
312(mpz(-4), mpz(-3))
313>>> _g.f_divmod(-17,-5)
314(mpz(3), mpz(-2))
315>>> _g.t_divmod(17,5)
316(mpz(3), mpz(2))
317>>> _g.t_divmod(-17,5)
318(mpz(-3), mpz(-2))
319>>> _g.t_divmod(17,-5)
320(mpz(-3), mpz(2))
321>>> _g.t_divmod(-17,-5)
322(mpz(3), mpz(-2))
323'''
324
325__test__['cmpr']=\
326r'''
327>>> a=_g.xmpz(123)
328>>> b=_g.xmpz(456)
329>>> c=_g.xmpz(a)
330>>> c is a
3310
332>>> c==a
3331
334>>> c>a
3350
336>>> c<a
3370
338>>> d=a.copy()
339>>> a is d
3400
341>>> a == d
3421
343>>> a>b
3440
345>>> a<b
3461
347>>> not _g.xmpz(0)
3481
349>>> not a
3500
351>>> _g.xmpz(1) == None
352False
353>>> _g.xmpz(1) == '1'
354False
355>>> _g.xmpz(1) == 'abc'
356False
357>>> [_g.xmpz(23), None].count(None)
3581
359>>> _g.xmpz(3.14)
360xmpz(3)
361>>> _g.xmpz(_g.mpq(17,3))
362xmpz(5)
363>>> _g.xmpz(23)
364xmpz(23)
365>>> _g.xmpz(-23)
366xmpz(-23)
367>>> x=1000*1000*1000*1000*1000*1000*1000
368>>> _g.xmpz(x)
369xmpz(1000000000000000000000)
370>>> a == float('Inf')
371False
372>>> a != float('Inf')
373True
374>>> a > float('Inf')
375False
376>>> a >= float('Inf')
377False
378>>> a < float('Inf')
379True
380>>> a <= float('Inf')
381True
382>>> a == float('-Inf')
383False
384>>> a != float('-Inf')
385True
386>>> a > float('-Inf')
387True
388>>> a >= float('-Inf')
389True
390>>> a < float('-Inf')
391False
392>>> a <= float('-Inf')
393False
394>>> a == float('nan')
395False
396>>> a != float('nan')
397True
398>>> a > float('nan')
399False
400>>> a >= float('nan')
401False
402>>> a < float('nan')
403False
404>>> a <= float('nan')
405False
406'''
407
408__test__['bitops']=\
409r'''
410>>> a=_g.xmpz(123)
411>>> b=_g.xmpz(456)
412>>> ~a
413>>> a
414xmpz(-124)
415>>> a=_g.xmpz(123)
416>>> a&b
417mpz(72)
418>>> a|b
419mpz(507)
420>>> a^b
421mpz(435)
422>>> a<<1
423mpz(246)
424>>> a>>1
425mpz(61)
426>>> a<<-1
427Traceback (innermost last):
428  ...
429ValueError: negative shift count
430>>> a>>-2
431Traceback (innermost last):
432  ...
433ValueError: negative shift count
434>>> a<<0
435mpz(123)
436>>> a>>0
437mpz(123)
438>>> a.bit_set(20)
439mpz(1048699)
440>>> a=_g.xmpz(123)
441>>> a.bit_clear(0)
442mpz(122)
443>>> a=_g.xmpz(123)
444>>> for i in range(8):
445...     print(a.bit_test(i))
446...
447True
448True
449False
450True
451True
452True
453True
454False
455>>> for i in range(10):
456...     print(b.bit_test(i))
457...
458False
459False
460False
461True
462False
463False
464True
465True
466True
467False
468>>> [a.bit_scan0(j) for j in range(33)]
469[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]
470>>> [a.bit_scan1(j) for j in range(10)]
471[0, 1, 3, 3, 4, 5, 6, None, None, None]
472>>> n=_g.xmpz(-(7+6*16+5*256+7*4092))
473>>> [n.bit_scan0(j) for j in range(18)]
474[1, 1, 3, 3, 6, 6, 6, 8, 8, 10, 10, 12, 12, 13, 14, -1, None, None]
475>>> [n.bit_scan1(j) for j in range(33)]
476[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]
477>>> _g.xmpz(0).bit_length()
4780
479>>> _g.xmpz(12345).bit_length()
48014
481>>> _g.bit_mask(9)
482mpz(511)
483>>> _g.xbit_mask(9)
484xmpz(511)
485'''
486
487__test__['format']=\
488r'''
489>>> a=_g.xmpz(123)
490>>> b=_g.xmpz(456)
491>>> str(a)
492'123'
493>>> repr(a)
494'xmpz(123)'
495>>> hex(a)
496'0x7b'
497>>> oct(a)
498'0o173'
499>>> _g.xmpz('123')
500xmpz(123)
501>>> _g.xmpz('1001001011',2)
502xmpz(587)
503>>> _g.xmpz('0b1001001011')
504xmpz(587)
505>>> _g.xmpz('1001001011',2).digits(2)
506'0b1001001011'
507>>> for i in range(2,63):
508...     print(a.digits(i))
509...
5100b1111011
51111120
5121323
513443
514323
515234
5160o173
517146
518123
519102
520a3
52196
5228b
52383
5240x7b
52574
5266f
52769
52863
5295i
5305d
53158
53253
5334n
5344j
5354f
5364b
53747
53843
5393u
5403r
5413o
5423l
5433i
5443f
5453C
54639
54736
54833
54930
5502d
5512b
5522Z
5532X
5542V
5552T
5562R
5572P
5582N
5592L
5602J
5612H
5622F
5632D
5642B
56529
56627
56725
56823
56921
5701z
571>>> print(a.digits(63))
572Traceback (innermost last):
573  ...
574ValueError: base must be in the interval 2 ... 62
575>>> _g.xmpz('43')
576xmpz(43)
577>>> _g.xmpz('043')
578xmpz(43)
579>>> _g.xmpz('43',10)
580xmpz(43)
581>>> _g.xmpz('0o43',0)
582xmpz(35)
583>>> _g.xmpz('0x43')
584xmpz(67)
585>>> _g.xmpz('0x43',10)
586Traceback (innermost last):
587  File "<pyshell#181>", line 1, in ?
588    _g.xmpz('0x43')
589ValueError: invalid digits
590>>>
591'''
592
593__test__['number']=\
594r'''
595>>> a=_g.xmpz(123)
596>>> b=_g.xmpz(456)
597>>> _g.is_square(a)
5980
599>>> _g.is_power(a)
6000
601>>> _g.is_square(99*99)
6021
603>>> _g.is_square(99*99*99)
6040
605>>> _g.is_square(0)
6061
607>>> _g.is_square(-1)
6080
609>>> _g.is_power(99*99*99)
6101
611>>> _g.gcd(a,b)
612mpz(3)
613>>> temp=_g.gcdext(a,b)
614>>> temp[0]==a*temp[1]+b*temp[2]
615True
616>>> _g.lcm(a,b)
617mpz(18696)
618>>> _g.fac(7)
619mpz(5040)
620>>> _g.fib(17)
621mpz(1597)
622>>> del temp
623>>> for i in range(10):
624...     print(_g.bincoef(10,i))
625...
6261
62710
62845
629120
630210
631252
632210
633120
63445
63510
636>>> _g.divm(b,a,_g.xmpz(20))
637mpz(12)
638>>> _g.divm(a,b,100)
639Traceback (innermost last):
640  File "<pyshell#184>", line 1, in ?
641    _g.divm(a,b,100)
642ZeroDivisionError: not invertible
643>>> _g.divm(6,12,14)
644mpz(4)
645>>> _g.divm(0,1,2)
646mpz(0)
647'''
648
649def _test(chat=None):
650    if chat:
651        print("Unit tests for gmpy2 (xmpz functionality)")
652        print("    on Python %s" % sys.version)
653        print("Testing gmpy2 {0}".format(_g.version()))
654        print("  Mutliple-precision library:   {0}".format(_g.mp_version()))
655        print("  Floating-point library:       {0}".format(_g.mpfr_version()))
656        print("  Complex library:              {0}".format(_g.mpc_version()))
657        print("  Caching Values: (Number)      {0}".format(_g.get_cache()[0]))
658        print("  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1]))
659    thismod = sys.modules.get(__name__)
660    doctest.testmod(thismod, report=0)
661
662    if chat: print("Repeating tests, with caching disabled")
663    _g.set_cache(0,128)
664
665    sav = sys.stdout
666    class _Dummy:
667        encoding = None
668        def write(self,*whatever):
669            pass
670    try:
671        sys.stdout = _Dummy()
672        doctest.testmod(thismod, report=0)
673    finally:
674        sys.stdout = sav
675
676    if chat:
677        print()
678        print("Overall results for mpz:")
679    return doctest.master.summarize(chat)
680
681
682if __name__=='__main__':
683    _test(1)
684
685