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