File: gmpy_test_cvr.py

package info (click to toggle)
python-gmpy2 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,628 kB
  • ctags: 1,123
  • sloc: ansic: 21,036; python: 5,846; makefile: 163
file content (476 lines) | stat: -rw-r--r-- 13,447 bytes parent folder | download
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
# partial unit test for gmpy2 extra cover
# relies on Tim Peters' "doctest.py" test-driver
r'''
>>> _g.version()
'2.0.3'
>>> int('gmpy2.c' in _g._cvsid())
1
'''

import gmpy2 as _g, doctest, sys
__test__={}

__test__['misc_stuff']=\
r'''
>>> print _g.mpfr(3.0)
3.0
>>> _g.mp_limbsize() in (32, 64)
True
>>> _g.mpz(u"123")
mpz(123)
>>> _g.mpq(u"12/37")
mpq(12,37)
>>> _g.mpfr(u"123")
mpfr('123.0')
>>> _g.next_prime(2357*7069-1) == 2357*7069
False
'''

# If the next_prime() test fails, you have encountered a bug in MPIR. Please
# see https://code.google.com/p/gmpy/issues/detail?id=76 for more details.

try:
    x = float('inf')
except ValueError:
    pass
else:
    __test__['infinity'] = \
r'''
>>> x = float('inf')
>>> n = float('nan')
>>> _g.mpfr(x)
mpfr('inf')
>>> _g.mpfr(-x)
mpfr('-inf')
>>> _g.mpq(x)
Traceback (most recent call last):
  ...
OverflowError: 'mpq' does not support Infinity
>>> _g.mpq(-x)
Traceback (most recent call last):
  ...
OverflowError: 'mpq' does not support Infinity
>>> _g.mpz(x)
Traceback (most recent call last):
  ...
OverflowError: 'mpz' does not support Infinity
>>> _g.mpz(-x)
Traceback (most recent call last):
  ...
OverflowError: 'mpz' does not support Infinity
>>> _g.mpfr(n)
mpfr('nan')
>>> _g.mpq(n)
Traceback (most recent call last):
  ...
ValueError: 'mpq' does not support NaN
>>> _g.mpz(n)
Traceback (most recent call last):
  ...
ValueError: 'mpz' does not support NaN
'''

__test__['user_errors']=\
r'''
>>> _g.version(23)
Traceback (most recent call last):
  ...
TypeError: version() takes no arguments (1 given)
>>> _g.mp_version(23)
Traceback (most recent call last):
  ...
TypeError: mp_version() takes no arguments (1 given)
>>> _g.get_cache(23)
Traceback (most recent call last):
  ...
TypeError: get_cache() takes no arguments (1 given)
>>> _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 size must between 0 and 1000
>>> _g.set_cache(-23,256)
Traceback (most recent call last):
  ...
ValueError: cache size must between 0 and 1000
>>> _g.set_cache(200,256000)
Traceback (most recent call last):
  ...
ValueError: object size must between 0 and 16384
>>> _g.context().precision = -1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid value for precision
>>> _g.mpz('12'+chr(0)+'34')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: string contains NULL characters
>>> _g.mpfr('12'+chr(0)+'34')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid digits
>>> _g.mpq('12'+chr(0)+'34')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: string contains NULL characters
>>> _g.mpq_from_old_binary('bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (too short)
>>> _g.mpq_from_old_binary('bologna')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid mpq binary (num len)
>>> _g.mpq_from_old_binary('\001\000\000\000\003\002')
mpq(3,2)
>>> _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: zero denominator in 'mpq'
>>> _g.mpfr([])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpfr() requires numeric or string argument
>>> _g.mpfr_from_old_binary('bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid mpf binary encoding (too short)
>>> _g.mpfr('bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid digits
>>> int(_g.mpz(1000L*1000*1000*1000*1000*1000*1000))
1000000000000000000000L
>>> _g.bit_scan0(12,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: starting bit must be >= 0
>>> _g.bit_scan1(12,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: starting bit must be >= 0
>>> _g.bit_test(12,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: bit_index must be >= 0
>>> _g.bit_set(12,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: bit_index must be >= 0
>>> _g.mpz(23).bit_set(12,1,2,3)
Traceback (most recent call last):
  ...
TypeError: bit_set() takes exactly one argument (4 given)
>>> _g.bit_set(12,1,2,3)
Traceback (most recent call last):
  ...
TypeError: bit_set() requires 'mpz','int' arguments
>>> _g.iroot(12,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: n must be > 0
>>> _g.iroot(12,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: n must be > 0
>>> _g.iroot(-12,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: iroot() of negative number
>>> _g.digits(3.14)
('31400000000000001', 1, 53)
>>> _g.digits(3,'peep')
Traceback (most recent call last):
  ...
TypeError: digits() requires 'int' argument for base
>>> _g.digits(3.14,'peep')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.digits(3,'peep')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: digits() requires 'int' argument for base
>>> _g.mpz(3).digits('bu')
Traceback (most recent call last):
  ...
TypeError: digits() requires 'int' argument for base
>>> _g.mpfr(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.mpfr(1))
mpz(3)
>>> _g.qdiv(3,1.0)
mpz(3)
>>> _g.qdiv(3,1L)
mpz(3)
>>> _g.qdiv(3)
mpz(3)
>>> _g.qdiv(3,'bu')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: second argument cannot be converted to 'mpq'
>>> _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 cannot be converted to 'mpq'
>>> _g.qdiv(1.0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: first argument cannot be converted to 'mpq'
>>> _g.qdiv(1,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: division or modulo by zero in qdiv
>>> _g.f2q(-1.0)
mpz(-1)
>>> _g.mpz(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: function takes at most 2 arguments (3 given)
>>> _g.mpz('bi','bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.mpz('bi',99)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: base for mpz() must be 0 or in the interval 2 ... 62
>>> _g.mpz(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpz() with non-string argument needs exactly 1 argument
>>> _g.mpz(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpz() requires numeric or string argument
>>> _g.mpq(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpq() requires 0, 1 or 2 arguments
>>> _g.mpq('bi','bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.mpq('bi',99)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: base for mpq() must be 0 or in the interval 2 ... 62
>>> _g.mpq(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpq() requires numeric or string argument
>>> _g.mpq(1,None)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpq() requires numeric or string argument
>>> _g.mpq(1,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: zero denominator in 'mpq'
>>> _g.mpfr()
mpfr('0.0')
>>> _g.mpfr(1,'bo')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.mpfr(1,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: precision for mpfr() must be >= 0
>>> _g.mpfr('ba',0,'bu')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: an integer is required
>>> _g.mpfr('ba',0,99)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: base for mpfr() must be 0 or in the interval 2 ... 62
>>> _g.mpfr(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: function takes at most 2 arguments (3 given)
>>> +_g.mpz(1)
mpz(1)
>>> +_g.mpfr(1)
mpfr('1.0')
>>> +_g.mpq(1)
mpq(1,1)
>>> _g.mpz(2)**-2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: pow() exponent cannot be negative
>>> _g.mpz(2)**_g.mpz(1000000000000000L*1000000000000000L)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: pow() outrageous exponent
>>> pow(_g.mpz(2),3,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: pow() 3rd argument cannot be 0
>>> pow(_g.mpz(2),3,-5)
mpz(-2)
>>> pow(_g.mpq(2),3,-5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: mpq.pow() no modulo allowed
>>> a=10000000000L**2
>>> _g.mpq(2)**a
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: mpq.pow() outrageous exponent
>>> _g.mpq(2)**_g.mpq(1,a)
mpfr('1.0')
>>> _g.mpq(2)**0
mpq(1,1)
>>> _g.mpq(2)**-1
mpq(1,2)
>>> _g.mpq(2)**_g.mpq(1,2)
mpfr('1.4142135623730951')
>>> _g.mpq(-2)**_g.mpq(1,2)
mpfr('nan')
>>> _g.mpq(0)**_g.mpq(1,2)
mpfr('0.0')
>>> _g.mpq(0)**-1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: mpq.pow() 0 base to negative exponent
>>> _g.mpq(-1)**-1
mpq(-1,1)
>>> _g.mpfr(9,100)**2
mpfr('81.0')
>>> _g.mpfr(9,100)**0.5
mpfr('3.0')
>>> _g.mpfr(9,100)**_g.mpfr(0.5)
mpfr('3.0')
>>> _g.mpfr(0)**2
mpfr('0.0')
>>> pow(_g.mpfr(2),3,-5)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: pow() 3rd argument not allowed unless all arguments are integers
>>> _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.mpfr(1)
mpfr('2.0')
>>> _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.mpfr(1)+'bu'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'mpfr' and 'str'
>>> _g.mpfr(1)+_g.mpq(2)
mpfr('3.0')
>>> divmod(_g.mpz(3),0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: division or modulo 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: fac() 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.isqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: isqrt() of negative number
>>> _g.isqrt_rem(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: isqrt_rem() of negative number
>>> _g.jacobi(23, -34)
Traceback (most recent call last):
  ...
ValueError: y must be odd and >0
>>> _g.legendre(23, -34)
Traceback (most recent call last):
  ...
ValueError: 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 gmpy2 (extra cover)"
        print "    on Python %s" % sys.version
        print "Testing gmpy2 {0}".format(_g.version())
        print "  Mutliple-precision library:   {0}".format(_g.mp_version())
        print "  Floating-point library:       {0}".format(_g.mpfr_version())
        print "  Complex library:              {0}".format(_g.mpc_version())
        print "  Caching Values: (Number)      {0}".format(_g.get_cache()[0])
        print "  Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1])

    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)