|
| 1 | +import math |
1 | 2 | import sys |
2 | 3 | import unittest |
3 | 4 | from test.support import import_helper |
@@ -31,3 +32,46 @@ def test_hash_getfuncdef(self): |
31 | 32 | self.assertEqual(func_def.name, hash_info.algorithm) |
32 | 33 | self.assertEqual(func_def.hash_bits, hash_info.hash_bits) |
33 | 34 | self.assertEqual(func_def.seed_bits, hash_info.seed_bits) |
| 35 | + |
| 36 | + def test_hash_double(self): |
| 37 | + # Test PyHash_Double() |
| 38 | + hash_double = _testcapi.hash_double |
| 39 | + |
| 40 | + def check_number(value, expected): |
| 41 | + self.assertEqual(hash_double(value), (1, expected)) |
| 42 | + |
| 43 | + # test some integers |
| 44 | + integers = [ |
| 45 | + *range(1, 30), |
| 46 | + 2**30 - 1, |
| 47 | + 2 ** 233, |
| 48 | + int(sys.float_info.max), |
| 49 | + ] |
| 50 | + for x in integers: |
| 51 | + with self.subTest(x=x): |
| 52 | + check_number(float(x), hash(x)) |
| 53 | + check_number(float(-x), hash(-x)) |
| 54 | + |
| 55 | + # test positive and negative zeros |
| 56 | + check_number(float(0.0), 0) |
| 57 | + check_number(float(-0.0), 0) |
| 58 | + |
| 59 | + # test +inf and -inf |
| 60 | + inf = float("inf") |
| 61 | + check_number(inf, sys.hash_info.inf) |
| 62 | + check_number(-inf, -sys.hash_info.inf) |
| 63 | + |
| 64 | + # special float values: compare with Python hash() function |
| 65 | + special_values = ( |
| 66 | + math.nextafter(0.0, 1.0), # smallest positive subnormal number |
| 67 | + sys.float_info.min, # smallest positive normal number |
| 68 | + sys.float_info.epsilon, |
| 69 | + sys.float_info.max, # largest positive finite number |
| 70 | + ) |
| 71 | + for x in special_values: |
| 72 | + with self.subTest(x=x): |
| 73 | + check_number(x, hash(x)) |
| 74 | + check_number(-x, hash(-x)) |
| 75 | + |
| 76 | + # test not-a-number (NaN) |
| 77 | + self.assertEqual(hash_double(float('nan')), (0, sys.hash_info.nan)) |
0 commit comments