|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
3 | 5 | using NUnit.Framework; |
| 6 | + |
4 | 7 | using Python.Runtime; |
5 | 8 |
|
| 9 | +using PyRuntime = Python.Runtime.Runtime; |
| 10 | + |
6 | 11 | namespace Python.EmbeddingTest |
7 | 12 | { |
8 | 13 | public class TestConverter |
9 | 14 | { |
| 15 | + static readonly Type[] _numTypes = new Type[] |
| 16 | + { |
| 17 | + typeof(short), |
| 18 | + typeof(ushort), |
| 19 | + typeof(int), |
| 20 | + typeof(uint), |
| 21 | + typeof(long), |
| 22 | + typeof(ulong) |
| 23 | + }; |
| 24 | + |
10 | 25 | [OneTimeSetUp] |
11 | 26 | public void SetUp() |
12 | 27 | { |
@@ -47,6 +62,60 @@ public void TestConvertDoubleToManaged( |
47 | 62 | Assert.IsTrue(((double) convertedValue).Equals(testValue)); |
48 | 63 | } |
49 | 64 |
|
| 65 | + [Test] |
| 66 | + public void CovertTypeError() |
| 67 | + { |
| 68 | + Type[] floatTypes = new Type[] |
| 69 | + { |
| 70 | + typeof(float), |
| 71 | + typeof(double) |
| 72 | + }; |
| 73 | + using (var s = new PyString("abc")) |
| 74 | + { |
| 75 | + foreach (var type in _numTypes.Union(floatTypes)) |
| 76 | + { |
| 77 | + object value; |
| 78 | + try |
| 79 | + { |
| 80 | + bool res = Converter.ToManaged(s.Handle, type, out value, true); |
| 81 | + Assert.IsFalse(res); |
| 82 | + var bo = Exceptions.ExceptionMatches(Exceptions.TypeError); |
| 83 | + Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.TypeError) |
| 84 | + || Exceptions.ExceptionMatches(Exceptions.ValueError)); |
| 85 | + } |
| 86 | + finally |
| 87 | + { |
| 88 | + Exceptions.Clear(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + public void ConvertOverflow() |
| 96 | + { |
| 97 | + using (var num = new PyLong(ulong.MaxValue)) |
| 98 | + { |
| 99 | + IntPtr largeNum = PyRuntime.PyNumber_Add(num.Handle, num.Handle); |
| 100 | + try |
| 101 | + { |
| 102 | + object value; |
| 103 | + foreach (var type in _numTypes) |
| 104 | + { |
| 105 | + bool res = Converter.ToManaged(largeNum, type, out value, true); |
| 106 | + Assert.IsFalse(res); |
| 107 | + Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.OverflowError)); |
| 108 | + Exceptions.Clear(); |
| 109 | + } |
| 110 | + } |
| 111 | + finally |
| 112 | + { |
| 113 | + Exceptions.Clear(); |
| 114 | + PyRuntime.XDecref(largeNum); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
50 | 119 | [Test] |
51 | 120 | public void RawListProxy() |
52 | 121 | { |
|
0 commit comments