diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs index 8f911a4cd41..431c65f2aa6 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs @@ -3026,5 +3026,507 @@ public static void IsNotBetweenOrEqualTo(decimal value, decimal minimum, decimal ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); } + + /// + /// Asserts that the input value must be equal to a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEqualTo(nint value, nint target, string name) + { + if (value == target) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); + } + + /// + /// Asserts that the input value must be not equal to a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is == . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEqualTo(nint value, nint target, string name) + { + if (value != target) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); + } + + /// + /// Asserts that the input value must be less than a specified value. + /// + /// The input value to test. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsLessThan(nint value, nint maximum, string name) + { + if (value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name); + } + + /// + /// Asserts that the input value must be less than or equal to a specified value. + /// + /// The input value to test. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsLessThanOrEqualTo(nint value, nint maximum, string name) + { + if (value <= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); + } + + /// + /// Asserts that the input value must be greater than a specified value. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsGreaterThan(nint value, nint minimum, string name) + { + if (value > minimum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); + } + + /// + /// Asserts that the input value must be greater than or equal to a specified value. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsGreaterThanOrEqualTo(nint value, nint minimum, string name) + { + if (value >= minimum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); + } + + /// + /// Asserts that the input value must be in a given range. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < or >= . + /// + /// This API asserts the equivalent of " in [, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(nint value, nint minimum, nint maximum, string name) + { + if (value >= minimum && value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given range. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= or < . + /// + /// This API asserts the equivalent of " not in [, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(nint value, nint minimum, nint maximum, string name) + { + if (value < minimum || value >= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is <= or >= . + /// + /// This API asserts the equivalent of " in (, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(nint value, nint minimum, nint maximum, string name) + { + if (value > minimum && value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is > or < . + /// + /// This API asserts the equivalent of " not in (, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(nint value, nint minimum, nint maximum, string name) + { + if (value <= minimum || value >= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < or > . + /// + /// This API asserts the equivalent of " in [, ]", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(nint value, nint minimum, nint maximum, string name) + { + if (value >= minimum && value <= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= or <= . + /// + /// This API asserts the equivalent of " not in [, ]", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(nint value, nint minimum, nint maximum, string name) + { + if (value < minimum || value > maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must be equal to a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEqualTo(nuint value, nuint target, string name) + { + if (value == target) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); + } + + /// + /// Asserts that the input value must be not equal to a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is == . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEqualTo(nuint value, nuint target, string name) + { + if (value != target) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); + } + + /// + /// Asserts that the input value must be less than a specified value. + /// + /// The input value to test. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsLessThan(nuint value, nuint maximum, string name) + { + if (value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name); + } + + /// + /// Asserts that the input value must be less than or equal to a specified value. + /// + /// The input value to test. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsLessThanOrEqualTo(nuint value, nuint maximum, string name) + { + if (value <= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); + } + + /// + /// Asserts that the input value must be greater than a specified value. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsGreaterThan(nuint value, nuint minimum, string name) + { + if (value > minimum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); + } + + /// + /// Asserts that the input value must be greater than or equal to a specified value. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsGreaterThanOrEqualTo(nuint value, nuint minimum, string name) + { + if (value >= minimum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); + } + + /// + /// Asserts that the input value must be in a given range. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < or >= . + /// + /// This API asserts the equivalent of " in [, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(nuint value, nuint minimum, nuint maximum, string name) + { + if (value >= minimum && value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given range. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= or < . + /// + /// This API asserts the equivalent of " not in [, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(nuint value, nuint minimum, nuint maximum, string name) + { + if (value < minimum || value >= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is <= or >= . + /// + /// This API asserts the equivalent of " in (, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(nuint value, nuint minimum, nuint maximum, string name) + { + if (value > minimum && value < maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The input value to test. + /// The exclusive minimum value that is accepted. + /// The exclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is > or < . + /// + /// This API asserts the equivalent of " not in (, )", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(nuint value, nuint minimum, nuint maximum, string name) + { + if (value <= minimum || value >= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is < or > . + /// + /// This API asserts the equivalent of " in [, ]", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(nuint value, nuint minimum, nuint maximum, string name) + { + if (value >= minimum && value <= maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The input value to test. + /// The inclusive minimum value that is accepted. + /// The inclusive maximum value that is accepted. + /// The name of the input parameter being tested. + /// Thrown if is >= or <= . + /// + /// This API asserts the equivalent of " not in [, ]", using arithmetic notation. + /// The method is generic to avoid boxing the parameters, if they are value types. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(nuint value, nuint minimum, nuint maximum, string name) + { + if (value < minimum || value > maximum) + { + return; + } + + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); + } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 4977a75660e..7e0bd9db3b7 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -13,14 +13,15 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# -GenerateTextForItems(NumericTypes, type => +GenerateTextForItems(NumericTypes, typeInfo => { + var (type, prefix) = typeInfo; #> /// /// Asserts that the input value must be equal to a specified value. /// - /// The input value to test. - /// The target value to test for. + /// The input ="<#=type#>"/> value to test. + /// The target ="<#=type#>"/> value to test for. /// The name of the input parameter being tested. /// Thrown if is != . [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -37,8 +38,8 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be not equal to a specified value. /// - /// The input value to test. - /// The target value to test for. + /// The input ="<#=type#>"/> value to test. + /// The target ="<#=type#>"/> value to test for. /// The name of the input parameter being tested. /// Thrown if is == . /// The method is generic to avoid boxing the parameters, if they are value types. @@ -56,8 +57,8 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be less than a specified value. /// - /// The input value to test. - /// The exclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= . /// The method is generic to avoid boxing the parameters, if they are value types. @@ -75,8 +76,8 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be less than or equal to a specified value. /// - /// The input value to test. - /// The inclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > . /// The method is generic to avoid boxing the parameters, if they are value types. @@ -94,8 +95,8 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be greater than a specified value. /// - /// The input value to test. - /// The exclusive minimum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The exclusive minimum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= . /// The method is generic to avoid boxing the parameters, if they are value types. @@ -113,8 +114,8 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be greater than or equal to a specified value. /// - /// The input value to test. - /// The inclusive minimum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < . /// The method is generic to avoid boxing the parameters, if they are value types. @@ -132,9 +133,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be in a given range. /// - /// The input value to test. - /// The inclusive minimum value that is accepted. - /// The exclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive minimum ="<#=type#>"/> value that is accepted. + /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . /// @@ -155,9 +156,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must not be in a given range. /// - /// The input value to test. - /// The inclusive minimum value that is accepted. - /// The exclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive minimum ="<#=type#>"/> value that is accepted. + /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . /// @@ -178,9 +179,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be in a given interval. /// - /// The input value to test. - /// The exclusive minimum value that is accepted. - /// The exclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The exclusive minimum ="<#=type#>"/> value that is accepted. + /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . /// @@ -201,9 +202,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must not be in a given interval. /// - /// The input value to test. - /// The exclusive minimum value that is accepted. - /// The exclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The exclusive minimum ="<#=type#>"/> value that is accepted. + /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . /// @@ -224,9 +225,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must be in a given interval. /// - /// The input value to test. - /// The inclusive minimum value that is accepted. - /// The inclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive minimum ="<#=type#>"/> value that is accepted. + /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . /// @@ -247,9 +248,9 @@ GenerateTextForItems(NumericTypes, type => /// /// Asserts that the input value must not be in a given interval. /// - /// The input value to test. - /// The inclusive minimum value that is accepted. - /// The inclusive maximum value that is accepted. + /// The input ="<#=type#>"/> value to test. + /// The inclusive minimum ="<#=type#>"/> value that is accepted. + /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . /// diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs index e75e6c5081d..1499c68fccc 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs @@ -8,894 +8,811 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Microsoft.Toolkit.Extensions; namespace Microsoft.Toolkit.Diagnostics { /// - /// Helper methods to efficiently throw exceptions. + /// Helper methods to verify conditions when running code. /// - public static partial class ThrowHelper + public static partial class Guard { /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(Span span, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size less than or equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Span span, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {span.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(Span).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than or equal to {size}, had a size of {span.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlySpan span, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {span.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size less than or equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, Memory destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Memory memory, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Memory memory, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {memory.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(Memory).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than or equal to {size}, had a size of {memory.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlyMemory memory, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {memory.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size equal to {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size not equal to {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size over {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size of at least {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {size}, had a size of {array.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {array.Length.ToAssertString()} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, T[] array, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {array.Length.ToAssertString()} to be an invalid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(List list, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size equal to {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size not equal to {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size over {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size of at least {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size less than {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List list, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size less than or equal to {size}, had a size of {list.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(List source, List destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List source, List destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {list.Count.ToAssertString()} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, List list, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {list.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(List).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size less than or equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection source, ICollection destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ICollection collection, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {collection.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than or equal to {size}, had a size of {collection.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than or equal to {destination.Count.ToAssertString()} (the destination), had a size of {source.Count.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index.ToAssertString()}"); - } - - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, IReadOnlyCollection collection, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {collection.Count.ToAssertString()} to be an invalid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index.ToAssertString()}"); + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must be empty, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size over {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(Span).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(span.Length)} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Span span, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(span.Length)} to be an invalid index for the target collection ({typeof(Span).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size over {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(span.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(span.Length)} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlySpan span, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(span.Length)} to be an invalid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must be empty, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size over {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, Memory destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(Memory).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Memory memory, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(memory.Length)} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Memory memory, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(memory.Length)} to be an invalid index for the target collection ({typeof(Memory).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size over {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(memory.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(memory.Length)} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlyMemory memory, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(memory.Length)} to be an invalid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size over {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(array.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(T[]).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(array.Length)} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, T[] array, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(array.Length)} to be an invalid index for the target collection ({typeof(T[]).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(List list, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must be empty, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size over {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List list, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(list.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, List destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List source, List destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(List).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(list.Count)} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, List list, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(list.Count)} to be an invalid index for the target collection ({typeof(List).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size over {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection source, ICollection destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(ICollection).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(collection.Count)} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ICollection collection, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(collection.Count)} to be an invalid index for the target collection ({typeof(ICollection).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size over {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(collection.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than or equal to {AssertString(destination.Count)} (the destination), had a size of {AssertString(source.Count)}", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(collection.Count)} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {AssertString(index)}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, IReadOnlyCollection collection, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(collection.Count)} to be an invalid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {AssertString(index)}"); + } } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 1a58e50ba03..83e74189031 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -5,131 +5,125 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Microsoft.Toolkit.Extensions; namespace Microsoft.Toolkit.Diagnostics { /// - /// Helper methods to efficiently throw exceptions. + /// Helper methods to verify conditions when running code. /// - public static partial class ThrowHelper + public static partial class Guard { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { <# GenerateTextForItems(EnumerableTypes, item => { #> - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(<#=item.Type#> <#=item.Name#>, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(<#=item.Type#> <#=item.Name#>, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {AssertString(destination.<#=item.Size#>)} (the destination), had a size of {AssertString(source.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {destination.<#=item.Size#>.ToAssertString()} (the destination), had a size of {source.<#=item.Size#>.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {AssertString(destination.<#=item.Size#>)} (the destination), had a size of {AssertString(source.<#=item.Size#>)}", name); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(<#=item.Name#>.<#=item.Size#>)} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {AssertString(index)}"); + } - /// - /// Throws an when (or an overload) fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}"); - } + /// + /// Throws an when (or an overload) fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(<#=item.Name#>.<#=item.Size#>)} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {AssertString(index)}"); + } <# }); #> + } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude index 1bcffa5513f..52350d1eb12 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude +++ b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude @@ -81,7 +81,23 @@ /// /// Gets the list of available numeric types to generate APIs for /// - static readonly IReadOnlyList NumericTypes = new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }; + static readonly IReadOnlyList<(string Name, string Prefix)> NumericTypes = new[] + { + ("byte", "cref"), + ("sbyte", "cref"), + ("short", "cref"), + ("ushort", "cref"), + ("char", "cref"), + ("int", "cref"), + ("uint", "cref"), + ("float", "cref"), + ("long", "cref"), + ("ulong", "cref"), + ("double", "cref"), + ("decimal", "cref"), + ("nint", "langword"), + ("nuint", "langword") + }; /// /// Generates text for a given sequence of items, automatically adding the necessary spacing diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs index 45f0c00daed..fb0e5aa18d3 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs @@ -23,18 +23,18 @@ public static partial class Guard [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsCloseTo(int value, int target, uint delta, string name) { - // Cast to long before calculating the difference to avoid overflows - // when the values are at the two extremes of the supported range. - // Then cast to double to calculate the absolute value: this allows - // the JIT compiler to use AVX instructions on X64 CPUs instead of - // conditional jumps, which results in more efficient assembly code. - // The IEEE 754 specs guarantees that a 32 bit integer value can - // be stored within a double precision floating point value with - // no loss of precision, so the result will always be correct here. - // The difference is then cast to uint as that's the maximum possible - // value it can have, and comparing two 32 bit integer values - // results in shorter and slightly faster code than using doubles. - if ((uint)Math.Abs((double)((long)value - target)) <= delta) + uint difference; + + if (value >= target) + { + difference = (uint)(value - target); + } + else + { + difference = (uint)(target - value); + } + + if (difference <= delta) { return; } @@ -53,7 +53,18 @@ public static void IsCloseTo(int value, int target, uint delta, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotCloseTo(int value, int target, uint delta, string name) { - if ((uint)Math.Abs((double)((long)value - target)) > delta) + uint difference; + + if (value >= target) + { + difference = (uint)(value - target); + } + else + { + difference = (uint)(target - value); + } + + if (difference > delta) { return; } @@ -69,12 +80,21 @@ public static void IsNotCloseTo(int value, int target, uint delta, string name) /// The maximum distance to allow between and . /// The name of the input parameter being tested. /// Thrown if ( - ) > . - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsCloseTo(long value, long target, ulong delta, string name) { - // This method and the one below are not inlined because - // using the decimal type results in quite a bit of code. - if ((ulong)Math.Abs((decimal)value - target) <= delta) + ulong difference; + + if (value >= target) + { + difference = (ulong)(value - target); + } + else + { + difference = (ulong)(target - value); + } + + if (difference <= delta) { return; } @@ -90,10 +110,21 @@ public static void IsCloseTo(long value, long target, ulong delta, string name) /// The maximum distance to allow between and . /// The name of the input parameter being tested. /// Thrown if ( - ) <= . - [MethodImpl(MethodImplOptions.NoInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotCloseTo(long value, long target, ulong delta, string name) { - if ((ulong)Math.Abs((decimal)value - target) > delta) + ulong difference; + + if (value >= target) + { + difference = (ulong)(value - target); + } + else + { + difference = (ulong)(target - value); + } + + if (difference > delta) { return; } @@ -176,5 +207,65 @@ public static void IsNotCloseTo(double value, double target, double delta, strin ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); } + + /// + /// Asserts that the input value must be within a given distance from a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The maximum distance to allow between and . + /// The name of the input parameter being tested. + /// Thrown if ( - ) > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsCloseTo(nint value, nint target, nuint delta, string name) + { + nuint difference; + + if (value >= target) + { + difference = (nuint)(value - target); + } + else + { + difference = (nuint)(target - value); + } + + if (difference <= delta) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name); + } + + /// + /// Asserts that the input value must not be within a given distance from a specified value. + /// + /// The input value to test. + /// The target value to test for. + /// The maximum distance to allow between and . + /// The name of the input parameter being tested. + /// Thrown if ( - ) <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotCloseTo(nint value, nint target, nuint delta, string name) + { + nuint difference; + + if (value >= target) + { + difference = (nuint)(value - target); + } + else + { + difference = (nuint)(target - value); + } + + if (difference > delta) + { + return; + } + + ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); + } } } diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.Collection.Generic.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Collection.Generic.ThrowHelper.cs new file mode 100644 index 00000000000..0353f63d25d --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Collection.Generic.ThrowHelper.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + /// The item of items in the input instance. + /// This method is needed because can't be used as a generic type parameter. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotEmptyWithSpan(string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(Span).ToTypeString()}) must not be empty", name); + } + + /// + /// Throws an when fails. + /// + /// The item of items in the input instance. + /// This method is needed because can't be used as a generic type parameter. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(ReadOnlySpan).ToTypeString()}) must not be empty", name); + } + + /// + /// Throws an when (or an overload) fails. + /// + /// The item of items in the input collection. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotEmpty(string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be empty", name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Generic.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Generic.ThrowHelper.cs new file mode 100644 index 00000000000..dd7cc5c2abc --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Generic.ThrowHelper.cs @@ -0,0 +1,175 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + /// The type of value type being tested. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsDefault(T value, string name) + where T : struct + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be the default value {AssertString(default(T))}, was {AssertString(value)}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of value type being tested. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotDefault(string name) + where T : struct + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be the default value {AssertString(default(T))}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be equal to {AssertString(target)}, was {AssertString(value)}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be equal to {AssertString(target)}, was {AssertString(value)}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of input values being compared. + [DoesNotReturn] + public static void ThrowArgumentExceptionForBitwiseEqualTo(T value, T target, string name) + where T : unmanaged + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) is not a bitwise match, was <{value.ToHexString()}> instead of <{target.ToHexString()}>", name); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be less than {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be less than or equal to {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be greater than {AssertString(minimum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be greater than or equal to {AssertString(minimum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be in the range given by {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be in the range given by {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be between {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be between {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be between or equal to {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + + /// + /// Throws an when fails. + /// + /// The type of values being tested. + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + { + throw new ArgumentOutOfRangeException(name, value!, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be between or equal to {AssertString(minimum)} and {AssertString(maximum)}, was {AssertString(value)}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Numeric.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Numeric.ThrowHelper.cs new file mode 100644 index 00000000000..98ea1cf68f4 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Comparable.Numeric.ThrowHelper.cs @@ -0,0 +1,112 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCloseTo(int value, int target, uint delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(int).ToTypeString()}) must be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs((double)((long)value - target)))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, uint delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(int).ToTypeString()}) must not be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs((double)((long)value - target)))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCloseTo(long value, long target, ulong delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(long).ToTypeString()}) must be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs((decimal)value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCloseTo(long value, long target, ulong delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(long).ToTypeString()}) must not be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs((decimal)value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCloseTo(float value, float target, float delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(float).ToTypeString()}) must be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCloseTo(float value, float target, float delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(float).ToTypeString()}) must not be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCloseTo(double value, double target, double delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(double).ToTypeString()}) must be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCloseTo(double value, double target, double delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(double).ToTypeString()}) must not be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCloseTo(nint value, nint target, nuint delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(nint).ToTypeString()}) must be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCloseTo(nint value, nint target, nuint delta, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(nint).ToTypeString()}) must not be within a distance of {AssertString(delta)} from {AssertString(target)}, was {AssertString(value)} and had a distance of {AssertString(Math.Abs(value - target))}", name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.IO.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.IO.ThrowHelper.cs new file mode 100644 index 00000000000..a51341c8305 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.IO.ThrowHelper.cs @@ -0,0 +1,59 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForCanRead(Stream stream, string name) + { + throw new ArgumentException($"Stream {AssertString(name)} ({stream.GetType().ToTypeString()}) doesn't support reading", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForCanWrite(Stream stream, string name) + { + throw new ArgumentException($"Stream {AssertString(name)} ({stream.GetType().ToTypeString()}) doesn't support writing", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForCanSeek(Stream stream, string name) + { + throw new ArgumentException($"Stream {AssertString(name)} ({stream.GetType().ToTypeString()}) doesn't support seeking", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name) + { + throw new ArgumentException($"Stream {AssertString(name)} ({stream.GetType().ToTypeString()}) must be at position {AssertString(0)}, was at {AssertString(stream.Position)}", name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.String.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.String.ThrowHelper.cs new file mode 100644 index 00000000000..e4ef188fca0 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.String.ThrowHelper.cs @@ -0,0 +1,183 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must be null or empty, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or empty, was {(text is null ? "null" : "empty")}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNullOrWhiteSpace(string? text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must be null or whitespace, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotNullOrWhiteSpace(string? text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsEmpty(string text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must be empty, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be empty", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsWhiteSpace(string text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must be whitespace, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotWhiteSpace(string text, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be whitespace, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must have a size equal to {size}, had a size of {text.Length} and was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must not have a size equal to {size}, was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThan(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must have a size over {size}, had a size of {text.Length} and was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must have a size of at least {size}, had a size of {text.Length} and was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must have a size less than {size}, had a size of {text.Length} and was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} (string) must have a size less than or equal to {size}, had a size of {text.Length} and was {AssertString(text)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeEqualTo(string source, string destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} (string) must have a size equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string source, string destination, string name) + { + throw new ArgumentException($"The source {AssertString(name)} (string) must have a size less than or equal to {AssertString(destination.Length)} (the destination), had a size of {AssertString(source.Length)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, string text, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(text.Length)} to be a valid index for the target string, was {AssertString(index)}"); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, string text, string name) + { + throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(text.Length)} to be an invalid index for the target string, was {AssertString(index)}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.Tasks.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Tasks.ThrowHelper.cs new file mode 100644 index 00000000000..a6b9de57009 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.Tasks.ThrowHelper.cs @@ -0,0 +1,113 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must be completed, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must not be completed, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must be completed successfully, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must be faulted, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must not be faulted, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must be canceled, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must not be canceled, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must have status {status}, had status {AssertString(task.Status)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} ({task.GetType().ToTypeString()}) must not have status {AssertString(status)}", name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/Guard.ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/Internals/Guard.ThrowHelper.cs new file mode 100644 index 00000000000..11cbcda3e82 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Internals/Guard.ThrowHelper.cs @@ -0,0 +1,205 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; +using Microsoft.Toolkit.Extensions; + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Helper methods to efficiently throw exceptions. + /// + private static partial class ThrowHelper + { + /// + /// Returns a formatted representation of the input value. + /// + /// The input to format. + /// A formatted representation of to display in error messages. + [Pure] + private static string AssertString(object? obj) + { + return obj switch + { + string _ => $"\"{obj}\"", + null => "null", + _ => $"<{obj}>" + }; + } + + /// + /// Throws an when (where is ) fails. + /// + /// The type of the input value. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNull(T value, string name) + where T : class + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be null, was {AssertString(value)} ({value.GetType().ToTypeString()})", name); + } + + /// + /// Throws an when (where is ) fails. + /// + /// The type of the input value. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNull(T? value, string name) + where T : struct + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T?).ToTypeString()}) must be null, was {AssertString(value)} ({typeof(T).ToTypeString()})", name); + } + + /// + /// Throws an when fails. + /// + /// The type of the input value. + [DoesNotReturn] + public static void ThrowArgumentNullExceptionForIsNotNull(string name) + { + throw new ArgumentNullException(name, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be not null)"); + } + + /// + /// Throws an when fails. + /// + /// The type of the input value. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsOfType(object value, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of the input value. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotOfType(object value, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must not be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotOfType(object value, Type type, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must not be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + /// The type being checked against. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsAssignableToType(object value, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + /// The type being checked against. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotAssignableToType(object value, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must not be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsNotAssignableToType(object value, Type type, string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must not be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}", name); + } + + /// + /// Throws an when fails. + /// + /// The type of input value being compared. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsReferenceEqualTo(string name) + where T : class + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be the same instance as the target object", name); + } + + /// + /// Throws an when fails. + /// + /// The type of input value being compared. + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name) + where T : class + { + throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be the same instance as the target object", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsTrue(string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be true, was false", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsTrue(string name, string message) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be true, was false: {AssertString(message)}", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsFalse(string name) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be false, was true", name); + } + + /// + /// Throws an when fails. + /// + [DoesNotReturn] + public static void ThrowArgumentExceptionForIsFalse(string name, string message) + { + throw new ArgumentException($"Parameter {AssertString(name)} must be false, was true: {AssertString(message)}", name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Collection.Generic.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Collection.Generic.cs deleted file mode 100644 index a3bf9ed0cc5..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Collection.Generic.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - /// The item of items in the input instance. - /// This method is needed because can't be used as a generic type parameter. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotEmptyWithSpan(string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when fails. - /// - /// The item of items in the input instance. - /// This method is needed because can't be used as a generic type parameter. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when (or an overload) fails. - /// - /// The item of items in the input collection. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotEmpty(string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Generic.cs deleted file mode 100644 index 40d0cdad741..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Generic.cs +++ /dev/null @@ -1,185 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - /// The type of value type being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsDefault(T value, string name) - where T : struct - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be the default value {default(T).ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of value type being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotDefault(string name) - where T : struct - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be the default value {default(T).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be equal to {target.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be equal to {target.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of input values being compared. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForBitwiseEqualTo(T value, T target, string name) - where T : unmanaged - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) is not a bitwise match, was <{value.ToHexString()}> instead of <{target.ToHexString()}>"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than {minimum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than or equal to {minimum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between or equal to {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of values being tested. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, value!, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be between or equal to {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Numeric.cs deleted file mode 100644 index 5afc5a1354d..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Comparable.Numeric.cs +++ /dev/null @@ -1,97 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCloseTo(int value, int target, uint delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(int).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((double)((long)value - target)).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, uint delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(int).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((double)((long)value - target)).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCloseTo(long value, long target, ulong delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(long).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((decimal)value - target).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCloseTo(long value, long target, ulong delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(long).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs((decimal)value - target).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCloseTo(float value, float target, float delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(float).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCloseTo(float value, float target, float delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(float).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCloseTo(double value, double target, double delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(double).ToTypeString()}) must be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCloseTo(double value, double target, double delta, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(double).ToTypeString()}) must not be within a distance of {delta.ToAssertString()} from {target.ToAssertString()}, was {value.ToAssertString()} and had a distance of {Math.Abs(value - target).ToAssertString()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.IO.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.IO.cs deleted file mode 100644 index c6ddc4b24c1..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.IO.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForCanRead(Stream stream, string name) - { - ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support reading"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForCanWrite(Stream stream, string name) - { - ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support writing"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForCanSeek(Stream stream, string name) - { - ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support seeking"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name) - { - ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) must be at position {0.ToAssertString()}, was at {stream.Position.ToAssertString()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.String.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.String.cs deleted file mode 100644 index 84433f7c5c1..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.String.cs +++ /dev/null @@ -1,196 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or empty, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or empty, was {(text is null ? "null" : "empty")}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNullOrWhiteSpace(string? text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or whitespace, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotNullOrWhiteSpace(string? text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsEmpty(string text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be empty, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsWhiteSpace(string text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be whitespace, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotWhiteSpace(string text, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be whitespace, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not have a size equal to {size}, was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThan(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size over {size}, had a size of {text.Length} and was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size of at least {size}, had a size of {text.Length} and was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than {size}, had a size of {text.Length} and was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than or equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeEqualTo(string source, string destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} (string) must have a size equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string source, string destination, string name) - { - ThrowArgumentException(name, $"The source {name.ToAssertString()} (string) must have a size less than or equal to {destination.Length.ToAssertString()} (the destination), had a size of {source.Length.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, string text, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must be in the range given by <0> and {text.Length.ToAssertString()} to be a valid index for the target string, was {index.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, string text, string name) - { - ThrowArgumentOutOfRangeException(name, index, $"Parameter {name.ToAssertString()} (int) must not be in the range given by <0> and {text.Length.ToAssertString()} to be an invalid index for the target string, was {index.ToAssertString()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Tasks.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Tasks.cs deleted file mode 100644 index 174d88c6a60..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.Tasks.cs +++ /dev/null @@ -1,118 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCompleted(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsFaulted(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsCanceled(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not have status {status.ToAssertString()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.cs b/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.cs deleted file mode 100644 index 4228d086a64..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Internals/ThrowHelper.Guard.cs +++ /dev/null @@ -1,217 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics.CodeAnalysis; -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to efficiently throw exceptions. - /// - public static partial class ThrowHelper - { - /// - /// Returns a formatted representation of the input value. - /// - /// The input to format. - /// A formatted representation of to display in error messages. - [Pure] - private static string ToAssertString(this object? obj) - { - return obj switch - { - string _ => $"\"{obj}\"", - null => "null", - _ => $"<{obj}>" - }; - } - - /// - /// Throws an when (where is ) fails. - /// - /// The type of the input value. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNull(T value, string name) - where T : class - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be null, was {value.ToAssertString()} ({value.GetType().ToTypeString()})"); - } - - /// - /// Throws an when (where is ) fails. - /// - /// The type of the input value. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNull(T? value, string name) - where T : struct - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T?).ToTypeString()}) must be null, was {value.ToAssertString()} ({typeof(T).ToTypeString()})"); - } - - /// - /// Throws an when fails. - /// - /// The type of the input value. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentNullExceptionForIsNotNull(string name) - { - ThrowArgumentNullException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be not null)"); - } - - /// - /// Throws an when fails. - /// - /// The type of the input value. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsOfType(object value, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of the input value. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotOfType(object value, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotOfType(object value, Type type, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type being checked against. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsAssignableToType(object value, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type being checked against. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotAssignableToType(object value, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsNotAssignableToType(object value, Type type, string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must not be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); - } - - /// - /// Throws an when fails. - /// - /// The type of input value being compared. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsReferenceEqualTo(string name) - where T : class - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be the same instance as the target object"); - } - - /// - /// Throws an when fails. - /// - /// The type of input value being compared. - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name) - where T : class - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be the same instance as the target object"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsTrue(string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsTrue(string name, string message) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false: {message.ToAssertString()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsFalse(string name) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - [DoesNotReturn] - internal static void ThrowArgumentExceptionForIsFalse(string name, string message) - { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true: {message.ToAssertString()}"); - } - } -} diff --git a/UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.Comparable.Numeric.cs b/UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.Comparable.Numeric.cs index a2df22682cd..831990a409c 100644 --- a/UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.Comparable.Numeric.cs +++ b/UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.Comparable.Numeric.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Diagnostics.CodeAnalysis; using Microsoft.Toolkit.Diagnostics; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -13,88 +12,102 @@ public partial class Test_Guard { [TestCategory("Guard")] [TestMethod] - public void Test_Guard_IsCloseToInt_Ok() + [DataRow(0, 20, 10u, false)] + [DataRow(0, 6, 5u, false)] + [DataRow(0, int.MaxValue, 500u, false)] + [DataRow(-500, -530, 10u, false)] + [DataRow(1000, 800, 100u, false)] + [DataRow(int.MaxValue, int.MaxValue - 10, 7u, false)] + [DataRow(int.MinValue, int.MaxValue, (uint)int.MaxValue, false)] + [DataRow(0, 5, 10u, true)] + [DataRow(0, 5, 5u, true)] + [DataRow(0, int.MaxValue, (uint)int.MaxValue, true)] + [DataRow(-500, -530, 50u, true)] + [DataRow(1000, 800, 200u, true)] + [DataRow(int.MaxValue, int.MaxValue - 10, 10u, true)] + public void Test_Guard_IsCloseOrNotToInt(int value, int target, uint delta, bool isClose) { - Guard.IsCloseTo(0, 5, 10, nameof(Test_Guard_IsCloseToInt_Ok)); - Guard.IsCloseTo(0, 5, 5, nameof(Test_Guard_IsCloseToInt_Ok)); - Guard.IsCloseTo(0, int.MaxValue, int.MaxValue, nameof(Test_Guard_IsCloseToInt_Ok)); - Guard.IsCloseTo(-500, -530, 50, nameof(Test_Guard_IsCloseToInt_Ok)); - Guard.IsCloseTo(1000, 800, 200, nameof(Test_Guard_IsCloseToInt_Ok)); - Guard.IsCloseTo(int.MaxValue, int.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToInt_Ok)); - } - - [TestCategory("Guard")] - [TestMethod] - [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")] - public void Test_Guard_IsCloseToInt_Fail() - { - foreach (var item in new (int Value, int Target, uint Delta)[] - { - (0, 20, 10), - (0, 6, 5), - (0, int.MaxValue, 500), - (-500, -530, 10), - (1000, 800, 100), - (int.MaxValue, int.MaxValue - 10, 7), - (int.MinValue, int.MaxValue, int.MaxValue) - }) + void Test(int value, int target) { - bool fail = false; + bool isFailed = false; + + try + { + Guard.IsCloseTo(value, target, delta, nameof(Test_Guard_IsCloseOrNotToInt)); + } + catch (ArgumentException) + { + isFailed = true; + } + + Assert.AreEqual(isClose, !isFailed); + + isFailed = false; try { - Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToInt_Fail)); + Guard.IsNotCloseTo(value, target, delta, nameof(Test_Guard_IsCloseOrNotToInt)); } catch (ArgumentException) { - fail = true; + isFailed = true; } - Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}"); + Assert.AreEqual(isClose, isFailed); } - } - [TestCategory("Guard")] - [TestMethod] - public void Test_Guard_IsCloseToFloat_Ok() - { - Guard.IsCloseTo(0f, 5, 10, nameof(Test_Guard_IsCloseToFloat_Ok)); - Guard.IsCloseTo(0f, 5, 5, nameof(Test_Guard_IsCloseToFloat_Ok)); - Guard.IsCloseTo(0f, float.MaxValue, float.MaxValue, nameof(Test_Guard_IsCloseToFloat_Ok)); - Guard.IsCloseTo(-500f, -530, 50, nameof(Test_Guard_IsCloseToFloat_Ok)); - Guard.IsCloseTo(1000f, 800, 200, nameof(Test_Guard_IsCloseToFloat_Ok)); - Guard.IsCloseTo(float.MaxValue, float.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToFloat_Ok)); + Test(value, target); + Test(target, value); } [TestCategory("Guard")] [TestMethod] - [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")] - public void Test_Guard_IsCloseToFloat_Fail() + [DataRow(0f, 20f, 10f, false)] + [DataRow(0f, 6f, 5f, false)] + [DataRow(0f, float.MaxValue, 500f, false)] + [DataRow(-500f, -530f, 10f, false)] + [DataRow(1000f, 800f, 100f, false)] + [DataRow(float.MaxValue, float.MaxValue / 2, 7f, false)] + [DataRow(float.MinValue, float.MaxValue, float.MaxValue, false)] + [DataRow(0f, 5f, 10f, true)] + [DataRow(0f, 5f, 5f, true)] + [DataRow(0f, float.MaxValue, float.MaxValue, true)] + [DataRow(-500f, -530f, 50f, true)] + [DataRow(1000f, 800f, 200f, true)] + [DataRow(float.MaxValue, float.MaxValue - 10, 10f, true)] + public void Test_Guard_IsCloseToFloat(float value, float target, float delta, bool isClose) { - foreach (var item in new (float Value, float Target, float Delta)[] + void Test(float value, float target) { - (0, 20, 10), - (0, 6, 5), - (0, float.MaxValue, 500), - (-500, -530, 10), - (1000, 800, 100), - (float.MaxValue, float.MaxValue / 2, 7), - (float.MinValue, float.MaxValue, float.MaxValue) - }) - { - bool fail = false; + bool isFailed = false; try { - Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToFloat_Fail)); + Guard.IsCloseTo(value, target, delta, nameof(Test_Guard_IsCloseToFloat)); } catch (ArgumentException) { - fail = true; + isFailed = true; } - Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}"); + Assert.AreEqual(isClose, !isFailed); + + isFailed = false; + + try + { + Guard.IsNotCloseTo(value, target, delta, nameof(Test_Guard_IsCloseToFloat)); + } + catch (ArgumentException) + { + isFailed = true; + } + + Assert.AreEqual(isClose, isFailed); } + + Test(value, target); + Test(target, value); } } }