|
| 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 | +} |
0 commit comments