From 0ae60f903606c78e862b41134070c79e4c7ed959 Mon Sep 17 00:00:00 2001 From: Timothy Risi Date: Wed, 7 Dec 2016 08:22:30 -0900 Subject: [GuiUnit] Update numerics comparisons to work with nfloat, nuint and nint (#11) --- src/framework/Constraints/Numerics.cs | 95 ++++++++++++++++++++++-------- src/framework/GuiUnit/AdditionalAsserts.cs | 48 +++++++++++++++ 2 files changed, 117 insertions(+), 26 deletions(-) diff --git a/src/framework/Constraints/Numerics.cs b/src/framework/Constraints/Numerics.cs index 20ada05..3fe43b4 100755 --- a/src/framework/Constraints/Numerics.cs +++ b/src/framework/Constraints/Numerics.cs @@ -30,6 +30,10 @@ namespace NUnit.Framework.Constraints /// public class Numerics { + static Type nintType; + static Type nuintType; + static Type nfloatType; + #region Numeric Type Recognition /// /// Checks the type of the object, returning true if @@ -54,6 +58,7 @@ namespace NUnit.Framework.Constraints { if (obj is System.Double) return true; if (obj is System.Single) return true; + if (obj.GetType () == Type.GetType ("System.nfloat, Xamarin.Mac")) return true; } return false; } @@ -76,6 +81,8 @@ namespace NUnit.Framework.Constraints if (obj is System.UInt64) return true; if (obj is System.Int16) return true; if (obj is System.UInt16) return true; + if (obj.GetType () == Type.GetType ("System.nint, Xamarin.Mac")) return true; + if (obj.GetType () == Type.GetType ("System.nuint, Xamarin.Mac")) return true; } return false; } @@ -93,28 +100,49 @@ namespace NUnit.Framework.Constraints /// True if the values are equal public static bool AreEqual(object expected, object actual, ref Tolerance tolerance) { - if (expected is double || actual is double) - return AreEqual(Convert.ToDouble(expected), Convert.ToDouble(actual), ref tolerance); + _double = (expected is double || actual is double); + if (!_double && (IntPtr.Size == 8)) + _double = (expected.GetType () == nfloatType || actual.GetType () == nfloatType); + + if (_double) + return AreEqual (Convert.ToDouble (expected), Convert.ToDouble (actual), ref tolerance); + + bool _float = (expected is float || actual is float); + if (!_float && (IntPtr.Size == 4)) + _float = (expected.GetType () == nfloatType || actual.GetType () == nfloatType); - if (expected is float || actual is float) - return AreEqual(Convert.ToSingle(expected), Convert.ToSingle(actual), ref tolerance); + if (_float) + return AreEqual (Convert.ToSingle (expected), Convert.ToSingle (actual), ref tolerance); if (tolerance.Mode == ToleranceMode.Ulps) - throw new InvalidOperationException("Ulps may only be specified for floating point arguments"); + throw new InvalidOperationException ("Ulps may only be specified for floating point arguments"); if (expected is decimal || actual is decimal) - return AreEqual(Convert.ToDecimal(expected), Convert.ToDecimal(actual), tolerance); + return AreEqual (Convert.ToDecimal (expected), Convert.ToDecimal (actual), tolerance); + + bool _ulong = (expected is ulong || actual is ulong); + if (!_ulong && (IntPtr.Size == 8)) + _ulong = (expected.GetType () == nuintType || actual.GetType ()== nuintType); + + if (_ulong) + return AreEqual (Convert.ToUInt64 (expected), Convert.ToUInt64 (actual), tolerance); - if (expected is ulong || actual is ulong) - return AreEqual(Convert.ToUInt64(expected), Convert.ToUInt64(actual), tolerance); + bool _long = (expected is long || actual is long); + if (!_long && (IntPtr.Size == 8)) + _long = (expected.GetType () == nintType || actual.GetType () == nintType); - if (expected is long || actual is long) - return AreEqual(Convert.ToInt64(expected), Convert.ToInt64(actual), tolerance); + if (_long) + return AreEqual (Convert.ToInt64 (expected), Convert.ToInt64 (actual), tolerance); - if (expected is uint || actual is uint) - return AreEqual(Convert.ToUInt32(expected), Convert.ToUInt32(actual), tolerance); + bool _uint = (expected is uint || actual is uint); + if (!_uint && (IntPtr.Size == 4)) + _uint = (expected.GetType () == nuintType || actual.GetType () == nuintType); - return AreEqual(Convert.ToInt32(expected), Convert.ToInt32(actual), tolerance); + if (_uint) + return AreEqual (Convert.ToUInt32 (expected), Convert.ToUInt32 (actual), tolerance); + + // int or nint on 32bits archs + return AreEqual (Convert.ToInt32 (expected), Convert.ToInt32 (actual), tolerance); } private static bool AreEqual(double expected, double actual, ref Tolerance tolerance) @@ -351,30 +379,45 @@ namespace NUnit.Framework.Constraints /// The relationship of the values to each other public static int Compare(object expected, object actual) { - if (!IsNumericType(expected) || !IsNumericType(actual)) - throw new ArgumentException("Both arguments must be numeric"); + if (!IsNumericType (expected) || !IsNumericType (actual)) + throw new ArgumentException ("Both arguments must be numeric"); - if (IsFloatingPointNumeric(expected) || IsFloatingPointNumeric(actual)) - return Convert.ToDouble(expected).CompareTo(Convert.ToDouble(actual)); + if (IsFloatingPointNumeric (expected) || IsFloatingPointNumeric (actual)) + return Convert.ToDouble (expected).CompareTo (Convert.ToDouble (actual)); if (expected is decimal || actual is decimal) - return Convert.ToDecimal(expected).CompareTo(Convert.ToDecimal(actual)); + return Convert.ToDecimal (expected).CompareTo (Convert.ToDecimal (actual)); + + bool _ulong = (expected is ulong || actual is ulong); + if (!_ulong && (IntPtr.Size == 8)) + _ulong = (expected.GetType () == nuintType || actual.GetType () == nuintType); + + if (_ulong) + return Convert.ToUInt64 (expected).CompareTo (Convert.ToUInt64 (actual)); + + bool _long = (expected is long || actual is long); + if (!_long && (IntPtr.Size == 8)) + _long = (expected.GetType () == nintType || actual.GetType () == nintType); - if (expected is ulong || actual is ulong) - return Convert.ToUInt64(expected).CompareTo(Convert.ToUInt64(actual)); + if (_long) + return Convert.ToInt64 (expected).CompareTo (Convert.ToInt64 (actual)); - if (expected is long || actual is long) - return Convert.ToInt64(expected).CompareTo(Convert.ToInt64(actual)); + bool _uint = (expected is uint || actual is uint); + if (!_uint && (IntPtr.Size == 4)) + _uint = (expected.GetType () == nuintType || actual.GetType () == nuintType); - if (expected is uint || actual is uint) - return Convert.ToUInt32(expected).CompareTo(Convert.ToUInt32(actual)); + if (_uint) + return Convert.ToUInt32 (expected).CompareTo (Convert.ToUInt32 (actual)); - return Convert.ToInt32(expected).CompareTo(Convert.ToInt32(actual)); + return Convert.ToInt32 (expected).CompareTo (Convert.ToInt32 (actual)); } #endregion - private Numerics() + static Numerics() { + nintType = Type.GetType ("System.nint, Xamarin.Mac"); + nuintType = Type.GetType ("System.nuint, Xamarin.Mac"); + nfloatType = Type.GetType ("System.nfloat, Xamarin.Mac"); } } } \ No newline at end of file diff --git a/src/framework/GuiUnit/AdditionalAsserts.cs b/src/framework/GuiUnit/AdditionalAsserts.cs index f09f614..d0dd205 100644 --- a/src/framework/GuiUnit/AdditionalAsserts.cs +++ b/src/framework/GuiUnit/AdditionalAsserts.cs @@ -99,6 +99,54 @@ namespace NUnit.Framework Assert.That(actual, Is.InstanceOf ()); } + #region IsInstanceOfType + /// + /// Verifies that the object that is passed in is equal to null + /// If the object is not null then an + /// is thrown. + /// + /// The object that is to be tested + /// The message to display in case of failure + /// Array of objects to be used in formatting the message + public static void IsInstanceOfType (object anObject, string message, params object [] args) + { + Assert.That (anObject, Is.Null, message, args); + } + + /// + /// Asserts that an object is an instance of a given type. + /// + /// The expected Type + /// The object being examined + public static void IsInstanceOfType (System.Type expected, object actual) + { + IsInstanceOfType (expected, actual, string.Empty, null); + } + + /// + /// Asserts that an object is an instance of a given type. + /// + /// The expected Type + /// The object being examined + /// A message to display in case of failure + public static void IsInstanceOfType (System.Type expected, object actual, string message) + { + IsInstanceOfType (expected, actual, message, null); + } + + /// + /// Asserts that an object is an instance of a given type. + /// + /// The expected Type + /// The object being examined + /// A message to display in case of failure + /// An array of objects to be used in formatting the message + public static void IsInstanceOfType (System.Type expected, object actual, string message, params object [] args) + { + Assert.That (actual.GetType (), Is.EqualTo (expected), message, args); + } + #endregion + #endregion } } -- cgit v1.2.3