8000 NumPy array iteration test · losttech/pythonnet@2b5902f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b5902f

Browse files
committed
NumPy array iteration test
1 parent ad7f83b commit 2b5902f

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/embed_tests/NumPyTests.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ public void Dispose()
2121
PythonEngine.Shutdown();
2222
}
2323

24-
[Test]
25-
public void TestReadme()
26-
{
27-
dynamic np;
28-
try
29-
{
24+
static PyObject GetNumPy() {
25+
PyObject np;
26+
try {
3027
np = Py.Import("numpy");
31-
}
32-
catch (PythonException)
33-
{
28+
} catch (PythonException) {
3429
Assert.Inconclusive("Numpy or dependency not installed");
35-
return;
30+
throw;
3631
}
37-
32+
return np;
33+
}
34+
35+
[Test]
36+
public void TestReadme()
37+
{
38+
dynamic np = GetNumPy();
39+
3840
Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());
3941

4042
dynamic sin = np.sin;
@@ -55,19 +57,27 @@ public void TestReadme()
5557
[Test]
5658
public void MultidimensionalNumPyArray()
5759
{
58-
PyObject np;
59-
try {
60-
np = Py.Import("numpy");
61-
} catch (PythonException) {
62-
Assert.Inconclusive("Numpy or dependency not installed");
63-
return;
64-
}
60+
PyObject np = GetNumPy();
6561

6662
var array = new[,] { { 1, 2 }, { 3, 4 } };
6763
var ndarray = np.InvokeMethod("asarray", array.ToPython());
6864
Assert.AreEqual((2,2), ndarray.GetAttr("shape").As<(int,int)>());
6965
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].As<int>());
7066
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].As<int>());
7167
}
68+
69+
[Test]
70+
public void Iterate()
71+
{
72+
PyObject np = GetNumPy();
73+
74+
var size = new[] { 3, 2 };
75+
var ndarray = np.InvokeMethod("zeros", size.ToPython());
76+
var iterator = ndarray.GetIterator();
77+
Assert.True(iterator.MoveNext());
78+
Assert.True(iterator.MoveNext());
79+
Assert.True(iterator.MoveNext());
80+
Assert.False(iterator.MoveNext());
81+
}
7282
}
7383
}

src/runtime/pyiter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public PyIter(IntPtr ptr) : base(ptr)
2525
{
2626
}
2727
/// <summary>
28-
/// Creates new <see cref="PyIter"/> from an untyped reference to Python object.
28+
/// Creates new <see cref="PyIter"/> from an untyped reference to Python iterator object.
2929
/// </summary>
3030
public PyIter(PyObject pyObject) : base(FromPyObject(pyObject)) { }
3131
static BorrowedReference FromPyObject(PyObject pyObject) {

0 commit comments

Comments
 (0)
0