MCPcopy Index your code
hub / github.com/RustPython/RustPython / verify

Method verify

Lib/test/test_buffer.py:780–982  ·  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

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

Calls 15

lenFunction · 0.85
listClass · 0.85
isinstanceFunction · 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
unpack_fromMethod · 0.80
toreadonlyMethod · 0.80

Tested by

no test coverage detected