/test2/gmpy_test_mpz.py

http://gmpy.googlecode.com/ · Python · 849 lines · 796 code · 3 blank · 50 comment · 21 complexity · fb8de7d5e89b132168d104e61447fcfd MD5 · raw file

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