MCPcopy Index your code
hub / github.com/python/cpython / verify

Method verify

Lib/test/test_buffer.py:792–994  ·  view source on GitHub ↗
(self, result, *, obj,
                     itemsize, fmt, readonly,
                     ndim, shape, strides,
                     lst, sliced=False, cast=False)

Source from the content-addressed store, hash-verified

790 self.sizeof_void_p = get_sizeof_void_p()
791
792 def verify(self, result, *, obj,
793 itemsize, fmt, readonly,
794 ndim, shape, strides,
795 lst, sliced=False, cast=False):
796 # Verify buffer contents against expected values.
797 if shape:
798 expected_len = prod(shape)*itemsize
799 else:
800 if not fmt: # array has been implicitly cast to unsigned bytes
801 expected_len = len(lst)
802 else: # ndim = 0
803 expected_len = itemsize
804
805 # Reconstruct suboffsets from strides. Support for slicing
806 # could be added, but is currently only needed for test_getbuf().
807 suboffsets = ()
808 if result.suboffsets:
809 self.assertGreater(ndim, 0)
810
811 suboffset0 = 0
812 for n in range(1, ndim):
813 if shape[n] == 0:
814 break
815 if strides[n] <= 0:
816 suboffset0 += -strides[n] * (shape[n]-1)
817
818 suboffsets = [suboffset0] + [-1 for v in range(ndim-1)]
819
820 # Not correct if slicing has occurred in the first dimension.
821 stride0 = self.sizeof_void_p
822 if strides[0] < 0:
823 stride0 = -stride0
824 strides = [stride0] + list(strides[1:])
825
826 self.assertIs(result.obj, obj)
827 self.assertEqual(result.nbytes, expected_len)
828 self.assertEqual(result.itemsize, itemsize)
829 self.assertEqual(result.format, fmt)
830 self.assertIs(result.readonly, readonly)
831 self.assertEqual(result.ndim, ndim)
832 self.assertEqual(result.shape, tuple(shape))
833 if not (sliced and suboffsets):
834 self.assertEqual(result.strides, tuple(strides))
835 self.assertEqual(result.suboffsets, tuple(suboffsets))
836
837 if isinstance(result, ndarray) or is_memoryview_format(fmt):
838 rep = result.tolist() if fmt else result.tobytes()
839 self.assertEqual(rep, lst)
840
841 if not fmt: # array has been cast to unsigned bytes,
842 return # the remaining tests won't work.
843
844 # PyBuffer_GetPointer() is the definition how to access an item.
845 # If PyBuffer_GetPointer(indices) is correct for all possible
846 # combinations of indices, the buffer is correct.
847 #
848 # Also test tobytes() against the flattened 'lst', with all items
849 # packed to bytes.

Calls 15

listClass · 0.85
is_memoryview_formatFunction · 0.85
indicesFunction · 0.85
get_itemFunction · 0.85
flattenFunction · 0.85
transposeFunction · 0.85
assertGreaterMethod · 0.80
assertTrueMethod · 0.80
prodFunction · 0.70
assertIsMethod · 0.45
assertEqualMethod · 0.45
packMethod · 0.45

Tested by

no test coverage detected