1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
# partial unit test for gmpy 1.11 extra cover
# relies on Tim Peters' "doctest.py" test-driver
# test-version 1.11
r'''
>>> _g.gmp_version()[:3] in ('5.0', '4.3', '4.2', '4.1', '4.0', '3.1', '')
True
>>> _g.mpir_version()[:3] in ('0.9', '1.0', '1.1', '1.2', '1.3', '')
True
>>> _g.version()
'1.11'
>>> int('gmpy.c' in _g._cvsid())
1
'''
import gmpy as _g, doctest, sys
__test__={}
r = _g.rand
__test__['misc_stuff']=\
r'''
>>> junk=_g.set_debug(0)
>>> knuj=_g.set_debug(junk)
>>> junk==_g.set_debug(junk)
1
>>> _g.set_fcoform(None)
>>> _g.set_fcoform()
>>> print(_g.mpf(3.0))
3.0
>>> _g.gmp_limbsize() in (32, 64)
True
'''
try:
x = float('inf')
except ValueError:
pass
else:
__test__['infinity'] = \
r'''
>>> x = float('inf')
>>> n = float('nan')
>>> _g.mpf(x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpf(-x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpq(x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpq(-x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpz(x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpz(-x)
Traceback (most recent call last):
...
ValueError: gmpy does not handle infinity
>>> _g.mpf(n)
Traceback (most recent call last):
...
ValueError: gmpy does not handle nan
>>> _g.mpf(n)
Traceback (most recent call last):
...
ValueError: gmpy does not handle nan
>>> _g.mpf(n)
Traceback (most recent call last):
...
ValueError: gmpy does not handle nan
'''
__test__['user_errors']=\
r'''
>>> _g.version(23)
Traceback (most recent call last):
...
TypeError: version expects 0 arguments
>>> _g.gmp_version(23)
Traceback (most recent call last):
...
TypeError: gmp_version expects 0 arguments
>>> _g.get_cache(23)
Traceback (most recent call last):
...
TypeError: get_cache expects 0 arguments
>>> _g.set_cache()
Traceback (most recent call last):
...
TypeError: function takes exactly 2 arguments (0 given)
>>> _g.set_cache(200)
Traceback (most recent call last):
...
TypeError: function takes exactly 2 arguments (1 given)
>>> _g.set_cache(200,-23)
Traceback (most recent call last):
...
ValueError: object size must between 0 and 16384
>>> _g.set_cache(2000,256)
Traceback (most recent call last):
...
ValueError: cache must between 0 and 1000
>>> _g.set_cache(-23,256)
Traceback (most recent call last):
...
ValueError: cache must between 0 and 1000
>>> _g.set_cache(200,256000)
Traceback (most recent call last):
...
ValueError: object size must between 0 and 16384
>>> _g.set_debug()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes exactly 1 argument (0 given)
>>> _g.set_debug(2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes exactly 1 argument (2 given)
>>> _g.set_debug('boh')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.set_minprec(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: minimum precision must be >= 0
>>> _g.set_fcoform(33)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: number of digits n must be 0<n<=30
>>> _g.set_fcoform([])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: set_fcoform argument must be int, string, or None
>>> _g.mpz('12'+chr(0)+'34')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: string without NULL characters expected
>>> _g.mpf('12'+chr(0)+'34')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: string without NULL characters expected
>>> _g.mpq('12'+chr(0)+'34')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: string without NULL characters expected
>>> _g.mpq('bo',256)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (too short)
>>> _g.mpq('bologna',256)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (num len)
>>> _g.mpq(b'\001\000\000\000\003\002',256)
mpq(3,2)
>>> _g.mpq(b'\002\000\000\000\003\377\002',256)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (num sgn)
>>> _g.mpq(b'\001\000\000\000\003\002\377',256)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (den sgn)
>>> _g.mpq('ba/bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid digits
>>> print('ba/bo')
ba/bo
>>> _g.mpq('1/bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid digits
>>> print('1/bo')
1/bo
>>> _g.mpq('1/0')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: mpq: zero denominator
>>> _g.mpf([])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpf() expects numeric or string argument
>>> _g.mpf('bo',0,256)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: string too short to be a gmpy.mpf binary encoding
>>> _g.mpf('bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid digits
>>> int(_g.mpz(1000*1000*1000*1000*1000*1000*1000))
1000000000000000000000
>>> _g.scan0(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: starting bit must be >= 0
>>> _g.scan1(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: starting bit must be >= 0
>>> _g.lowbits(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: nbits must be > 0
>>> _g.getbit(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: bit_index must be >= 0
>>> _g.setbit(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: bit_index must be >= 0
>>> _g.mpz(23).setbit(12,1,2,3)
Traceback (most recent call last):
...
TypeError: setbit() expects 'mpz','int'[,'int'] arguments
>>> _g.setbit(12,1,2,3)
Traceback (most recent call last):
...
TypeError: setbit() expects 'mpz','int'[,'int'] arguments
>>> _g.root(12,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: n must be > 0
>>> _g.root(12,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: n must be > 0
>>> _g.root(-12,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: root of negative number
>>> _g.digits(3,'peep')
Traceback (most recent call last):
...
TypeError: digits() expects 'mpz',['int'] arguments
>>> _g.digits(3.14)
Traceback (most recent call last):
...
TypeError: digits() expects 'mpz',['int'] arguments
>>> _g.fdigits(3.14,'peep')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.qdigits(3.14)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument can not be converted to mpq
>>> _g.qdigits(3,'peep')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.mpz(3).digits('bu')
Traceback (most recent call last):
...
TypeError: digits() expects 'mpz',['int'] arguments
>>> _g.mpf(3).digits('bu')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.qdiv(3,_g.mpq(1))
mpz(3)
>>> _g.qdiv(3,_g.mpz(1))
mpz(3)
>>> _g.qdiv(3,_g.mpf(1))
mpz(3)
>>> _g.qdiv(3,1.0)
mpz(3)
>>> _g.qdiv(3,1)
mpz(3)
>>> _g.qdiv(3)
mpz(3)
>>> _g.qdiv(3,'bu')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: second argument can not be converted to mpq
>>> _g.mpq(2).qdiv(3,4)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at most 1 argument (2 given)
>>> _g.qdiv(3,4,5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes at most 2 arguments (3 given)
>>> _g.qdiv('bu')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: first argument can not be converted to mpq
>>> _g.qdiv(1.0,1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: first argument can not be converted to mpq
>>> _g.qdiv(1,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: qdiv: zero divisor
>>> _g.f2q(-1.0)
mpz(-1)
>>> _g.mpz(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpz() requires 1 or 2 arguments
>>> _g.mpz('bi','bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpz(): base must be an integer
>>> _g.mpz('bi',99)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: base for gmpy.mpz must be 0, 256, or in the interval 2 ... 36 .
>>> _g.mpz(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpz() with numeric argument needs exactly 1 argument
>>> _g.mpz(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpz() expects numeric or string argument
>>> _g.mpq(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpq() requires 1 or 2 arguments
>>> _g.mpq('bi','bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpq(): base must be an integer
>>> _g.mpq('bi',99)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: base for gmpy.mpq() must be 0, 256, or in the interval 2 ... 36 .
>>> _g.mpq(None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpq() expects numeric or string argument
>>> _g.mpq(1,None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: argument can not be converted to mpq
>>> _g.mpq(1,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: mpq: zero denominator
>>> _g.mpf()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpf() requires 1 to 3 arguments
>>> _g.mpf(1,'bo')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpf(): bits must be an integer
>>> _g.mpf(1,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: bits for gmpy.mpf must be >= 0
>>> _g.mpf('ba',0,'bu')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpf(): base must be an integer
>>> _g.mpf('ba',0,99)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: base for gmpy.mpf must be 0, 256, or in the interval 2 ... 36 .
>>> _g.mpf(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: gmpy.mpf() with numeric 1st argument needs 1 or 2 arguments
>>> +_g.mpz(1)
mpz(1)
>>> +_g.mpf(1)
mpf('1.e0')
>>> +_g.mpq(1)
mpq(1,1)
>>> _g.mpz(2)**-2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpz.pow with negative power
>>> _g.mpz(2)**_g.mpz(1000000*10000000000000)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpz.pow outrageous exponent
>>> pow(_g.mpz(2),3,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpz.pow divide by zero
>>> pow(_g.mpz(2),3,-5)
mpz(-2)
>>> pow(_g.mpq(2),3,-5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpq.pow no modulo allowed
>>> a=10000000000**2
>>> _g.mpq(2)**a
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpq.pow outrageous exp num
>>> _g.mpq(2)**_g.mpq(1,a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpq.pow outrageous exp den
>>> _g.mpq(2)**0
mpq(1,1)
>>> _g.mpq(2)**-1
mpq(1,2)
>>> _g.mpq(2)**_g.mpq(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpq.pow fractional exponent, inexact-root
>>> _g.mpq(-2)**_g.mpq(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpq.pow fractional exponent, nonreal-root
>>> _g.mpq(0)**_g.mpq(1,2)
mpq(0,1)
>>> _g.mpq(0)**-1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: mpq.pow 0 base to <0 exponent
>>> _g.mpq(-1)**-1
mpq(-1,1)
>>> _g.mpf(9,100)**2
mpf('8.1e1',100)
>>> _g.mpf(9,100)**0.5
mpf('3.e0',100)
>>> _g.mpf(9,100)**_g.mpf(0.5)
mpf('3.e0')
>>> _g.mpf(0)**2
mpf('0.e0')
>>> pow(_g.mpf(2),3,-5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: mpf.pow no modulo allowed
>>> _g.mpz(1)+'bu'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'mpz' and 'str'
>>> _g.mpz(1)+_g.mpf(1)
mpf('2.e0')
>>> _g.mpz(1)+_g.mpq(1)
mpq(2,1)
>>> _g.mpq(1)+'bu'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'mpq' and 'str'
>>> _g.mpf(1)+'bu'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'mpf' and 'str'
>>> _g.mpf(1)+_g.mpq(2)
mpf('3.e0')
>>> divmod(_g.mpz(3),0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: mpz divmod by zero
>>> divmod(_g.mpz(0),3)
(mpz(0), mpz(0))
>>> _g.divm(1,2,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: not invertible
>>> abs(_g.mpq(0))
mpq(0,1)
>>> _g.mpz(0)**2
mpz(0)
>>> _g.mpq(-2)**0
mpq(1,1)
>>> _g.fac(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: factorial of negative number
>>> _g.fib(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: Fibonacci of negative number
>>> _g.comb(7,-3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: binomial coefficient with negative k
>>> _g.sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: sqrt of negative number
>>> _g.sqrtrem(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: sqrt of negative number
>>> _g.fsqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: sqrt of negative number
>>> _g.jacobi(23, -34)
Traceback (most recent call last):
...
ValueError: jacobi's y must be odd prime > 0
>>> _g.legendre(23, -34)
Traceback (most recent call last):
...
ValueError: legendre's y must be odd and > 0
>>> # guard against conversion error on 64-bit systems
>>> _g.mpz(2**32) != _g.mpz(0)
True
>>> # test hash properties on 64-bit systems
>>> temp = 123456789012345678901234567890
>>> hash(temp) == hash(_g.mpz(temp))
True
>>> del temp
'''
def _test(chat=None):
if chat:
print("Unit tests for gmpy 1.11 (extra cover)")
print(" running on Python", sys.version)
print()
if _g.gmp_version():
print("Testing gmpy %s (GMP %s), default caching (%s, %s)" % (
(_g.version(), _g.gmp_version(), _g.get_cache()[0],
_g.get_cache()[1])))
else:
print("Testing gmpy %s (MPIR %s), default caching (%s, %s)" % (
(_g.version(), _g.mpir_version(), _g.get_cache()[0],
_g.get_cache()[1])))
print(__test__.keys())
thismod = sys.modules.get(__name__)
doctest.testmod(thismod, report=0)
if chat:
print()
print("Overall results for cvr:")
return doctest.master.summarize(chat)
if __name__=='__main__':
_test(1)
|