Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add explicit tests for vararg call#1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
filmor merged 1 commit intomasterfromvararg-call
May 31, 2022
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletionssrc/embed_tests/NumPyTests.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;

using NUnit.Framework;

using Python.Runtime;
using Python.Runtime.Codecs;

Expand All@@ -24,17 +26,6 @@ public void Dispose()
[Test]
public void TestReadme()
{
dynamic np;
try
{
np = Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return;
}

Assert.AreEqual("1.0", np.cos(np.pi * 2).ToString());

dynamic sin = np.sin;
Expand All@@ -55,40 +46,52 @@ public void TestReadme()
[Test]
public void MultidimensionalNumPyArray()
{
PyObject np;
try {
np = Py.Import("numpy");
} catch (PythonException) {
Assert.Inconclusive("Numpy or dependency not installed");
return;
}

var array = new[,] { { 1, 2 }, { 3, 4 } };
var ndarray = np.InvokeMethod("asarray", array.ToPython());
Assert.AreEqual((2,2), ndarray.GetAttr("shape").As<(int,int)>());
Assert.AreEqual((2,2), ndarray.GetAttr("shape").As<(int,int)>());
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<int>());
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<int>());
}

[Test]
public void Int64Array()
{
PyObject np;
try
{
np = Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return;
}

var array = new long[,] { { 1, 2 }, { 3, 4 } };
var ndarray = np.InvokeMethod("asarray", array.ToPython());
Assert.AreEqual((2, 2), ndarray.GetAttr("shape").As<(int, int)>());
Assert.AreEqual(1, ndarray[(0, 0).ToPython()].InvokeMethod("__int__").As<long>());
Assert.AreEqual(array[1, 0], ndarray[(1, 0).ToPython()].InvokeMethod("__int__").As<long>());
}

[Test]
public void VarArg()
{
dynamic zX = np.array(new[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 8, 9, 0 } });
dynamic grad = np.gradient(zX, 4.0, 5.0);
dynamic grad2 = np.InvokeMethod("gradient", new PyObject[] {zX, new PyFloat(4.0), new PyFloat(5.0)});

Assert.AreEqual(4.125, grad[0].sum().__float__().As<double>(), 0.001);
Assert.AreEqual(-1.2, grad[1].sum().__float__().As<double>(), 0.001);
Assert.AreEqual(4.125, grad2[0].sum().__float__().As<double>(), 0.001);
Assert.AreEqual(-1.2, grad2[1].sum().__float__().As<double>(), 0.001);
}

#pragma warning disable IDE1006
dynamic np
{
get
{
try
{
return Py.Import("numpy");
}
catch (PythonException)
{
Assert.Inconclusive("Numpy or dependency not installed");
return null;
}
}
}

}
}

[8]ページ先頭

©2009-2025 Movatter.jp