summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-12-10 10:12:33 (GMT)
committerGitHub <noreply@github.com>2024-12-10 10:12:33 (GMT)
commit4331832db02ff4a7598dcdd99cae31087173dce0 (patch)
tree1a0b95b73e42dc9bb23babc36130acddabb942cb /Lib
parent050d59bd1765de417bf4ec8b5c3cbdac65695f5e (diff)
downloadcpython-4331832db02ff4a7598dcdd99cae31087173dce0.zip
cpython-4331832db02ff4a7598dcdd99cae31087173dce0.tar.gz
cpython-4331832db02ff4a7598dcdd99cae31087173dce0.tar.bz2
gh-125420: implement `Sequence.count` API on `memoryview` objects (#125443)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_memoryview.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 8f07828..96320eb 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -90,6 +90,22 @@ class AbstractMemoryTests:
m = self._view(b)
self.assertEqual(list(m), [m[i] for i in range(len(m))])
+ def test_count(self):
+ for tp in self._types:
+ b = tp(self._source)
+ m = self._view(b)
+ l = m.tolist()
+ for ch in list(m):
+ self.assertEqual(m.count(ch), l.count(ch))
+
+ b = tp((b'a' * 5) + (b'c' * 3))
+ m = self._view(b) # may be sliced
+ l = m.tolist()
+ with self.subTest('count', buffer=b):
+ self.assertEqual(m.count(ord('a')), l.count(ord('a')))
+ self.assertEqual(m.count(ord('b')), l.count(ord('b')))
+ self.assertEqual(m.count(ord('c')), l.count(ord('c')))
+
def test_setitem_readonly(self):
if not self.ro_type:
self.skipTest("no read-only type to test")
@@ -464,6 +480,18 @@ class BaseMemoryviewTests:
def _check_contents(self, tp, obj, contents):
self.assertEqual(obj, tp(contents))
+ def test_count(self):
+ super().test_count()
+ for tp in self._types:
+ b = tp((b'a' * 5) + (b'c' * 3))
+ m = self._view(b) # should not be sliced
+ self.assertEqual(len(b), len(m))
+ with self.subTest('count', buffer=b):
+ self.assertEqual(m.count(ord('a')), 5)
+ self.assertEqual(m.count(ord('b')), 0)
+ self.assertEqual(m.count(ord('c')), 3)
+
+
class BaseMemorySliceTests:
source_bytes = b"XabcdefY"