summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2024-12-10 02:48:38 (GMT)
committerGitHub <noreply@github.com>2024-12-10 02:48:38 (GMT)
commit58c753827ac7aa3d7f1495ac206c28bf2f6c67e8 (patch)
treee8aeddd9663ea5a9ef0bd6af10d1e19d9a73db30 /Lib/test
parent3b18af964da9814474a5db9e502962c7e0593e8d (diff)
downloadcpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.zip
cpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.tar.gz
cpython-58c753827ac7aa3d7f1495ac206c28bf2f6c67e8.tar.bz2
gh-125420: implement `Sequence.index` API on `memoryview` objects (#125446)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_memoryview.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 2d4bf5f..8f07828 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -15,6 +15,7 @@ import copy
import pickle
import struct
+from itertools import product
from test.support import import_helper
@@ -58,6 +59,31 @@ class AbstractMemoryTests:
for tp in self._types:
self.check_getitem_with_type(tp)
+ def test_index(self):
+ for tp in self._types:
+ b = tp(self._source)
+ m = self._view(b) # may be a sub-view
+ l = m.tolist()
+ k = 2 * len(self._source)
+
+ for chi in self._source:
+ if chi in l:
+ self.assertEqual(m.index(chi), l.index(chi))
+ else:
+ self.assertRaises(ValueError, m.index, chi)
+
+ for start, stop in product(range(-k, k), range(-k, k)):
+ index = -1
+ try:
+ index = l.index(chi, start, stop)
+ except ValueError:
+ pass
+
+ if index == -1:
+ self.assertRaises(ValueError, m.index, chi, start, stop)
+ else:
+ self.assertEqual(m.index(chi, start, stop), index)
+
def test_iter(self):
for tp in self._types:
b = tp(self._source)