Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish IConvertible members in BigIntegerOps #1366

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions Src/IronPython/Runtime/Operations/BigIntegerOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ private static bool IsInt32(this BigInteger self)

#endregion

#region Mimic some IConvertible members
#region Mimic IConvertible members

[PythonHidden]
public static bool ToBoolean(BigInteger self, IFormatProvider provider) {
Expand Down Expand Up @@ -727,11 +727,53 @@ private static bool IsInt32(this BigInteger self)
}

[PythonHidden]
public static object ToType(BigInteger self, Type conversionType, IFormatProvider provider) {
if (conversionType == typeof(BigInteger)) {
public static DateTime ToDateTime(BigInteger self, IFormatProvider provider) {
throw new InvalidCastException("Invalid cast from 'BigInteger' to 'DateTime'");
}

[PythonHidden]
public static object ToType(BigInteger self, Type targetType, IFormatProvider provider) {

if (targetType is null) throw new ArgumentNullException(nameof(targetType));

if (targetType == typeof(BigInteger))
return self;
}
throw new NotImplementedException();
if (targetType == typeof(Boolean))
return ToBoolean(self, provider);
if (targetType == typeof(Char))
return ToChar(self, provider);
if (targetType == typeof(SByte))
return ToSByte(self, provider);
if (targetType == typeof(Byte))
return ToByte(self, provider);
if (targetType == typeof(Int16))
return ToInt16(self, provider);
if (targetType == typeof(UInt16))
return ToUInt16(self, provider);
if (targetType == typeof(Int32))
return ToInt32(self, provider);
if (targetType == typeof(UInt32))
return ToUInt32(self, provider);
if (targetType == typeof(Int64))
return ToInt64(self, provider);
if (targetType == typeof(UInt64))
return ToUInt64(self, provider);
if (targetType == typeof(Single))
return ToSingle(self, provider);
if (targetType == typeof(Double))
return ToDouble(self, provider);
if (targetType == typeof(Decimal))
return ToDecimal(self, provider);
if (targetType == typeof(DateTime))
return ToDateTime(self, provider);
if (targetType == typeof(String))
return self.ToString(provider);
if (targetType == typeof(Object))
return (object)self;
if (targetType.IsEnum)
throw new InvalidCastException($"Invalid cast from '{self.GetType().FullName}' to '{targetType.FullName}'.");

throw new InvalidCastException($"Unable to cast object of type '{self.GetType().FullName}' to type '{targetType.FullName}'.");
}

[PythonHidden]
Expand Down
54 changes: 51 additions & 3 deletions Tests/test_ironmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@

import unittest

from iptest import IronPythonTestCase, big, run_test, skipUnlessIronPython
from iptest import IronPythonTestCase, big, is_mono, run_test, skipUnlessIronPython

@skipUnlessIronPython()
class IronMathTest(IronPythonTestCase):
def setUp(self):
super(IronMathTest, self).setUp()

import clr
import clr, System
from System import IFormatProvider
class myFormatProvider(IFormatProvider):
def ToString():pass
def ToString():
pass
def GetFormat(self, formatType):
return System.Globalization.NumberFormatInfo.InvariantInfo

self.p = myFormatProvider()

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

def test_to_type_conversions(self):
from System import Decimal, Double, Single
from System import Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte
from System import Boolean, Char, DateTime, Object, Enum, DateTimeKind

val = 1
for i in [big(val), Int32(val)]:
self.assertEqual(i.ToDecimal(self.p), val)
self.assertIsInstance(i.ToDecimal(self.p), Decimal)
self.assertEqual(i.ToDouble(self.p), val)
self.assertIsInstance(i.ToDouble(self.p), Double)
self.assertEqual(i.ToSingle(self.p), val)
self.assertIsInstance(i.ToSingle(self.p), Single)
self.assertEqual(i.ToInt64(self.p), val)
self.assertIsInstance(i.ToInt64(self.p), Int64)
self.assertEqual(i.ToUInt64(self.p), val)
self.assertIsInstance(i.ToUInt64(self.p), UInt64)
self.assertEqual(i.ToInt32(self.p), val)
self.assertIsInstance(i.ToInt32(self.p), Int32)
self.assertEqual(i.ToUInt32(self.p), val)
self.assertIsInstance(i.ToUInt32(self.p), UInt32)
self.assertEqual(i.ToInt16(self.p), val)
self.assertIsInstance(i.ToInt16(self.p), Int16)
self.assertEqual(i.ToUInt16(self.p), val)
self.assertIsInstance(i.ToUInt16(self.p), UInt16)
self.assertEqual(i.ToByte(self.p), val)
self.assertIsInstance(i.ToByte(self.p), Byte)
self.assertEqual(i.ToSByte(self.p), val)
self.assertIsInstance(i.ToSByte(self.p), SByte)
self.assertEqual(i.ToBoolean(self.p), val)
self.assertIsInstance(i.ToBoolean(self.p), Boolean)
self.assertEqual(i.ToChar(self.p), Char(val))
self.assertEqual(i.ToString(self.p), str(val))
self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToDateTime, self.p)

for t in [Decimal, Double, Single, Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean, Char, str]:
self.assertEqual(i.ToType(t, self.p), t(i))

self.assertEqual(i.ToType(Object, self.p), i)
self.assertIsInstance(i.ToType(Object, self.p), Object)
self.assertRaisesRegex(TypeError, r"Invalid cast from '\w+' to 'DateTime'", i.ToType, DateTime, self.p)
self.assertRaisesRegex(TypeError, r"Invalid cast from '[\w.]+' to 'System.DateTimeKind'\.", i.ToType, DateTimeKind, self.p)
if not is_mono:
self.assertRaisesRegex(TypeError, r"Unable to cast object of type '[\w.]+' to type 'System.Enum'\.", i.ToType, Enum, self.p)

def test_misc(self):
from System import ArgumentException, ArgumentNullException
from System.Numerics import BigInteger
Expand Down