Skip to content

Commit c1dab27

Browse files
authored
Add explicit tests for vararg call (#1805)
1 parent 3cb2529 commit c1dab27

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/embed_tests/NumPyTests.cs

+34-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
34
using NUnit.Framework;
5+
46
using Python.Runtime;
57
using Python.Runtime.Codecs;
68

@@ -24,17 +26,6 @@ public void Dispose()
2426
[Test]
2527
public void TestReadme()
2628
{
27-
dynamic np;
28-
try
29-
{
30-
np = Py.Import("numpy");
31-
}
32-
catch (PythonException)
33-
{
34-
Assert.Inconclusive("Numpy or dependency not installed");
35-
return;
36-
}
37-
3829
Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());
3930

4031
dynamic sin = np.sin;
@@ -55,40 +46,52 @@ public void TestReadme()
5546
[Test]
5647
public void MultidimensionalNumPyArray()
5748
{
58-
PyObject np;
59-
try {
60-
np = Py.Import("numpy");
61-
} catch (PythonException) {
62-
Assert.Inconclusive("Numpy or dependency not installed");
63-
return;
64-
}
65-
6649
var array = new[,] { { 1, 2 }, { 3, 4 } };
6750
var ndarray = np.InvokeMethod("asarray", array.ToPython());
68-
Assert.AreEqual((2,2), ndarray.GetAttr("shape").As<(int,int)>());
51+
Assert.AreEqual((2, 2), ndarray.GetAttr("shape").As<(int, int)>());
6952
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<int>());
7053
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<int>());
7154
}
7255

7356
[Test]
7457
public void Int64Array()
7558
{
76-
PyObject np;
77-
try
78-
{
79-
np = Py.Import("numpy");
80-
}
81-
catch (PythonException)
82-
{
83-
Assert.Inconclusive("Numpy or dependency not installed");
84-
return;
85-
}
86-
8759
var array = new long[,] { { 1, 2 }, { 3, 4 } };
8860
var ndarray = np.InvokeMethod("asarray", array.ToPython());
8961
Assert.AreEqual((2, 2), ndarray.GetAttr("shape").As<(int, int)>());
9062
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<long>());
9163
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<long>());
9264
}
65+
66+
[Test]
67+
public void VarArg()
68+
{
69+
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
70+
dynamic grad = np.gradient(zX, 4.0, 5.0);
71+
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)});
72+
73+
Assert.AreEqual(4.125, grad[0].sum().__float__().As<double>(), 0.001);
74+
Assert.AreEqual(-1.2, grad[1].sum().__float__().As<double>(), 0.001);
75+
Assert.AreEqual(4.125, grad2[0].sum().__float__().As<double>(), 0.001);
76+
Assert.AreEqual(-1.2, grad2[1].sum().__float__().As<double>(), 0.001);
77+
}
78+
79+
#pragma warning disable IDE1006
80+
dynamic np
81+
{
82+
get
83+
{
84+
try
85+
{
86+
return Py.Import("numpy");
87+
}
88+
catch (PythonException)
89+
{
90+
Assert.Inconclusive("Numpy or dependency not installed");
91+
return null;
92+
}
93+
}
94+
}
95+
9396
}
9497
}

0 commit comments

Comments
 (0)