未验证 提交 aca3fcc1 编写于 作者: S Stephen Toub 提交者: GitHub

Add ArgumentOutOfRangeException.ThrowIf{Not}Equal (#83853)

* Add ArgumentOutOfRangeException.ThrowIf{Not}Equal

* Address PR feedback

* Address PR feedback
上级 2d05a601
......@@ -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);
}
......
......@@ -396,9 +396,6 @@
<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>
......
......@@ -50,18 +50,9 @@ public sealed override Expression Bind(object[] args, ReadOnlyCollection<Paramet
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
......
......@@ -179,22 +179,10 @@ public static DebugInfoExpression ClearDebugInfo(SymbolDocumentInfo document)
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();
......
......@@ -1006,13 +1006,6 @@ internal static ArgumentException ArgumentCannotBeOfTypeVoid(string? paramName)
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)
......
......@@ -643,11 +643,6 @@ internal static class Strings
/// </summary>
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>
......
......@@ -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);
......
......@@ -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
......
......@@ -1926,25 +1926,31 @@
<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>
......
......@@ -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<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 @@ public static void ThrowIfNegativeOrZero<T>(T value, [CallerArgumentExpression(n
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>
......
......@@ -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();
......
......@@ -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;
}
......
......@@ -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!;
}
}
......
......@@ -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>(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
{
......@@ -74,6 +74,8 @@ public static void Ctor_String_Object_String()
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 @@ public static void GenericHelpers_ThrowIfZero_Throws()
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 @@ public static void GenericHelpers_ThrowIfNegativeZero_Throws()
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 @@ public static void GenericHelpers_ThrowIfGreaterThan_Throws()
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 @@ public static void GenericHelpers_ThrowIfGreaterThanOrEqual_Throws()
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 @@ public static void GenericHelpers_ThrowIfLessThan_Throws()
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")();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册