Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: add PyBuffer test for strides
  • Loading branch information
legomanww committed Mar 11, 2023
commit 15606111d46d61343ca201bb03bdd8223e973142
36 changes: 36 additions & 0 deletions src/embed_tests/TestPyBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public void Finalization()
Assert.AreEqual(1, arr.Refcount);
}

[Test]
public void MultidimensionalNumPyArray()
{
var ndarray = np.arange(24).reshape(1,2,3,4).T;
PyObject ndim = ndarray.ndim;
PyObject shape = ndarray.shape;
PyObject strides = ndarray.strides;
PyObject contiguous = ndarray.flags["C_CONTIGUOUS"];

using PyBuffer buf = ndarray.GetBuffer(PyBUF.STRIDED);

Assert.Multiple(() =>
{
Assert.That(buf.Dimensions, Is.EqualTo(ndim.As<int>()));
Assert.That(buf.Shape, Is.EqualTo(shape.As<long[]>()));
Assert.That(buf.Strides, Is.EqualTo(strides.As<long[]>()));
Assert.That(buf.IsContiguous(BufferOrderStyle.C), Is.EqualTo(contiguous.As<bool>()));
});
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void MakeBufAndLeak(PyObject bufProvider)
{
Expand All @@ -121,5 +141,21 @@ static PyObject ByteArrayFromAsciiString(string str)
using var scope = Py.CreateScope();
return Runtime.Runtime.PyByteArray_FromStringAndSize(str).MoveToPyObject();
}

dynamic np
{
get
{
try
{
return Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return null;
}
}
}
}
}