diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs index 082e91bc719c9..25445726f2900 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs @@ -626,14 +626,10 @@ private static bool InvokeMemoryLoadChangeNotifications() /// delegate to invoke when operation occurss internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent, float highMemoryPercent, Action notification) { - if (highMemoryPercent < 0 || highMemoryPercent > 1.0 || highMemoryPercent <= lowMemoryPercent) - { - throw new ArgumentOutOfRangeException(nameof(highMemoryPercent)); - } - if (lowMemoryPercent < 0) - { - throw new ArgumentOutOfRangeException(nameof(lowMemoryPercent)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(highMemoryPercent, 0); + ArgumentOutOfRangeException.ThrowIfGreaterThan(highMemoryPercent, 1.0); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(highMemoryPercent, lowMemoryPercent); + ArgumentOutOfRangeException.ThrowIfLessThan(lowMemoryPercent, 0); ArgumentNullException.ThrowIfNull(notification); lock (s_notifications) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs index a5f4fdd8ba7d5..c4892500d1746 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs @@ -652,8 +652,7 @@ public void SetSignature( public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string? strParamName) { - if (position < 0) - throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence); + ArgumentOutOfRangeException.ThrowIfNegative(position); ThrowIfGeneric(); m_containingType.ThrowIfCreated(); diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index 14c36da882f52..86afe347af9c8 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -314,8 +314,7 @@ public static IntPtr AllocateTypeAssociatedMemory(Type type, int size) if (type is not RuntimeType rt) throw new ArgumentException(SR.Arg_MustBeType, nameof(type)); - if (size < 0) - throw new ArgumentOutOfRangeException(nameof(size)); + ArgumentOutOfRangeException.ThrowIfNegative(size); return AllocateTypeAssociatedMemory(new QCallTypeHandle(ref rt), (uint)size); } diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index 762886be5e9f3..51b4292025eeb 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -1862,6 +1862,27 @@ Year must be between 1 and 9999. + + '{0}' must be a non-zero value. + + + '{0}' must be a non-negative value. + + + '{0}' must be a non-negative and non-zero value. + + + '{0}' must be less than or equal to '{1}'. + + + '{0}' must be less than '{1}'. + + + '{0}' must be greater than or equal to '{1}'. + + + '{0}' must be greater than '{1}'. + Function does not accept floating point Not-a-Number values. @@ -2735,9 +2756,6 @@ ValueFactory attempted to access the Value property of this instance. - - The spinCount argument must be in the range 0 to {0}, inclusive. - There are too many threads currently waiting on the event. A maximum of {0} waiting threads are supported. diff --git a/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs b/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs index 65563b20d495b..912df39b273de 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs @@ -11,6 +11,9 @@ =============================================================================*/ using System.Runtime.Serialization; +using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; +using System.Numerics; namespace System { @@ -85,5 +88,121 @@ public override string Message // Gets the value of the argument that caused the exception. public virtual object? ActualValue => _actualValue; + + [DoesNotReturn] + private static void ThrowZero(string? paramName) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName)); + } + + [DoesNotReturn] + private static void ThrowNegative(string? paramName) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName)); + } + + [DoesNotReturn] + private static void ThrowNegativeOrZero(string? paramName) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName)); + } + + [DoesNotReturn] + private static void ThrowGreater(string? paramName, T other) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other)); + } + + [DoesNotReturn] + private static void ThrowGreaterEqual(string? paramName, T other) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other)); + } + + [DoesNotReturn] + private static void ThrowLess(string? paramName, T other) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other)); + } + + [DoesNotReturn] + private static void ThrowLessEqual(string? paramName, T other) + { + throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other)); + } + + /// Throws an if is zero. + /// The argument to validate as non-zero. + /// The name of the parameter with which corresponds. + public static void ThrowIfZero(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : INumberBase + { + if (T.IsZero(value)) + ThrowZero(paramName); + } + + /// Throws an if is negative. + /// The argument to validate as non-negative. + /// The name of the parameter with which corresponds. + public static void ThrowIfNegative(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : INumberBase + { + if (T.IsNegative(value)) + ThrowNegative(paramName); + } + + /// Throws an if is negative or zero. + /// The argument to validate as non-zero or non-negative. + /// The name of the parameter with which corresponds. + public static void ThrowIfNegativeOrZero(T value, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : INumberBase + { + if (T.IsNegative(value) || T.IsZero(value)) + ThrowNegativeOrZero(paramName); + } + + /// Throws an if is greater than . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfGreaterThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : IComparable + { + if (value.CompareTo(other) > 0) + ThrowGreater(paramName, other); + } + + /// Throws an if is greater than or equal . + /// The argument to validate as less than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfGreaterThanOrEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : IComparable + { + if (value.CompareTo(other) >= 0) + ThrowGreaterEqual(paramName, other); + } + + /// Throws an if is less than . + /// The argument to validate as greatar than or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfLessThan(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : IComparable + { + if (value.CompareTo(other) < 0) + ThrowLess(paramName, other); + } + + /// Throws an if is less than or equal . + /// The argument to validate as greatar than than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfLessThanOrEqual(T value, T other, [CallerArgumentExpression(nameof(value))] string? paramName = null) + where T : IComparable + { + if (value.CompareTo(other) <= 0) + ThrowLessEqual(paramName, other); + } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs index 60caf8e76d275..f8e965bf4ed82 100644 --- a/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs @@ -657,8 +657,7 @@ public static string ToString(byte[] value, int startIndex, int length) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); if (startIndex < 0 || startIndex >= value.Length && startIndex > 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLess); - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_GenericPositive); + ArgumentOutOfRangeException.ThrowIfNegative(length); if (startIndex > value.Length - length) ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall, ExceptionArgument.value); @@ -667,11 +666,8 @@ public static string ToString(byte[] value, int startIndex, int length) return string.Empty; } - if (length > (int.MaxValue / 3)) - { - // (int.MaxValue / 3) == 715,827,882 Bytes == 699 MB - throw new ArgumentOutOfRangeException(nameof(length), SR.Format(SR.ArgumentOutOfRange_LengthTooLarge, int.MaxValue / 3)); - } + // (int.MaxValue / 3) == 715,827,882 Bytes == 699 MB + ArgumentOutOfRangeException.ThrowIfGreaterThan(length, int.MaxValue / 3); return string.Create(length * 3 - 1, (value, startIndex, length), static (dst, state) => { diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffer.cs b/src/libraries/System.Private.CoreLib/src/System/Buffer.cs index 52bfa1d39d5db..af8ea7b4629e8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffer.cs @@ -42,12 +42,9 @@ public static unsafe void BlockCopy(Array src, int srcOffset, Array dst, int dst } } - if (srcOffset < 0) - throw new ArgumentOutOfRangeException(nameof(srcOffset), SR.ArgumentOutOfRange_MustBeNonNegInt32); - if (dstOffset < 0) - throw new ArgumentOutOfRangeException(nameof(dstOffset), SR.ArgumentOutOfRange_MustBeNonNegInt32); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_MustBeNonNegInt32); + ArgumentOutOfRangeException.ThrowIfNegative(srcOffset); + ArgumentOutOfRangeException.ThrowIfNegative(dstOffset); + ArgumentOutOfRangeException.ThrowIfNegative(count); nuint uCount = (nuint)count; nuint uSrcOffset = (nuint)srcOffset; diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs index 2188fc38dbb9f..13fa4a8d18835 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs @@ -21,14 +21,8 @@ internal ConfigurableArrayPool() : this(DefaultMaxArrayLength, DefaultMaxNumberO internal ConfigurableArrayPool(int maxArrayLength, int maxArraysPerBucket) { - if (maxArrayLength <= 0) - { - throw new ArgumentOutOfRangeException(nameof(maxArrayLength)); - } - if (maxArraysPerBucket <= 0) - { - throw new ArgumentOutOfRangeException(nameof(maxArraysPerBucket)); - } + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxArrayLength); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxArraysPerBucket); // Our bucketing algorithm has a min length of 2^4 and a max length of 2^30. // Constrain the actual max used to those values. @@ -61,11 +55,8 @@ public override T[] Rent(int minimumLength) // Arrays can't be smaller than zero. We allow requesting zero-length arrays (even though // pooling such an array isn't valuable) as it's a valid length array, and we want the pool // to be usable in general instead of using `new`, even for computed lengths. - if (minimumLength < 0) - { - throw new ArgumentOutOfRangeException(nameof(minimumLength)); - } - else if (minimumLength == 0) + ArgumentOutOfRangeException.ThrowIfNegative(minimumLength); + if (minimumLength == 0) { // No need for events with the empty array. Our pool is effectively infinite // and we'll never allocate for rents and never store for returns. diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs index bffa23fc67963..fb6fccfa6df15 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs @@ -105,9 +105,9 @@ public override T[] Rent(int minimumLength) // effectively infinite for empty arrays and we'll never allocate for rents and never store for returns. return Array.Empty(); } - else if (minimumLength < 0) + else { - throw new ArgumentOutOfRangeException(nameof(minimumLength)); + ArgumentOutOfRangeException.ThrowIfNegative(minimumLength); } buffer = GC.AllocateUninitializedArray(minimumLength); diff --git a/src/libraries/System.Private.CoreLib/src/System/Convert.cs b/src/libraries/System.Private.CoreLib/src/System/Convert.cs index e8e617cd6fa45..4ddd917956e19 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Convert.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Convert.cs @@ -2314,12 +2314,9 @@ public static string ToBase64String(byte[] inArray, int offset, int length, Base { ArgumentNullException.ThrowIfNull(inArray); - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_GenericPositive); - if (offset > (inArray.Length - length)) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_OffsetLength); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(offset); + ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); return ToBase64String(new ReadOnlySpan(inArray, offset, length), options); } @@ -2371,19 +2368,15 @@ public static unsafe int ToBase64CharArray(byte[] inArray, int offsetIn, int len ArgumentNullException.ThrowIfNull(inArray); ArgumentNullException.ThrowIfNull(outArray); - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); - if (offsetIn < 0) - throw new ArgumentOutOfRangeException(nameof(offsetIn), SR.ArgumentOutOfRange_GenericPositive); - if (offsetOut < 0) - throw new ArgumentOutOfRangeException(nameof(offsetOut), SR.ArgumentOutOfRange_GenericPositive); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(offsetIn); + ArgumentOutOfRangeException.ThrowIfNegative(offsetOut); if (options < Base64FormattingOptions.None || options > Base64FormattingOptions.InsertLineBreaks) throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)options), nameof(options)); int inArrayLength = inArray.Length; - if (offsetIn > (inArrayLength - length)) - throw new ArgumentOutOfRangeException(nameof(offsetIn), SR.ArgumentOutOfRange_OffsetLength); + ArgumentOutOfRangeException.ThrowIfGreaterThan(offsetIn, inArrayLength - length); if (inArrayLength == 0) return 0; @@ -2395,8 +2388,7 @@ public static unsafe int ToBase64CharArray(byte[] inArray, int offsetIn, int len bool insertLineBreaks = options == Base64FormattingOptions.InsertLineBreaks; int charLengthRequired = ToBase64_CalculateAndValidateOutputLength(length, insertLineBreaks); - if (offsetOut > outArrayLength - charLengthRequired) - throw new ArgumentOutOfRangeException(nameof(offsetOut), SR.ArgumentOutOfRange_OffsetOut); + ArgumentOutOfRangeException.ThrowIfGreaterThan(offsetOut, outArrayLength - charLengthRequired); if (Vector128.IsHardwareAccelerated && !insertLineBreaks && length >= Base64VectorizationLengthThreshold) { @@ -2783,15 +2775,9 @@ private static void CopyToTempBufferWithoutWhiteSpace(ReadOnlySpan chars, public static byte[] FromBase64CharArray(char[] inArray, int offset, int length) { ArgumentNullException.ThrowIfNull(inArray); - - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); - - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_GenericPositive); - - if (offset > inArray.Length - length) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_OffsetLength); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(offset); + ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); if (inArray.Length == 0) { @@ -2980,12 +2966,9 @@ public static string ToHexString(byte[] inArray, int offset, int length) { ArgumentNullException.ThrowIfNull(inArray); - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); - if (offset < 0) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_GenericPositive); - if (offset > (inArray.Length - length)) - throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_OffsetLength); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(offset); + ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, inArray.Length - length); return ToHexString(new ReadOnlySpan(inArray, offset, length)); } @@ -3000,8 +2983,7 @@ public static string ToHexString(ReadOnlySpan bytes) { if (bytes.Length == 0) return string.Empty; - if (bytes.Length > int.MaxValue / 2) - throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_InputTooLarge); + ArgumentOutOfRangeException.ThrowIfGreaterThan(bytes.Length, int.MaxValue / 2, nameof(bytes)); return HexConverter.ToString(bytes, HexConverter.Casing.Upper); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs index cc7354014f489..71895a2427352 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Decimal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Decimal.cs @@ -307,8 +307,7 @@ public Decimal(ReadOnlySpan bits) // public Decimal(int lo, int mid, int hi, bool isNegative, byte scale) { - if (scale > 28) - throw new ArgumentOutOfRangeException(nameof(scale), SR.ArgumentOutOfRange_DecimalScale); + ArgumentOutOfRangeException.ThrowIfGreaterThan(scale, 28); _lo64 = (uint)lo + ((ulong)(uint)mid << 32); _hi32 = (uint)hi; _flags = ((int)scale) << 16; diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.cs index 80f523f4ebc58..74acfef13fccf 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.cs @@ -47,14 +47,8 @@ internal EventPipeProviderConfiguration( uint loggingLevel, string? filterData) { - if (string.IsNullOrEmpty(providerName)) - { - throw new ArgumentNullException(nameof(providerName)); - } - if (loggingLevel > 5) // 5 == Verbose, the highest value in EventPipeLoggingLevel. - { - throw new ArgumentOutOfRangeException(nameof(loggingLevel)); - } + ArgumentException.ThrowIfNullOrEmpty(providerName); + ArgumentOutOfRangeException.ThrowIfGreaterThan(loggingLevel, 5u); // 5 == Verbose, the highest value in EventPipeLoggingLevel. m_providerName = providerName; m_keywords = keywords; m_loggingLevel = loggingLevel; diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs index d0738641a3e2e..82ccbde6cdad4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs @@ -885,14 +885,9 @@ public static Matrix4x4 CreateOrthographicOffCenter(float left, float right, flo /// is greater than or equal to . public static Matrix4x4 CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance) { - if (nearPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); - - if (farPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(farPlaneDistance)); - - if (nearPlaneDistance >= farPlaneDistance) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance); Matrix4x4 result; @@ -929,17 +924,12 @@ public static Matrix4x4 CreatePerspective(float width, float height, float nearP /// is greater than or equal to . public static Matrix4x4 CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance) { - if (fieldOfView <= 0.0f || fieldOfView >= MathF.PI) - throw new ArgumentOutOfRangeException(nameof(fieldOfView)); - - if (nearPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(fieldOfView, 0.0f); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(fieldOfView, MathF.PI); - if (farPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(farPlaneDistance)); - - if (nearPlaneDistance >= farPlaneDistance) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance); float yScale = 1.0f / MathF.Tan(fieldOfView * 0.5f); float xScale = yScale / aspectRatio; @@ -978,14 +968,9 @@ public static Matrix4x4 CreatePerspectiveFieldOfView(float fieldOfView, float as /// is greater than or equal to . public static Matrix4x4 CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance) { - if (nearPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); - - if (farPlaneDistance <= 0.0f) - throw new ArgumentOutOfRangeException(nameof(farPlaneDistance)); - - if (nearPlaneDistance >= farPlaneDistance) - throw new ArgumentOutOfRangeException(nameof(nearPlaneDistance)); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(nearPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(farPlaneDistance, 0.0f); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(nearPlaneDistance, farPlaneDistance); Matrix4x4 result; diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs index 4469a15bab522..b3758d335c900 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs @@ -380,10 +380,7 @@ public static int Compare(string? strA, int indexA, string? strB, int indexB, in return strA == null ? -1 : 1; } - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength); - } + ArgumentOutOfRangeException.ThrowIfNegative(length); if (indexA < 0 || indexB < 0) { @@ -475,10 +472,7 @@ public static int CompareOrdinal(string? strA, int indexA, string? strB, int ind // COMPAT: Checking for nulls should become before the arguments are validated, // but other optimizations which allow us to return early should come after. - if (length < 0) - { - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeCount); - } + ArgumentOutOfRangeException.ThrowIfNegative(length); if (indexA < 0 || indexB < 0) { diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index 264acfb6e607d..8522fd72c411f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -569,19 +569,9 @@ public static string Join(string? separator, string?[] value, int startIndex, in private static string JoinCore(ReadOnlySpan separator, string?[] value, int startIndex, int count) { ArgumentNullException.ThrowIfNull(value); - - if (startIndex < 0) - { - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount); - } - if (startIndex > value.Length - count) - { - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_IndexCountBuffer); - } + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex, value.Length - count); return JoinCore(separator, new ReadOnlySpan(value, startIndex, count)); } @@ -836,8 +826,7 @@ private static string JoinCore(ReadOnlySpan separator, ReadOnlySpan oldLength - startIndex) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_IndexCount); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, oldLength - startIndex); if (count == 0) return this; @@ -1319,9 +1304,7 @@ public string[] Split(char[]? separator, int count, StringSplitOptions options) private string[] SplitInternal(ReadOnlySpan separators, int count, StringSplitOptions options) { - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), - SR.ArgumentOutOfRange_NegativeCount); + ArgumentOutOfRangeException.ThrowIfNegative(count); CheckStringSplitOptions(options); @@ -1382,11 +1365,7 @@ public string[] Split(string[]? separator, int count, StringSplitOptions options private string[] SplitInternal(string? separator, string?[]? separators, int count, StringSplitOptions options) { - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count), - SR.ArgumentOutOfRange_NegativeCount); - } + ArgumentOutOfRangeException.ThrowIfNegative(count); CheckStringSplitOptions(options); diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index 39d9153f1d172..971f1bbbef9fa 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -85,15 +85,9 @@ private static string Ctor(char[]? value) private static string Ctor(char[] value, int startIndex, int length) { ArgumentNullException.ThrowIfNull(value); - - if (startIndex < 0) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex); - - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength); - - if (startIndex > value.Length - length) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex, value.Length - length); if (length == 0) return Empty; @@ -139,11 +133,8 @@ private static unsafe string Ctor(char* ptr) private static unsafe string Ctor(char* ptr, int startIndex, int length) { - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength); - - if (startIndex < 0) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); char* pStart = ptr + startIndex; @@ -190,11 +181,8 @@ private static unsafe string Ctor(sbyte* value) private static unsafe string Ctor(sbyte* value, int startIndex, int length) { - if (startIndex < 0) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex); - - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength); + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); + ArgumentOutOfRangeException.ThrowIfNegative(length); if (value == null) { @@ -250,11 +238,8 @@ private static unsafe string Ctor(sbyte* value, int startIndex, int length, Enco if (enc == null) return new string(value, startIndex, length); - if (length < 0) - throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NeedNonNegNum); - - if (startIndex < 0) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex); + ArgumentOutOfRangeException.ThrowIfNegative(length); + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); if (value == null) { @@ -396,14 +381,11 @@ public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIn { ArgumentNullException.ThrowIfNull(destination); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount); - if (sourceIndex < 0) - throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); - if (count > Length - sourceIndex) - throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_IndexCount); - if (destinationIndex > destination.Length - count || destinationIndex < 0) - throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_IndexCount); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfNegative(sourceIndex); + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, Length - sourceIndex, nameof(sourceIndex)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(destinationIndex, destination.Length - count); + ArgumentOutOfRangeException.ThrowIfNegative(destinationIndex); Buffer.Memmove( destination: ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(destination), destinationIndex), @@ -463,8 +445,8 @@ public char[] ToCharArray() public char[] ToCharArray(int startIndex, int length) { // Range check everything. - if (startIndex < 0 || startIndex > Length || startIndex > Length - length) - throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); + ArgumentOutOfRangeException.ThrowIfGreaterThan((uint)startIndex, (uint)Length, nameof(startIndex)); + ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex, Length - length); if (length <= 0) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs index a3c4a722103ed..6cca9d2396711 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs @@ -129,10 +129,7 @@ protected Encoding() : this(0) protected Encoding(int codePage) { // Validate code page - if (codePage < 0) - { - throw new ArgumentOutOfRangeException(nameof(codePage)); - } + ArgumentOutOfRangeException.ThrowIfNegative(codePage); // Remember code page _codePage = codePage; @@ -147,10 +144,7 @@ protected Encoding(int codePage) protected Encoding(int codePage, EncoderFallback? encoderFallback, DecoderFallback? decoderFallback) { // Validate code page - if (codePage < 0) - { - throw new ArgumentOutOfRangeException(nameof(codePage)); - } + ArgumentOutOfRangeException.ThrowIfNegative(codePage); // Remember code page _codePage = codePage; @@ -556,16 +550,9 @@ public virtual int GetByteCount(string s) public int GetByteCount(string s, int index, int count) { ArgumentNullException.ThrowIfNull(s); - - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index), - SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), - SR.ArgumentOutOfRange_NeedNonNegNum); - if (index > s.Length - count) - throw new ArgumentOutOfRangeException(nameof(index), - SR.ArgumentOutOfRange_IndexCount); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(index, s.Length - count); unsafe { @@ -584,10 +571,7 @@ public int GetByteCount(string s, int index, int count) public virtual unsafe int GetByteCount(char* chars, int count) { ArgumentNullException.ThrowIfNull(chars); - - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), - SR.ArgumentOutOfRange_NeedNonNegNum); + ArgumentOutOfRangeException.ThrowIfNegative(count); char[] arrChar = new ReadOnlySpan(chars, count).ToArray(); @@ -654,16 +638,9 @@ public virtual byte[] GetBytes(string s) public byte[] GetBytes(string s, int index, int count) { ArgumentNullException.ThrowIfNull(s); - - if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index), - SR.ArgumentOutOfRange_NeedNonNegNum); - if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), - SR.ArgumentOutOfRange_NeedNonNegNum); - if (index > s.Length - count) - throw new ArgumentOutOfRangeException(nameof(index), - SR.ArgumentOutOfRange_IndexCount); + ArgumentOutOfRangeException.ThrowIfNegative(index); + ArgumentOutOfRangeException.ThrowIfNegative(count); + ArgumentOutOfRangeException.ThrowIfGreaterThan(index, s.Length - count); unsafe { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ManualResetEventSlim.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ManualResetEventSlim.cs index cdcfe33ee97ac..428f6ddc4b9b2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ManualResetEventSlim.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ManualResetEventSlim.cs @@ -173,17 +173,8 @@ public ManualResetEventSlim(bool initialState) /// 0 or greater than the maximum allowed value. public ManualResetEventSlim(bool initialState, int spinCount) { - if (spinCount < 0) - { - throw new ArgumentOutOfRangeException(nameof(spinCount)); - } - - if (spinCount > SpinCountState_MaxValue) - { - throw new ArgumentOutOfRangeException( - nameof(spinCount), - SR.Format(SR.ManualResetEventSlim_ctor_SpinCountOutOfRange, SpinCountState_MaxValue)); - } + ArgumentOutOfRangeException.ThrowIfNegative(spinCount); + ArgumentOutOfRangeException.ThrowIfGreaterThan(spinCount, SpinCountState_MaxValue); // We will suppress default spin because the user specified a count. Initialize(initialState, spinCount); @@ -482,10 +473,7 @@ public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken) ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); // an early convenience check - if (millisecondsTimeout < -1) - { - throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1); if (!IsSet) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/ReaderWriterLockSlim.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/ReaderWriterLockSlim.cs index 016142ded2499..9f7e7caf5c1c8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/ReaderWriterLockSlim.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/ReaderWriterLockSlim.cs @@ -249,8 +249,7 @@ public TimeoutTracker(TimeSpan timeout) public TimeoutTracker(int millisecondsTimeout) { - if (millisecondsTimeout < -1) - throw new ArgumentOutOfRangeException(nameof(millisecondsTimeout)); + ArgumentOutOfRangeException.ThrowIfLessThan(millisecondsTimeout, -1); _total = millisecondsTimeout; if (_total != -1 && _total != 0) _start = Environment.TickCount; diff --git a/src/libraries/System.Private.CoreLib/src/System/Version.cs b/src/libraries/System.Private.CoreLib/src/System/Version.cs index 1c7751e6f19c4..c672ef8d69d07 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Version.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Version.cs @@ -26,17 +26,10 @@ public sealed class Version : ICloneable, IComparable, IComparable, IE public Version(int major, int minor, int build, int revision) { - if (major < 0) - throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version); - - if (minor < 0) - throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version); - - if (build < 0) - throw new ArgumentOutOfRangeException(nameof(build), SR.ArgumentOutOfRange_Version); - - if (revision < 0) - throw new ArgumentOutOfRangeException(nameof(revision), SR.ArgumentOutOfRange_Version); + ArgumentOutOfRangeException.ThrowIfNegative(major); + ArgumentOutOfRangeException.ThrowIfNegative(minor); + ArgumentOutOfRangeException.ThrowIfNegative(build); + ArgumentOutOfRangeException.ThrowIfNegative(revision); _Major = major; _Minor = minor; @@ -46,14 +39,9 @@ public Version(int major, int minor, int build, int revision) public Version(int major, int minor, int build) { - if (major < 0) - throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version); - - if (minor < 0) - throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version); - - if (build < 0) - throw new ArgumentOutOfRangeException(nameof(build), SR.ArgumentOutOfRange_Version); + ArgumentOutOfRangeException.ThrowIfNegative(major); + ArgumentOutOfRangeException.ThrowIfNegative(minor); + ArgumentOutOfRangeException.ThrowIfNegative(build); _Major = major; _Minor = minor; @@ -63,11 +51,8 @@ public Version(int major, int minor, int build) public Version(int major, int minor) { - if (major < 0) - throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version); - - if (minor < 0) - throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version); + ArgumentOutOfRangeException.ThrowIfNegative(major); + ArgumentOutOfRangeException.ThrowIfNegative(minor); _Major = major; _Minor = minor; diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index e9e5ed6eb9489..1eeac52d4ecdb 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -301,6 +301,13 @@ public ArgumentOutOfRangeException(string? paramName, string? message) { } 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 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 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 96d625f7ba9a3..a9c364fc514b7 100644 --- a/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs +++ b/src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Numerics; using Xunit; namespace System.Tests @@ -65,5 +66,70 @@ public static void Ctor_String_Object_String() Assert.Contains(argumentName, exception.Message); Assert.Contains(argumentValue.ToString(), exception.Message); } + + private const string HelpersParamName = "value"; + private static Action ZeroHelper(T value) where T : INumberBase => () => ArgumentOutOfRangeException.ThrowIfZero(value); + private static Action NegativeOrZeroHelper(T value) where T : INumberBase => () => ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); + private static Action GreaterThanHelper(T value, T other) where T : IComparable => () => ArgumentOutOfRangeException.ThrowIfGreaterThan(value, other); + 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); + + [Fact] + public static void GenericHelpers_ThrowIfZero_Throws() + { + AssertExtensions.Throws(HelpersParamName, ZeroHelper(0)); + AssertExtensions.Throws(HelpersParamName, ZeroHelper(0)); + + AssertExtensions.Throws(HelpersParamName, ZeroHelper(0.0f)); + AssertExtensions.Throws(HelpersParamName, ZeroHelper(-0.0f)); + AssertExtensions.Throws(HelpersParamName, ZeroHelper(0)); + AssertExtensions.Throws(HelpersParamName, ZeroHelper(+0.0)); + AssertExtensions.Throws(HelpersParamName, ZeroHelper(-0.0)); + } + + [Fact] + public static void GenericHelpers_ThrowIfNegativeZero_Throws() + { + AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-1)); + AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-0.0f)); + AssertExtensions.Throws(HelpersParamName, NegativeOrZeroHelper(-0.0)); + } + + [Fact] + public static void GenericHelpers_ThrowIfGreaterThan_Throws() + { + AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1, 0)); + AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1, 0)); + AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1.000000001, 1)); + AssertExtensions.Throws(HelpersParamName, GreaterThanHelper(1.00001f, 1)); + } + + [Fact] + public static void GenericHelpers_ThrowIfGreaterThanOrEqual_Throws() + { + AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, GreaterThanOrEqualHelper(1, 1)); + } + + [Fact] + public static void GenericHelpers_ThrowIfLessThan_Throws() + { + AssertExtensions.Throws(HelpersParamName, LessThanHelper(0, 1)); + AssertExtensions.Throws(HelpersParamName, LessThanHelper(0, 1)); + AssertExtensions.Throws(HelpersParamName, LessThanHelper(1, 1.000000001)); + AssertExtensions.Throws(HelpersParamName, LessThanHelper(1, 1.00001f)); + } + + [Fact] + public static void GenericHelpers_ThrowIfLessThanOrEqual_Throws() + { + AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)); + AssertExtensions.Throws(HelpersParamName, LessThanOrEqualHelper(1, 1)); + } } }