Skip to content

Commit 0df9230

Browse files
authored
Finish IConvertible members in BigIntegerOps (#1366)
* Finish IConvertible members in BigIntegerOps * Disable error message test on Mono
1 parent ea0523a commit 0df9230

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

Src/IronPython/Runtime/Operations/BigIntegerOps.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public static BigInteger ToBigInteger(BigInteger self) {
655655

656656
#endregion
657657

658-
#region Mimic some IConvertible members
658+
#region Mimic IConvertible members
659659

660660
[PythonHidden]
661661
public static bool ToBoolean(BigInteger self, IFormatProvider provider) {
@@ -727,11 +727,53 @@ public static ulong ToUInt64(BigInteger self, IFormatProvider provider) {
727727
}
728728

729729
[PythonHidden]
730-
public static object ToType(BigInteger self, Type conversionType, IFormatProvider provider) {
731-
if (conversionType == typeof(BigInteger)) {
730+
public static DateTime ToDateTime(BigInteger self, IFormatProvider provider) {
731+
throw new InvalidCastException("Invalid cast from 'BigInteger' to 'DateTime'");
732+
}
733+
734+
[PythonHidden]
735+
public static object ToType(BigInteger self, Type targetType, IFormatProvider provider) {
736+
737+
if (targetType is null) throw new ArgumentNullException(nameof(targetType));
738+
739+
if (targetType == typeof(BigInteger))
732740
return self;
733-
}
734-
throw new NotImplementedException();
741+
if (targetType == typeof(Boolean))
742+
return ToBoolean(self, provider);
743+
if (targetType == typeof(Char))
744+
return ToChar(self, provider);
745+
if (targetType == typeof(SByte))
746+
return ToSByte(self, provider);
747+
if (targetType == typeof(Byte))
748+
return ToByte(self, provider);
749+
if (targetType == typeof(Int16))
750+
return ToInt16(self, provider);
751+
if (targetType == typeof(UInt16))
752+
return ToUInt16(self, provider);
753+
if (targetType == typeof(Int32))
754+
return ToInt32(self, provider);
755+
if (targetType == typeof(UInt32))
756+
return ToUInt32(self, provider);
757+
if (targetType == typeof(Int64))
758+
return ToInt64(self, provider);
759+
if (targetType == typeof(UInt64))
760+
return ToUInt64(self, provider);
761+
if (targetType == typeof(Single))
762+
return ToSingle(self, provider);
763+
if (targetType == typeof(Double))
764+
return ToDouble(self, provider);
765+
if (targetType == typeof(Decimal))
766+
return ToDecimal(self, provider);
767+
if (targetType == typeof(DateTime))
768+
return ToDateTime(self, provider);
769+
if (targetType == typeof(String))
770+
return self.ToString(provider);
771+
if (targetType == typeof(Object))
772+
return (object)self;
773+
if (targetType.IsEnum)
774+
throw new InvalidCastException($"Invalid cast from '{self.GetType().FullName}' to '{targetType.FullName}'.");
775+
776+
throw new InvalidCastException($"Unable to cast object of type '{self.GetType().FullName}' to type '{targetType.FullName}'.");
735777
}
736778

737779
[PythonHidden]

Tests/test_ironmath.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88

99
import unittest
1010

11-
from iptest import IronPythonTestCase, big, run_test, skipUnlessIronPython
11+
from iptest import IronPythonTestCase, big, is_mono, run_test, skipUnlessIronPython
1212

1313
@skipUnlessIronPython()
1414
class IronMathTest(IronPythonTestCase):
1515
def setUp(self):
1616
super(IronMathTest, self).setUp()
1717

18-
import clr
18+
import clr, System
1919
from System import IFormatProvider
2020
class myFormatProvider(IFormatProvider):
21-
def ToString():pass
21+
def ToString():
22+
pass
23+
def GetFormat(self, formatType):
24+
return System.Globalization.NumberFormatInfo.InvariantInfo
2225

2326
self.p = myFormatProvider()
2427

@@ -247,6 +250,51 @@ def CheckDwordConversions(bigint, dwords):
247250
CheckDwordConversions(big(1<<31) + 9, [0x80000009])
248251
CheckDwordConversions(big(1<<32), [0x00000000, 0x00000001])
249252

253+
def test_to_type_conversions(self):
254+
from System import Decimal, Double, Single
255+
from System import Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte
256+
from System import Boolean, Char, DateTime, Object, Enum, DateTimeKind
257+
258+
val = 1
259+
for i in [big(val), Int32(val)]:
260+
self.assertEqual(i.ToDecimal(self.p), val)
261+
self.assertIsInstance(i.ToDecimal(self.p), Decimal)
262+
self.assertEqual(i.ToDouble(self.p), val)
263+
self.assertIsInstance(i.ToDouble(self.p), Double)
264+
self.assertEqual(i.ToSingle(self.p), val)
265+
self.assertIsInstance(i.ToSingle(self.p), Single)
266+
self.assertEqual(i.ToInt64(self.p), val)
267+
self.assertIsInstance(i.ToInt64(self.p), Int64)
268+
self.assertEqual(i.ToUInt64(self.p), val)
269+
self.assertIsInstance(i.ToUInt64(self.p), UInt64)
270+
self.assertEqual(i.ToInt32(self.p), val)
271+
self.assertIsInstance(i.ToInt32(self.p), Int32)
272+
self.assertEqual(i.ToUInt32(self.p), val)
273+
self.assertIsInstance(i.ToUInt32(self.p), UInt32)
274+
self.assertEqual(i.ToInt16(self.p), val)
275+
self.assertIsInstance(i.ToInt16(self.p), Int16)
276+
self.assertEqual(i.ToUInt16(self.p), val)
277+
self.assertIsInstance(i.ToUInt16(self.p), UInt16)
278+
self.assertEqual(i.ToByte(self.p), val)
279+
self.assertIsInstance(i.ToByte(self.p), Byte)
280+
self.assertEqual(i.ToSByte(self.p), val)
281+
self.assertIsInstance(i.ToSByte(self.p), SByte)
282+
self.assertEqual(i.ToBoolean(self.p), val)
283+
self.assertIsInstance(i.ToBoolean(self.p), Boolean)
284+
self.assertEqual(i.ToChar(self.p), Char(val))
285+
self.assertEqual(i.ToString(self.p), str(val))
286+
self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToDateTime, self.p)
287+
288+
for t in [Decimal, Double, Single, Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean, Char, str]:
289+
self.assertEqual(i.ToType(t, self.p), t(i))
290+
291+
self.assertEqual(i.ToType(Object, self.p), i)
292+
self.assertIsInstance(i.ToType(Object, self.p), Object)
293+
self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToType, DateTime, self.p)
294+
self.assertRaisesRegex(TypeError, r"Invalid cast from '[\w.]+' to 'System.DateTimeKind'\.", i.ToType, DateTimeKind, self.p)
295+
if not is_mono:
296+
self.assertRaisesRegex(TypeError, r"Unable to cast object of type '[\w.]+' to type 'System.Enum'\.", i.ToType, Enum, self.p)
297+
250298
def test_misc(self):
251299
from System import ArgumentException, ArgumentNullException
252300
from System.Numerics import BigInteger

0 commit comments

Comments
 (0)