Skip to content

Commit

Permalink
Merge pull request #23 from NextThought/issue21
Browse files Browse the repository at this point in the history
Fix tests for TimeStamp hashcode under 32-bit Pythons.
  • Loading branch information
tseaver committed Apr 10, 2015
2 parents cad848a + cd847ce commit 7f673b5
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions persistent/tests/test_timestamp.py
Expand Up @@ -272,7 +272,9 @@ def test_py_hash_32_64_bit(self):
self.assertEqual(hash(py), bit_32_hash)

persistent.timestamp.c_long = ctypes.c_int64
self.assertEqual(hash(py), bit_64_hash)
# call __hash__ directly to avoid interpreter truncation
# in hash() on 32-bit platforms
self.assertEqual(py.__hash__(), bit_64_hash)
finally:
persistent.timestamp.c_long = orig_c_long

Expand All @@ -286,6 +288,10 @@ def test_py_hash_32_64_bit(self):
def test_hash_equal_constants(self):
# The simple constants make it easier to diagnose
# a difference in algorithms
import persistent.timestamp
import ctypes
is_32_bit = persistent.timestamp.c_long == ctypes.c_int32

c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), 8)
self.assertEqual(hash(c), hash(py))
Expand All @@ -298,25 +304,41 @@ def test_hash_equal_constants(self):
self.assertEqual(hash(c), 1000011)
self.assertEqual(hash(c), hash(py))

# overflow kicks in here on 32-bit platforms
c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x00\x01\x00\x00')
self.assertEqual(hash(c), 1000006000001)
if is_32_bit:
self.assertEqual(hash(c), -721379967)
else:
self.assertEqual(hash(c), 1000006000001)
self.assertEqual(hash(c), hash(py))

c, py = self._make_C_and_Py(b'\x00\x00\x00\x00\x01\x00\x00\x00')
self.assertEqual(hash(c), 1000009000027000019)
if is_32_bit:
self.assertEqual(hash(c), 583896275)
else:
self.assertEqual(hash(c), 1000009000027000019)
self.assertEqual(hash(c), hash(py))

# Overflow kicks in at this point
# Overflow kicks in at this point on 64-bit platforms
c, py = self._make_C_and_Py(b'\x00\x00\x00\x01\x00\x00\x00\x00')
self.assertEqual(hash(c), -4442925868394654887)
if is_32_bit:
self.assertEqual(hash(c), 1525764953)
else:
self.assertEqual(hash(c), -4442925868394654887)
self.assertEqual(hash(c), hash(py))

c, py = self._make_C_and_Py(b'\x00\x00\x01\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), -3993531167153147845)
if is_32_bit:
self.assertEqual(hash(c), -429739973)
else:
self.assertEqual(hash(c), -3993531167153147845)
self.assertEqual(hash(c), hash(py))

c, py = self._make_C_and_Py(b'\x01\x00\x00\x00\x00\x00\x00\x00')
self.assertEqual(hash(c), -3099646879006235965)
if is_32_bit:
self.assertEqual(hash(c), 263152323)
else:
self.assertEqual(hash(c), -3099646879006235965)
self.assertEqual(hash(c), hash(py))

def test_ordering(self):
Expand Down

0 comments on commit 7f673b5

Please sign in to comment.