| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,12 +84,19 @@ | |||
| 84 | 84 | <Compile Include="pyimport.cs" /> | |
| 85 | 85 | <Compile Include="pyinitialize.cs" /> | |
| 86 | 86 | <Compile Include="pyrunstring.cs" /> | |
| 87 | + <Compile Include="TestCustomMarshal.cs" /> | ||
| 88 | + <Compile Include="TestPyAnsiString.cs" /> | ||
| 89 | + <Compile Include="TestPyFloat.cs" /> | ||
| 90 | + <Compile Include="TestPyInt.cs" /> | ||
| 87 | 91 | <Compile Include="TestPyList.cs" /> | |
| 88 | 92 | <Compile Include="TestPyLong.cs" /> | |
| 93 | + <Compile Include="TestPyNumber.cs" /> | ||
| 94 | + <Compile Include="TestPySequence.cs" /> | ||
| 89 | 95 | <Compile Include="TestPyString.cs" /> | |
| 90 | 96 | <Compile Include="TestPythonException.cs" /> | |
| 91 | 97 | <Compile Include="TestPythonEngineProperties.cs" /> | |
| 92 | 98 | <Compile Include="TestPyTuple.cs" /> | |
| 99 | + <Compile Include="TestRuntime.cs" /> | ||
| 93 | 100 | </ItemGroup> | |
| 94 | 101 | <ItemGroup> | |
| 95 | 102 | <ProjectReference Include="..\runtime\Python.Runtime.csproj"> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + using System; | ||
| 2 | + using NUnit.Framework; | ||
| 3 | + using Python.Runtime; | ||
| 4 | + | ||
| 5 | + namespace Python.EmbeddingTest | ||
| 6 | + { | ||
| 7 | + public class TestCustomMarshal | ||
| 8 | + { | ||
| 9 | + [Test] | ||
| 10 | + public static void GetManagedStringTwice() | ||
| 11 | + { | ||
| 12 | + const string expected = "FooBar"; | ||
| 13 | + using (Py.GIL()) | ||
| 14 | + { | ||
| 15 | + IntPtr op = Runtime.Runtime.PyUnicode_FromString(expected); | ||
| 16 | + string s1 = Runtime.Runtime.GetManagedString(op); | ||
| 17 | + string s2 = Runtime.Runtime.GetManagedString(op); | ||
| 18 | + | ||
| 19 | + Assert.AreEqual(1, Runtime.Runtime.Refcount(op)); | ||
| 20 | + Assert.AreEqual(expected, s1); | ||
| 21 | + Assert.AreEqual(expected, s2); | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,96 @@ | |||
| 1 | + using System; | ||
| 2 | + using NUnit.Framework; | ||
| 3 | + using Python.Runtime; | ||
| 4 | + | ||
| 5 | + namespace Python.EmbeddingTest | ||
| 6 | + { | ||
| 7 | + public class TestPyAnsiString | ||
| 8 | + { | ||
| 9 | + [OneTimeSetUp] | ||
| 10 | + public void SetUp() | ||
| 11 | + { | ||
| 12 | + PythonEngine.Initialize(); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + [OneTimeTearDown] | ||
| 16 | + public void Dispose() | ||
| 17 | + { | ||
| 18 | + PythonEngine.Shutdown(); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + [Test] | ||
| 22 | + public void TestStringCtor() | ||
| 23 | + { | ||
| 24 | + const string expected = "foo"; | ||
| 25 | + var actual = new PyAnsiString(expected); | ||
| 26 | + Assert.AreEqual(expected, actual.ToString()); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + [Test] | ||
| 30 | + public void TestEmptyStringCtor() | ||
| 31 | + { | ||
| 32 | + const string expected = ""; | ||
| 33 | + var actual = new PyAnsiString(expected); | ||
| 34 | + Assert.AreEqual(expected, actual.ToString()); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + [Test] | ||
| 38 | + public void TestPyObjectCtor() | ||
| 39 | + { | ||
| 40 | + const string expected = "Foo"; | ||
| 41 | + | ||
| 42 | + var t = new PyAnsiString(expected); | ||
| 43 | + var actual = new PyAnsiString(t); | ||
| 44 | + | ||
| 45 | + Assert.AreEqual(expected, actual.ToString()); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + [Test] | ||
| 49 | + public void TestBadPyObjectCtor() | ||
| 50 | + { | ||
| 51 | + var t = new PyInt(5); | ||
| 52 | + PyAnsiString actual = null; | ||
| 53 | + | ||
| 54 | + var ex = Assert.Throws<ArgumentException>(() => actual = new PyAnsiString(t)); | ||
| 55 | + | ||
| 56 | + StringAssert.StartsWith("object is not a string", ex.Message); | ||
| 57 | + Assert.IsNull(actual); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + [Test] | ||
| 61 | + public void TestCtorPtr() | ||
| 62 | + { | ||
| 63 | + const string expected = "foo"; | ||
| 64 | + | ||
| 65 | + var t = new PyAnsiString(expected); | ||
| 66 | + var actual = new PyAnsiString(t.Handle); | ||
| 67 | + | ||
| 68 | + Assert.AreEqual(expected, actual.ToString()); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + [Test] | ||
| 72 | + public void IsStringTrue() | ||
| 73 | + { | ||
| 74 | + var t = new PyAnsiString("foo"); | ||
| 75 | + | ||
| 76 | + Assert.True(PyAnsiString.IsStringType(t)); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + [Test] | ||
| 80 | + public void IsStringFalse() | ||
| 81 | + { | ||
| 82 | + var t = new PyInt(5); | ||
| 83 | + | ||
| 84 | + Assert.False(PyAnsiString.IsStringType(t)); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + [Test] | ||
| 88 | + [Ignore("Ambiguous behavior between PY2/PY3")] | ||
| 89 | + public void TestUnicode() | ||
| 90 | + { | ||
| 91 | + const string expected = "foo\u00e9"; | ||
| 92 | + PyObject actual = new PyAnsiString(expected); | ||
| 93 | + Assert.AreEqual(expected, actual.ToString()); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,138 @@ | |||
| 1 | + using System; | ||
| 2 | + using NUnit.Framework; | ||
| 3 | + using Python.Runtime; | ||
| 4 | + | ||
| 5 | + namespace Python.EmbeddingTest | ||
| 6 | + { | ||
| 7 | + /// <remarks> | ||
| 8 | + /// PyFloat implementation isn't complete, thus tests aren't complete. | ||
| 9 | + /// </remarks> | ||
| 10 | + public class TestPyFloat | ||
| 11 | + { | ||
| 12 | + [OneTimeSetUp] | ||
| 13 | + public void SetUp() | ||
| 14 | + { | ||
| 15 | + PythonEngine.Initialize(); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + [OneTimeTearDown] | ||
| 19 | + public void Dispose() | ||
| 20 | + { | ||
| 21 | + PythonEngine.Shutdown(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + [Test] | ||
| 25 | + public void IntPtrCtor() | ||
| 26 | + { | ||
| 27 | + var i = new PyFloat(1); | ||
| 28 | + var ii = new PyFloat(i.Handle); | ||
| 29 | + Assert.AreEqual(i.Handle, ii.Handle); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + [Test] | ||
| 33 | + public void FloatCtor() | ||
| 34 | + { | ||
| 35 | + const float a = 4.5F; | ||
| 36 | + var i = new PyFloat(a); | ||
| 37 | + Assert.True(PyFloat.IsFloatType(i)); | ||
| 38 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + [Test] | ||
| 42 | + public void PyObjectCtorGood() | ||
| 43 | + { | ||
| 44 | + var i = new PyFloat(5); | ||
| 45 | + var a = new PyFloat(i); | ||
| 46 | + Assert.True(PyFloat.IsFloatType(a)); | ||
| 47 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + [Test] | ||
| 51 | + public void PyObjectCtorBad() | ||
| 52 | + { | ||
| 53 | + var i = new PyString("Foo"); | ||
| 54 | + PyFloat a = null; | ||
| 55 | + | ||
| 56 | + var ex = Assert.Throws<ArgumentException>(() => a = new PyFloat(i)); | ||
| 57 | + | ||
| 58 | + StringAssert.StartsWith("object is not a float", ex.Message); | ||
| 59 | + Assert.IsNull(a); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + [Test] | ||
| 63 | + public void DoubleCtor() | ||
| 64 | + { | ||
| 65 | + const double a = 4.5; | ||
| 66 | + var i = new PyFloat(a); | ||
| 67 | + Assert.True(PyFloat.IsFloatType(i)); | ||
| 68 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + [Test] | ||
| 72 | + public void StringIntCtor() | ||
| 73 | + { | ||
| 74 | + const string a = "5"; | ||
| 75 | + var i = new PyFloat(a); | ||
| 76 | + Assert.True(PyFloat.IsFloatType(i)); | ||
| 77 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + [Test] | ||
| 81 | + public void StringDoubleCtor() | ||
| 82 | + { | ||
| 83 | + const string a = "4.5"; | ||
| 84 | + var i = new PyFloat(a); | ||
| 85 | + Assert.True(PyFloat.IsFloatType(i)); | ||
| 86 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + [Test] | ||
| 90 | + public void StringBadCtor() | ||
| 91 | + { | ||
| 92 | + const string i = "Foo"; | ||
| 93 | + PyFloat a = null; | ||
| 94 | + | ||
| 95 | + var ex = Assert.Throws<PythonException>(() => a = new PyFloat(i)); | ||
| 96 | + | ||
| 97 | + StringAssert.StartsWith("ValueError : could not convert string to float", ex.Message); | ||
| 98 | + Assert.IsNull(a); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + [Test] | ||
| 102 | + public void IsFloatTrue() | ||
| 103 | + { | ||
| 104 | + const double a = 4.5; | ||
| 105 | + var i = new PyFloat(a); | ||
| 106 | + Assert.True(PyFloat.IsFloatType(i)); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + [Test] | ||
| 110 | + public void IsFloatFalse() | ||
| 111 | + { | ||
| 112 | + var i = new PyString("Foo"); | ||
| 113 | + Assert.False(PyFloat.IsFloatType(i)); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + [Test] | ||
| 117 | + public void AsFloatGood() | ||
| 118 | + { | ||
| 119 | + const double a = 4.5; | ||
| 120 | + var i = new PyFloat(a); | ||
| 121 | + PyFloat s = PyFloat.AsFloat(i); | ||
| 122 | + | ||
| 123 | + Assert.True(PyFloat.IsFloatType(s)); | ||
| 124 | + // Assert.Assert.AreEqual(i, a.ToInt32()); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + [Test] | ||
| 128 | + public void AsFloatBad() | ||
| 129 | + { | ||
| 130 | + var s = new PyString("Foo"); | ||
| 131 | + PyFloat a = null; | ||
| 132 | + | ||
| 133 | + var ex = Assert.Throws<PythonException>(() => a = PyFloat.AsFloat(s)); | ||
| 134 | + StringAssert.StartsWith("ValueError : could not convert string to float", ex.Message); | ||
| 135 | + Assert.IsNull(a); | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments