summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2007-02-19 02:03:19 (GMT)
committerRaymond Hettinger <python@rcn.com>2007-02-19 02:03:19 (GMT)
commitd6fc72a5ae27048dae56c773896ccbd6152d9b9b (patch)
treefceb231e6ba8e5b85b6f5baab0a908d056b9390d /Lib
parentf7ccc101d2d623428dd03114bd8723946d5dbcae (diff)
downloadcpython-d6fc72a5ae27048dae56c773896ccbd6152d9b9b.zip
cpython-d6fc72a5ae27048dae56c773896ccbd6152d9b9b.tar.gz
cpython-d6fc72a5ae27048dae56c773896ccbd6152d9b9b.tar.bz2
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_set.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 23b4f11..cd96581 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -26,6 +26,14 @@ class ReprWrapper:
def __repr__(self):
return repr(self.value)
+class HashCountingInt(int):
+ 'int-like object that counts the number of times __hash__ is called'
+ def __init__(self, *args):
+ self.hash_count = 0
+ def __hash__(self):
+ self.hash_count += 1
+ return int.__hash__(self)
+
class TestJointOps(unittest.TestCase):
# Tests common to both set and frozenset
@@ -270,6 +278,18 @@ class TestJointOps(unittest.TestCase):
fo.close()
os.remove(test_support.TESTFN)
+ def test_do_not_rehash_dict_keys(self):
+ n = 10
+ d = dict.fromkeys(map(HashCountingInt, xrange(n)))
+ self.assertEqual(sum(elem.hash_count for elem in d), n)
+ s = self.thetype(d)
+ self.assertEqual(sum(elem.hash_count for elem in d), n)
+ s.difference(d)
+ self.assertEqual(sum(elem.hash_count for elem in d), n)
+ if hasattr(s, 'symmetric_difference_update'):
+ s.symmetric_difference_update(d)
+ self.assertEqual(sum(elem.hash_count for elem in d), n)
+
class TestSet(TestJointOps):
thetype = set