diff options
author | Raymond Hettinger <python@rcn.com> | 2007-02-19 03:04:45 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-02-19 03:04:45 (GMT) |
commit | 1bff7969832877398b129a97ad8d307c27c80fba (patch) | |
tree | 392e02958b3be31f28a71c77815bda867698aab4 /Lib | |
parent | 497380f48c266a1d1addc4a20a93aef9a380ffdf (diff) | |
download | cpython-1bff7969832877398b129a97ad8d307c27c80fba.zip cpython-1bff7969832877398b129a97ad8d307c27c80fba.tar.gz cpython-1bff7969832877398b129a97ad8d307c27c80fba.tar.bz2 |
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_set.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 49bdec3..45f61b2 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 |