/test3/test_hash.py

http://gmpy.googlecode.com/ · Python · 32 lines · 24 code · 7 blank · 1 comment · 11 complexity · b1bc9c3502c4e8ea1e371dc08a8a95cb MD5 · raw file

  1. # Hash test for Python 3.2.
  2. import gmpy2
  3. import fractions
  4. import sys
  5. try:
  6. m = sys.hash_info.modulus
  7. except NameError:
  8. print("new-style hash is not supported")
  9. sys.exit(0)
  10. for s in [0, m//2, m, m*2, m*m, 7, 13, 19, 87907, 79797, 44*44]:
  11. for i in range(-10,10):
  12. 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]:
  13. val = k*(s + i)
  14. assert hash(val) == hash(gmpy2.mpz(val)), (val, hash(val), hash(gmpy2.mpz(val)))
  15. print("hash tests for integer values passed")
  16. for d in [1, -2, 3, -47, m, m*2, 324, 797080, -979]:
  17. for s in [0, m//2, m, m*2, m*m]:
  18. for i in range(-10,10):
  19. 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]:
  20. val = k*(s + i)
  21. if val:
  22. assert hash(fractions.Fraction(d,val)) == hash(gmpy2.mpq(d,val)), (d,val,hash(fractions.Fraction(d,val)),hash(gmpy.mpq(d,val)))
  23. if d:
  24. assert hash(fractions.Fraction(val,d)) == hash(gmpy2.mpq(val,d)), (val,d,hash(fractions.Fraction(val,d)),hash(gmpy.mpq(val,d)))
  25. print("hash tests for rational values passed")