# check that hash(d) == hash(int(d)) for integral values
for value in test_values:
- self.assertEqual(hashit(value), hashit(int(value)))
-
- #the same hash that to an int
- self.assertEqual(hashit(Decimal(23)), hashit(23))
- self.assertRaises(TypeError, hash, Decimal('sNaN'))
- self.assertTrue(hashit(Decimal('Inf')))
- self.assertTrue(hashit(Decimal('-Inf')))
+ self.assertEqual(hashit(value), hash(int(value)))
# check that the hashes of a Decimal float match when they
# represent exactly the same values
for s in test_strings:
f = float(s)
d = Decimal(s)
- self.assertEqual(hashit(f), hashit(d))
+ self.assertEqual(hashit(d), hash(f))
with localcontext() as c:
# check that the value of the hash doesn't depend on the
x = 1100 ** 1248
self.assertEqual(hashit(Decimal(x)), hashit(x))
+ def test_hash_method_nan(self):
+ Decimal = self.decimal.Decimal
+ self.assertRaises(TypeError, hash, Decimal('sNaN'))
+ value = Decimal('NaN')
+ self.assertEqual(hash(value), object.__hash__(value))
+ class H:
+ def __hash__(self):
+ return 42
+ class D(Decimal, H):
+ pass
+ value = D('NaN')
+ self.assertEqual(hash(value), object.__hash__(value))
+
def test_min_and_max_methods(self):
Decimal = self.decimal.Decimal
#self.assertTrue(0.0 < pow_op(2.0, -1047) < 1e-315)
#self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)
+ def test_hash(self):
+ for x in range(-30, 30):
+ self.assertEqual(hash(float(x)), hash(x))
+ self.assertEqual(hash(float(sys.float_info.max)),
+ hash(int(sys.float_info.max)))
+ self.assertEqual(hash(float('inf')), sys.hash_info.inf)
+ self.assertEqual(hash(float('-inf')), -sys.hash_info.inf)
+
+ def test_hash_nan(self):
+ value = float('nan')
+ self.assertEqual(hash(value), object.__hash__(value))
+ class H:
+ def __hash__(self):
+ return 42
+ class F(float, H):
+ pass
+ value = F('nan')
+ self.assertEqual(hash(value), object.__hash__(value))
+
@requires_setformat
class FormatFunctionsTestCase(unittest.TestCase):