diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs
index c7c2e98a837bda2423740cfcd6c3c4fe6d013b0f..dc8d8ccb4c7ccac529b02de888dfe951154ed367 100644
--- a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs
+++ b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs
@@ -58,8 +58,7 @@ public static unsafe void LoadInMemoryAssemblyInContext(IntPtr moduleHandle, Int
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);
}
diff --git a/src/libraries/System.Linq.Expressions/src/Resources/Strings.resx b/src/libraries/System.Linq.Expressions/src/Resources/Strings.resx
index 4386b7c2fcd87766202913c50da46a880bbc21fc..01f6ec7be8ddc50b22632fbcd4b56e34f5b39b50 100644
--- a/src/libraries/System.Linq.Expressions/src/Resources/Strings.resx
+++ b/src/libraries/System.Linq.Expressions/src/Resources/Strings.resx
@@ -396,9 +396,6 @@
Argument type cannot be System.Void.
-
- {0} must be greater than or equal to {1}
-
Cannot redefine label '{0}' in an inner block.
diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs
index 96157d4f793c51ae8d320a92359f89651545919c..7e39da3f5313d9f7d44b66df4870f4a33b4a8fee 100644
--- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs
+++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs
@@ -50,18 +50,9 @@ public sealed override Expression Bind(object[] args, ReadOnlyCollection endLine)
{
throw Error.StartEndMustBeOrdered();
diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs
index f1869bda25cc84ab057d7e0e056ee3f0edc4ec21..aa0eac79d0a59d71ee21fa1d19d9516864b21a20 100644
--- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs
+++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Error.cs
@@ -1006,13 +1006,6 @@ internal static ArgumentException ArgumentCannotBeOfTypeVoid(string? paramName)
return new ArgumentException(Strings.ArgumentCannotBeOfTypeVoid, paramName);
}
///
- /// ArgumentOutOfRangeException with message like "{0} must be greater than or equal to {1}"
- ///
- internal static ArgumentException OutOfRange(string? paramName, object? p1)
- {
- return new ArgumentOutOfRangeException(paramName, Strings.OutOfRange(paramName, p1));
- }
- ///
/// InvalidOperationException with message like "Cannot redefine label '{0}' in an inner block."
///
internal static InvalidOperationException LabelTargetAlreadyDefined(object? p0)
diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Strings.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Strings.cs
index eb365167d4a1120f3b4fce97d557c1bbd86336d0..b1f53512b8dcd3f24eff2aac529443c40b2af76c 100644
--- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Strings.cs
+++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Strings.cs
@@ -643,11 +643,6 @@ internal static class Strings
///
internal static string ArgumentCannotBeOfTypeVoid => SR.ArgumentCannotBeOfTypeVoid;
- ///
- /// A string like "{0} must be greater than or equal to {1}"
- ///
- internal static string OutOfRange(object? p0, object? p1) => SR.Format(SR.OutOfRange, p0, p1);
-
///
/// A string like "Cannot redefine label '{0}' in an inner block."
///
diff --git a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs
index ef36f49b95fa52603be27b39ce18fc4f80918a41..4475eaf34e6a5edb007f14f58284882103211da4 100644
--- a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs
+++ b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs
@@ -328,11 +328,7 @@ internal sealed class SingleItemReadOnlyList : IList
{
get
{
- if (index != 0)
- {
- throw new ArgumentOutOfRangeException(nameof(index));
- }
-
+ ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
return _item;
}
set => throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection);
diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs
index 402d1a58d39f71499505d6b91cf7e5b73e7c366a..d543fc8c5d7916a7b8b737c51708136c5516b64a 100644
--- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs
+++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs
@@ -76,11 +76,7 @@ private static string MarshalToString(IntPtr arg, string argName)
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 @@ public static unsafe int LoadAssembly(IntPtr assemblyPathNative, IntPtr loadCont
{
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 @@ private static void LoadAssemblyImpl(string assemblyPath)
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
diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
index ee3a1e1bff862a14b47f9e0a86749518420fffdb..16ad6d3e156b1881ab4274db4d2cf67ed39ed683 100644
--- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
+++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
@@ -1926,25 +1926,31 @@
Year must be between 1 and 9999.
- '{0}' must be a non-zero value.
+ {0} ('{1}') must be a non-zero value.
- '{0}' must be a non-negative value.
+ {0} ('{1}') must be a non-negative value.
- '{0}' must be a non-negative and non-zero value.
+ {0} ('{1}') must be a non-negative and non-zero value.
- '{0}' must be less than or equal to '{1}'.
+ {0} ('{1}') must be less than or equal to '{2}'.
- '{0}' must be less than '{1}'.
+ {0} ('{1}') must be less than '{2}'.
- '{0}' must be greater than or equal to '{1}'.
+ {0} ('{1}') must be greater than or equal to '{2}'.
- '{0}' must be greater than '{1}'.
+ {0} ('{1}') must be greater than '{2}'.
+
+
+ {0} ('{1}') must be equal to '{2}'.
+
+
+ {0} ('{1}') must not be equal to '{2}'.
Function does not accept floating point Not-a-Number values.
diff --git a/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs b/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
index 830a425a3caec3e8aec5fdb3368dfa77941c3ec1..d2ec8ca54f08787cf1a5411d4012e548a2f27394 100644
--- a/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
@@ -14,6 +14,7 @@
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
+using System.Collections.Generic;
namespace System
{
@@ -90,46 +91,40 @@ public override string Message
public virtual object? ActualValue => _actualValue;
[DoesNotReturn]
- private static void ThrowZero(string? paramName, T value)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName));
- }
+ private static void ThrowZero(string? paramName, T value) =>
+ throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName, value));
[DoesNotReturn]
- private static void ThrowNegative(string? paramName, T value)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName));
- }
+ private static void ThrowNegative(string? paramName, T value) =>
+ throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName, value));
[DoesNotReturn]
- private static void ThrowNegativeOrZero(string? paramName, T value)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName));
- }
+ private static void ThrowNegativeOrZero(string? paramName, T value) =>
+ throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName, value));
[DoesNotReturn]
- private static void ThrowGreater(string? paramName, T value, T other)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other));
- }
+ private static void ThrowGreater(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(string? paramName, T value, T other)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other));
- }
+ private static void ThrowGreaterEqual(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(string? paramName, T value, T other)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other));
- }
+ private static void ThrowLess(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(string? paramName, T value, T other)
- {
- throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other));
- }
+ private static void ThrowLessEqual(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(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(string? paramName, T value, T other) =>
+ throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeEqual, paramName, (object?)value ?? "null", (object?)other ?? "null"));
/// Throws an if is zero.
/// The argument to validate as non-zero.
@@ -161,6 +156,26 @@ public static void ThrowIfNegativeOrZero(T value, [CallerArgumentExpression(n
ThrowNegativeOrZero(paramName, value);
}
+ /// Throws an if is equal to .
+ /// The argument to validate as not equal to .
+ /// The value to compare with .
+ /// The name of the parameter with which corresponds.
+ public static void ThrowIfEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable?
+ {
+ if (EqualityComparer.Default.Equals(value, other))
+ ThrowEqual(paramName, value, other);
+ }
+
+ /// Throws an if is not equal to .
+ /// The argument to validate as equal to .
+ /// The value to compare with .
+ /// The name of the parameter with which corresponds.
+ public static void ThrowIfNotEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) where T : IEquatable?
+ {
+ if (!EqualityComparer.Default.Equals(value, other))
+ ThrowNotEqual(paramName, value, other);
+ }
+
/// Throws an if is greater than .
/// The argument to validate as less or equal than .
/// The value to compare with .
diff --git a/src/libraries/System.Private.Uri/src/System/UriScheme.cs b/src/libraries/System.Private.Uri/src/System/UriScheme.cs
index c9177b891371b3ec177318ce7302dcbc184b92a4..c798b4f665b4950fd52bf6e754588fa747433e8e 100644
--- a/src/libraries/System.Private.Uri/src/System/UriScheme.cs
+++ b/src/libraries/System.Private.Uri/src/System/UriScheme.cs
@@ -168,13 +168,12 @@ public static void Register(UriParser uriParser, string schemeName, int defaultP
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();
diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNamedNodeMap.SmallXmlNodeList.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNamedNodeMap.SmallXmlNodeList.cs
index 021941038895b4807ae045c184b25262f98b33b1..914ae97f45551a20577f3c2ffce0f97def018585 100644
--- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNamedNodeMap.SmallXmlNodeList.cs
+++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNamedNodeMap.SmallXmlNodeList.cs
@@ -45,8 +45,7 @@ public int Count
if (list != null)
return list[index];
- if (index != 0)
- throw new ArgumentOutOfRangeException(nameof(index));
+ ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
return _field;
}
@@ -97,8 +96,7 @@ public void RemoveAt(int index)
return;
}
- if (index != 0)
- throw new ArgumentOutOfRangeException(nameof(index));
+ ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
_field = null;
}
@@ -107,8 +105,7 @@ public void Insert(int index, object value)
{
if (_field == null)
{
- if (index != 0)
- throw new ArgumentOutOfRangeException(nameof(index));
+ ArgumentOutOfRangeException.ThrowIfNotEqual(index, 0);
Add(value);
return;
}
diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaCollection.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaCollection.cs
index 918fc6c43481567162d7767b0bda3ed52f87f58f..c9502b79877bb205dfd86adcf4854a70cf01a310 100644
--- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaCollection.cs
+++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaCollection.cs
@@ -227,14 +227,11 @@ public XmlSchemaCollectionEnumerator GetEnumerator()
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 @@ void ICollection.CopyTo(Array array, int index)
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!;
}
}
diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs
index bf955c2f418a77c3f73c530a571e887e6529018b..1de1056254206dd37f0e21c59837e689466861e7 100644
--- a/src/libraries/System.Runtime/ref/System.Runtime.cs
+++ b/src/libraries/System.Runtime/ref/System.Runtime.cs
@@ -301,13 +301,15 @@ public partial class ArgumentOutOfRangeException : System.ArgumentException
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 value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
- public static void ThrowIfNegative(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
- public static void ThrowIfNegativeOrZero(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
+ public static void ThrowIfEqual(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null) where T : System.IEquatable? { throw null; }
public static void ThrowIfGreaterThan(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable { throw null; }
public static void ThrowIfGreaterThanOrEqual(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable { throw null; }
public static void ThrowIfLessThan(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable { throw null; }
public static void ThrowIfLessThanOrEqual(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.IComparable { throw null; }
+ public static void ThrowIfNegative(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
+ public static void ThrowIfNegativeOrZero(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
+ public static void ThrowIfNotEqual(T value, T other, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null) where T : System.IEquatable? { throw null; }
+ public static void ThrowIfZero(T value, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute(nameof(value))] string? paramName = null) where T : System.Numerics.INumberBase { throw null; }
}
public partial class ArithmeticException : System.SystemException
{
diff --git a/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs b/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs
index 777bf25f0fb03d09ddc35b2dc210414155aaaead..7fc6828e9dfdc4db181e5c8dd6ada3390a09dc9a 100644
--- a/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs
+++ b/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs
@@ -74,6 +74,8 @@ public static void Ctor_String_Object_String()
private static Action GreaterThanOrEqualHelper(T value, T other) where T : IComparable => () => ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(value, other);
private static Action LessThanHelper(T value, T other) where T : IComparable => () => ArgumentOutOfRangeException.ThrowIfLessThan(value, other);
private static Action LessThanOrEqualHelper(T value, T other) where T : IComparable => () => ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, other);
+ private static Action EqualHelper(T value, T other) where T : IEquatable => () => ArgumentOutOfRangeException.ThrowIfEqual(value, other);
+ private static Action NotEqualHelper(T value, T other) where T : IEquatable => () => ArgumentOutOfRangeException.ThrowIfNotEqual(value, other);
[Fact]
public static void GenericHelpers_ThrowIfZero_Throws()
@@ -86,6 +88,8 @@ public static void GenericHelpers_ThrowIfZero_Throws()
Assert.Equal((double)0, AssertExtensions.Throws(HelpersParamName, ZeroHelper(0)).ActualValue);
Assert.Equal(+0.0, AssertExtensions.Throws(HelpersParamName, ZeroHelper(+0.0)).ActualValue);
Assert.Equal(-0.0, AssertExtensions.Throws(HelpersParamName, ZeroHelper(-0.0)).ActualValue);
+
+ ZeroHelper(1)();
}
[Fact]
@@ -94,6 +98,8 @@ public static void GenericHelpers_ThrowIfNegativeZero_Throws()
Assert.Equal(-1, AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-1)).ActualValue);
Assert.Equal(-0.0f, AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-0.0f)).ActualValue);
Assert.Equal(-0.0, AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-0.0)).ActualValue);
+
+ NegativeOrZeroHelper(1)();
}
[Fact]
@@ -103,6 +109,8 @@ public static void GenericHelpers_ThrowIfGreaterThan_Throws()
Assert.Equal(1u, AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1, 0)).ActualValue);
Assert.Equal(1.000000001, AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1.000000001, 1)).ActualValue);
Assert.Equal(1.00001f, AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1.00001f, 1)).ActualValue);
+
+ GreaterThanHelper(1, 2)();
}
[Fact]
@@ -112,6 +120,13 @@ public static void GenericHelpers_ThrowIfGreaterThanOrEqual_Throws()
Assert.Equal(1u, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)).ActualValue);
Assert.Equal((double)1, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)).ActualValue);
Assert.Equal(1f, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)).ActualValue);
+
+ Assert.Equal(3, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(3, 1)).ActualValue);
+ Assert.Equal(4u, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(4, 1)).ActualValue);
+ Assert.Equal((double)1.1, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1.1, 1)).ActualValue);
+ Assert.Equal(2.1f, AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(2.1f, 1)).ActualValue);
+
+ GreaterThanOrEqualHelper(1, 2)();
}
[Fact]
@@ -121,15 +136,53 @@ public static void GenericHelpers_ThrowIfLessThan_Throws()
Assert.Equal(0u, AssertExtensions.Throws(HelpersParamName, LessThanHelper(0, 1)).ActualValue);
Assert.Equal((double)1, AssertExtensions.Throws(HelpersParamName, LessThanHelper(1, 1.000000001)).ActualValue);
Assert.Equal(1f, AssertExtensions.Throws(HelpersParamName, LessThanHelper(1, 1.00001f)).ActualValue);
+
+ LessThanHelper(2, 1)();
}
[Fact]
public static void GenericHelpers_ThrowIfLessThanOrEqual_Throws()
{
+ Assert.Equal(-1, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(-1, 1)).ActualValue);
+ Assert.Equal(0u, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(0, 1)).ActualValue);
+ Assert.Equal((double)0.9, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(0.9, 1)).ActualValue);
+ Assert.Equal(-0.1f, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(-0.1f, 1)).ActualValue);
+
Assert.Equal(1, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)).ActualValue);
Assert.Equal(1u, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)).ActualValue);
Assert.Equal((double)1, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)).ActualValue);
Assert.Equal(1f, AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)).ActualValue);
+
+ LessThanHelper(2, 1)();
+ }
+
+ [Fact]
+ public static void GenericHelpers_ThrowIfEqual_Throws()
+ {
+ Assert.Equal(1, AssertExtensions.Throws(HelpersParamName, EqualHelper(1, 1)).ActualValue);
+ Assert.Equal(1u, AssertExtensions.Throws(HelpersParamName, EqualHelper(1, 1)).ActualValue);
+ Assert.Equal((double)1, AssertExtensions.Throws(HelpersParamName, EqualHelper(1, 1)).ActualValue);
+ Assert.Equal(1f, AssertExtensions.Throws(HelpersParamName, EqualHelper(1, 1)).ActualValue);
+ Assert.Null(AssertExtensions.Throws(HelpersParamName, EqualHelper(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(HelpersParamName, NotEqualHelper(-1, 1)).ActualValue);
+ Assert.Equal(2u, AssertExtensions.Throws(HelpersParamName, NotEqualHelper(2, 1)).ActualValue);
+ Assert.Equal((double)2, AssertExtensions.Throws(HelpersParamName, NotEqualHelper(2, 1)).ActualValue);
+ Assert.Equal(1f, AssertExtensions.Throws(HelpersParamName, NotEqualHelper(1, 2)).ActualValue);
+ Assert.Equal("test", AssertExtensions.Throws(HelpersParamName, NotEqualHelper("test", null)).ActualValue);
+ Assert.Null(AssertExtensions.Throws(HelpersParamName, NotEqualHelper(null, "test")).ActualValue);
+
+ NotEqualHelper(2, 2)();
+ NotEqualHelper("test", "test")();
}
}
}