|
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] |
@@ -70,16 +73,38 @@ public void GetPointer() |
70 | 73 |
|
71 | 74 | using PyBuffer buf = ndArray.GetBuffer(PyBUF.STRIDES); |
72 | 75 |
|
73 | | - // this throws System.ExecutionEngineException |
74 | 76 | var ptr = buf.GetPointer([0, 0, 0]); |
75 | 77 |
|
76 | 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 | + Assert.That(ptrEnd, Is.Not.EqualTo(ptr)); |
| 83 | + |
| 84 | + Assert.Throws<ArgumentNullException>(() => buf.GetPointer(null!)); |
| 85 | + |
| 86 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0])); |
| 87 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 0, 0])); |
| 88 | + |
| 89 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([-1, 0, 0])); |
| 90 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, -1, 0])); |
| 91 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, -1])); |
| 92 | + |
| 93 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([10, 0, 0])); |
| 94 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 10, 0])); |
| 95 | + Assert.Throws<ArgumentOutOfRangeException>(() => buf.GetPointer([0, 0, 10])); |
| 96 | + |
| 97 | + using PyBuffer buf2 = ndArray.GetBuffer(PyBUF.ND); |
| 98 | + |
| 99 | + Assert.Throws<InvalidOperationException>( |
| 100 | + () => buf2.GetPointer([0, 0, 0]) |
| 101 | + ); |
77 | 102 | } |
78 | 103 |
|
79 | 104 | [Test] |
80 | 105 | public void ArrayHasBuffer() |
81 | 106 | { |
82 | | - var array = new[,] {{1, 2}, {3,4}}; |
| 107 | + var array = new[,] { { 1, 2 }, { 3, 4 } }; |
83 | 108 | var memoryView = PythonEngine.Eval("memoryview"); |
84 | 109 | var mem = memoryView.Invoke(array.ToPython()); |
85 | 110 | Assert.That(mem[(0, 0).ToPython()].As<int>(), Is.EqualTo(1)); |
@@ -128,7 +153,7 @@ public void Finalization() |
128 | 153 | [Test] |
129 | 154 | public void MultidimensionalNumPyArray() |
130 | 155 | { |
131 | | - var ndarray = np.arange(24).reshape(1,2,3,4).T; |
| 156 | + var ndarray = np.arange(24).reshape(1, 2, 3, 4).T; |
132 | 157 | PyObject ndim = ndarray.ndim; |
133 | 158 | PyObject shape = ndarray.shape; |
134 | 159 | PyObject strides = ndarray.strides; |
|
0 commit comments