|
2 | 2 | using System.Runtime.CompilerServices; |
3 | 3 | using System.Text; |
4 | 4 | using System.Threading; |
| 5 | + |
5 | 6 | using NUnit.Framework; |
| 7 | + |
6 | 8 | using Python.Runtime; |
7 | 9 | using Python.Runtime.Codecs; |
8 | 10 |
|
9 | | -namespace Python.EmbeddingTest { |
| 11 | +namespace Python.EmbeddingTest |
| 12 | +{ |
10 | 13 | class TestPyBuffer |
11 | 14 | { |
12 | 15 | [OneTimeSetUp] |
@@ -58,13 +61,49 @@ public void TestBufferRead() |
58 | 61 | } |
59 | 62 |
|
60 | 63 | string result = new UTF8Encoding().GetString(managedArray); |
61 | | - Assert.That(result == " " + bufferTestString.Substring(1), Is.True); |
| 64 | + Assert.That(result, Is.EqualTo($" {bufferTestString.Substring(1)}")); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void GetPointer() |
| 69 | + { |
| 70 | + // create 3D NumPy array |
| 71 | + int[] shape = [10, 10, 10]; |
| 72 | + using PyObject ndArray = np.zeros(shape); |
| 73 | + |
| 74 | + using PyBuffer buf = ndArray.GetBuffer(PyBUF.STRIDES); |
| 75 | + |
| 76 | + var ptr = buf.GetPointer([0, 0, 0]); |
| 77 | + |
| 78 | + Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero)); |
| 79 | + |
| 80 | + var ptrEnd = buf.GetPointer([9, 9, 9]); |
| 81 | + Assert.That(ptrEnd, Is.Not.EqualTo(IntPtr.Zero)); |
| 82 | + |
| 83 | + Assert.Throws<ArgumentNullException>(() => buf.GetPointer(null!)); |
| 84 | + |
| 85 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0])); |
| 86 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 0, 0])); |
| 87 | + |
| 88 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([-1, 0, 0])); |
| 89 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, -1, 0])); |
| 90 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, -1])); |
| 91 | + |
| 92 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([10, 0, 0])); |
| 93 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 10, 0])); |
| 94 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 10])); |
| 95 | + |
| 96 | + using PyBuffer buf2 = ndArray.GetBuffer(PyBUF.ND); |
| 97 | + |
| 98 | + Assert.Throws<InvalidOperationException>( |
| 99 | + () => buf2.GetPointer([0, 0, 0]) |
| 100 | + ); |
62 | 101 | } |
63 | 102 |
|
64 | 103 | [Test] |
65 | 104 | public void ArrayHasBuffer() |
66 | 105 | { |
67 | | - var array = new[,] {{1, 2}, {3,4}}; |
| 106 | + var array = new[,] { { 1, 2 }, { 3, 4 } }; |
68 | 107 | var memoryView = PythonEngine.Eval("memoryview"); |
69 | 108 | var mem = memoryView.Invoke(array.ToPython()); |
70 | 109 | Assert.That(mem[(0, 0).ToPython()].As<int>(), Is.EqualTo(1)); |
@@ -113,7 +152,7 @@ public void Finalization() |
113 | 152 | [Test] |
114 | 153 | public void MultidimensionalNumPyArray() |
115 | 154 | { |
116 | | - var ndarray = np.arange(24).reshape(1,2,3,4).T; |
| 155 | + var ndarray = np.arange(24).reshape(1, 2, 3, 4).T; |
117 | 156 | PyObject ndim = ndarray.ndim; |
118 | 157 | PyObject shape = ndarray.shape; |
119 | 158 | PyObject strides = ndarray.strides; |
|
0 commit comments