PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 1ms

/test3/test_hash.py

http://gmpy.googlecode.com/
Python | 32 lines | 24 code | 7 blank | 1 comment | 16 complexity | b1bc9c3502c4e8ea1e371dc08a8a95cb MD5 | raw file
 1# Hash test for Python 3.2.
 2
 3import gmpy2
 4import fractions
 5
 6import sys
 7try:
 8    m = sys.hash_info.modulus
 9except NameError:
10    print("new-style hash is not supported")
11    sys.exit(0)
12
13for s in [0, m//2, m, m*2, m*m, 7, 13, 19, 87907, 79797, 44*44]:
14    for i in range(-10,10):
15        for k in [-1, 1, 7, 11, -(2**15), 2**16, 2**30, 2**31, 2**32, 2**33, 2**61, -(2**62), 2**63, 2**64]:
16            val = k*(s + i)
17            assert hash(val) == hash(gmpy2.mpz(val)), (val, hash(val), hash(gmpy2.mpz(val)))
18
19print("hash tests for integer values passed")
20
21for d in [1, -2, 3, -47, m, m*2, 324, 797080, -979]:
22    for s in [0, m//2, m, m*2, m*m]:
23        for i in range(-10,10):
24            for k in [-1, 1, 7, 11, -(2**15), 2**16, 2**30, 2**31, 2**32, 2**33, 2**61, -(2**62), 2**63, 2**64, 131313164, -4643131646131346460964347]:
25                val = k*(s + i)
26                if val:
27                    assert hash(fractions.Fraction(d,val)) == hash(gmpy2.mpq(d,val)), (d,val,hash(fractions.Fraction(d,val)),hash(gmpy.mpq(d,val)))
28                if d:
29                    assert hash(fractions.Fraction(val,d)) == hash(gmpy2.mpq(val,d)), (val,d,hash(fractions.Fraction(val,d)),hash(gmpy.mpq(val,d)))
30
31
32print("hash tests for rational values passed")