Add ArgumentOutOfRangeException.ThrowIf{Not}Equal (#83853)
authorStephen Toub <stoub@microsoft.com>
Thu, 6 Apr 2023 02:33:21 +0000 (22:33 -0400)
committerGitHub <noreply@github.com>
Thu, 6 Apr 2023 02:33:21 +0000 (22:33 -0400)
* Add ArgumentOutOfRangeException.ThrowIf{Not}Equal

* Address PR feedback

* Address PR feedback

15 files changed:
src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs
src/libraries/System.Linq.Expressions/src/Resources/Strings.resx
src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs
src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/DebugInfoExpression.cs
src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs
src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Strings.cs
src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs
src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
src/libraries/System.Private.Uri/src/System/UriScheme.cs
src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNamedNodeMap.SmallXmlNodeList.cs
src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaCollection.cs
src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs

index c7c2e98..dc8d8cc 100644 (file)
@@ -58,8 +58,7 @@ namespace Internal.Runtime.InteropServices
             if (!IsSupported)
                 throw new NotSupportedException(SR.NotSupported_CppCli);
 
-            if (loadContext != IntPtr.Zero)
-                throw new ArgumentOutOfRangeException(nameof(loadContext));
+            ArgumentOutOfRangeException.ThrowIfNotEqual(loadContext, IntPtr.Zero);
 
             LoadInMemoryAssemblyInContextImpl(moduleHandle, assemblyPath, AssemblyLoadContext.Default);
         }
index 4386b7c..01f6ec7 100644 (file)
   <data name="ArgumentCannotBeOfTypeVoid" xml:space="preserve">
     <value>Argument type cannot be System.Void.</value>
   </data>
-  <data name="OutOfRange" xml:space="preserve">
-    <value>{0} must be greater than or equal to {1}</value>
-  </data>
   <data name="LabelTargetAlreadyDefined" xml:space="preserve">
     <value>Cannot redefine label '{0}' in an inner block.</value>
   </data>
index 96157d4..7e39da3 100644 (file)
@@ -50,18 +50,9 @@ namespace System.Dynamic
             ArgumentNullException.ThrowIfNull(args);
             ArgumentNullException.ThrowIfNull(parameters);
             ArgumentNullException.ThrowIfNull(returnLabel);
-            if (args.Length == 0)
-            {
-                throw System.Linq.Expressions.Error.OutOfRange("args.Length", 1);
-            }
-            if (parameters.Count == 0)
-            {
-                throw System.Linq.Expressions.Error.OutOfRange("parameters.Count", 1);
-            }
-            if (args.Length != parameters.Count)
-            {
-                throw new ArgumentOutOfRangeException(nameof(args));
-            }
+            ArgumentOutOfRangeException.ThrowIfZero(args.Length);
+            ArgumentOutOfRangeException.ThrowIfZero(parameters.Count);
+            ArgumentOutOfRangeException.ThrowIfNotEqual(args.Length, parameters.Count);
 
             // Ensure that the binder's ReturnType matches CallSite's return
             // type. We do this so meta objects and language binders can
index ca3f0e7..0f946b1 100644 (file)
@@ -179,22 +179,10 @@ namespace System.Linq.Expressions
 
         private static void ValidateSpan(int startLine, int startColumn, int endLine, int endColumn)
         {
-            if (startLine < 1)
-            {
-                throw Error.OutOfRange(nameof(startLine), 1);
-            }
-            if (startColumn < 1)
-            {
-                throw Error.OutOfRange(nameof(startColumn), 1);
-            }
-            if (endLine < 1)
-            {
-                throw Error.OutOfRange(nameof(endLine), 1);
-            }
-            if (endColumn < 1)
-            {
-                throw Error.OutOfRange(nameof(endColumn), 1);
-            }
+            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(startLine);
+            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(startColumn);
+            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(endLine);
+            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(endColumn);
             if (startLine > endLine)
             {
                 throw Error.StartEndMustBeOrdered();
index f1869bd..aa0eac7 100644 (file)
@@ -1006,13 +1006,6 @@ namespace System.Linq.Expressions
             return new ArgumentException(Strings.ArgumentCannotBeOfTypeVoid, paramName);
         }
         /// <summary>
-        /// ArgumentOutOfRangeException with message like "{0} must be greater than or equal to {1}"
-        /// </summary>
-        internal static ArgumentException OutOfRange(string? paramName, object? p1)
-        {
-            return new ArgumentOutOfRangeException(paramName, Strings.OutOfRange(paramName, p1));
-        }
-        /// <summary>
         /// InvalidOperationException with message like "Cannot redefine label '{0}' in an inner block."
         /// </summary>
         internal static InvalidOperationException LabelTargetAlreadyDefined(object? p0)
index eb36516..b1f5351 100644 (file)
@@ -644,11 +644,6 @@ namespace System.Linq.Expressions
         internal static string ArgumentCannotBeOfTypeVoid => SR.ArgumentCannotBeOfTypeVoid;
 
         /// <summary>
-        /// A string like "{0} must be greater than or equal to {1}"
-        /// </summary>
-        internal static string OutOfRange(object? p0, object? p1) => SR.Format(SR.OutOfRange, p0, p1);
-
-        /// <summary>
         /// A string like "Cannot redefine label '{0}' in an inner block."
         /// </summary>
         internal static string LabelTargetAlreadyDefined(object? p0) => SR.Format(SR.LabelTargetAlreadyDefined, p0);
index ef36f49..4475eaf 100644 (file)
@@ -328,11 +328,7 @@ namespace System.Collections.Specialized
         {
             get
             {
-                if (index != 0)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(index));
-                }
-
+                ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
                 return _item;
             }
             set => throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection);
index 402d1a5..d543fc8 100644 (file)
@@ -76,11 +76,7 @@ namespace Internal.Runtime.InteropServices
                 string typeName = MarshalToString(typeNameNative, nameof(typeNameNative));
                 string methodName = MarshalToString(methodNameNative, nameof(methodNameNative));
 
-                if (reserved != IntPtr.Zero)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(reserved));
-                }
-
+                ArgumentOutOfRangeException.ThrowIfNotEqual(reserved, IntPtr.Zero);
                 ArgumentNullException.ThrowIfNull(functionHandle);
 
                 // Set up the AssemblyLoadContext for this delegate.
@@ -119,15 +115,8 @@ namespace Internal.Runtime.InteropServices
             {
                 string assemblyPath = MarshalToString(assemblyPathNative, nameof(assemblyPathNative));
 
-                if (loadContext != IntPtr.Zero)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(loadContext));
-                }
-
-                if (reserved != IntPtr.Zero)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(reserved));
-                }
+                ArgumentOutOfRangeException.ThrowIfNotEqual(loadContext, IntPtr.Zero);
+                ArgumentOutOfRangeException.ThrowIfNotEqual(reserved, IntPtr.Zero);
 
                 LoadAssemblyLocal(assemblyPath);
             }
@@ -215,16 +204,8 @@ namespace Internal.Runtime.InteropServices
                 string typeName = MarshalToString(typeNameNative, nameof(typeNameNative));
                 string methodName = MarshalToString(methodNameNative, nameof(methodNameNative));
 
-                if (loadContext != IntPtr.Zero)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(loadContext));
-                }
-
-                if (reserved != IntPtr.Zero)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(reserved));
-                }
-
+                ArgumentOutOfRangeException.ThrowIfNotEqual(loadContext, IntPtr.Zero);
+                ArgumentOutOfRangeException.ThrowIfNotEqual(reserved, IntPtr.Zero);
                 ArgumentNullException.ThrowIfNull(functionHandle);
 
 #pragma warning disable IL2026 // suppressed in ILLink.Suppressions.LibraryBuild.xml
index ee3a1e1..16ad6d3 100644 (file)
     <value>Year must be between 1 and 9999.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeNonZero" xml:space="preserve">
-    <value>'{0}' must be a non-zero value.</value>
+    <value>{0} ('{1}') must be a non-zero value.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeNonNegative" xml:space="preserve">
-    <value>'{0}' must be a non-negative value.</value>
+    <value>{0} ('{1}') must be a non-negative value.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero" xml:space="preserve">
-    <value>'{0}' must be a non-negative and non-zero value.</value>
+    <value>{0} ('{1}') must be a non-negative and non-zero value.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeLessOrEqual" xml:space="preserve">
-    <value>'{0}' must be less than or equal to '{1}'.</value>
+    <value>{0} ('{1}') must be less than or equal to '{2}'.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeLess" xml:space="preserve">
-    <value>'{0}' must be less than '{1}'.</value>
+    <value>{0} ('{1}') must be less than '{2}'.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeGreaterOrEqual" xml:space="preserve">
-    <value>'{0}' must be greater than or equal to '{1}'.</value>
+    <value>{0} ('{1}') must be greater than or equal to '{2}'.</value>
   </data>
   <data name="ArgumentOutOfRange_Generic_MustBeGreater" xml:space="preserve">
-    <value>'{0}' must be greater than '{1}'.</value>
+    <value>{0} ('{1}') must be greater than '{2}'.</value>
+  </data>
+  <data name="ArgumentOutOfRange_Generic_MustBeEqual" xml:space="preserve">
+    <value>{0} ('{1}') must be equal to '{2}'.</value>
+  </data>
+  <data name="ArgumentOutOfRange_Generic_MustBeNotEqual" xml:space="preserve">
+    <value>{0} ('{1}') must not be equal to '{2}'.</value>
   </data>
   <data name="Arithmetic_NaN" xml:space="preserve">
     <value>Function does not accept floating point Not-a-Number values.</value>
index 830a425..d2ec8ca 100644 (file)
@@ -14,6 +14,7 @@ using System.Runtime.Serialization;
 using System.Runtime.CompilerServices;
 using System.Diagnostics.CodeAnalysis;
 using System.Numerics;
+using System.Collections.Generic;
 
 namespace System
 {
@@ -90,46 +91,40 @@ namespace System
         public virtual object? ActualValue => _actualValue;
 
         [DoesNotReturn]
-        private static void ThrowZero<T>(string? paramName, T value)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName));
-        }
+        private static void ThrowZero<T>(string? paramName, T value) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowNegative<T>(string? paramName, T value)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName));
-        }
+        private static void ThrowNegative<T>(string? paramName, T value) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowNegativeOrZero<T>(string? paramName, T value)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName));
-        }
+        private static void ThrowNegativeOrZero<T>(string? paramName, T value) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName, value));
 
         [DoesNotReturn]
-        private static void ThrowGreater<T>(string? paramName, T value, T other)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other));
-        }
+        private static void ThrowGreater<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowGreaterEqual<T>(string? paramName, T value, T other)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other));
-        }
+        private static void ThrowGreaterEqual<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowLess<T>(string? paramName, T value, T other)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other));
-        }
+        private static void ThrowLess<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, value, other));
 
         [DoesNotReturn]
-        private static void ThrowLessEqual<T>(string? paramName, T value, T other)
-        {
-            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other));
-        }
+        private static void ThrowLessEqual<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, value, other));
+
+        [DoesNotReturn]
+        private static void ThrowEqual<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNotEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
+
+        [DoesNotReturn]
+        private static void ThrowNotEqual<T>(string? paramName, T value, T other) =>
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary>
         /// <param name="value">The argument to validate as non-zero.</param>
@@ -161,6 +156,26 @@ namespace System
                 ThrowNegativeOrZero(paramName, value);
         }
 
+        /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is equal to <paramref name="other"/>.</summary>
+        /// <param name="value">The argument to validate as not equal to <paramref name="other"/>.</param>
+        /// <param name="other">The value to compare with <paramref name="value"/>.</param>
+        /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
+        public static void ThrowIfEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
+        {
+            if (EqualityComparer<T>.Default.Equals(value, other))
+                ThrowEqual(paramName, value, other);
+        }
+
+        /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is not equal to <paramref name="other"/>.</summary>
+        /// <param name="value">The argument to validate as equal to <paramref name="other"/>.</param>
+        /// <param name="other">The value to compare with <paramref name="value"/>.</param>
+        /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
+        public static void ThrowIfNotEqual<T>(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable<T>?
+        {
+            if (!EqualityComparer<T>.Default.Equals(value, other))
+                ThrowNotEqual(paramName, value, other);
+        }
+
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary>
         /// <param name="value">The argument to validate as less or equal than <paramref name="other"/>.</param>
         /// <param name="other">The value to compare with <paramref name="value"/>.</param>
index c9177b8..c798b4f 100644 (file)
@@ -168,13 +168,12 @@ namespace System
             ArgumentNullException.ThrowIfNull(uriParser);
             ArgumentNullException.ThrowIfNull(schemeName);
 
-            if (schemeName.Length == 1)
-                throw new ArgumentOutOfRangeException(nameof(schemeName));
+            ArgumentOutOfRangeException.ThrowIfEqual(schemeName.Length, 1);
 
             if (!Uri.CheckSchemeName(schemeName))
                 throw new ArgumentOutOfRangeException(nameof(schemeName));
 
-            if ((defaultPort > 0xFFFF || defaultPort < 0) && defaultPort != -1)
+            if ((uint)defaultPort > 0xFFFF && defaultPort != -1)
                 throw new ArgumentOutOfRangeException(nameof(defaultPort));
 
             schemeName = schemeName.ToLowerInvariant();
index 0219410..914ae97 100644 (file)
@@ -45,8 +45,7 @@ namespace System.Xml
                     if (list != null)
                         return list[index];
 
-                    if (index != 0)
-                        throw new ArgumentOutOfRangeException(nameof(index));
+                    ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
 
                     return _field;
                 }
@@ -97,8 +96,7 @@ namespace System.Xml
                     return;
                 }
 
-                if (index != 0)
-                    throw new ArgumentOutOfRangeException(nameof(index));
+                ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
 
                 _field = null;
             }
@@ -107,8 +105,7 @@ namespace System.Xml
             {
                 if (_field == null)
                 {
-                    if (index != 0)
-                        throw new ArgumentOutOfRangeException(nameof(index));
+                    ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
                     Add(value);
                     return;
                 }
index 918fc6c..c9502b7 100644 (file)
@@ -227,14 +227,11 @@ namespace System.Xml.Schema
         void ICollection.CopyTo(Array array, int index)
         {
             ArgumentNullException.ThrowIfNull(array);
-
             ArgumentOutOfRangeException.ThrowIfNegative(index);
+
             for (XmlSchemaCollectionEnumerator e = this.GetEnumerator(); e.MoveNext();)
             {
-                if (index == array.Length)
-                {
-                    throw new ArgumentOutOfRangeException(nameof(index));
-                }
+                ArgumentOutOfRangeException.ThrowIfEqual(index, array.Length);
                 array.SetValue(e.Current, index++);
             }
         }
@@ -242,18 +239,14 @@ namespace System.Xml.Schema
         public void CopyTo(XmlSchema[] array, int index)
         {
             ArgumentNullException.ThrowIfNull(array);
-
             ArgumentOutOfRangeException.ThrowIfNegative(index);
+
             for (XmlSchemaCollectionEnumerator e = this.GetEnumerator(); e.MoveNext();)
             {
                 XmlSchema? schema = e.Current;
                 if (schema != null)
                 {
-                    if (index == array.Length)
-                    {
-                        throw new ArgumentOutOfRangeException(nameof(index));
-                    }
-
+                    ArgumentOutOfRangeException.ThrowIfEqual(index, array.Length);
                     array[index++] = e.Current!;
                 }
             }
index bf955c2..1de1056 100644 (file)
@@ -301,13 +301,15 @@ namespace System
         public virtual object? ActualValue { get { throw null; } }
         public override string Message { get { throw null; } }
         public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
-        public static void ThrowIfZero<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
-        public static void ThrowIfNegative<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
-        public static void ThrowIfNegativeOrZero<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
+        public static void ThrowIfEqual<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null) where T : System.IEquatable<T>? { throw null; }
         public static void ThrowIfGreaterThan<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable<T> { throw null; }
         public static void ThrowIfGreaterThanOrEqual<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable<T> { throw null; }
         public static void ThrowIfLessThan<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable<T> { throw null; }
         public static void ThrowIfLessThanOrEqual<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable<T> { throw null; }
+        public static void ThrowIfNegative<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
+        public static void ThrowIfNegativeOrZero<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
+        public static void ThrowIfNotEqual<T>(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null) where T : System.IEquatable<T>? { throw null; }
+        public static void ThrowIfZero<T>(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase<T> { throw null; }
     }
     public partial class ArithmeticException : System.SystemException
     {
index 777bf25..7fc6828 100644 (file)
@@ -74,6 +74,8 @@ namespace System.Tests
         private static Action GreaterThanOrEqualHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(value, other);
         private static Action LessThanHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfLessThan(value, other);
         private static Action LessThanOrEqualHelper<T>(T value, T other) where T : IComparable<T> => () => ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, other);
+        private static Action EqualHelper<T>(T value, T other) where T : IEquatable<T> => () => ArgumentOutOfRangeException.ThrowIfEqual(value, other);
+        private static Action NotEqualHelper<T>(T value, T other) where T : IEquatable<T> => () => ArgumentOutOfRangeException.ThrowIfNotEqual(value, other);
 
         [Fact]
         public static void GenericHelpers_ThrowIfZero_Throws()
@@ -86,6 +88,8 @@ namespace System.Tests
             Assert.Equal((double)0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(0)).ActualValue);
             Assert.Equal(+0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(+0.0)).ActualValue);
             Assert.Equal(-0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(-0.0)).ActualValue);
+
+            ZeroHelper(1)();
         }
 
         [Fact]
@@ -94,6 +98,8 @@ namespace System.Tests
             Assert.Equal(-1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<int>(-1)).ActualValue);
             Assert.Equal(-0.0f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<float>(-0.0f)).ActualValue);
             Assert.Equal(-0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<double>(-0.0)).ActualValue);
+
+            NegativeOrZeroHelper(1)();
         }
 
         [Fact]
@@ -103,6 +109,8 @@ namespace System.Tests
             Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<uint>(1, 0)).ActualValue);
             Assert.Equal(1.000000001, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<double>(1.000000001, 1)).ActualValue);
             Assert.Equal(1.00001f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<float>(1.00001f, 1)).ActualValue);
+
+            GreaterThanHelper(1, 2)();
         }
 
         [Fact]
@@ -112,6 +120,13 @@ namespace System.Tests
             Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<uint>(1, 1)).ActualValue);
             Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<double>(1, 1)).ActualValue);
             Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<float>(1, 1)).ActualValue);
+
+            Assert.Equal(3, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<int>(3, 1)).ActualValue);
+            Assert.Equal(4u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<uint>(4, 1)).ActualValue);
+            Assert.Equal((double)1.1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<double>(1.1, 1)).ActualValue);
+            Assert.Equal(2.1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<float>(2.1f, 1)).ActualValue);
+
+            GreaterThanOrEqualHelper(1, 2)();
         }
 
         [Fact]
@@ -121,15 +136,53 @@ namespace System.Tests
             Assert.Equal(0u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<uint>(0, 1)).ActualValue);
             Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<double>(1, 1.000000001)).ActualValue);
             Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<float>(1, 1.00001f)).ActualValue);
+
+            LessThanHelper(2, 1)();
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfLessThanOrEqual_Throws()
         {
+            Assert.Equal(-1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<int>(-1, 1)).ActualValue);
+            Assert.Equal(0u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<uint>(0, 1)).ActualValue);
+            Assert.Equal((double)0.9, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<double>(0.9, 1)).ActualValue);
+            Assert.Equal(-0.1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<float>(-0.1f, 1)).ActualValue);
+
             Assert.Equal(1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<int>(1, 1)).ActualValue);
             Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<uint>(1, 1)).ActualValue);
             Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<double>(1, 1)).ActualValue);
             Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<float>(1, 1)).ActualValue);
+
+            LessThanHelper(2, 1)();
+        }
+
+        [Fact]
+        public static void GenericHelpers_ThrowIfEqual_Throws()
+        {
+            Assert.Equal(1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, EqualHelper<int>(1, 1)).ActualValue);
+            Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, EqualHelper<uint>(1, 1)).ActualValue);
+            Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, EqualHelper<double>(1, 1)).ActualValue);
+            Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, EqualHelper<float>(1, 1)).ActualValue);
+            Assert.Null(AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, EqualHelper<string>(null, null)).ActualValue);
+
+            EqualHelper(1, 2)();
+            EqualHelper("test1", "test2")();
+            EqualHelper("test1", null)();
+            EqualHelper(null, "test2")();
+        }
+
+        [Fact]
+        public static void GenericHelpers_ThrowIfNotEqual_Throws()
+        {
+            Assert.Equal(-1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<int>(-1, 1)).ActualValue);
+            Assert.Equal(2u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<uint>(2, 1)).ActualValue);
+            Assert.Equal((double)2, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<double>(2, 1)).ActualValue);
+            Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<float>(1, 2)).ActualValue);
+            Assert.Equal("test", AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<string>("test", null)).ActualValue);
+            Assert.Null(AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NotEqualHelper<string>(null, "test")).ActualValue);
+
+            NotEqualHelper(2, 2)();
+            NotEqualHelper("test", "test")();
         }
     }
 }