diff options
author | Yury Selivanov <yury@magic.io> | 2018-01-22 16:54:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-22 16:54:41 (GMT) |
commit | b0a7a037b8fde56b62f886d5188bced7776777b4 (patch) | |
tree | a9722f72b836b19446e9e716fbf7b45702664e20 /Lib/test | |
parent | a4b1bb4801f7a941ff9e86b96da539be1c288833 (diff) | |
download | cpython-b0a7a037b8fde56b62f886d5188bced7776777b4.zip cpython-b0a7a037b8fde56b62f886d5188bced7776777b4.tar.gz cpython-b0a7a037b8fde56b62f886d5188bced7776777b4.tar.bz2 |
bpo-31179: Make dict.copy() up to 5.5 times faster. (#3067)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_dict.py | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py index 4386eda..aa149d3 100644 --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -271,11 +271,57 @@ class DictTest(unittest.TestCase): self.assertEqual(baddict3.fromkeys({"a", "b", "c"}), res) def test_copy(self): - d = {1:1, 2:2, 3:3} - self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) + d = {1: 1, 2: 2, 3: 3} + self.assertIsNot(d.copy(), d) + self.assertEqual(d.copy(), d) + self.assertEqual(d.copy(), {1: 1, 2: 2, 3: 3}) + + copy = d.copy() + d[4] = 4 + self.assertNotEqual(copy, d) + self.assertEqual({}.copy(), {}) self.assertRaises(TypeError, d.copy, None) + def test_copy_fuzz(self): + for dict_size in [10, 100, 1000, 10000, 100000]: + dict_size = random.randrange( + dict_size // 2, dict_size + dict_size // 2) + with self.subTest(dict_size=dict_size): + d = {} + for i in range(dict_size): + d[i] = i + + d2 = d.copy() + self.assertIsNot(d2, d) + self.assertEqual(d, d2) + d2['key'] = 'value' + self.assertNotEqual(d, d2) + self.assertEqual(len(d2), len(d) + 1) + + def test_copy_maintains_tracking(self): + class A: + pass + + key = A() + + for d in ({}, {'a': 1}, {key: 'val'}): + d2 = d.copy() + self.assertEqual(gc.is_tracked(d), gc.is_tracked(d2)) + + def test_copy_noncompact(self): + # Dicts don't compact themselves on del/pop operations. + # Copy will use a slow merging strategy that produces + # a compacted copy when roughly 33% of dict is a non-used + # keys-space (to optimize memory footprint). + # In this test we want to hit the slow/compacting + # branch of dict.copy() and make sure it works OK. + d = {k: k for k in range(1000)} + for k in range(950): + del d[k] + d2 = d.copy() + self.assertEqual(d2, d) + def test_get(self): d = {} self.assertIs(d.get('c'), None) |