| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,11 +2,14 @@ | |||
| 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; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,10 +110,37 @@ public bool IsContiguous(BufferOrderStyle order) | |||
| 110 | 110 | /// </summary> | |
| 111 | 111 | public IntPtr GetPointer(long[] indices) | |
| 112 | 112 | { | |
| 113 | - if (indices is null) throw new ArgumentNullException(nameof(indices)); | ||
| 113 | + if (indices is null) | ||
| 114 | + throw new ArgumentNullException(nameof(indices)); | ||
| 115 | + | ||
| 116 | + if (Strides is null) | ||
| 117 | + throw new InvalidOperationException($"{nameof(GetPointer)} requires {nameof(Strides)} to be filled"); | ||
| 118 | + | ||
| 119 | + if (indices.Length != Dimensions) | ||
| 120 | + throw new ArgumentOutOfRangeException($"{nameof(indices)} must be exactly {Dimensions} long"); | ||
| 121 | + | ||
| 122 | + if (Dimensions == 0 && Shape is null) | ||
| 123 | + throw new InvalidOperationException($"{nameof(GetPointer)} requires {nameof(Shape)} to be filled for dimensional buffers"); | ||
| 124 | + | ||
| 114 | 125 | ThrowIfDisposed(); | |
| 126 | + | ||
| 115 | 127 | if (Runtime.PyVersion < new Version(3, 7)) | |
| 116 | 128 | throw new NotSupportedException("GetPointer requires at least Python 3.7"); | |
| 129 | + | ||
| 130 | + var indicesArr = new nint[Dimensions]; | ||
| 131 | + | ||
| 132 | + for (var i = 0; i < Dimensions; i++) | ||
| 133 | + { | ||
| 134 | + indicesArr[i] = checked((nint)indices[i]); | ||
| 135 | + | ||
| 136 | + if (indicesArr[i] < 0 || indicesArr[i] >= Shape![i]) | ||
| 137 | + { | ||
| 138 | + throw new ArgumentOutOfRangeException( | ||
| 139 | + $"Index {i} has a value of {indicesArr[i]} not in [0, {Shape![i]})" | ||
| 140 | + ); | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + | ||
| 117 | 144 | return Runtime.PyBuffer_GetPointer(ref _view, indices.Select(x => checked((nint)x)).ToArray()); | |
| 118 | 145 | } | |
| 119 | 146 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments