summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-02-21 17:19:00 (GMT)
committerGitHub <noreply@github.com>2019-02-21 17:19:00 (GMT)
commit407c7343266eb3e5a2f5c1f4913082c84f8dd8a0 (patch)
treeec7a7cf9dc5bbcd4ecdd1b92475e6953632c67fd /Lib/test
parent86f093f71a594dcaf21b67ba13dda72863e9bde9 (diff)
downloadcpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.zip
cpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.tar.gz
cpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.tar.bz2
bpo-36057 Update docs and tests for ordering in collections.Counter [no behavior change] (#11962)
* Add tests for Counter order. No behavior change. * Update docs and tests * Fix doctest output and capitalization
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_collections.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 2d5a266..4f8a841 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -1881,6 +1881,63 @@ class TestCounter(unittest.TestCase):
self.assertRaises(TypeError, Counter, (), ())
self.assertRaises(TypeError, Counter.__init__)
+ def test_order_preservation(self):
+ # Input order dictates items() order
+ self.assertEqual(list(Counter('abracadabra').items()),
+ [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)])
+ # letters with same count: ^----------^ ^---------^
+
+ # Verify retention of order even when all counts are equal
+ self.assertEqual(list(Counter('xyzpdqqdpzyx').items()),
+ [('x', 2), ('y', 2), ('z', 2), ('p', 2), ('d', 2), ('q', 2)])
+
+ # Input order dictates elements() order
+ self.assertEqual(list(Counter('abracadabra simsalabim').elements()),
+ ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b','r',
+ 'r', 'c', 'd', ' ', 's', 's', 'i', 'i', 'm', 'm', 'l'])
+
+ # Math operations order first by the order encountered in the left
+ # operand and then by the order encounted in the right operand.
+ ps = 'aaabbcdddeefggghhijjjkkl'
+ qs = 'abbcccdeefffhkkllllmmnno'
+ order = {letter: i for i, letter in enumerate(dict.fromkeys(ps + qs))}
+ def correctly_ordered(seq):
+ 'Return true if the letters occur in the expected order'
+ positions = [order[letter] for letter in seq]
+ return positions == sorted(positions)
+
+ p, q = Counter(ps), Counter(qs)
+ self.assertTrue(correctly_ordered(+p))
+ self.assertTrue(correctly_ordered(-p))
+ self.assertTrue(correctly_ordered(p + q))
+ self.assertTrue(correctly_ordered(p - q))
+ self.assertTrue(correctly_ordered(p | q))
+ self.assertTrue(correctly_ordered(p & q))
+
+ p, q = Counter(ps), Counter(qs)
+ p += q
+ self.assertTrue(correctly_ordered(p))
+
+ p, q = Counter(ps), Counter(qs)
+ p -= q
+ self.assertTrue(correctly_ordered(p))
+
+ p, q = Counter(ps), Counter(qs)
+ p |= q
+ self.assertTrue(correctly_ordered(p))
+
+ p, q = Counter(ps), Counter(qs)
+ p &= q
+ self.assertTrue(correctly_ordered(p))
+
+ p, q = Counter(ps), Counter(qs)
+ p.update(q)
+ self.assertTrue(correctly_ordered(p))
+
+ p, q = Counter(ps), Counter(qs)
+ p.subtract(q)
+ self.assertTrue(correctly_ordered(p))
+
def test_update(self):
c = Counter()
c.update(self=42)