File: test_pack.py

package info (click to toggle)
python-gmpy2 2.1.0~a4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,048 kB
  • sloc: ansic: 24,051; python: 325; makefile: 117
file content (30 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (3)
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
from __future__ import print_function
import gmpy2

def test(repeat=1):
    """Test gmpy2.pack and gmpy2.unpack."""
    r = gmpy2.random_state(42)
    try:
        for counter in range(repeat):
            for t in (10, 30, 60, 500, 1000, 2000, 10000, 100000):
                v = gmpy2.mpz_rrandomb(r, t)
                for b in range(1, max(1001,t)):
                    temp = gmpy2.unpack(v, b)
                    u = gmpy2.pack(temp, b)
                    if u != v:
                        raise ValueError
        return True
    except ValueError:
        return False


if __name__ == "__main__":
    import sys
    try:
        repeat = abs(int(sys.argv[1]))
    except:
        repeat = 1
    print("Testing pack/unpack for a large number of values.")
    print("This test may take a few minutes.")
    test(repeat)
    print("Test successful.")