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

Method test_ndarray_random_slice_assign

Lib/test/test_buffer.py:1968–2028  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1966 self.assertTrue(numpy_err)
1967
1968 def test_ndarray_random_slice_assign(self):
1969 # valid slice assignments
1970 for _ in range(ITERATIONS):
1971 for fmt in fmtdict['@']:
1972 itemsize = struct.calcsize(fmt)
1973
1974 lshape, rshape, lslices, rslices = \
1975 rand_aligned_slices(maxdim=MAXDIM, maxshape=MAXSHAPE)
1976 tl = rand_structure(itemsize, True, shape=lshape)
1977 tr = rand_structure(itemsize, True, shape=rshape)
1978 self.assertTrue(verify_structure(*tl))
1979 self.assertTrue(verify_structure(*tr))
1980 litems = randitems_from_structure(fmt, tl)
1981 ritems = randitems_from_structure(fmt, tr)
1982
1983 xl = ndarray_from_structure(litems, fmt, tl)
1984 xr = ndarray_from_structure(ritems, fmt, tr)
1985 xl[lslices] = xr[rslices]
1986 xllist = xl.tolist()
1987 xrlist = xr.tolist()
1988
1989 ml = memoryview(xl)
1990 mr = memoryview(xr)
1991 self.assertEqual(ml.tolist(), xllist)
1992 self.assertEqual(mr.tolist(), xrlist)
1993
1994 if tl[2] > 0 and tr[2] > 0:
1995 # ndim > 0: test against suboffsets representation.
1996 yl = ndarray_from_structure(litems, fmt, tl, flags=ND_PIL)
1997 yr = ndarray_from_structure(ritems, fmt, tr, flags=ND_PIL)
1998 yl[lslices] = yr[rslices]
1999 yllist = yl.tolist()
2000 yrlist = yr.tolist()
2001 self.assertEqual(xllist, yllist)
2002 self.assertEqual(xrlist, yrlist)
2003
2004 ml = memoryview(yl)
2005 mr = memoryview(yr)
2006 self.assertEqual(ml.tolist(), yllist)
2007 self.assertEqual(mr.tolist(), yrlist)
2008
2009 if numpy_array:
2010 if 0 in lshape or 0 in rshape:
2011 continue # https://github.com/numpy/numpy/issues/2503
2012
2013 zl = numpy_array_from_structure(litems, fmt, tl)
2014 zr = numpy_array_from_structure(ritems, fmt, tr)
2015 zl[lslices] = zr[rslices]
2016
2017 if not is_overlapping(tl) and not is_overlapping(tr):
2018 # Slice assignment of overlapping structures
2019 # is undefined in NumPy.
2020 self.verify(xl, obj=None,
2021 itemsize=zl.itemsize, fmt=fmt, readonly=False,
2022 ndim=zl.ndim, shape=zl.shape,
2023 strides=zl.strides, lst=zl.tolist())
2024
2025 self.verify(xr, obj=None,

Callers

nothing calls this directly

Calls 10

verifyMethod · 0.95
rand_aligned_slicesFunction · 0.85
rand_structureFunction · 0.85
verify_structureFunction · 0.85
randitems_from_structureFunction · 0.85
ndarray_from_structureFunction · 0.85
is_overlappingFunction · 0.85
assertTrueMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected