From 7642044a0c6dd484e259933fce976b2725f9ac59 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 13 Dec 2018 12:48:29 -0500 Subject: Add MapMutation.update(); make creating Map from a Map faster; fix bugs * Add new MapMutation.update() method that behaves like MutableMapping.update() * Make it faster to create a Map() from another Map() -- it's now an O(1) operation. * update() method had a bug that could cause the update Map object to have a wrong number of elements. --- tests/test_map.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/test_map.py b/tests/test_map.py index 959e00c..66d07e7 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -1089,11 +1089,6 @@ class BaseMapTest: with self.assertRaises(HashingError): self.Map(src) - src = self.Map({key1: 123}) - with HashKeyCrasher(error_on_hash=True): - with self.assertRaises(HashingError): - self.Map(src) - src = [(1, 2), (key1, 123)] with HashKeyCrasher(error_on_hash=True): with self.assertRaises(HashingError): @@ -1195,6 +1190,47 @@ class BaseMapTest: self.assertEqual(mm.finish(), self.Map(z=100, b=2)) self.assertEqual(m, self.Map(a=1, b=2)) + def test_map_mut_16(self): + m = self.Map(a=1, b=2) + hash(m) + + m2 = self.Map(m) + m3 = self.Map(m, c=3) + + self.assertEqual(m, m2) + self.assertEqual(len(m), len(m2)) + self.assertEqual(hash(m), hash(m2)) + + self.assertIsNot(m, m2) + self.assertEqual(m3, self.Map(a=1, b=2, c=3)) + + def test_map_mut_17(self): + m = self.Map(a=1) + with m.mutate() as mm: + with self.assertRaisesRegex( + TypeError, 'cannot create Maps from MapMutations'): + self.Map(mm) + + def test_map_mut_18(self): + m = self.Map(a=1, b=2) + with m.mutate() as mm: + mm.update(self.Map(x=1), z=2) + mm.update(c=3) + mm.update({'n': 100, 'a': 20}) + m2 = mm.finish() + + expected = self.Map( + {'b': 2, 'c': 3, 'n': 100, 'z': 2, 'x': 1, 'a': 20}) + + self.assertEqual(len(m2), 6) + self.assertEqual(m2, expected) + self.assertEqual(m, self.Map(a=1, b=2)) + + def test_map_mut_19(self): + m = self.Map(a=1, b=2) + m2 = m.update({'a': 20}) + self.assertEqual(len(m2), 2) + def test_map_mut_stress(self): COLLECTION_SIZE = 7000 TEST_ITERS_EVERY = 647 -- cgit v1.2.3