diff options
author | Raymond Hettinger <python@rcn.com> | 2011-10-19 13:40:37 -0700 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-10-19 13:40:37 -0700 |
commit | becd56822ac62ba2ddb4956e84f6854366945617 (patch) | |
tree | f1a399031f43a18d7784fe8f0c47d434320edb92 /Lib/test/test_collections.py | |
parent | 3bb8be6d78130dfcf49c4860f0009300508ff92b (diff) | |
download | cpython-git-becd56822ac62ba2ddb4956e84f6854366945617.tar.gz |
Issue #13121: Support in-place math operators for collections.Counter().
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r-- | Lib/test/test_collections.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 04c4d97c55..ec20938917 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -932,6 +932,27 @@ class TestCounter(unittest.TestCase): set_result = setop(set(p.elements()), set(q.elements())) self.assertEqual(counter_result, dict.fromkeys(set_result, 1)) + def test_inplace_operations(self): + elements = 'abcd' + for i in range(1000): + # test random pairs of multisets + p = Counter(dict((elem, randrange(-2,4)) for elem in elements)) + p.update(e=1, f=-1, g=0) + q = Counter(dict((elem, randrange(-2,4)) for elem in elements)) + q.update(h=1, i=-1, j=0) + for inplace_op, regular_op in [ + (Counter.__iadd__, Counter.__add__), + (Counter.__isub__, Counter.__sub__), + (Counter.__ior__, Counter.__or__), + (Counter.__iand__, Counter.__and__), + ]: + c = p.copy() + c_id = id(c) + regular_result = regular_op(c, q) + inplace_result = inplace_op(c, q) + self.assertEqual(inplace_result, regular_result) + self.assertEqual(id(inplace_result), c_id) + def test_subtract(self): c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40) c.subtract(a=1, b=2, c=-3, d=10, e=20, f=30, h=-50) |