diff options
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) |