File: test_hash.py

package info (click to toggle)
python-gmpy 1.15-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 768 kB
  • ctags: 739
  • sloc: ansic: 9,767; python: 5,627; makefile: 38
file content (32 lines) | stat: -rw-r--r-- 1,120 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
31
32
# Hash test for Python 3.2.

import gmpy
import fractions

import sys
try:
    m = sys.hash_info.modulus
except NameError:
    print("new-style hash is not supported")
    sys.exit(0)

for s in [0, m//2, m, m*2, m*m]:
    for i in range(-10,10):
        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]:
            val = k*(s + i)
            assert hash(val) == hash(gmpy.mpz(val))

print("hash tests for integer values passed")

for d in [1, -2, 3, -47, m, m*2]:
    for s in [0, m//2, m, m*2, m*m]:
        for i in range(-10,10):
            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]:
                val = k*(s + i)
                if val:
                    assert hash(fractions.Fraction(d,val)) == hash(gmpy.mpq(d,val)), (d,val,hash(fractions.Fraction(d,val)),hash(gmpy.mpq(d,val)))
                if d:
                    assert hash(fractions.Fraction(val,d)) == hash(gmpy.mpq(val,d)), (val,d,hash(fractions.Fraction(val,d)),hash(gmpy.mpq(val,d)))


print("hash tests for rational values passed")