PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/test3/test_leaks.py

http://gmpy.googlecode.com/
Python | 79 lines | 63 code | 13 blank | 3 comment | 13 complexity | 4e3d50464948aaa87ae9d4b57b75fc34 MD5 | raw file
 1from __future__ import print_function
 2import gmpy2
 3import fractions
 4import decimal
 5
 6MPZ = gmpy2.mpz
 7MPQ = gmpy2.mpq
 8MPF = gmpy2.mpf
 9FR = fractions.Fraction
10DC = decimal.Decimal
11
12int_vals = []
13int_vals.extend([1, 2, 3, -10, 256, 23**65])
14int_vals.extend([MPZ(1), MPZ(2), MPZ(3), MPZ(-10), MPZ(23**65)])
15
16float_vals = []
17float_vals.extend([1.23, -3.124159, 0.0234, float("nan")])
18float_vals.extend([MPF(1.23), MPF("-3.124159"), MPF("0.0234")])
19
20frac_vals = []
21frac_vals.extend([FR(1,2), FR(-7,2345)])
22frac_vals.extend([MPQ(1,2), MPQ(-7,2345)])
23
24all_vals = int_vals + float_vals + frac_vals
25
26def test_leaks1(bits = 80, chunk = 150, terms = 20):
27    """Test gmpy2.pack and gmpy2.unpack."""
28
29    for t in range(2, terms):
30        for b in range(1, bits):
31            # Test with all bits set to 1.
32            v = [ 2**b - 1 ] * t
33            for c in range(b, chunk):
34                temp = gmpy2.pack(v, c)
35                u = gmpy2.unpack(temp, c)
36                assert u == v, (v, temp, u, (t, b, c))
37
38def test_leaks2():
39    """Test binary operations."""
40
41    def test_binary(a, b):
42        t = a + b
43        t = a - b
44        t = a * b
45        try:
46            t = a / b
47        except:
48            pass
49        try:
50            t = a // b
51        except:
52            pass
53        try:
54            t = divmod(a, b)
55        except:
56            pass
57        t = gmpy2.add(a, b)
58        t = gmpy2.sub(a, b)
59        t = gmpy2.mul(a, b)
60        t = gmpy2.div(a, b)
61        t = gmpy2.agm(a, b)
62        t = gmpy2.atan2(a, b)
63
64    for x in all_vals:
65        for y in all_vals:
66            test_binary(x, y)
67            test_binary(y, x)
68
69
70def main(count = 100):
71    print("Testing for memory leaks by repeating a set of calculations.")
72    print("This test may take a few minutes.")
73    for i in range(count):
74        print("Pass: {0}".format(i))
75        test_leaks2()
76    print("Test successful.")
77
78if __name__ == "__main__":
79    main()