1 | n/a | # |
---|
2 | n/a | # These tests require gmpy and test the limits of the 32-bit build. The |
---|
3 | n/a | # limits of the 64-bit build are so large that they cannot be tested |
---|
4 | n/a | # on accessible hardware. |
---|
5 | n/a | # |
---|
6 | n/a | |
---|
7 | n/a | import sys |
---|
8 | n/a | from decimal import * |
---|
9 | n/a | from gmpy import mpz |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | _PyHASH_MODULUS = sys.hash_info.modulus |
---|
13 | n/a | # hash values to use for positive and negative infinities, and nans |
---|
14 | n/a | _PyHASH_INF = sys.hash_info.inf |
---|
15 | n/a | _PyHASH_NAN = sys.hash_info.nan |
---|
16 | n/a | |
---|
17 | n/a | # _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS |
---|
18 | n/a | _PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS) |
---|
19 | n/a | |
---|
20 | n/a | def xhash(coeff, exp): |
---|
21 | n/a | sign = 1 |
---|
22 | n/a | if coeff < 0: |
---|
23 | n/a | sign = -1 |
---|
24 | n/a | coeff = -coeff |
---|
25 | n/a | if exp >= 0: |
---|
26 | n/a | exp_hash = pow(10, exp, _PyHASH_MODULUS) |
---|
27 | n/a | else: |
---|
28 | n/a | exp_hash = pow(_PyHASH_10INV, -exp, _PyHASH_MODULUS) |
---|
29 | n/a | hash_ = coeff * exp_hash % _PyHASH_MODULUS |
---|
30 | n/a | ans = hash_ if sign == 1 else -hash_ |
---|
31 | n/a | return -2 if ans == -1 else ans |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | x = mpz(10) ** 425000000 - 1 |
---|
35 | n/a | coeff = int(x) |
---|
36 | n/a | |
---|
37 | n/a | d = Decimal('9' * 425000000 + 'e-849999999') |
---|
38 | n/a | |
---|
39 | n/a | h1 = xhash(coeff, -849999999) |
---|
40 | n/a | h2 = hash(d) |
---|
41 | n/a | |
---|
42 | n/a | assert h2 == h1 |
---|