gh-125420: implement Sequence.index API on memoryview objects (#125446)

This commit is contained in:
Bénédikt Tran 2024-12-10 03:48:38 +01:00 committed by GitHub
parent 3b18af964d
commit 58c753827a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 171 additions and 1 deletions

View file

@ -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)