From 1c791578cde353db078b334a090053a28b9855c5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 11:30:00 +0100 Subject: [PATCH 001/123] Initial guard APIs added --- Microsoft.Toolkit/Diagnostics/Guard.cs | 143 +++++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 1 + 2 files changed, 144 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs new file mode 100644 index 00000000000..a73eaa93672 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -0,0 +1,143 @@ +// 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; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + [DebuggerStepThrough] + public static class Guard + { + /// + /// Asserts that the input value is not . + /// + /// The input value to test. + /// The name of the input parameter being tested. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeNotNull(object? value, string name) + { + if (value is null) + { + throw new ArgumentNullException(name, $"Parameter {name} must be not null"); + } + } + + /// + /// Asserts that the input value is of a specific type. + /// + /// The type of the input value. + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if is not of type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeOf(object value, string name) + where T : class + { + if (value.GetType() != typeof(T)) + { + throw new ArgumentException($"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}", name); + } + } + + /// + /// Asserts that the input value can be cast to a specified type. + /// + /// The type to check the input value against. + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if can't be cast to type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeAssignableTo(object value, string name) + { + if (!(value is T)) + { + throw new ArgumentException($"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}", name); + } + } + + /// + /// Asserts that the input value must be less than a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeLessThan(T value, T max, string name) + where T : notnull, IComparable + { + if (value.CompareTo(max) >= 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); + } + } + + /// + /// Asserts that the input value must be less than or equal to a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeLessThanOrEqualTo(T value, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(maximum) > 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); + } + } + + /// + /// Asserts that the input value must be greater than a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeGreaterThan(T value, T minimum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) <= 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); + } + } + + /// + /// Asserts that the input value must be greater than or equal to a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeGreaterThanOrEqualTo(T value, T minimum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 8d5936399cc..39f8ca000da 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 8.0 Windows Community Toolkit .NET Standard This package includes .NET Standard code only helpers such as: From 0554d2ff0056477136742ccc39c36069780bde77 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 11:41:06 +0100 Subject: [PATCH 002/123] Added EqualTo and NotEqualTo APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index a73eaa93672..786b52ae894 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -139,5 +139,43 @@ public static void MustBeGreaterThanOrEqualTo(T value, T minimum, string name throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); } } + + /// + /// Asserts that the input value must be equal to a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + if (!value.Equals(target)) + { + throw new ArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + } + } + + /// + /// Asserts that the input value must be not equal to a specified value. + /// + /// The type of input values to compare. + /// 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 MustBeNotEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + if (value.Equals(target)) + { + throw new ArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + } + } } } From 018e5c4aa214930b83adfbf980c84f5074354719 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 11:49:10 +0100 Subject: [PATCH 003/123] Added more Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 786b52ae894..0a8d1334835 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -31,6 +31,24 @@ public static void MustBeNotNull(object? value, string name) } } + /// + /// Asserts that the input value is not . + /// + /// The type of nullable value type being tested. + /// The input value to test. + /// 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 MustBeNotNull(T? value, string name) + where T : struct + { + if (value is null) + { + throw new ArgumentNullException(name, $"Parameter {name} must be not null"); + } + } + /// /// Asserts that the input value is of a specific type. /// @@ -64,6 +82,36 @@ public static void MustBeAssignableTo(object value, string name) } } + /// + /// Asserts that the input value must be . + /// + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeTrue(bool value, string name) + { + if (!value) + { + throw new ArgumentException($"Parameter {name} must be true, was false", name); + } + } + + /// + /// Asserts that the input value must be . + /// + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeFalse(bool value, string name) + { + if (value) + { + throw new ArgumentException($"Parameter {name} must be false, was true", name); + } + } + /// /// Asserts that the input value must be less than a specified value. /// From 8d78ba3c089e0a12d5a1640e7db9c5a3b957e319 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 11:56:08 +0100 Subject: [PATCH 004/123] New interval Guard APIs added --- Microsoft.Toolkit/Diagnostics/Guard.cs | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 0a8d1334835..c3af0188c98 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -188,6 +188,86 @@ public static void MustBeGreaterThanOrEqualTo(T value, T minimum, string name } } + /// + /// Asserts that the input value must be in a given interval. + /// + /// The type of input values to compare. + /// 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); + } + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The type of input values to compare. + /// 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeNotBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) > 0 || value.CompareTo(maximum) < 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} and >= {maximum}, was {value}"); + } + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The type of input values to compare. + /// 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); + } + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The type of input values to compare. + /// 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) <= 0) + { + throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} and > {maximum}, was {value}"); + } + } + /// /// Asserts that the input value must be equal to a specified value. /// From 0b76fb9d4f624099733dbca030a6976ce0b412d9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 12:23:59 +0100 Subject: [PATCH 005/123] Added Guard APIs for IEnumerable values --- Microsoft.Toolkit/Diagnostics/Guard.cs | 122 +++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index c3af0188c98..c406bed92de 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -3,7 +3,9 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Runtime.CompilerServices; #nullable enable @@ -305,5 +307,125 @@ public static void MustBeNotEqualTo(T value, T target, string name) throw new ArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); } } + + /// + /// Adderts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeSizedEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) != size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) != size) || + (actualSize = enumerable.Count()) != size) + { + throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {actualSize}"); + } + } + + /// + /// Adderts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeSizedAtLeast(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) <= size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) <= size) || + (actualSize = enumerable.Count()) <= size) + { + throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + } + } + + /// + /// Adderts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeSizedAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) < size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) < size) || + (actualSize = enumerable.Count()) < size) + { + throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); + } + } + + /// + /// Adderts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeSizedLessThan(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) >= size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) >= size) || + (actualSize = enumerable.Count()) >= size) + { + throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {actualSize}"); + } + } + + /// + /// Adderts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void MustBeSizedLessThanOrEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) > size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) > size) || + (actualSize = enumerable.Count()) > size) + { + throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); + } + } } } From 8028741f02cf5be839b48cbde37f6e1d964fcc54 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 12:49:04 +0100 Subject: [PATCH 006/123] Renamed APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 42 +++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index c406bed92de..8166159ede8 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -25,7 +25,7 @@ public static class Guard /// The name of the input parameter being tested. /// Thrown if is . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeNotNull(object? value, string name) + public static void IsNotNull(object? value, string name) { if (value is null) { @@ -42,7 +42,7 @@ public static void MustBeNotNull(object? value, string name) /// Thrown if is . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeNotNull(T? value, string name) + public static void IsNotNull(T? value, string name) where T : struct { if (value is null) @@ -59,7 +59,7 @@ public static void MustBeNotNull(T? value, string name) /// The name of the input parameter being tested. /// Thrown if is not of type . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeOf(object value, string name) + public static void IsOfType(object value, string name) where T : class { if (value.GetType() != typeof(T)) @@ -76,7 +76,7 @@ public static void MustBeOf(object value, string name) /// The name of the input parameter being tested. /// Thrown if can't be cast to type . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeAssignableTo(object value, string name) + public static void IsAssignableToType(object value, string name) { if (!(value is T)) { @@ -91,7 +91,7 @@ public static void MustBeAssignableTo(object value, string name) /// The name of the input parameter being tested. /// Thrown if is . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeTrue(bool value, string name) + public static void IsTrue(bool value, string name) { if (!value) { @@ -106,7 +106,7 @@ public static void MustBeTrue(bool value, string name) /// The name of the input parameter being tested. /// Thrown if is . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeFalse(bool value, string name) + public static void IsFalse(bool value, string name) { if (value) { @@ -124,7 +124,7 @@ public static void MustBeFalse(bool value, string name) /// Thrown if is >= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeLessThan(T value, T max, string name) + public static void IsLessThan(T value, T max, string name) where T : notnull, IComparable { if (value.CompareTo(max) >= 0) @@ -143,7 +143,7 @@ public static void MustBeLessThan(T value, T max, string name) /// Thrown if is > . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeLessThanOrEqualTo(T value, T maximum, string name) + public static void IsLessThanOrEqualTo(T value, T maximum, string name) where T : notnull, IComparable { if (value.CompareTo(maximum) > 0) @@ -162,7 +162,7 @@ public static void MustBeLessThanOrEqualTo(T value, T maximum, string name) /// Thrown if is <= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeGreaterThan(T value, T minimum, string name) + public static void IsGreaterThan(T value, T minimum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) <= 0) @@ -181,7 +181,7 @@ public static void MustBeGreaterThan(T value, T minimum, string name) /// Thrown if is < . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeGreaterThanOrEqualTo(T value, T minimum, string name) + public static void IsGreaterThanOrEqualTo(T value, T minimum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) < 0) @@ -201,7 +201,7 @@ public static void MustBeGreaterThanOrEqualTo(T value, T minimum, string name /// Thrown if is <= or >= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeBetween(T value, T minimum, T maximum, string name) + public static void IsBetween(T value, T minimum, T maximum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) @@ -221,7 +221,7 @@ public static void MustBeBetween(T value, T minimum, T maximum, string name) /// Thrown if is > or < . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeNotBetween(T value, T minimum, T maximum, string name) + public static void IsNotBetween(T value, T minimum, T maximum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) > 0 || value.CompareTo(maximum) < 0) @@ -241,7 +241,7 @@ public static void MustBeNotBetween(T value, T minimum, T maximum, string nam /// Thrown if is < or > . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeBetweenOrEqualTo(T value, T minimum, T maximum, string name) + public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) @@ -261,7 +261,7 @@ public static void MustBeBetweenOrEqualTo(T value, T minimum, T maximum, stri /// Thrown if is >= or <= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) where T : notnull, IComparable { if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) <= 0) @@ -280,7 +280,7 @@ public static void MustBeNotBetweenOrEqualTo(T value, T minimum, T maximum, s /// Thrown if is != . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeEqualTo(T value, T target, string name) + public static void IsEqualTo(T value, T target, string name) where T : notnull, IEquatable { if (!value.Equals(target)) @@ -299,7 +299,7 @@ public static void MustBeEqualTo(T value, T target, string name) /// Thrown if is == . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeNotEqualTo(T value, T target, string name) + public static void IsNotEqualTo(T value, T target, string name) where T : notnull, IEquatable { if (value.Equals(target)) @@ -318,7 +318,7 @@ public static void MustBeNotEqualTo(T value, T target, string name) /// Thrown if the size of is != . /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeSizedEqualTo(IEnumerable enumerable, int size, string name) + public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) { int actualSize; @@ -342,7 +342,7 @@ public static void MustBeSizedEqualTo(IEnumerable enumerable, int size, st /// Thrown if the size of is <= . /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeSizedAtLeast(IEnumerable enumerable, int size, string name) + public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) { int actualSize; @@ -366,7 +366,7 @@ public static void MustBeSizedAtLeast(IEnumerable enumerable, int size, st /// Thrown if the size of is < . /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeSizedAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) + public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) { int actualSize; @@ -390,7 +390,7 @@ public static void MustBeSizedAtLeastOrEqualTo(IEnumerable enumerable, int /// Thrown if the size of is >= . /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeSizedLessThan(IEnumerable enumerable, int size, string name) + public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) { int actualSize; @@ -414,7 +414,7 @@ public static void MustBeSizedLessThan(IEnumerable enumerable, int size, s /// Thrown if the size of is > . /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void MustBeSizedLessThanOrEqualTo(IEnumerable enumerable, int size, string name) + public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) { int actualSize; From cd232755adcc07b3964f61433dcae7de44d0b9e4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 12:55:55 +0100 Subject: [PATCH 007/123] Added Guard APIs for Stream values --- Microsoft.Toolkit/Diagnostics/Guard.cs | 71 ++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 8166159ede8..d11d3f06169 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -309,7 +310,7 @@ public static void IsNotEqualTo(T value, T target, string name) } /// - /// Adderts that the input instance must have a size of a specified value. + /// Asserts that the input instance must have a size of a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -333,7 +334,7 @@ public static void HasSizeEqualTo(IEnumerable enumerable, int size, string } /// - /// Adderts that the input instance must have a size of at least specified value. + /// Asserts that the input instance must have a size of at least specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -357,7 +358,7 @@ public static void HasSizeAtLeast(IEnumerable enumerable, int size, string } /// - /// Adderts that the input instance must have a size of at least or equal to a specified value. + /// Asserts that the input instance must have a size of at least or equal to a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -381,7 +382,7 @@ public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int siz } /// - /// Adderts that the input instance must have a size of less than a specified value. + /// Asserts that the input instance must have a size of less than a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -405,7 +406,7 @@ public static void HasSizeLessThan(IEnumerable enumerable, int size, strin } /// - /// Adderts that the input instance must have a size of less than or equal to a specified value. + /// Asserts that the input instance must have a size of less than or equal to a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -427,5 +428,65 @@ public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int si throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); } } + + /// + /// Asserts that the input instance must support reading. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support reading. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanRead(Stream stream, string name) + { + if (!stream.CanRead) + { + throw new ArgumentException($"Stream {name} doesn't support reading"); + } + } + + /// + /// Asserts that the input instance must support writing. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support writing. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanWrite(Stream stream, string name) + { + if (!stream.CanWrite) + { + throw new ArgumentException($"Stream {name} doesn't support writing"); + } + } + + /// + /// Asserts that the input instance must support seeking. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support seeking. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanSeek(Stream stream, string name) + { + if (!stream.CanSeek) + { + throw new ArgumentException($"Stream {name} doesn't support seeking"); + } + } + + /// + /// Asserts that the input instance must be at the starting position. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is not at the starting position. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsAtStartPosition(Stream stream, string name) + { + if (stream.Position != 0) + { + throw new ArgumentException($"Stream {name} must be at start position, was at {stream.Position}"); + } + } } } From a54441c5632beba70dc711d75a29ff537a4fdf3a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:01:32 +0100 Subject: [PATCH 008/123] Added Guard APIs for ReadOnlySpan values --- Microsoft.Toolkit/Diagnostics/Guard.cs | 85 ++++++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 4 + 2 files changed, 89 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index d11d3f06169..52b425b4850 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -429,6 +429,91 @@ public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int si } } + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length != size) + { + throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + if (span.Length <= size) + { + throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length < size) + { + throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) + { + if (span.Length >= size) + { + throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length > size) + { + throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + } + } + /// /// Asserts that the input instance must support reading. /// diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 39f8ca000da..d445cf5f4e4 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -15,4 +15,8 @@ Full + + + + \ No newline at end of file From 8dee59a5979ed8e91863d686830616e79f98dae5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:02:58 +0100 Subject: [PATCH 009/123] Added Guard APIs for ReadOnlyMemory values --- Microsoft.Toolkit/Diagnostics/Guard.cs | 85 ++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 52b425b4850..428e5f6a182 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -514,6 +514,91 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, s } } + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length != size) + { + throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length <= size) + { + throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length < size) + { + throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length >= size) + { + throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length > size) + { + throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + } + } + /// /// Asserts that the input instance must support reading. /// From 2d32333b21a018d6c0d8658a49e614b61465076e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:05:35 +0100 Subject: [PATCH 010/123] Code refactoring --- Microsoft.Toolkit/Diagnostics/Guard.IO.cs | 78 ++++++ Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 187 ++++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.cs | 233 +----------------- 3 files changed, 266 insertions(+), 232 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.IO.cs create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Memory.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs new file mode 100644 index 00000000000..928919d992c --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs @@ -0,0 +1,78 @@ +// 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.IO; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must support reading. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support reading. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanRead(Stream stream, string name) + { + if (!stream.CanRead) + { + throw new ArgumentException($"Stream {name} doesn't support reading"); + } + } + + /// + /// Asserts that the input instance must support writing. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support writing. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanWrite(Stream stream, string name) + { + if (!stream.CanWrite) + { + throw new ArgumentException($"Stream {name} doesn't support writing"); + } + } + + /// + /// Asserts that the input instance must support seeking. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if doesn't support seeking. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void CanSeek(Stream stream, string name) + { + if (!stream.CanSeek) + { + throw new ArgumentException($"Stream {name} doesn't support seeking"); + } + } + + /// + /// Asserts that the input instance must be at the starting position. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is not at the starting position. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsAtStartPosition(Stream stream, string name) + { + if (stream.Position != 0) + { + throw new ArgumentException($"Stream {name} must be at start position, was at {stream.Position}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs new file mode 100644 index 00000000000..6087b9b9a7b --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -0,0 +1,187 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length != size) + { + throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + if (span.Length <= size) + { + throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length < size) + { + throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) + { + if (span.Length >= size) + { + throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length > size) + { + throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length != size) + { + throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length <= size) + { + throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length < size) + { + throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length >= size) + { + throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length > size) + { + throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 428e5f6a182..689a48f88b2 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -17,7 +16,7 @@ namespace Microsoft.Toolkit.Diagnostics /// Helper methods to verify conditions when running code. /// [DebuggerStepThrough] - public static class Guard + public static partial class Guard { /// /// Asserts that the input value is not . @@ -428,235 +427,5 @@ public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int si throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); } } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length != size) - { - throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {span.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) - { - if (span.Length <= size) - { - throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {span.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length < size) - { - throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) - { - if (span.Length >= size) - { - throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {span.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length > size) - { - throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length != size) - { - throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length <= size) - { - throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length < size) - { - throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length >= size) - { - throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length > size) - { - throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Asserts that the input instance must support reading. - /// - /// The input instance to test. - /// The name of the input parameter being tested. - /// Thrown if doesn't support reading. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CanRead(Stream stream, string name) - { - if (!stream.CanRead) - { - throw new ArgumentException($"Stream {name} doesn't support reading"); - } - } - - /// - /// Asserts that the input instance must support writing. - /// - /// The input instance to test. - /// The name of the input parameter being tested. - /// Thrown if doesn't support writing. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CanWrite(Stream stream, string name) - { - if (!stream.CanWrite) - { - throw new ArgumentException($"Stream {name} doesn't support writing"); - } - } - - /// - /// Asserts that the input instance must support seeking. - /// - /// The input instance to test. - /// The name of the input parameter being tested. - /// Thrown if doesn't support seeking. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CanSeek(Stream stream, string name) - { - if (!stream.CanSeek) - { - throw new ArgumentException($"Stream {name} doesn't support seeking"); - } - } - - /// - /// Asserts that the input instance must be at the starting position. - /// - /// The input instance to test. - /// The name of the input parameter being tested. - /// Thrown if is not at the starting position. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsAtStartPosition(Stream stream, string name) - { - if (stream.Position != 0) - { - throw new ArgumentException($"Stream {name} must be at start position, was at {stream.Position}"); - } - } } } From 19e3bcc4a22b679b51b926b0ace9d3197d793be3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:06:32 +0100 Subject: [PATCH 011/123] Minor code refactoring --- Microsoft.Toolkit/Diagnostics/Guard.cs | 76 +++++++++++++------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 689a48f88b2..e367c205913 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -84,6 +84,44 @@ public static void IsAssignableToType(object value, string name) } } + /// + /// Asserts that the input value must be equal to a specified value. + /// + /// The type of input values to compare. + /// 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 IsEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + if (!value.Equals(target)) + { + throw new ArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + } + } + + /// + /// Asserts that the input value must be not equal to a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T target, string name) + where T : notnull, IEquatable + { + if (value.Equals(target)) + { + throw new ArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + } + } + /// /// Asserts that the input value must be . /// @@ -270,44 +308,6 @@ public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, strin } } - /// - /// Asserts that the input value must be equal to a specified value. - /// - /// The type of input values to compare. - /// 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 IsEqualTo(T value, T target, string name) - where T : notnull, IEquatable - { - if (!value.Equals(target)) - { - throw new ArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); - } - } - - /// - /// Asserts that the input value must be not equal to a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T target, string name) - where T : notnull, IEquatable - { - if (value.Equals(target)) - { - throw new ArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); - } - } - /// /// Asserts that the input instance must have a size of a specified value. /// From 0ff1f0070dda1b022538237ad02fd7482ee93036 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:12:48 +0100 Subject: [PATCH 012/123] Added Guard.IsBitwiseEqualTo API --- Microsoft.Toolkit/Diagnostics/Guard.cs | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index e367c205913..e7f2b35ec22 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -122,6 +122,34 @@ public static void IsNotEqualTo(T value, T target, string name) } } + /// + /// Asserts that the input value must be a bitwise match with a specified value. + /// + /// The type of input values to compare. + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is not a bitwise match for . + [MethodImpl(MethodImplOptions.NoInlining)] + public static void IsBitwiseEqualTo(T value, T target, string name) + where T : unmanaged + { + ref byte valueRef = ref Unsafe.As(ref value); + ref byte targetRef = ref Unsafe.As(ref target); + int bytesCount = Unsafe.SizeOf(); + + for (int i = 0; i < bytesCount; i++) + { + byte valueByte = Unsafe.Add(ref valueRef, i); + byte targetByte = Unsafe.Add(ref targetRef, i); + + if (valueByte != targetByte) + { + throw new ArgumentException($"Parameter {name} must is not a bitwise match (byte #{i} was {valueByte} instead of {targetByte})", name); + } + } + } + /// /// Asserts that the input value must be . /// From 9dd8a80dd1a1acf3dccf123c72103bb0869187bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:21:18 +0100 Subject: [PATCH 013/123] Added Guard.IsNull API --- Microsoft.Toolkit/Diagnostics/Guard.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index e7f2b35ec22..033bdfee7ca 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -18,6 +18,21 @@ namespace Microsoft.Toolkit.Diagnostics [DebuggerStepThrough] public static partial class Guard { + /// + /// Asserts that the input value is . + /// + /// The input value to test. + /// The name of the input parameter being tested. + /// Thrown if is not . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNull(object? value, string name) + { + if (value != null) + { + throw new ArgumentNullException(name, $"Parameter {name} must be null"); + } + } + /// /// Asserts that the input value is not . /// From f2a45007bf3891a76b7dc948508c330cbc30e6ec Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:33:01 +0100 Subject: [PATCH 014/123] Code refactoring and optimizations --- Microsoft.Toolkit/Diagnostics/Guard.IO.cs | 8 +-- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 20 +++---- .../Diagnostics/Guard.ThrowExceptions.cs | 56 +++++++++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.cs | 47 ++++++++-------- 4 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs index 928919d992c..49ba55ea006 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs @@ -26,7 +26,7 @@ public static void CanRead(Stream stream, string name) { if (!stream.CanRead) { - throw new ArgumentException($"Stream {name} doesn't support reading"); + ThrowArgumentException(name, $"Stream {name} doesn't support reading"); } } @@ -41,7 +41,7 @@ public static void CanWrite(Stream stream, string name) { if (!stream.CanWrite) { - throw new ArgumentException($"Stream {name} doesn't support writing"); + ThrowArgumentException(name, $"Stream {name} doesn't support writing"); } } @@ -56,7 +56,7 @@ public static void CanSeek(Stream stream, string name) { if (!stream.CanSeek) { - throw new ArgumentException($"Stream {name} doesn't support seeking"); + ThrowArgumentException(name, $"Stream {name} doesn't support seeking"); } } @@ -71,7 +71,7 @@ public static void IsAtStartPosition(Stream stream, string name) { if (stream.Position != 0) { - throw new ArgumentException($"Stream {name} must be at start position, was at {stream.Position}"); + ThrowArgumentException(name, $"Stream {name} must be at start position, was at {stream.Position}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 6087b9b9a7b..aca5fdd166f 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -27,7 +27,7 @@ public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name { if (span.Length != size) { - throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); } } @@ -44,7 +44,7 @@ public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name { if (span.Length <= size) { - throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {span.Length}"); } } @@ -61,7 +61,7 @@ public static void HasSizeAtLeastOrEqualTo(ReadOnlySpan span, int size, st { if (span.Length < size) { - throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); } } @@ -78,7 +78,7 @@ public static void HasSizeLessThan(ReadOnlySpan span, int size, string nam { if (span.Length >= size) { - throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); } } @@ -95,7 +95,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, s { if (span.Length > size) { - throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); } } @@ -112,7 +112,7 @@ public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string { if (memory.Length != size) { - throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); } } @@ -129,7 +129,7 @@ public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string { if (memory.Length <= size) { - throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); } } @@ -146,7 +146,7 @@ public static void HasSizeAtLeastOrEqualTo(ReadOnlyMemory memory, int size { if (memory.Length < size) { - throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); } } @@ -163,7 +163,7 @@ public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string { if (memory.Length >= size) { - throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); } } @@ -180,7 +180,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int siz { if (memory.Length > size) { - throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs b/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs new file mode 100644 index 00000000000..53a4eef237b --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs @@ -0,0 +1,56 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + /// This method is marked as to preserve the stack trace to this location. + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ThrowArgumentException(string name, string message) + { + throw new ArgumentException(message, name); + } + + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + /// This method is marked as to preserve the stack trace to this location. + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ThrowArgumentNullException(string name, string message) + { + throw new ArgumentNullException(name, message); + } + + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + /// This method is marked as to preserve the stack trace to this location. + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ThrowArgumentOutOfRangeException(string name, string message) + { + throw new ArgumentOutOfRangeException(name, message); + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 033bdfee7ca..0b1072fbccd 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -29,7 +29,7 @@ public static void IsNull(object? value, string name) { if (value != null) { - throw new ArgumentNullException(name, $"Parameter {name} must be null"); + ThrowArgumentNullException(name, $"Parameter {name} must be null"); } } @@ -44,7 +44,7 @@ public static void IsNotNull(object? value, string name) { if (value is null) { - throw new ArgumentNullException(name, $"Parameter {name} must be not null"); + ThrowArgumentNullException(name, $"Parameter {name} must be not null"); } } @@ -62,7 +62,7 @@ public static void IsNotNull(T? value, string name) { if (value is null) { - throw new ArgumentNullException(name, $"Parameter {name} must be not null"); + ThrowArgumentNullException(name, $"Parameter {name} must be not null"); } } @@ -79,7 +79,7 @@ public static void IsOfType(object value, string name) { if (value.GetType() != typeof(T)) { - throw new ArgumentException($"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}", name); + ThrowArgumentException(name, $"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}"); } } @@ -95,7 +95,7 @@ public static void IsAssignableToType(object value, string name) { if (!(value is T)) { - throw new ArgumentException($"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}", name); + ThrowArgumentException(name, $"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}"); } } @@ -114,7 +114,7 @@ public static void IsEqualTo(T value, T target, string name) { if (!value.Equals(target)) { - throw new ArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + ThrowArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); } } @@ -133,7 +133,7 @@ public static void IsNotEqualTo(T value, T target, string name) { if (value.Equals(target)) { - throw new ArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + ThrowArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); } } @@ -145,7 +145,6 @@ public static void IsNotEqualTo(T value, T target, string name) /// The target value to test for. /// The name of the input parameter being tested. /// Thrown if is not a bitwise match for . - [MethodImpl(MethodImplOptions.NoInlining)] public static void IsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { @@ -160,7 +159,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueByte != targetByte) { - throw new ArgumentException($"Parameter {name} must is not a bitwise match (byte #{i} was {valueByte} instead of {targetByte})", name); + ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match (byte #{i} was {valueByte} instead of {targetByte})"); } } } @@ -176,7 +175,7 @@ public static void IsTrue(bool value, string name) { if (!value) { - throw new ArgumentException($"Parameter {name} must be true, was false", name); + ThrowArgumentException(name, $"Parameter {name} must be true, was false"); } } @@ -191,7 +190,7 @@ public static void IsFalse(bool value, string name) { if (value) { - throw new ArgumentException($"Parameter {name} must be false, was true", name); + ThrowArgumentException(name, $"Parameter {name} must be false, was true"); } } @@ -210,7 +209,7 @@ public static void IsLessThan(T value, T max, string name) { if (value.CompareTo(max) >= 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); } } @@ -229,7 +228,7 @@ public static void IsLessThanOrEqualTo(T value, T maximum, string name) { if (value.CompareTo(maximum) > 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); } } @@ -248,7 +247,7 @@ public static void IsGreaterThan(T value, T minimum, string name) { if (value.CompareTo(minimum) <= 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); } } @@ -267,7 +266,7 @@ public static void IsGreaterThanOrEqualTo(T value, T minimum, string name) { if (value.CompareTo(minimum) < 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); } } @@ -287,7 +286,7 @@ public static void IsBetween(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); } } @@ -307,7 +306,7 @@ public static void IsNotBetween(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) > 0 || value.CompareTo(maximum) < 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} and >= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} and >= {maximum}, was {value}"); } } @@ -327,7 +326,7 @@ public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string n { if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); } } @@ -347,7 +346,7 @@ public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, strin { if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) <= 0) { - throw new ArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} and > {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} and > {maximum}, was {value}"); } } @@ -371,7 +370,7 @@ public static void HasSizeEqualTo(IEnumerable enumerable, int size, string (actualSize = readOnlyCollectionCount) != size) || (actualSize = enumerable.Count()) != size) { - throw new ArgumentException($"Parameter {name} must be sized == {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {actualSize}"); } } @@ -395,7 +394,7 @@ public static void HasSizeAtLeast(IEnumerable enumerable, int size, string (actualSize = readOnlyCollectionCount) <= size) || (actualSize = enumerable.Count()) <= size) { - throw new ArgumentException($"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); } } @@ -419,7 +418,7 @@ public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int siz (actualSize = readOnlyCollectionCount) < size) || (actualSize = enumerable.Count()) < size) { - throw new ArgumentException($"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); } } @@ -443,7 +442,7 @@ public static void HasSizeLessThan(IEnumerable enumerable, int size, strin (actualSize = readOnlyCollectionCount) >= size) || (actualSize = enumerable.Count()) >= size) { - throw new ArgumentException($"Parameter {name} must be sized < {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {actualSize}"); } } @@ -467,7 +466,7 @@ public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int si (actualSize = readOnlyCollectionCount) > size) || (actualSize = enumerable.Count()) > size) { - throw new ArgumentException($"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); } } } From b79daa7b1bd5424bed3e06a4ab2ebab9752c210c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:47:33 +0100 Subject: [PATCH 015/123] Code refactoring --- .../Diagnostics/Guard.Enumerable.cs | 139 ++++++++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.cs | 120 --------------- 2 files changed, 139 insertions(+), 120 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs new file mode 100644 index 00000000000..b75e517b6c1 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -0,0 +1,139 @@ +// 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.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) != size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) != size) || + (actualSize = enumerable.Count()) != size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {actualSize}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) <= size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) <= size) || + (actualSize = enumerable.Count()) <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) < size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) < size) || + (actualSize = enumerable.Count()) < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) >= size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) >= size) || + (actualSize = enumerable.Count()) >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {actualSize}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) > size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) > size) || + (actualSize = enumerable.Count()) > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 0b1072fbccd..d75c51d1ca5 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -349,125 +349,5 @@ public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, strin ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} and > {maximum}, was {value}"); } } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - /// The method will skip enumerating if possible (if it's an or ). - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) - { - int actualSize; - - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) != size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) != size) || - (actualSize = enumerable.Count()) != size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {actualSize}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - /// The method will skip enumerating if possible (if it's an or ). - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) - { - int actualSize; - - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) <= size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) <= size) || - (actualSize = enumerable.Count()) <= size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); - } - } - - /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - /// The method will skip enumerating if possible (if it's an or ). - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) - { - int actualSize; - - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) < size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) < size) || - (actualSize = enumerable.Count()) < size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - /// The method will skip enumerating if possible (if it's an or ). - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) - { - int actualSize; - - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) >= size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) >= size) || - (actualSize = enumerable.Count()) >= size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {actualSize}"); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - /// The method will skip enumerating if possible (if it's an or ). - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) - { - int actualSize; - - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) > size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) > size) || - (actualSize = enumerable.Count()) > size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); - } - } } } From d44e63385dccd9aae24d343beb6096d1b6567577 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:49:44 +0100 Subject: [PATCH 016/123] Added Guard.HasSizeNotEqualTo API --- .../Diagnostics/Guard.Enumerable.cs | 24 +++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index b75e517b6c1..2bf6dc942da 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -40,6 +40,30 @@ public static void HasSizeEqualTo(IEnumerable enumerable, int size, string } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + /// The method will skip enumerating if possible (if it's an or ). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, string name) + { + int actualSize; + + if (((enumerable as ICollection)?.Count is int collectionCount && + (actualSize = collectionCount) == size) || + ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && + (actualSize = readOnlyCollectionCount) == size) || + (actualSize = enumerable.Count()) == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {actualSize}"); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index aca5fdd166f..2d64746abea 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -31,6 +31,23 @@ public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// @@ -116,6 +133,23 @@ public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// From 0292f9db88e615b2d400b5e8036119e3321b1a06 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:54:24 +0100 Subject: [PATCH 017/123] Added Guard size API overloads for the string type --- .../Diagnostics/Guard.Enumerable.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 2bf6dc942da..a20eed698db 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -16,6 +16,90 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(string text, int size, string name) + { + HasSizeEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(string text, int size, string name) + { + HasSizeNotEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(string text, int size, string name) + { + HasSizeAtLeast(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(string text, int size, string name) + { + HasSizeAtLeastOrEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(string text, int size, string name) + { + HasSizeLessThan(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(string text, int size, string name) + { + HasSizeLessThanOrEqualTo(text.AsSpan(), size, name); + } + /// /// Asserts that the input instance must have a size of a specified value. /// From 073a20576af8a420fb54631a2dd2fc79b6fef786 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 13:58:53 +0100 Subject: [PATCH 018/123] Added Guard size API overloads for T[] arrays --- .../Diagnostics/Guard.Enumerable.cs | 90 +++++++++++++++++-- 1 file changed, 84 insertions(+), 6 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index a20eed698db..1b1f08e37d3 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -19,7 +19,6 @@ public static partial class Guard /// /// Asserts that the input instance must have a size of a specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -33,7 +32,6 @@ public static void HasSizeEqualTo(string text, int size, string name) /// /// Asserts that the input instance must have a size not equal to a specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -47,7 +45,6 @@ public static void HasSizeNotEqualTo(string text, int size, string name) /// /// Asserts that the input instance must have a size of at least specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -61,7 +58,6 @@ public static void HasSizeAtLeast(string text, int size, string name) /// /// Asserts that the input instance must have a size of at least or equal to a specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -75,7 +71,6 @@ public static void HasSizeAtLeastOrEqualTo(string text, int size, string name) /// /// Asserts that the input instance must have a size of less than a specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -89,7 +84,6 @@ public static void HasSizeLessThan(string text, int size, string name) /// /// Asserts that the input instance must have a size of less than or equal to a specified value. /// - /// The type of items in the input instance. /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. @@ -100,6 +94,90 @@ public static void HasSizeLessThanOrEqualTo(string text, int size, string name) HasSizeLessThanOrEqualTo(text.AsSpan(), size, name); } + /// + /// Asserts that the input array instance must have a size of a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] array, int size, string name) + { + HasSizeEqualTo(new ReadOnlySpan(array), size, name); + } + + /// + /// Asserts that the input array instance must have a size not equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(T[] array, int size, string name) + { + HasSizeNotEqualTo(new ReadOnlySpan(array), size, name); + } + + /// + /// Asserts that the input array instance must have a size of at least specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(T[] array, int size, string name) + { + HasSizeAtLeast(new ReadOnlySpan(array), size, name); + } + + /// + /// Asserts that the input array instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) + { + HasSizeAtLeastOrEqualTo(new ReadOnlySpan(array), size, name); + } + + /// + /// Asserts that the input array instance must have a size of less than a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(T[] array, int size, string name) + { + HasSizeLessThan(new ReadOnlySpan(array), size, name); + } + + /// + /// Asserts that the input array instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + HasSizeLessThanOrEqualTo(new ReadOnlySpan(array), size, name); + } + /// /// Asserts that the input instance must have a size of a specified value. /// From 5edef16a3d7955afe9aa576eea2d51631f2f3bfe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 14:03:03 +0100 Subject: [PATCH 019/123] Code refactoring --- .../Diagnostics/Guard.Enumerable.cs | 78 --------------- Microsoft.Toolkit/Diagnostics/Guard.String.cs | 95 +++++++++++++++++++ 2 files changed, 95 insertions(+), 78 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.String.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 1b1f08e37d3..e2fc4ed98a9 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -16,84 +16,6 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(string text, int size, string name) - { - HasSizeEqualTo(text.AsSpan(), size, name); - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(string text, int size, string name) - { - HasSizeNotEqualTo(text.AsSpan(), size, name); - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(string text, int size, string name) - { - HasSizeAtLeast(text.AsSpan(), size, name); - } - - /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(string text, int size, string name) - { - HasSizeAtLeastOrEqualTo(text.AsSpan(), size, name); - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(string text, int size, string name) - { - HasSizeLessThan(text.AsSpan(), size, name); - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(string text, int size, string name) - { - HasSizeLessThanOrEqualTo(text.AsSpan(), size, name); - } - /// /// Asserts that the input array instance must have a size of a specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.String.cs b/Microsoft.Toolkit/Diagnostics/Guard.String.cs new file mode 100644 index 00000000000..11d95a1f944 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.String.cs @@ -0,0 +1,95 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(string text, int size, string name) + { + HasSizeEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(string text, int size, string name) + { + HasSizeNotEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(string text, int size, string name) + { + HasSizeAtLeast(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(string text, int size, string name) + { + HasSizeAtLeastOrEqualTo(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(string text, int size, string name) + { + HasSizeLessThan(text.AsSpan(), size, name); + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(string text, int size, string name) + { + HasSizeLessThanOrEqualTo(text.AsSpan(), size, name); + } + } +} From 1a19fea6cd5f9efd591f8faee5eab1e18d4d7b1e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 14:04:03 +0100 Subject: [PATCH 020/123] Removed unnecessary using directives --- Microsoft.Toolkit/Converters.cs | 4 ---- Microsoft.Toolkit/Diagnostics/Guard.cs | 2 -- Microsoft.Toolkit/Extensions/ArrayExtensions.cs | 1 - .../IncrementalLoadingCollection/IIncrementalSource.cs | 1 - 4 files changed, 8 deletions(-) diff --git a/Microsoft.Toolkit/Converters.cs b/Microsoft.Toolkit/Converters.cs index a7a2ef509ad..20cb290b8b0 100644 --- a/Microsoft.Toolkit/Converters.cs +++ b/Microsoft.Toolkit/Converters.cs @@ -2,10 +2,6 @@ // 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.Collections.Generic; -using System.Text; - namespace Microsoft.Toolkit { /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index d75c51d1ca5..9b38d431682 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -3,9 +3,7 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Runtime.CompilerServices; #nullable enable diff --git a/Microsoft.Toolkit/Extensions/ArrayExtensions.cs b/Microsoft.Toolkit/Extensions/ArrayExtensions.cs index 5e90dcda17c..a7e870492e8 100644 --- a/Microsoft.Toolkit/Extensions/ArrayExtensions.cs +++ b/Microsoft.Toolkit/Extensions/ArrayExtensions.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace Microsoft.Toolkit.Extensions { diff --git a/Microsoft.Toolkit/IncrementalLoadingCollection/IIncrementalSource.cs b/Microsoft.Toolkit/IncrementalLoadingCollection/IIncrementalSource.cs index 7dc874a2802..45059162607 100644 --- a/Microsoft.Toolkit/IncrementalLoadingCollection/IIncrementalSource.cs +++ b/Microsoft.Toolkit/IncrementalLoadingCollection/IIncrementalSource.cs @@ -2,7 +2,6 @@ // 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.Collections.Generic; using System.Threading; using System.Threading.Tasks; From d38ad1383ea57e71a279168446afbef528d3ef3e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 14:08:10 +0100 Subject: [PATCH 021/123] Added Guard reference check APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 9b38d431682..2b2b0e59e9d 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -162,6 +162,44 @@ public static void IsBitwiseEqualTo(T value, T target, string name) } } + /// + /// Asserts that the input value must be the same instance as the target value. + /// + /// The type of input values to compare. + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is not the same instance as . + /// The method is generic to prevent using it with value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsReferenceEqualTo(T value, T target, string name) + where T : class + { + if (!ReferenceEquals(value, target)) + { + ThrowArgumentException(name, $"Parameter {name} must be the same instance as the target object"); + } + } + + /// + /// Asserts that the input value must not be the same instance as the target value. + /// + /// The type of input values to compare. + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is the same instance as . + /// The method is generic to prevent using it with value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsReferenceNotEqualTo(T value, T target, string name) + where T : class + { + if (ReferenceEquals(value, target)) + { + ThrowArgumentException(name, $"Parameter {name} must not be the same instance as the target object"); + } + } + /// /// Asserts that the input value must be . /// From e9108fc022c365fcfc165446bc83ec7cf036131f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 14:20:35 +0100 Subject: [PATCH 022/123] Added new Guaard APIs for string values --- Microsoft.Toolkit/Diagnostics/Guard.String.cs | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.String.cs b/Microsoft.Toolkit/Diagnostics/Guard.String.cs index 11d95a1f944..7213ec6d93e 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.String.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.String.cs @@ -14,6 +14,126 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input instance must be or empty. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is neither nor empty. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNullOrEmpty(string? text, string name) + { + if (!string.IsNullOrEmpty(text)) + { + ThrowArgumentException(name, $"Parameter {name} must be null or empty, was \"{text}\""); + } + } + + /// + /// Asserts that the input instance must not be or empty. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is or empty. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotNullOrEmpty(string? text, string name) + { + if (string.IsNullOrEmpty(text)) + { + ThrowArgumentException(name, $"Parameter {name} must not be null or empty, was {(text is null ? "null" : "empty")}"); + } + } + + /// + /// Asserts that the input instance must be or whitespace. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is neither nor whitespace. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNullOrWhitespace(string? text, string name) + { + if (!string.IsNullOrWhiteSpace(text)) + { + ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + } + } + + /// + /// Asserts that the input instance must not be or whitespace. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is or whitespace. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotNullOrWhitespace(string? text, string name) + { + if (string.IsNullOrWhiteSpace(text)) + { + ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is empty. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(string text, string name) + { + if (text.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, was \"{text}\""); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is empty. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(string text, string name) + { + if (text.Length == 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty, was empty"); + } + } + + /// + /// Asserts that the input instance must be whitespace. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is neither nor whitespace. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsWhitespace(string text, string name) + { + if (!string.IsNullOrWhiteSpace(text)) + { + ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + } + } + + /// + /// Asserts that the input instance must not be or whitespace. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is or whitespace. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotWhitespace(string text, string name) + { + if (string.IsNullOrWhiteSpace(text)) + { + ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// From 18bb2d4a6d3c68b2d7a37b72d325d46db01a0d64 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 18:47:24 +0100 Subject: [PATCH 023/123] Improved Guard.IsBitwiseEqualTo API --- Microsoft.Toolkit/Diagnostics/Guard.cs | 68 +++++++++++++++++++--- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 1 + 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 2b2b0e59e9d..3eef50d21f9 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -143,21 +143,71 @@ public static void IsNotEqualTo(T value, T target, string name) /// The target value to test for. /// The name of the input parameter being tested. /// Thrown if is not a bitwise match for . - public static void IsBitwiseEqualTo(T value, T target, string name) + public static unsafe void IsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { - ref byte valueRef = ref Unsafe.As(ref value); - ref byte targetRef = ref Unsafe.As(ref target); - int bytesCount = Unsafe.SizeOf(); - - for (int i = 0; i < bytesCount; i++) + /* Include some fast paths if the input type is of size 1, 2, 4 or 8. + * In those cases, just reinterpret the bytes as values of an integer type, + * and compare them directly, which is much faster than having explicitly + * loop through every single byte. This also allows for more expressive error + * messages, since the entire input values can be expressed as hexadecimal values. + * The conditional branches below are known at compile time by the JIT compiler, + * so that only the right one will actually be translated into native code. */ + if (sizeof(T) == sizeof(byte)) { - byte valueByte = Unsafe.Add(ref valueRef, i); - byte targetByte = Unsafe.Add(ref targetRef, i); + byte valueByte = Unsafe.As(ref value); + byte targetByte = Unsafe.As(ref target); if (valueByte != targetByte) { - ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match (byte #{i} was {valueByte} instead of {targetByte})"); + ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueByte:X2} instead of 0x{targetByte:X2}"); + } + } + else if (sizeof(T) == sizeof(ushort)) + { + ushort valueUShort = Unsafe.As(ref value); + ushort targetUShort = Unsafe.As(ref target); + + if (valueUShort != targetUShort) + { + ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUShort:X4} instead of 0x{targetUShort:X4}"); + } + } + else if (sizeof(T) == sizeof(uint)) + { + uint valueUInt = Unsafe.As(ref value); + uint targetUInt = Unsafe.As(ref target); + + if (valueUInt != targetUInt) + { + ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUInt:X8} instead of 0x{targetUInt:X8}"); + } + } + else if (sizeof(T) == sizeof(ulong)) + { + ulong valueULong = Unsafe.As(ref value); + ulong targetULong = Unsafe.As(ref target); + + if (valueULong != targetULong) + { + ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueULong:X16} instead of 0x{targetULong:X16}"); + } + } + else + { + ref byte valueRef = ref Unsafe.As(ref value); + ref byte targetRef = ref Unsafe.As(ref target); + int bytesCount = Unsafe.SizeOf(); + + for (int i = 0; i < bytesCount; i++) + { + byte valueByte = Unsafe.Add(ref valueRef, i); + byte targetByte = Unsafe.Add(ref targetRef, i); + + if (valueByte != targetByte) + { + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match (byte #{i} was 0x{valueByte:X2} instead of 0x{targetByte:X2})"); + } } } } diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index d445cf5f4e4..cb0dcd79d28 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -3,6 +3,7 @@ netstandard2.0 8.0 + true Windows Community Toolkit .NET Standard This package includes .NET Standard code only helpers such as: From 2d9536f6e5e7f6cb8aea54254815fe36587a9b20 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Feb 2020 18:54:08 +0100 Subject: [PATCH 024/123] Improved Guard.IsNull APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 3eef50d21f9..031099fa8c2 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -19,11 +19,31 @@ public static partial class Guard /// /// Asserts that the input value is . /// + /// The type of reference value type being tested. /// The input value to test. /// The name of the input parameter being tested. /// Thrown if is not . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNull(object? value, string name) + public static void IsNull(T? value, string name) + where T : class + { + if (value != null) + { + ThrowArgumentNullException(name, $"Parameter {name} must be null"); + } + } + + /// + /// Asserts that the input value is . + /// + /// The type of nullable value type being tested. + /// The input value to test. + /// The name of the input parameter being tested. + /// Thrown if is not . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNull(T? value, string name) + where T : struct { if (value != null) { @@ -34,11 +54,13 @@ public static void IsNull(object? value, string name) /// /// Asserts that the input value is not . /// + /// The type of reference value type being tested. /// The input value to test. /// The name of the input parameter being tested. /// Thrown if is . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotNull(object? value, string name) + public static void IsNotNull(T? value, string name) + where T : class { if (value is null) { From a927dadde101b7c14529267c4b612605c0c162fa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 00:21:00 +0100 Subject: [PATCH 025/123] Added new size APIs for copy operations --- .../Diagnostics/Guard.Enumerable.cs | 28 ++++++++ Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 68 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index e2fc4ed98a9..6fa4be14334 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -100,6 +100,34 @@ public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) HasSizeLessThanOrEqualTo(new ReadOnlySpan(array), size, name); } + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] source, T[] destination, string name) + { + HasSizeEqualTo(new ReadOnlySpan(source), destination.AsSpan(), name); + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + HasSizeLessThanOrEqualTo(new ReadOnlySpan(source), destination.AsSpan(), name); + } + /// /// Asserts that the input instance must have a size of a specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 2d64746abea..87addb6f583 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -116,6 +116,40 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, s } } + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {destination.Length}"); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// @@ -217,5 +251,39 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int siz ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); } } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {destination.Length}"); + } + } } } From 56a3a183c3e932f7857de08a9a1a9cf6cbf89dcb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 00:55:58 +0100 Subject: [PATCH 026/123] Added new type test APIs, minor code tweaks --- Microsoft.Toolkit/Diagnostics/Guard.cs | 33 +++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 031099fa8c2..93f5ff88377 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -95,7 +95,6 @@ public static void IsNotNull(T? value, string name) /// Thrown if is not of type . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsOfType(object value, string name) - where T : class { if (value.GetType() != typeof(T)) { @@ -103,6 +102,22 @@ public static void IsOfType(object value, string name) } } + /// + /// Asserts that the input value is of a specific type. + /// + /// The input to test. + /// The type to look for. + /// The name of the input parameter being tested. + /// Thrown if the type of is not the same as . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsOfType(object value, Type type, string name) + { + if (value.GetType() != type) + { + ThrowArgumentException(name, $"Parameter {name} must be of type {type}, was {value.GetType()}"); + } + } + /// /// Asserts that the input value can be cast to a specified type. /// @@ -119,6 +134,22 @@ public static void IsAssignableToType(object value, string name) } } + /// + /// Asserts that the input value can be cast to a specified type. + /// + /// The input to test. + /// The type to look for. + /// The name of the input parameter being tested. + /// Thrown if can't be cast to . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsAssignableToType(object value, Type type, string name) + { + if (!type.IsAssignableFrom(value.GetType()) + { + ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); + } + } + /// /// Asserts that the input value must be equal to a specified value. /// From bbd10a805b6aec55c46fe2c003da0f17190f1e96 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 00:58:34 +0100 Subject: [PATCH 027/123] Minor bug fixes --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 8 ++++---- Microsoft.Toolkit/Diagnostics/Guard.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 87addb6f583..284423aed56 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -129,7 +129,7 @@ public static void HasSizeEqualTo(ReadOnlySpan source, Span destination { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {span.Length}"); + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); } } @@ -146,7 +146,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span d { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {destination.Length}"); + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); } } @@ -265,7 +265,7 @@ public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destina { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {span.Length}"); + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); } } @@ -282,7 +282,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory< { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {destination.Length}"); + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 93f5ff88377..e859164635e 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -144,7 +144,7 @@ public static void IsAssignableToType(object value, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsAssignableToType(object value, Type type, string name) { - if (!type.IsAssignableFrom(value.GetType()) + if (!type.IsAssignableFrom(value.GetType())) { ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); } From fbb580153c246492b4f223404072d9e9973921e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 01:14:25 +0100 Subject: [PATCH 028/123] Added Guard APIs for Task values --- Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs new file mode 100644 index 00000000000..f200bd2788d --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs @@ -0,0 +1,94 @@ +// 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.Runtime.CompilerServices; +using System.Threading.Tasks; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance is in a completed state. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is not in a completed state. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsCompleted(Task task, string name) + { + if (!task.IsCompleted) + { + ThrowArgumentException(name, $"Parameter {name} must be completed, had status {task.Status}"); + } + } + + /// + /// Asserts that the input instance has been completed successfully. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if has not been completed successfully. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsCompletedSuccessfully(Task task, string name) + { + if (task.Status != TaskStatus.RanToCompletion) + { + ThrowArgumentException(name, $"Parameter {name} must be completed successfully, had status {task.Status}"); + } + } + + /// + /// Asserts that the input instance is faulted. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is not faulted. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsFaulted(Task task, string name) + { + if (!task.IsFaulted) + { + ThrowArgumentException(name, $"Parameter {name} must be faulted, had status {task.Status}"); + } + } + + /// + /// Asserts that the input instance is canceled. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is not canceled. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsCanceled(Task task, string name) + { + if (!task.IsCanceled) + { + ThrowArgumentException(name, $"Parameter {name} must be canceled, had status {task.Status}"); + } + } + + /// + /// Asserts that the input instance has a specific status. + /// + /// The input instance to test. + /// The task status that is accepted. + /// The name of the input parameter being tested. + /// Thrown if doesn't match . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasStatus(Task task, TaskStatus status, string name) + { + if (task.Status != status) + { + ThrowArgumentException(name, $"Parameter {name} must have status {status}, had status {task.Status}"); + } + } + } +} From eba232eb8e6dcfdecca51173911dd11200a740c8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 01:14:35 +0100 Subject: [PATCH 029/123] Minor code tweak --- Microsoft.Toolkit/Diagnostics/Guard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index e859164635e..ee00d0911df 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -144,7 +144,7 @@ public static void IsAssignableToType(object value, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsAssignableToType(object value, Type type, string name) { - if (!type.IsAssignableFrom(value.GetType())) + if (!type.IsInstanceOfType(value)) { ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); } From 9071e12e4160dbcb0b0ebc6758eac5df2c9643d0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 01:46:55 +0100 Subject: [PATCH 030/123] Added new Guard.IsEmpty APIs --- .../Diagnostics/Guard.Enumerable.cs | 98 +++++++++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 64 ++++++++++++ 2 files changed, 162 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 6fa4be14334..5f39612965b 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -16,6 +16,32 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input array instance must be empty. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(T[] array, string name) + { + IsEmpty(new ReadOnlySpan(array), name); + } + + /// + /// Asserts that the input array instance must not be empty. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(T[] array, string name) + { + IsNotEmpty(new ReadOnlySpan(array), name); + } + /// /// Asserts that the input array instance must have a size of a specified value. /// @@ -128,6 +154,78 @@ public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, stri HasSizeLessThanOrEqualTo(new ReadOnlySpan(source), destination.AsSpan(), name); } + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(IEnumerable enumerable, string name) + { + if ((enumerable as ICollection)?.Count is int collectionCount) + { + if (collectionCount != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collectionCount}"); + + return; + } + } + + if ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount) + { + if (readOnlyCollectionCount != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {readOnlyCollectionCount}"); + + return; + } + } + + if (enumerable.Any()) + { + ThrowArgumentException(name, $"Parameter {name} must be empty"); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(IEnumerable enumerable, string name) + { + if ((enumerable as ICollection)?.Count is int collectionCount) + { + if (collectionCount == 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + + return; + } + } + + if ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount) + { + if (readOnlyCollectionCount == 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + + return; + } + } + + if (enumerable.Any()) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 284423aed56..dc6b198d55b 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -14,6 +14,38 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ReadOnlySpan span, string name) + { + if (span.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ReadOnlySpan span, string name) + { + if (span.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// @@ -150,6 +182,38 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span d } } + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// From edb1d41d770232242d456c190595d753934e4ab2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 12:35:34 +0100 Subject: [PATCH 031/123] Refactored code to remove unsafe requirement --- Microsoft.Toolkit/Diagnostics/Guard.cs | 10 +++++----- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index ee00d0911df..830449a685f 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -196,7 +196,7 @@ public static void IsNotEqualTo(T value, T target, string name) /// The target value to test for. /// The name of the input parameter being tested. /// Thrown if is not a bitwise match for . - public static unsafe void IsBitwiseEqualTo(T value, T target, string name) + public static void IsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { /* Include some fast paths if the input type is of size 1, 2, 4 or 8. @@ -206,7 +206,7 @@ public static unsafe void IsBitwiseEqualTo(T value, T target, string name) * messages, since the entire input values can be expressed as hexadecimal values. * The conditional branches below are known at compile time by the JIT compiler, * so that only the right one will actually be translated into native code. */ - if (sizeof(T) == sizeof(byte)) + if (typeof(T) == typeof(byte)) { byte valueByte = Unsafe.As(ref value); byte targetByte = Unsafe.As(ref target); @@ -216,7 +216,7 @@ public static unsafe void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueByte:X2} instead of 0x{targetByte:X2}"); } } - else if (sizeof(T) == sizeof(ushort)) + else if (typeof(T) == typeof(ushort)) { ushort valueUShort = Unsafe.As(ref value); ushort targetUShort = Unsafe.As(ref target); @@ -226,7 +226,7 @@ public static unsafe void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUShort:X4} instead of 0x{targetUShort:X4}"); } } - else if (sizeof(T) == sizeof(uint)) + else if (typeof(T) == typeof(uint)) { uint valueUInt = Unsafe.As(ref value); uint targetUInt = Unsafe.As(ref target); @@ -236,7 +236,7 @@ public static unsafe void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUInt:X8} instead of 0x{targetUInt:X8}"); } } - else if (sizeof(T) == sizeof(ulong)) + else if (typeof(T) == typeof(ulong)) { ulong valueULong = Unsafe.As(ref value); ulong targetULong = Unsafe.As(ref target); diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index cb0dcd79d28..d445cf5f4e4 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -3,7 +3,6 @@ netstandard2.0 8.0 - true Windows Community Toolkit .NET Standard This package includes .NET Standard code only helpers such as: From d9c83f4a99e9d6572ff41d30468ed77f07ca1be0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 14 Feb 2020 16:59:25 +0100 Subject: [PATCH 032/123] Added Guard.IsInRange APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 44 ++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 830449a685f..d6f5feb009a 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -409,6 +409,46 @@ public static void IsGreaterThanOrEqualTo(T value, T minimum, string name) } } + /// + /// Asserts that the input value must be in a given range. + /// + /// The type of input values to compare. + /// 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) >= 0) + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and < {maximum}, was {value}"); + } + } + + /// + /// Asserts that the input value must not be in a given range. + /// + /// The type of input values to compare. + /// 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) < 0) + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or >= {maximum}, was {value}"); + } + } + /// /// Asserts that the input value must be in a given interval. /// @@ -445,7 +485,7 @@ public static void IsNotBetween(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) > 0 || value.CompareTo(maximum) < 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} and >= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} or >= {maximum}, was {value}"); } } @@ -485,7 +525,7 @@ public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, strin { if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) <= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} and > {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or > {maximum}, was {value}"); } } } From e63402be6cdb3b03480497a92ae03eb842a38a01 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 17 Feb 2020 16:33:28 +0100 Subject: [PATCH 033/123] Added missing type checks in Guard.IsBitwiseEqualTo API --- Microsoft.Toolkit/Diagnostics/Guard.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index d6f5feb009a..0c2b4399116 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -206,7 +206,9 @@ public static void IsBitwiseEqualTo(T value, T target, string name) * messages, since the entire input values can be expressed as hexadecimal values. * The conditional branches below are known at compile time by the JIT compiler, * so that only the right one will actually be translated into native code. */ - if (typeof(T) == typeof(byte)) + if (typeof(T) == typeof(byte) || + typeof(T) == typeof(sbyte) || + typeof(T) == typeof(bool)) { byte valueByte = Unsafe.As(ref value); byte targetByte = Unsafe.As(ref target); @@ -216,7 +218,9 @@ public static void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueByte:X2} instead of 0x{targetByte:X2}"); } } - else if (typeof(T) == typeof(ushort)) + else if (typeof(T) == typeof(ushort) || + typeof(T) == typeof(short) || + typeof(T) == typeof(char)) { ushort valueUShort = Unsafe.As(ref value); ushort targetUShort = Unsafe.As(ref target); @@ -226,7 +230,9 @@ public static void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUShort:X4} instead of 0x{targetUShort:X4}"); } } - else if (typeof(T) == typeof(uint)) + else if (typeof(T) == typeof(uint) || + typeof(T) == typeof(int) || + typeof(T) == typeof(float)) { uint valueUInt = Unsafe.As(ref value); uint targetUInt = Unsafe.As(ref target); @@ -236,7 +242,9 @@ public static void IsBitwiseEqualTo(T value, T target, string name) ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUInt:X8} instead of 0x{targetUInt:X8}"); } } - else if (typeof(T) == typeof(ulong)) + else if (typeof(T) == typeof(ulong) || + typeof(T) == typeof(long) || + typeof(T) == typeof(double)) { ulong valueULong = Unsafe.As(ref value); ulong targetULong = Unsafe.As(ref target); From 87056c9f8f1f5b3c79fa2bb8acfe87ba7d8b27d4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 17:37:58 +0100 Subject: [PATCH 034/123] Added ValueTypeExtensions.ToHexString API --- .../Extensions/ValueTypeExtensions.cs | 62 +++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 1 + 2 files changed, 63 insertions(+) create mode 100644 Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs diff --git a/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs new file mode 100644 index 00000000000..cbd13480b74 --- /dev/null +++ b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs @@ -0,0 +1,62 @@ +// 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.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace Microsoft.Toolkit.Extensions +{ + /// + /// Helpers for working with value types. + /// + public static class ValueTypeExtensions + { + /// + /// Gets the table of hex characters (doesn't allocate, maps to .text section, see ) + /// + private static ReadOnlySpan HexCharactersTable => new[] + { + (byte)'0', (byte)'1', (byte)'2', (byte)'3', + (byte)'4', (byte)'5', (byte)'6', (byte)'7', + (byte)'8', (byte)'9', (byte)'A', (byte)'B', + (byte)'C', (byte)'D', (byte)'E', (byte)'F' + }; + + /// + /// Returns a hexadecimal representation of a given value, left-padded and ordered as big-endian. + /// + /// The input type to format to . + /// The input value to format to . + /// The hexadecimal representation of , left-padded and ordered as big-endian. + [MethodImpl(MethodImplOptions.NoInlining)] + public static unsafe string ToHexString(this T value) + where T : unmanaged + { + int + sizeOfT = Unsafe.SizeOf(), + bufferSize = (2 * sizeOfT) + 2; + char* p = stackalloc char[bufferSize]; + + p[0] = '0'; + p[1] = 'x'; + + ref byte r0 = ref Unsafe.As(ref value); + ref byte rh = ref MemoryMarshal.GetReference(HexCharactersTable); + + for (int i = 0, j = bufferSize - 2; i < sizeOfT; i++, j -= 2) + { + byte b = Unsafe.Add(ref r0, i); + int + low = b & 0x0F, + high = (b & 0xF0) >> 4; + + p[j + 1] = (char)Unsafe.Add(ref rh, low); + p[j] = (char)Unsafe.Add(ref rh, high); + } + + return new string(p, 0, bufferSize); + } + } +} diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index d445cf5f4e4..cb0dcd79d28 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -3,6 +3,7 @@ netstandard2.0 8.0 + true Windows Community Toolkit .NET Standard This package includes .NET Standard code only helpers such as: From 0d34c0aaa0215eca3d23668d4a8759811c7616aa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 17:39:25 +0100 Subject: [PATCH 035/123] Code refactoring --- Microsoft.Toolkit/Diagnostics/Guard.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 0c2b4399116..6f541de49a6 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -215,7 +216,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueByte != targetByte) { - ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueByte:X2} instead of 0x{targetByte:X2}"); + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); } } else if (typeof(T) == typeof(ushort) || @@ -227,7 +228,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueUShort != targetUShort) { - ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUShort:X4} instead of 0x{targetUShort:X4}"); + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); } } else if (typeof(T) == typeof(uint) || @@ -239,7 +240,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueUInt != targetUInt) { - ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueUInt:X8} instead of 0x{targetUInt:X8}"); + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); } } else if (typeof(T) == typeof(ulong) || @@ -251,7 +252,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueULong != targetULong) { - ThrowArgumentException(name, $"Parameter {name} must is not a bitwise match, was 0x{valueULong:X16} instead of 0x{targetULong:X16}"); + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); } } else @@ -267,7 +268,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueByte != targetByte) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match (byte #{i} was 0x{valueByte:X2} instead of 0x{targetByte:X2})"); + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); } } } From 497d1497f118e16c5fc11b1794e13012de67ee33 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 17:55:17 +0100 Subject: [PATCH 036/123] Added ValueTypeExtensions tests --- .../Extensions/Test_ValueTypeExtensions.cs | 26 +++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 27 insertions(+) create mode 100644 UnitTests/Extensions/Test_ValueTypeExtensions.cs diff --git a/UnitTests/Extensions/Test_ValueTypeExtensions.cs b/UnitTests/Extensions/Test_ValueTypeExtensions.cs new file mode 100644 index 00000000000..da7b8eae51d --- /dev/null +++ b/UnitTests/Extensions/Test_ValueTypeExtensions.cs @@ -0,0 +1,26 @@ +// 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 Microsoft.Toolkit.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTests.Extensions +{ + [TestClass] + public class Test_ValueTypeExtensions + { + [TestCategory("ValueTypeExtensions")] + [TestMethod] + public void Test_ValueTypeExtensions_ToHexString() + { + Assert.AreEqual(((byte)0).ToHexString(), "0x00"); + Assert.AreEqual(((byte)127).ToHexString(), "0x7F"); + Assert.AreEqual(((byte)255).ToHexString(), "0xFF"); + Assert.AreEqual(((ushort)6458).ToHexString(), "0x193A"); + Assert.AreEqual(6458.ToHexString(), "0x0000193A"); + Assert.AreEqual((-1).ToHexString(), "0xFFFFFFFF"); + Assert.AreEqual(true.ToHexString(), "0x01"); + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 1813e7836db..7a5e254f8bc 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -133,6 +133,7 @@ + From bc0fc1b0061703eed78f0a3a379dacaaee19a9ef Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:27:19 +0100 Subject: [PATCH 037/123] Added general Guard tests --- UnitTests/Diagnostics/Test_Guard.cs | 485 ++++++++++++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 486 insertions(+) create mode 100644 UnitTests/Diagnostics/Test_Guard.cs diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs new file mode 100644 index 00000000000..887a4065798 --- /dev/null +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -0,0 +1,485 @@ +// 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 Microsoft.Toolkit.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTests.Diagnostics +{ + [TestClass] + public class Test_Guard + { + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNull_Ok() + { + Guard.IsNull(null, nameof(Test_Guard_IsNull_Ok)); + Guard.IsNull(null, nameof(Test_Guard_IsNull_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Test_Guard_IsNull_ClassFail() + { + Guard.IsNull(new object(), nameof(Test_Guard_IsNull_ClassFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Test_Guard_IsNull_StructFail() + { + Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotNull_Ok() + { + Guard.IsNotNull(new object(), nameof(Test_Guard_IsNotNull_Ok)); + Guard.IsNotNull(7, nameof(Test_Guard_IsNotNull_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Test_Guard_IsNotNull_ClassFail() + { + Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_ClassFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void Test_Guard_IsNotNull_StructFail() + { + Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_StructFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsOfT_Ok() + { + Guard.IsOfType("Hello", nameof(Test_Guard_IsOfT_Ok)); + Guard.IsOfType(7, nameof(Test_Guard_IsOfT_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsOfT_Fail() + { + Guard.IsOfType(7, nameof(Test_Guard_IsOfT_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsOfType_Ok() + { + Guard.IsOfType("Hello", typeof(string), nameof(Test_Guard_IsOfType_Ok)); + Guard.IsOfType(7, typeof(int), nameof(Test_Guard_IsOfType_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsOfType_Fail() + { + Guard.IsOfType(7, typeof(string), nameof(Test_Guard_IsOfType_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsAssignableToT_Ok() + { + Guard.IsAssignableToType("Hello", nameof(Test_Guard_IsAssignableToT_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsAssignableToT_Fail() + { + Guard.IsAssignableToType(7, nameof(Test_Guard_IsAssignableToT_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsAssignableToType_Ok() + { + Guard.IsAssignableToType("Hello", typeof(string), nameof(Test_Guard_IsAssignableToType_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsAssignableToType_Fail() + { + Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsEqualTo_Ok() + { + Guard.IsEqualTo("Hello", "Hello", nameof(Test_Guard_IsEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsEqualTo_Fail() + { + Guard.IsEqualTo("Hello", "World", nameof(Test_Guard_IsEqualTo_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotEqualTo_Ok() + { + Guard.IsNotEqualTo("Hello", "World", nameof(Test_Guard_IsNotEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsNotEqualTo_Fail() + { + Guard.IsNotEqualTo("Hello", "Hello", nameof(Test_Guard_IsNotEqualTo_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsBitwiseEqualTo_Ok() + { + Guard.IsBitwiseEqualTo(byte.MaxValue, byte.MaxValue, nameof(Test_Guard_IsBitwiseEqualTo_Ok)); + Guard.IsBitwiseEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsBitwiseEqualTo_Ok)); + Guard.IsBitwiseEqualTo(double.Epsilon, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_Ok)); + Guard.IsBitwiseEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsBitwiseEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsBitwiseEqualTo_SingleFail() + { + Guard.IsBitwiseEqualTo(double.PositiveInfinity, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_SingleFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsBitwiseEqualTo_LoopFail() + { + Guard.IsBitwiseEqualTo(DateTime.Now, DateTime.Today, nameof(Test_Guard_IsBitwiseEqualTo_LoopFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsReferenceEqualTo_Ok() + { + var obj = new object(); + + Guard.IsReferenceEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsReferenceEqualTo_Fail() + { + Guard.IsReferenceEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsReferenceNotEqualTo_Ok() + { + Guard.IsReferenceNotEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsReferenceNotEqualTo_Fail() + { + var obj = new object(); + + Guard.IsReferenceNotEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsTrue_Ok() + { + Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsTrue_Fail() + { + Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsFalse_Ok() + { + Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsFalse_Fail() + { + Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsLessThan_Ok() + { + Guard.IsLessThan(1, 2, nameof(Test_Guard_IsLessThan_Ok)); + Guard.IsLessThan(1.2f, 3.14f, nameof(Test_Guard_IsLessThan_Ok)); + Guard.IsLessThan(DateTime.Now, DateTime.MaxValue, nameof(Test_Guard_IsLessThan_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsLessThan_EqualsFalse() + { + Guard.IsLessThan(1, 1, nameof(Test_Guard_IsLessThan_EqualsFalse)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsLessThan_GreaterFalse() + { + Guard.IsLessThan(2, 1, nameof(Test_Guard_IsLessThan_GreaterFalse)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsLessThanOrEqualTo_Ok() + { + Guard.IsLessThanOrEqualTo(1, 2, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + Guard.IsLessThanOrEqualTo(1, 1, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + Guard.IsLessThanOrEqualTo(0.1f, MathF.PI, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + Guard.IsLessThanOrEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + Guard.IsLessThanOrEqualTo(DateTime.Today, DateTime.MaxValue, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + Guard.IsLessThanOrEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsLessThanOrEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsLessThanOrEqualTo_False() + { + Guard.IsLessThanOrEqualTo(2, 1, nameof(Test_Guard_IsLessThanOrEqualTo_False)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsGreaterThan_Ok() + { + Guard.IsGreaterThan(2, 1, nameof(Test_Guard_IsGreaterThan_Ok)); + Guard.IsGreaterThan(3.14f, 2.1f, nameof(Test_Guard_IsGreaterThan_Ok)); + Guard.IsGreaterThan(DateTime.MaxValue, DateTime.Today, nameof(Test_Guard_IsGreaterThan_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsGreaterThan_EqualsFalse() + { + Guard.IsGreaterThan(1, 1, nameof(Test_Guard_IsGreaterThan_EqualsFalse)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsGreaterThan_LowerFalse() + { + Guard.IsGreaterThan(1, 2, nameof(Test_Guard_IsGreaterThan_LowerFalse)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsGreaterThanOrEqualTo_Ok() + { + Guard.IsGreaterThanOrEqualTo(2,1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + Guard.IsGreaterThanOrEqualTo(1, 1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + Guard.IsGreaterThanOrEqualTo(MathF.PI, 1, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + Guard.IsGreaterThanOrEqualTo(MathF.PI, MathF.PI, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + Guard.IsGreaterThanOrEqualTo(DateTime.MaxValue, DateTime.Today, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + Guard.IsGreaterThanOrEqualTo(DateTime.MaxValue, DateTime.MaxValue, nameof(Test_Guard_IsGreaterThanOrEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsGreaterThanOrEqualTo_False() + { + Guard.IsGreaterThanOrEqualTo(1, 2, nameof(Test_Guard_IsGreaterThanOrEqualTo_False)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsInRange_Ok() + { + Guard.IsInRange(1, 0, 4, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(0, 0, 2, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(3.14f, 0, 10, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(1, 0, 3.14f, nameof(Test_Guard_IsInRange_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRange_LowerFail() + { + Guard.IsInRange(-3, 0, 4, nameof(Test_Guard_IsInRange_LowerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRange_EqualFail() + { + Guard.IsInRange(0, 4, 4, nameof(Test_Guard_IsInRange_EqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRange_HigherFail() + { + Guard.IsInRange(0, 20, 4, nameof(Test_Guard_IsInRange_HigherFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotInRange_Ok() + { + Guard.IsNotInRange(0, 4, 10, nameof(Test_Guard_IsNotInRange_Ok)); + Guard.IsNotInRange(-4, 0, 2, nameof(Test_Guard_IsNotInRange_Ok)); + Guard.IsNotInRange(12f, 0, 10, nameof(Test_Guard_IsNotInRange_Ok)); + Guard.IsNotInRange(-1, 0, 3.14f, nameof(Test_Guard_IsNotInRange_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotInRange_LowerEqualFail() + { + Guard.IsNotInRange(0, 0, 4, nameof(Test_Guard_IsNotInRange_LowerEqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotInRange_InnerFail() + { + Guard.IsNotInRange(2, 0, 4, nameof(Test_Guard_IsNotInRange_InnerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsBetween_Ok() + { + Guard.IsBetween(1, 0, 4, nameof(Test_Guard_IsBetween_Ok)); + Guard.IsBetween(3.14f, 0, 10, nameof(Test_Guard_IsBetween_Ok)); + Guard.IsBetween(1, 0, 3.14, nameof(Test_Guard_IsBetween_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsBetween_LowerFail() + { + Guard.IsBetween(-1, 0, 4, nameof(Test_Guard_IsBetween_LowerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsBetween_EqualFail() + { + Guard.IsBetween(0, 0, 4, nameof(Test_Guard_IsBetween_EqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsBetween_HigherFail() + { + Guard.IsBetween(6, 0, 4, nameof(Test_Guard_IsBetween_HigherFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotBetween_Ok() + { + Guard.IsNotBetween(0, 0, 4, nameof(Test_Guard_IsNotBetween_Ok)); + Guard.IsNotBetween(10, 0, 10, nameof(Test_Guard_IsNotBetween_Ok)); + Guard.IsNotBetween(-5, 0, 3.14, nameof(Test_Guard_IsNotBetween_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotBetween_Fail() + { + Guard.IsNotBetween(1, 0, 4, nameof(Test_Guard_IsNotBetween_Fail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsBetweenOrEqualTo_Ok() + { + Guard.IsBetweenOrEqualTo(1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_Ok)); + Guard.IsBetweenOrEqualTo(10, 0, 10, nameof(Test_Guard_IsBetweenOrEqualTo_Ok)); + Guard.IsBetweenOrEqualTo(1, 0, 3.14, nameof(Test_Guard_IsBetweenOrEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsBetweenOrEqualTo_LowerFail() + { + Guard.IsBetweenOrEqualTo(-1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_LowerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsBetweenOrEqualTo_HigherFail() + { + Guard.IsBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_HigherFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotBetweenOrEqualTo_Ok() + { + Guard.IsNotBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Ok)); + Guard.IsNotBetweenOrEqualTo(-10, 0, 10, nameof(Test_Guard_IsNotBetweenOrEqualTo_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotBetweenOrEqualTo_Fail() + { + Guard.IsNotBetweenOrEqualTo(3, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Fail)); + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 7a5e254f8bc..68f3df388ef 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -132,6 +132,7 @@ + From 606cbdee62da161fa0c6d4298978069cc4ec16cb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:27:27 +0100 Subject: [PATCH 038/123] Bug fixes in the Guard class --- Microsoft.Toolkit/Diagnostics/Guard.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 6f541de49a6..1cc46599dad 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -452,7 +452,7 @@ public static void IsInRange(T value, T minimum, T maximum, string name) public static void IsNotInRange(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) < 0) + if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) < 0) { ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or >= {maximum}, was {value}"); } @@ -492,7 +492,7 @@ public static void IsBetween(T value, T minimum, T maximum, string name) public static void IsNotBetween(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - if (value.CompareTo(minimum) > 0 || value.CompareTo(maximum) < 0) + if (value.CompareTo(minimum) > 0 && value.CompareTo(maximum) < 0) { ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} or >= {maximum}, was {value}"); } @@ -532,7 +532,7 @@ public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string n public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - if (value.CompareTo(minimum) >= 0 || value.CompareTo(maximum) <= 0) + if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) <= 0) { ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or > {maximum}, was {value}"); } From f29cd93ee5d00d04c12bc65f8641437b1c10c4e1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:31:57 +0100 Subject: [PATCH 039/123] Minor speed improvements --- .../Diagnostics/Guard.Enumerable.cs | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 5f39612965b..57b2d2b4e2d 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -26,7 +26,10 @@ public static partial class Guard [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsEmpty(T[] array, string name) { - IsEmpty(new ReadOnlySpan(array), name); + if (array.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); + } } /// @@ -39,7 +42,10 @@ public static void IsEmpty(T[] array, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotEmpty(T[] array, string name) { - IsNotEmpty(new ReadOnlySpan(array), name); + if (array.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } } /// @@ -53,7 +59,10 @@ public static void IsNotEmpty(T[] array, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(T[] array, int size, string name) { - HasSizeEqualTo(new ReadOnlySpan(array), size, name); + if (array.Length != size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); + } } /// @@ -67,7 +76,10 @@ public static void HasSizeEqualTo(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeNotEqualTo(T[] array, int size, string name) { - HasSizeNotEqualTo(new ReadOnlySpan(array), size, name); + if (array.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); + } } /// @@ -81,7 +93,10 @@ public static void HasSizeNotEqualTo(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(T[] array, int size, string name) { - HasSizeAtLeast(new ReadOnlySpan(array), size, name); + if (array.Length <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); + } } /// @@ -95,7 +110,10 @@ public static void HasSizeAtLeast(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) { - HasSizeAtLeastOrEqualTo(new ReadOnlySpan(array), size, name); + if (array.Length < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); + } } /// @@ -109,7 +127,10 @@ public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThan(T[] array, int size, string name) { - HasSizeLessThan(new ReadOnlySpan(array), size, name); + if (array.Length >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); + } } /// @@ -123,7 +144,10 @@ public static void HasSizeLessThan(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) { - HasSizeLessThanOrEqualTo(new ReadOnlySpan(array), size, name); + if (array.Length > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); + } } /// @@ -137,7 +161,10 @@ public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(T[] source, T[] destination, string name) { - HasSizeEqualTo(new ReadOnlySpan(source), destination.AsSpan(), name); + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } } /// @@ -151,7 +178,10 @@ public static void HasSizeEqualTo(T[] source, T[] destination, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) { - HasSizeLessThanOrEqualTo(new ReadOnlySpan(source), destination.AsSpan(), name); + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } } /// From 79d2dd41be58a7aed390d20b3b30d95b4d688887 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:32:45 +0100 Subject: [PATCH 040/123] Code refactoring --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 187 ++++++++++++++++++ .../Diagnostics/Guard.Enumerable.cs | 168 ---------------- 2 files changed, 187 insertions(+), 168 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Array.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs new file mode 100644 index 00000000000..f5984649d64 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -0,0 +1,187 @@ +// 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.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input array instance must be empty. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(T[] array, string name) + { + if (array.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must not be empty. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(T[] array, string name) + { + if (array.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + + /// + /// Asserts that the input array instance must have a size of a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] array, int size, string name) + { + if (array.Length != size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must have a size not equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(T[] array, int size, string name) + { + if (array.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must have a size of at least specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(T[] array, int size, string name) + { + if (array.Length <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must have a size of at least or equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) + { + if (array.Length < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must have a size of less than a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(T[] array, int size, string name) + { + if (array.Length >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the input array instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + if (array.Length > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] source, T[] destination, string name) + { + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 57b2d2b4e2d..efeae84e563 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -16,174 +16,6 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { - /// - /// Asserts that the input array instance must be empty. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(T[] array, string name) - { - if (array.Length != 0) - { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must not be empty. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(T[] array, string name) - { - if (array.Length != 0) - { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); - } - } - - /// - /// Asserts that the input array instance must have a size of a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(T[] array, int size, string name) - { - if (array.Length != size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must have a size not equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(T[] array, int size, string name) - { - if (array.Length == size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must have a size of at least specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(T[] array, int size, string name) - { - if (array.Length <= size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must have a size of at least or equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) - { - if (array.Length < size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must have a size of less than a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(T[] array, int size, string name) - { - if (array.Length >= size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the input array instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) - { - if (array.Length > size) - { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(T[] source, T[] destination, string name) - { - if (source.Length != destination.Length) - { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) - { - if (source.Length > destination.Length) - { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); - } - } - /// /// Asserts that the input instance must be empty. /// From 793c769f4e8b99420eaddd4f183fdbf346fdafc7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:33:00 +0100 Subject: [PATCH 041/123] Removed unnecessary using directives --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index f5984649d64..fc1eaf2b3b0 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -3,8 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; #nullable enable From 8e2f2d8d8e8529fa859569ffe3976f95d403895f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 22:44:43 +0100 Subject: [PATCH 042/123] More speed improvements and API refactoring --- .../Diagnostics/Guard.Enumerable.cs | 307 +++++++++++++----- 1 file changed, 226 insertions(+), 81 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index efeae84e563..830edb8a03c 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -16,6 +16,38 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ICollection collection, string name) + { + if (collection.Count != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(IReadOnlyCollection collection, string name) + { + if (collection.Count != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -26,29 +58,41 @@ public static partial class Guard [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsEmpty(IEnumerable enumerable, string name) { - if ((enumerable as ICollection)?.Count is int collectionCount) + if (!enumerable.Any()) { - if (collectionCount != 0) - { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collectionCount}"); - - return; - } + ThrowArgumentException(name, $"Parameter {name} must be empty"); } + } - if ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount) + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ICollection collection, string name) + { + if (collection.Count == 0) { - if (readOnlyCollectionCount != 0) - { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {readOnlyCollectionCount}"); - - return; - } + ThrowArgumentException(name, $"Parameter {name} must not be empty"); } + } - if (enumerable.Any()) + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(IReadOnlyCollection collection, string name) + { + if (collection.Count == 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty"); + ThrowArgumentException(name, $"Parameter {name} must not be empty"); } } @@ -62,29 +106,43 @@ public static void IsEmpty(IEnumerable enumerable, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotEmpty(IEnumerable enumerable, string name) { - if ((enumerable as ICollection)?.Count is int collectionCount) + if (enumerable.Any()) { - if (collectionCount == 0) - { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); - - return; - } + ThrowArgumentException(name, $"Parameter {name} must not be empty"); } + } - if ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount) + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ICollection collection, int size, string name) + { + if (collection.Count != size) { - if (readOnlyCollectionCount == 0) - { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); - - return; - } + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); } + } - if (enumerable.Any()) + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count != size) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); } } @@ -96,22 +154,51 @@ public static void IsNotEmpty(IEnumerable enumerable, string name) /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is != . - /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize; + int actualSize = enumerable.Count(); - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) != size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) != size) || - (actualSize = enumerable.Count()) != size) + if (actualSize != size) { ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {actualSize}"); } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ICollection collection, int size, string name) + { + if (collection.Count == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + } + } + /// /// Asserts that the input instance must have a size not equal to a specified value. /// @@ -120,22 +207,51 @@ public static void HasSizeEqualTo(IEnumerable enumerable, int size, string /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is == . - /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize; + int actualSize = enumerable.Count(); - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) == size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) == size) || - (actualSize = enumerable.Count()) == size) + if (actualSize == size) { ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {actualSize}"); } } + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ICollection collection, int size, string name) + { + if (collection.Count < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// @@ -144,43 +260,48 @@ public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, str /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is <= . - /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) { - int actualSize; + int actualSize = enumerable.Count(); - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) <= size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) <= size) || - (actualSize = enumerable.Count()) <= size) + if (actualSize < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); } } /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// Asserts that the input instance must have a size of less than a specified value. /// - /// The type of items in the input instance. - /// The input instance to check the size for. + /// The type of items in the input instance. + /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. - /// Thrown if the size of is < . - /// The method will skip enumerating if possible (if it's an or ). + /// Thrown if the size of is >= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int size, string name) + public static void HasSizeLessThan(ICollection collection, int size, string name) { - int actualSize; + if (collection.Count >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + } + } - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) < size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) < size) || - (actualSize = enumerable.Count()) < size) + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); } } @@ -192,22 +313,51 @@ public static void HasSizeAtLeastOrEqualTo(IEnumerable enumerable, int siz /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is >= . - /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) { - int actualSize; + int actualSize = enumerable.Count(); - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) >= size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) >= size) || - (actualSize = enumerable.Count()) >= size) + if (actualSize >= size) { ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {actualSize}"); } } + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ICollection collection, int size, string name) + { + if (collection.Count > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + } + } + /// /// Asserts that the input instance must have a size of less than or equal to a specified value. /// @@ -216,17 +366,12 @@ public static void HasSizeLessThan(IEnumerable enumerable, int size, strin /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is > . - /// The method will skip enumerating if possible (if it's an or ). [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize; + int actualSize = enumerable.Count(); - if (((enumerable as ICollection)?.Count is int collectionCount && - (actualSize = collectionCount) > size) || - ((enumerable as IReadOnlyCollection)?.Count is int readOnlyCollectionCount && - (actualSize = readOnlyCollectionCount) > size) || - (actualSize = enumerable.Count()) > size) + if (actualSize > size) { ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); } From 5b58e5ee9c12a2627430cba5aa4b68d210e0be73 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 18 Feb 2020 23:52:53 +0100 Subject: [PATCH 043/123] Refactored/fixed some Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 4 +- .../Diagnostics/Guard.Enumerable.cs | 59 ++++++++++++++++++- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 18 +++--- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index fc1eaf2b3b0..e95c9ae09ab 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -81,7 +81,7 @@ public static void HasSizeNotEqualTo(T[] array, int size, string name) } /// - /// Asserts that the input array instance must have a size of at least specified value. + /// Asserts that the input array instance must have a size over a specified value. /// /// The type of items in the input array instance. /// The input array instance to check the size for. @@ -89,7 +89,7 @@ public static void HasSizeNotEqualTo(T[] array, int size, string name) /// The name of the input parameter being tested. /// Thrown if the size of is <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(T[] array, int size, string name) + public static void HasSizeOver(T[] array, int size, string name) { if (array.Length <= size) { diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index 830edb8a03c..e06f95a30e4 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -219,7 +219,7 @@ public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, str } /// - /// Asserts that the input instance must have a size of at least specified value. + /// Asserts that the input instance must have a size over a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -227,6 +227,59 @@ public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, str /// The name of the input parameter being tested. /// Thrown if the size of is <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(ICollection collection, int size, string name) + { + if (collection.Count <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(IEnumerable enumerable, int size, string name) + { + int actualSize = enumerable.Count(); + + if (actualSize <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(ICollection collection, int size, string name) { if (collection.Count < size) @@ -242,7 +295,7 @@ public static void HasSizeAtLeast(ICollection collection, int size, string /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. - /// Thrown if the size of is <= . + /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(IReadOnlyCollection collection, int size, string name) { @@ -259,7 +312,7 @@ public static void HasSizeAtLeast(IReadOnlyCollection collection, int size /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. - /// Thrown if the size of is <= . + /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) { diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index dc6b198d55b..bcd6ee58f41 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -81,7 +81,7 @@ public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string n } /// - /// Asserts that the input instance must have a size of at least specified value. + /// Asserts that the input instance must have a size over a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -89,16 +89,16 @@ public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string n /// The name of the input parameter being tested. /// Thrown if the size of is <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) + public static void HasSizeOver(ReadOnlySpan span, int size, string name) { if (span.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); } } /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// Asserts that the input instance must have a size of at least specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -106,7 +106,7 @@ public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name /// The name of the input parameter being tested. /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(ReadOnlySpan span, int size, string name) + public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) { if (span.Length < size) { @@ -249,7 +249,7 @@ public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, stri } /// - /// Asserts that the input instance must have a size of at least specified value. + /// Asserts that the input instance must have a size over a specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -257,7 +257,7 @@ public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, stri /// The name of the input parameter being tested. /// Thrown if the size of is <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + public static void HasSizeOver(ReadOnlyMemory memory, int size, string name) { if (memory.Length <= size) { @@ -266,7 +266,7 @@ public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string } /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// Asserts that the input instance must have a size of at least specified value. /// /// The type of items in the input instance. /// The input instance to check the size for. @@ -274,7 +274,7 @@ public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string /// The name of the input parameter being tested. /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(ReadOnlyMemory memory, int size, string name) + public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) { if (memory.Length < size) { From b664c90ab50d3a1f874f6a0cef3f5b4eca2ffb5f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 19 Feb 2020 00:00:48 +0100 Subject: [PATCH 044/123] More bug fixes --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 2 +- Microsoft.Toolkit/Diagnostics/Guard.String.cs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index e95c9ae09ab..be501833e50 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -106,7 +106,7 @@ public static void HasSizeOver(T[] array, int size, string name) /// The name of the input parameter being tested. /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(T[] array, int size, string name) + public static void HasSizeAtLeast(T[] array, int size, string name) { if (array.Length < size) { diff --git a/Microsoft.Toolkit/Diagnostics/Guard.String.cs b/Microsoft.Toolkit/Diagnostics/Guard.String.cs index 7213ec6d93e..2c325b096af 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.String.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.String.cs @@ -161,29 +161,29 @@ public static void HasSizeNotEqualTo(string text, int size, string name) } /// - /// Asserts that the input instance must have a size of at least specified value. + /// Asserts that the input instance must have a size over a specified value. /// /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(string text, int size, string name) + public static void HasSizeOver(string text, int size, string name) { - HasSizeAtLeast(text.AsSpan(), size, name); + HasSizeOver(text.AsSpan(), size, name); } /// - /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// Asserts that the input instance must have a size of at least specified value. /// /// The input instance to check the size for. /// The target size to test. /// The name of the input parameter being tested. /// Thrown if the size of is < . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeastOrEqualTo(string text, int size, string name) + public static void HasSizeAtLeast(string text, int size, string name) { - HasSizeAtLeastOrEqualTo(text.AsSpan(), size, name); + HasSizeAtLeast(text.AsSpan(), size, name); } /// From 7f6518415983b574ce97f9898c94b6bea4e09cdf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 19 Feb 2020 00:02:12 +0100 Subject: [PATCH 045/123] Added Guard tests for array APIs --- UnitTests/Diagnostics/Test_Guard.Array.cs | 182 ++++++++++++++++++++++ UnitTests/Diagnostics/Test_Guard.cs | 2 +- UnitTests/UnitTests.csproj | 1 + 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 UnitTests/Diagnostics/Test_Guard.Array.cs diff --git a/UnitTests/Diagnostics/Test_Guard.Array.cs b/UnitTests/Diagnostics/Test_Guard.Array.cs new file mode 100644 index 00000000000..326cbab049c --- /dev/null +++ b/UnitTests/Diagnostics/Test_Guard.Array.cs @@ -0,0 +1,182 @@ +// 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 Microsoft.Toolkit.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTests.Diagnostics +{ + public partial class Test_Guard + { + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsEmpty_ArrayOk() + { + Guard.IsEmpty(new int[0], nameof(Test_Guard_IsEmpty_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsEmpty_ArrayFail() + { + Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotEmpty_ArrayOk() + { + Guard.IsNotEmpty(new int[1], nameof(Test_Guard_IsNotEmpty_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsNotEmpty_ArrayFail() + { + Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeEqualTo_ArrayOk() + { + Guard.HasSizeEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeEqualTo_ArrayFail() + { + Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeNotEqualTo_ArrayOk() + { + Guard.HasSizeNotEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeNotEqualTo_ArrayFail() + { + Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeOver_ArrayOk() + { + Guard.HasSizeOver(new int[5], 2, nameof(Test_Guard_HasSizeOver_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeOver_ArrayEqualFail() + { + Guard.HasSizeOver(new int[4], 4, nameof(Test_Guard_HasSizeOver_ArrayEqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeOver_ArraSmallerFail() + { + Guard.HasSizeOver(new int[1], 4, nameof(Test_Guard_HasSizeOver_ArraSmallerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeAtLeast_ArrayOk() + { + Guard.HasSizeAtLeast(new int[5], 2, nameof(Test_Guard_HasSizeAtLeast_ArrayOk)); + Guard.HasSizeAtLeast(new int[2], 2, nameof(Test_Guard_HasSizeAtLeast_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeAtLeast_ArrayFail() + { + Guard.HasSizeOver(new int[1], 4, nameof(Test_Guard_HasSizeAtLeast_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeLessThan_ArrayOk() + { + Guard.HasSizeLessThan(new int[1], 5, nameof(Test_Guard_HasSizeLessThan_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeLessThan_ArrayEqualFail() + { + Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeLessThan_ArrayGreaterFail() + { + Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk() + { + Guard.HasSizeLessThanOrEqualTo(new int[1], 5, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk)); + Guard.HasSizeLessThanOrEqualTo(new int[5], 5, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail() + { + Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeEqualToArray_ArrayOk() + { + Guard.HasSizeEqualTo(new int[1], new int[1], nameof(Test_Guard_HasSizeEqualToArray_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeEqualToArray_ArrayFail() + { + Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail)); + } + + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk() + { + Guard.HasSizeLessThanOrEqualTo(new int[2], new int[5], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk)); + Guard.HasSizeLessThanOrEqualTo(new int[4], new int[4], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail() + { + Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail)); + } + } +} diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs index 887a4065798..8bf9a58b55a 100644 --- a/UnitTests/Diagnostics/Test_Guard.cs +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -9,7 +9,7 @@ namespace UnitTests.Diagnostics { [TestClass] - public class Test_Guard + public partial class Test_Guard { [TestCategory("Guard")] [TestMethod] diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 68f3df388ef..60d36b809a9 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -132,6 +132,7 @@ + From 4304edd6271630c6cbc3c58d42127d5ea3ca7a5c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 19 Feb 2020 00:02:24 +0100 Subject: [PATCH 046/123] Fixed the Guard.IsNotEmpty array API --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index be501833e50..442d2e0fd3d 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -40,7 +40,7 @@ public static void IsEmpty(T[] array, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotEmpty(T[] array, string name) { - if (array.Length != 0) + if (array.Length == 0) { ThrowArgumentException(name, $"Parameter {name} must not be empty"); } From 4802936e0168f50682fd0a98cf88a15df4340224 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 18:44:20 +0100 Subject: [PATCH 047/123] Moved exception throwers to separate class --- .../Diagnostics/ThrowHelper.Exceptions.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs new file mode 100644 index 00000000000..762bf5b1128 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + internal static partial class ThrowHelper + { + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ThrowArgumentException(string name, string message) + { + throw new ArgumentException(message, name); + } + + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ThrowArgumentNullException(string name, string message) + { + throw new ArgumentNullException(name, message); + } + + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with and . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ThrowArgumentOutOfRangeException(string name, string message) + { + throw new ArgumentOutOfRangeException(name, message); + } + } +} From ed5315a99c8144af36b0c27138d1b263c93d19a7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 18:54:19 +0100 Subject: [PATCH 048/123] Moved general Guard throwers to separate class --- .../Diagnostics/Guard.ThrowExceptions.cs | 6 +- Microsoft.Toolkit/Diagnostics/Guard.cs | 64 ++--- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 249 ++++++++++++++++++ 3 files changed, 284 insertions(+), 35 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs b/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs index 53a4eef237b..c56181021de 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs @@ -20,7 +20,7 @@ public static partial class Guard /// The argument name. /// The message to include in the exception. /// Thrown with and . - /// This method is marked as to preserve the stack trace to this location. + /// This method is marked as to reduce the binary size of the caller. [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentException(string name, string message) { @@ -33,7 +33,7 @@ private static void ThrowArgumentException(string name, string message) /// The argument name. /// The message to include in the exception. /// Thrown with and . - /// This method is marked as to preserve the stack trace to this location. + /// This method is marked as to reduce the binary size of the caller. [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentNullException(string name, string message) { @@ -46,7 +46,7 @@ private static void ThrowArgumentNullException(string name, string message) /// The argument name. /// The message to include in the exception. /// Thrown with and . - /// This method is marked as to preserve the stack trace to this location. + /// This method is marked as to reduce the binary size of the caller. [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowArgumentOutOfRangeException(string name, string message) { diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 1cc46599dad..2db43ac14b7 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -5,7 +5,6 @@ using System; using System.Diagnostics; using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; #nullable enable @@ -23,14 +22,14 @@ public static partial class Guard /// The type of reference value type being tested. /// The input value to test. /// The name of the input parameter being tested. - /// Thrown if is not . + /// Thrown if is not . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNull(T? value, string name) where T : class { if (value != null) { - ThrowArgumentNullException(name, $"Parameter {name} must be null"); + ThrowHelper.ThrowArgumentExceptionForIsNull(value, name); } } @@ -40,7 +39,7 @@ public static void IsNull(T? value, string name) /// The type of nullable value type being tested. /// The input value to test. /// The name of the input parameter being tested. - /// Thrown if is not . + /// Thrown if is not . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNull(T? value, string name) @@ -48,7 +47,7 @@ public static void IsNull(T? value, string name) { if (value != null) { - ThrowArgumentNullException(name, $"Parameter {name} must be null"); + ThrowHelper.ThrowArgumentExceptionForIsNull(value, name); } } @@ -65,7 +64,7 @@ public static void IsNotNull(T? value, string name) { if (value is null) { - ThrowArgumentNullException(name, $"Parameter {name} must be not null"); + ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); } } @@ -83,7 +82,7 @@ public static void IsNotNull(T? value, string name) { if (value is null) { - ThrowArgumentNullException(name, $"Parameter {name} must be not null"); + ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); } } @@ -99,7 +98,7 @@ public static void IsOfType(object value, string name) { if (value.GetType() != typeof(T)) { - ThrowArgumentException(name, $"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}"); + ThrowHelper.ThrowArgumentExceptionForIsOfType(value, name); } } @@ -115,7 +114,7 @@ public static void IsOfType(object value, Type type, string name) { if (value.GetType() != type) { - ThrowArgumentException(name, $"Parameter {name} must be of type {type}, was {value.GetType()}"); + ThrowHelper.ThrowArgumentExceptionForIsOfType(value, type, name); } } @@ -131,7 +130,7 @@ public static void IsAssignableToType(object value, string name) { if (!(value is T)) { - ThrowArgumentException(name, $"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}"); + ThrowHelper.ThrowArgumentExceptionForIsAssignableToType(value, name); } } @@ -147,7 +146,7 @@ public static void IsAssignableToType(object value, Type type, string name) { if (!type.IsInstanceOfType(value)) { - ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); + ThrowHelper.ThrowArgumentExceptionForIsAssignableToType(value, type, name); } } @@ -166,7 +165,7 @@ public static void IsEqualTo(T value, T target, string name) { if (!value.Equals(target)) { - ThrowArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); } } @@ -185,7 +184,7 @@ public static void IsNotEqualTo(T value, T target, string name) { if (value.Equals(target)) { - ThrowArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); } } @@ -197,6 +196,7 @@ public static void IsNotEqualTo(T value, T target, string name) /// The target value to test for. /// The name of the input parameter being tested. /// Thrown if is not a bitwise match for . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { @@ -216,7 +216,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueByte != targetByte) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); } } else if (typeof(T) == typeof(ushort) || @@ -228,7 +228,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueUShort != targetUShort) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); } } else if (typeof(T) == typeof(uint) || @@ -240,7 +240,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueUInt != targetUInt) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); } } else if (typeof(T) == typeof(ulong) || @@ -252,7 +252,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueULong != targetULong) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); } } else @@ -268,7 +268,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) if (valueByte != targetByte) { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); } } } @@ -289,7 +289,7 @@ public static void IsReferenceEqualTo(T value, T target, string name) { if (!ReferenceEquals(value, target)) { - ThrowArgumentException(name, $"Parameter {name} must be the same instance as the target object"); + ThrowHelper.ThrowArgumentExceptionForIsReferenceEqualTo(value, target, name); } } @@ -308,7 +308,7 @@ public static void IsReferenceNotEqualTo(T value, T target, string name) { if (ReferenceEquals(value, target)) { - ThrowArgumentException(name, $"Parameter {name} must not be the same instance as the target object"); + ThrowHelper.ThrowArgumentExceptionForIsReferenceNotEqualTo(value, target, name); } } @@ -323,7 +323,7 @@ public static void IsTrue(bool value, string name) { if (!value) { - ThrowArgumentException(name, $"Parameter {name} must be true, was false"); + ThrowHelper.ThrowArgumentExceptionForIsTrue(name); } } @@ -338,7 +338,7 @@ public static void IsFalse(bool value, string name) { if (value) { - ThrowArgumentException(name, $"Parameter {name} must be false, was true"); + ThrowHelper.ThrowArgumentExceptionForIsFalse(name); } } @@ -357,7 +357,7 @@ public static void IsLessThan(T value, T max, string name) { if (value.CompareTo(max) >= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, max, name); } } @@ -376,7 +376,7 @@ public static void IsLessThanOrEqualTo(T value, T maximum, string name) { if (value.CompareTo(maximum) > 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); } } @@ -395,7 +395,7 @@ public static void IsGreaterThan(T value, T minimum, string name) { if (value.CompareTo(minimum) <= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); } } @@ -414,7 +414,7 @@ public static void IsGreaterThanOrEqualTo(T value, T minimum, string name) { if (value.CompareTo(minimum) < 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); } } @@ -434,7 +434,7 @@ public static void IsInRange(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) >= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and < {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name); } } @@ -454,7 +454,7 @@ public static void IsNotInRange(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) < 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or >= {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name); } } @@ -474,7 +474,7 @@ public static void IsBetween(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name); } } @@ -494,7 +494,7 @@ public static void IsNotBetween(T value, T minimum, T maximum, string name) { if (value.CompareTo(minimum) > 0 && value.CompareTo(maximum) < 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} or >= {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name); } } @@ -514,7 +514,7 @@ public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string n { if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); } } @@ -534,7 +534,7 @@ public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, strin { if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) <= 0) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or > {maximum}, was {value}"); + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs new file mode 100644 index 00000000000..12daad65521 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -0,0 +1,249 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNull(T? value, string name) + where T : class + { + ThrowArgumentException(name, $"Parameter {name} must be null, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNull(T? value, string name) + where T : struct + { + ThrowArgumentException(name, $"Parameter {name} must be null, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentNullExceptionForIsNotNull(string name) + { + ThrowArgumentNullException(name, $"Parameter {name} must be not null"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsOfType(object value, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be of type {type}, was {value.GetType()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsAssignableToType(object value, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + ThrowArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + ThrowArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForsBitwiseEqualTo(T value, T target, string name) + where T : unmanaged + { + ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsReferenceEqualTo(T value, T target, string name) + where T : class + { + ThrowArgumentException(name, $"Parameter {name} must be the same instance as the target object"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(T value, T target, string name) + where T : class + { + ThrowArgumentException(name, $"Parameter {name} must not be the same instance as the target object"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsTrue(string name) + { + ThrowArgumentException(name, $"Parameter {name} must be true, was false"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsFalse(string name) + { + ThrowArgumentException(name, $"Parameter {name} must be false, was true"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and < {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or >= {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} or >= {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or > {maximum}, was {value}"); + } + } +} From 1772a0f398d230b71830bf30dc25a5176f18b626 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 18:57:03 +0100 Subject: [PATCH 049/123] Disabled warning for XML overload resolution --- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 12daad65521..43553f6c1e1 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -13,6 +13,7 @@ namespace Microsoft.Toolkit.Diagnostics [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] internal static partial class ThrowHelper { +#pragma warning disable CS0419 /// /// Throws an when fails. /// @@ -41,6 +42,7 @@ public static void ThrowArgumentNullExceptionForIsNotNull(string name) { ThrowArgumentNullException(name, $"Parameter {name} must be not null"); } +#pragma warning restore CS0419 /// /// Throws an when fails. From 9814d9109d64d368d8ad506cf0eebb4777c44ee1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 19:09:41 +0100 Subject: [PATCH 050/123] Moved array Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 20 ++-- .../Diagnostics/ThrowHelper.Array.cs | 105 ++++++++++++++++++ 2 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index 442d2e0fd3d..0e0ac7d64e2 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -26,7 +26,7 @@ public static void IsEmpty(T[] array, string name) { if (array.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(array, name); } } @@ -42,7 +42,7 @@ public static void IsNotEmpty(T[] array, string name) { if (array.Length == 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(array, name); } } @@ -59,7 +59,7 @@ public static void HasSizeEqualTo(T[] array, int size, string name) { if (array.Length != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(array, size, name); } } @@ -76,7 +76,7 @@ public static void HasSizeNotEqualTo(T[] array, int size, string name) { if (array.Length == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(array, size, name); } } @@ -93,7 +93,7 @@ public static void HasSizeOver(T[] array, int size, string name) { if (array.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(array, size, name); } } @@ -110,7 +110,7 @@ public static void HasSizeAtLeast(T[] array, int size, string name) { if (array.Length < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(array, size, name); } } @@ -127,7 +127,7 @@ public static void HasSizeLessThan(T[] array, int size, string name) { if (array.Length >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(array, size, name); } } @@ -144,7 +144,7 @@ public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) { if (array.Length > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(array, size, name); } } @@ -161,7 +161,7 @@ public static void HasSizeEqualTo(T[] source, T[] destination, string name) { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); } } @@ -178,7 +178,7 @@ public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, stri { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs new file mode 100644 index 00000000000..46566aec005 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs @@ -0,0 +1,105 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(T[] array, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + } +} From 1b78344a8cfc96dfee8ef07bd1c44cb368320e26 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 19:13:35 +0100 Subject: [PATCH 051/123] Moved stream Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.IO.cs | 8 +-- .../Diagnostics/ThrowHelper.IO.cs | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs index 49ba55ea006..06228077370 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs @@ -26,7 +26,7 @@ public static void CanRead(Stream stream, string name) { if (!stream.CanRead) { - ThrowArgumentException(name, $"Stream {name} doesn't support reading"); + ThrowHelper.ThrowArgumentExceptionForCanRead(name); } } @@ -41,7 +41,7 @@ public static void CanWrite(Stream stream, string name) { if (!stream.CanWrite) { - ThrowArgumentException(name, $"Stream {name} doesn't support writing"); + ThrowHelper.ThrowArgumentExceptionForCanWrite(name); } } @@ -56,7 +56,7 @@ public static void CanSeek(Stream stream, string name) { if (!stream.CanSeek) { - ThrowArgumentException(name, $"Stream {name} doesn't support seeking"); + ThrowHelper.ThrowArgumentExceptionForCanSeek(name); } } @@ -71,7 +71,7 @@ public static void IsAtStartPosition(Stream stream, string name) { if (stream.Position != 0) { - ThrowArgumentException(name, $"Stream {name} must be at start position, was at {stream.Position}"); + ThrowHelper.ThrowArgumentExceptionForIsAtStartPosition(stream, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs new file mode 100644 index 00000000000..37ee27011d2 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForCanRead(string name) + { + ThrowArgumentException(name, $"Stream {name} doesn't support reading"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForCanWrite(string name) + { + ThrowArgumentException(name, $"Stream {name} doesn't support writing"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForCanSeek(string name) + { + ThrowArgumentException(name, $"Stream {name} doesn't support seeking"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name) + { + ThrowArgumentException(name, $"Stream {name} must be at start position, was at {stream.Position}"); + } + } +} From bc0dc1851c03f0cf3ed5909836b3f24174693276 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 19:15:29 +0100 Subject: [PATCH 052/123] Fixed some XML docs --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 16 ++++++++-------- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index 0e0ac7d64e2..c7f4f413341 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -149,11 +149,11 @@ public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) } /// - /// Asserts that the source instance must have the same size of a destination instance. + /// Asserts that the source array instance must have the same size of a destination array instance. /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. + /// The type of items in the input array instance. + /// The source array instance to check the size for. + /// The destination array instance to check the size for. /// The name of the input parameter being tested. /// Thrown if the size of is != the one of . [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -166,11 +166,11 @@ public static void HasSizeEqualTo(T[] source, T[] destination, string name) } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination array instance. /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. + /// The type of items in the input array instance. + /// The source array instance to check the size for. + /// The destination array instance to check the size for. /// The name of the input parameter being tested. /// Thrown if the size of is > the one of . [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index bcd6ee58f41..2204768b99d 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -334,7 +334,7 @@ public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destina } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The type of items in the input instance. /// The source instance to check the size for. From e000fe053d5284b9df1bc0eb5074b18b76df93c9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 19:56:31 +0100 Subject: [PATCH 053/123] Moved enumerable Guard throwers to separate class --- .../Diagnostics/Guard.Enumerable.cs | 72 +++---- .../Diagnostics/ThrowHelper.Enumerable.cs | 197 ++++++++++++++++++ 2 files changed, 227 insertions(+), 42 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index e06f95a30e4..c17b162ee87 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -28,7 +28,7 @@ public static void IsEmpty(ICollection collection, string name) { if (collection.Count != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); } } @@ -44,7 +44,7 @@ public static void IsEmpty(IReadOnlyCollection collection, string name) { if (collection.Count != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); } } @@ -60,7 +60,7 @@ public static void IsEmpty(IEnumerable enumerable, string name) { if (!enumerable.Any()) { - ThrowArgumentException(name, $"Parameter {name} must be empty"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(enumerable, name); } } @@ -76,7 +76,7 @@ public static void IsNotEmpty(ICollection collection, string name) { if (collection.Count == 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); } } @@ -92,7 +92,7 @@ public static void IsNotEmpty(IReadOnlyCollection collection, string name) { if (collection.Count == 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); } } @@ -108,7 +108,7 @@ public static void IsNotEmpty(IEnumerable enumerable, string name) { if (enumerable.Any()) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); } } @@ -125,7 +125,7 @@ public static void HasSizeEqualTo(ICollection collection, int size, string { if (collection.Count != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); } } @@ -142,7 +142,7 @@ public static void HasSizeEqualTo(IReadOnlyCollection collection, int size { if (collection.Count != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); } } @@ -157,11 +157,9 @@ public static void HasSizeEqualTo(IReadOnlyCollection collection, int size [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize != size) + if (enumerable.Count() != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(enumerable, size, name); } } @@ -178,7 +176,7 @@ public static void HasSizeNotEqualTo(ICollection collection, int size, str { if (collection.Count == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); } } @@ -195,7 +193,7 @@ public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int s { if (collection.Count == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); } } @@ -210,11 +208,9 @@ public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int s [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize == size) + if (enumerable.Count() == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); } } @@ -231,7 +227,7 @@ public static void HasSizeOver(ICollection collection, int size, string na { if (collection.Count <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); } } @@ -248,7 +244,7 @@ public static void HasSizeOver(IReadOnlyCollection collection, int size, s { if (collection.Count <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); } } @@ -263,11 +259,9 @@ public static void HasSizeOver(IReadOnlyCollection collection, int size, s [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeOver(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize <= size) + if (enumerable.Count() <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(enumerable, size, name); } } @@ -284,7 +278,7 @@ public static void HasSizeAtLeast(ICollection collection, int size, string { if (collection.Count < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); } } @@ -301,7 +295,7 @@ public static void HasSizeAtLeast(IReadOnlyCollection collection, int size { if (collection.Count < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); } } @@ -316,11 +310,9 @@ public static void HasSizeAtLeast(IReadOnlyCollection collection, int size [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize < size) + if (enumerable.Count() < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(enumerable, size, name); } } @@ -337,7 +329,7 @@ public static void HasSizeLessThan(ICollection collection, int size, strin { if (collection.Count >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); } } @@ -354,7 +346,7 @@ public static void HasSizeLessThan(IReadOnlyCollection collection, int siz { if (collection.Count >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); } } @@ -369,11 +361,9 @@ public static void HasSizeLessThan(IReadOnlyCollection collection, int siz [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize >= size) + if (enumerable.Count() >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(enumerable, size, name); } } @@ -390,7 +380,7 @@ public static void HasSizeLessThanOrEqualTo(ICollection collection, int si { if (collection.Count > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); } } @@ -407,7 +397,7 @@ public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection collection { if (collection.Count > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); } } @@ -422,11 +412,9 @@ public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection collection [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) { - int actualSize = enumerable.Count(); - - if (actualSize > size) + if (enumerable.Count() > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {actualSize}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(enumerable, size, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs new file mode 100644 index 00000000000..83a220250fb --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs @@ -0,0 +1,197 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(IEnumerable enumerable, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when , or fail. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IEnumerable enumerable, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when , or fail. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(IEnumerable enumerable, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(IEnumerable enumerable, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(IEnumerable enumerable, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {enumerable.Count()}"); + } + } +} From 21cf16a90ca53d0b66df7396eccadecdadb0b319 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 20:28:19 +0100 Subject: [PATCH 054/123] Moved task Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs | 10 +-- .../Diagnostics/ThrowHelper.Tasks.cs | 61 +++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs index f200bd2788d..79446de6be6 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs @@ -26,7 +26,7 @@ public static void IsCompleted(Task task, string name) { if (!task.IsCompleted) { - ThrowArgumentException(name, $"Parameter {name} must be completed, had status {task.Status}"); + ThrowHelper.ThrowArgumentExceptionForIsCompleted(task, name); } } @@ -41,7 +41,7 @@ public static void IsCompletedSuccessfully(Task task, string name) { if (task.Status != TaskStatus.RanToCompletion) { - ThrowArgumentException(name, $"Parameter {name} must be completed successfully, had status {task.Status}"); + ThrowHelper.ThrowArgumentExceptionForIsCompletedSuccessfully(task, name); } } @@ -56,7 +56,7 @@ public static void IsFaulted(Task task, string name) { if (!task.IsFaulted) { - ThrowArgumentException(name, $"Parameter {name} must be faulted, had status {task.Status}"); + ThrowHelper.ThrowArgumentExceptionForIsFaulted(task, name); } } @@ -71,7 +71,7 @@ public static void IsCanceled(Task task, string name) { if (!task.IsCanceled) { - ThrowArgumentException(name, $"Parameter {name} must be canceled, had status {task.Status}"); + ThrowHelper.ThrowArgumentExceptionForIsCanceled(task, name); } } @@ -87,7 +87,7 @@ public static void HasStatus(Task task, TaskStatus status, string name) { if (task.Status != status) { - ThrowArgumentException(name, $"Parameter {name} must have status {status}, had status {task.Status}"); + ThrowHelper.ThrowArgumentExceptionForHasStatus(task, status, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs new file mode 100644 index 00000000000..0d363bb11e6 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be completed, had status {task.Status}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be completed successfully, had status {task.Status}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be faulted, had status {task.Status}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be canceled, had status {task.Status}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasStatus(Task task, TaskStatus status, string name) + { + ThrowArgumentException(name, $"Parameter {name} must have status {status}, had status {task.Status}"); + } + } +} From c1b9c8b7213d572e68dabb0d197285e93cf81513 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 20:32:59 +0100 Subject: [PATCH 055/123] Added new Guard APIs for tasks --- Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs | 80 ++++++++++++++++++- .../Diagnostics/ThrowHelper.Tasks.cs | 49 +++++++++++- 2 files changed, 125 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs index 79446de6be6..ddcf9eacec7 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs @@ -30,6 +30,21 @@ public static void IsCompleted(Task task, string name) } } + /// + /// Asserts that the input instance is not in a completed state. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is in a completed state. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotCompleted(Task task, string name) + { + if (task.IsCompleted) + { + ThrowHelper.ThrowArgumentExceptionForIsNotCompleted(task, name); + } + } + /// /// Asserts that the input instance has been completed successfully. /// @@ -45,6 +60,21 @@ public static void IsCompletedSuccessfully(Task task, string name) } } + /// + /// Asserts that the input instance has not been completed successfully. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if has been completed successfully. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotCompletedSuccessfully(Task task, string name) + { + if (task.Status == TaskStatus.RanToCompletion) + { + ThrowHelper.ThrowArgumentExceptionForIsNotCompletedSuccessfully(task, name); + } + } + /// /// Asserts that the input instance is faulted. /// @@ -60,6 +90,21 @@ public static void IsFaulted(Task task, string name) } } + /// + /// Asserts that the input instance is not faulted. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is faulted. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotFaulted(Task task, string name) + { + if (task.IsFaulted) + { + ThrowHelper.ThrowArgumentExceptionForIsNotFaulted(task, name); + } + } + /// /// Asserts that the input instance is canceled. /// @@ -75,6 +120,21 @@ public static void IsCanceled(Task task, string name) } } + /// + /// Asserts that the input instance is not canceled. + /// + /// The input instance to test. + /// The name of the input parameter being tested. + /// Thrown if is canceled. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotCanceled(Task task, string name) + { + if (task.IsCanceled) + { + ThrowHelper.ThrowArgumentExceptionForIsNotCanceled(task, name); + } + } + /// /// Asserts that the input instance has a specific status. /// @@ -83,11 +143,27 @@ public static void IsCanceled(Task task, string name) /// The name of the input parameter being tested. /// Thrown if doesn't match . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasStatus(Task task, TaskStatus status, string name) + public static void HasStatusEqualTo(Task task, TaskStatus status, string name) { if (task.Status != status) { - ThrowHelper.ThrowArgumentExceptionForHasStatus(task, status, name); + ThrowHelper.ThrowArgumentExceptionForHasStatusEqualTo(task, status, name); + } + } + + /// + /// Asserts that the input instance has not a specific status. + /// + /// The input instance to test. + /// The task status that is accepted. + /// The name of the input parameter being tested. + /// Thrown if matches . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasStatusNotEqualTo(Task task, TaskStatus status, string name) + { + if (task.Status == status) + { + ThrowHelper.ThrowArgumentExceptionForHasStatusNotEqualTo(status, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs index 0d363bb11e6..242fefd033f 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -22,6 +22,15 @@ public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) ThrowArgumentException(name, $"Parameter {name} must be completed, had status {task.Status}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be completed, had status {task.Status}"); + } + /// /// Throws an when fails. /// @@ -31,6 +40,15 @@ public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, s ThrowArgumentException(name, $"Parameter {name} must be completed successfully, had status {task.Status}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be completed successfully, had status {task.Status}"); + } + /// /// Throws an when fails. /// @@ -40,6 +58,15 @@ public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) ThrowArgumentException(name, $"Parameter {name} must be faulted, had status {task.Status}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be faulted, had status {task.Status}"); + } + /// /// Throws an when fails. /// @@ -50,12 +77,30 @@ public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) } /// - /// Throws an when fails. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasStatus(Task task, TaskStatus status, string name) + public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be canceled, had status {task.Status}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name) { ThrowArgumentException(name, $"Parameter {name} must have status {status}, had status {task.Status}"); } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasStatusNotEqualTo(TaskStatus status, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not have status {status}"); + } } } From c784d4e2b14abe04545e268402cfaef6fb125f81 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 23:07:05 +0100 Subject: [PATCH 056/123] Moved string Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.String.cs | 46 ++++-- .../Diagnostics/ThrowHelper.String.cs | 141 ++++++++++++++++++ 2 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.String.cs b/Microsoft.Toolkit/Diagnostics/Guard.String.cs index 2c325b096af..05286bee164 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.String.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.String.cs @@ -25,7 +25,7 @@ public static void IsNullOrEmpty(string? text, string name) { if (!string.IsNullOrEmpty(text)) { - ThrowArgumentException(name, $"Parameter {name} must be null or empty, was \"{text}\""); + ThrowHelper.ThrowArgumentExceptionForIsNullOrEmpty(text, name); } } @@ -40,7 +40,7 @@ public static void IsNotNullOrEmpty(string? text, string name) { if (string.IsNullOrEmpty(text)) { - ThrowArgumentException(name, $"Parameter {name} must not be null or empty, was {(text is null ? "null" : "empty")}"); + ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name); } } @@ -55,7 +55,7 @@ public static void IsNullOrWhitespace(string? text, string name) { if (!string.IsNullOrWhiteSpace(text)) { - ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + ThrowHelper.ThrowArgumentExceptionForIsNullOrWhitespace(text, name); } } @@ -70,7 +70,7 @@ public static void IsNotNullOrWhitespace(string? text, string name) { if (string.IsNullOrWhiteSpace(text)) { - ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhitespace(text, name); } } @@ -85,7 +85,7 @@ public static void IsEmpty(string text, string name) { if (text.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, was \"{text}\""); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(text, name); } } @@ -100,7 +100,7 @@ public static void IsNotEmpty(string text, string name) { if (text.Length == 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty, was empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(text, name); } } @@ -115,7 +115,7 @@ public static void IsWhitespace(string text, string name) { if (!string.IsNullOrWhiteSpace(text)) { - ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + ThrowHelper.ThrowArgumentExceptionForIsWhitespace(text, name); } } @@ -130,7 +130,7 @@ public static void IsNotWhitespace(string text, string name) { if (string.IsNullOrWhiteSpace(text)) { - ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + ThrowHelper.ThrowArgumentExceptionForIsNotWhitespace(text, name); } } @@ -144,7 +144,10 @@ public static void IsNotWhitespace(string text, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(string text, int size, string name) { - HasSizeEqualTo(text.AsSpan(), size, name); + if (text.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, size, name); + } } /// @@ -157,7 +160,10 @@ public static void HasSizeEqualTo(string text, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeNotEqualTo(string text, int size, string name) { - HasSizeNotEqualTo(text.AsSpan(), size, name); + if (text.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(text, size, name); + } } /// @@ -170,7 +176,10 @@ public static void HasSizeNotEqualTo(string text, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeOver(string text, int size, string name) { - HasSizeOver(text.AsSpan(), size, name); + if (text.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(text, size, name); + } } /// @@ -183,7 +192,10 @@ public static void HasSizeOver(string text, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(string text, int size, string name) { - HasSizeAtLeast(text.AsSpan(), size, name); + if (text.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(text, size, name); + } } /// @@ -196,7 +208,10 @@ public static void HasSizeAtLeast(string text, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThan(string text, int size, string name) { - HasSizeLessThan(text.AsSpan(), size, name); + if (text.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(text, size, name); + } } /// @@ -209,7 +224,10 @@ public static void HasSizeLessThan(string text, int size, string name) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(string text, int size, string name) { - HasSizeLessThanOrEqualTo(text.AsSpan(), size, name); + if (text.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name); + } } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs new file mode 100644 index 00000000000..431ea9722c5 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs @@ -0,0 +1,141 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be null or empty, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be null or empty, was {(text is null ? "null" : "empty")}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(string text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsWhitespace(string text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be whitespace, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be whitespace, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {text.Length} and was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {text.Length} and was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {text.Length} and was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {text.Length} and was \"{text}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {text.Length} and was \"{text}\""); + } + } +} From 8cf87c37b84bc8e254c0de98a607bbe083a2fcab Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 23:55:44 +0100 Subject: [PATCH 057/123] Moved ReadOnlySpan Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 20 +- .../Diagnostics/ThrowHelper.Span.cs | 195 ++++++++++++++++++ 2 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 2204768b99d..ab750e86a1c 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -26,7 +26,7 @@ public static void IsEmpty(ReadOnlySpan span, string name) { if (span.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); } } @@ -42,7 +42,7 @@ public static void IsNotEmpty(ReadOnlySpan span, string name) { if (span.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); } } @@ -59,7 +59,7 @@ public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name { if (span.Length != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); } } @@ -76,7 +76,7 @@ public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string n { if (span.Length == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); } } @@ -93,7 +93,7 @@ public static void HasSizeOver(ReadOnlySpan span, int size, string name) { if (span.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); } } @@ -110,7 +110,7 @@ public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name { if (span.Length < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); } } @@ -127,7 +127,7 @@ public static void HasSizeLessThan(ReadOnlySpan span, int size, string nam { if (span.Length >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); } } @@ -144,7 +144,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, s { if (span.Length > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); } } @@ -161,7 +161,7 @@ public static void HasSizeEqualTo(ReadOnlySpan source, Span destination { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); } } @@ -178,7 +178,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span d { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs new file mode 100644 index 00000000000..d0b5bfdf995 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs @@ -0,0 +1,195 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlySpan span, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(Span span, string name) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + } +} From 24e03f520649abe430cf2abbc8f57a66bcb5266e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 23 Feb 2020 23:57:51 +0100 Subject: [PATCH 058/123] Added Span APIs to the Guard class --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index ab750e86a1c..38333ecdb7f 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -30,6 +30,22 @@ public static void IsEmpty(ReadOnlySpan span, string name) } } + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(Span span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); + } + } + /// /// Asserts that the input instance must not be empty. /// @@ -46,6 +62,22 @@ public static void IsNotEmpty(ReadOnlySpan span, string name) } } + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(Span span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// @@ -63,6 +95,23 @@ public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name } } + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span span, int size, string name) + { + if (span.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); + } + } + /// /// Asserts that the input instance must have a size not equal to a specified value. /// @@ -80,6 +129,23 @@ public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string n } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(Span span, int size, string name) + { + if (span.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); + } + } + /// /// Asserts that the input instance must have a size over a specified value. /// @@ -97,6 +163,23 @@ public static void HasSizeOver(ReadOnlySpan span, int size, string name) } } + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(Span span, int size, string name) + { + if (span.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// @@ -114,6 +197,23 @@ public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name } } + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(Span span, int size, string name) + { + if (span.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); + } + } + /// /// Asserts that the input instance must have a size of less than a specified value. /// @@ -131,6 +231,23 @@ public static void HasSizeLessThan(ReadOnlySpan span, int size, string nam } } + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(Span span, int size, string name) + { + if (span.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); + } + } + /// /// Asserts that the input instance must have a size of less than or equal to a specified value. /// @@ -148,6 +265,23 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, s } } + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span span, int size, string name) + { + if (span.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); + } + } + /// /// Asserts that the source instance must have the same size of a destination instance. /// @@ -165,6 +299,23 @@ public static void HasSizeEqualTo(ReadOnlySpan source, Span destination } } + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + /// /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// @@ -182,6 +333,23 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span d } } + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + /// /// Asserts that the input instance must be empty. /// From 6f1679bcf444af1fc718566e8eb52bd992a2cd35 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 00:02:52 +0100 Subject: [PATCH 059/123] Moved ReadOnlyMemory Guard throwers to separate class --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 20 +-- .../Diagnostics/ThrowHelper.Memory.cs | 135 ++++++++++++++++++ 2 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 38333ecdb7f..e1a5d44179a 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -362,7 +362,7 @@ public static void IsEmpty(ReadOnlyMemory memory, string name) { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); } } @@ -378,7 +378,7 @@ public static void IsNotEmpty(ReadOnlyMemory memory, string name) { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(memory, name); } } @@ -395,7 +395,7 @@ public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string { if (memory.Length != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); } } @@ -412,7 +412,7 @@ public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, stri { if (memory.Length == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); } } @@ -429,7 +429,7 @@ public static void HasSizeOver(ReadOnlyMemory memory, int size, string nam { if (memory.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); } } @@ -446,7 +446,7 @@ public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string { if (memory.Length < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); } } @@ -463,7 +463,7 @@ public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string { if (memory.Length >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); } } @@ -480,7 +480,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int siz { if (memory.Length > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); } } @@ -497,7 +497,7 @@ public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destina { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); } } @@ -514,7 +514,7 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory< { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs new file mode 100644 index 00000000000..2657eddfbc6 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs @@ -0,0 +1,135 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length != size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + } + } +} From 7e80e438a6f20267ba420e3e44a8e05c85132cf0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 00:07:18 +0100 Subject: [PATCH 060/123] Added Memory APIs to the Guard class --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 168 ++++++++++++++++++ .../Diagnostics/ThrowHelper.Memory.cs | 120 +++++++++++++ 2 files changed, 288 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index e1a5d44179a..9910c3e4494 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -366,6 +366,22 @@ public static void IsEmpty(ReadOnlyMemory memory, string name) } } + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(Memory memory, string name) + { + if (memory.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); + } + } + /// /// Asserts that the input instance must not be empty. /// @@ -382,6 +398,22 @@ public static void IsNotEmpty(ReadOnlyMemory memory, string name) } } + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(Memory memory, string name) + { + if (memory.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(memory, name); + } + } + /// /// Asserts that the input instance must have a size of a specified value. /// @@ -399,6 +431,23 @@ public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string } } + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Memory memory, int size, string name) + { + if (memory.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); + } + } + /// /// Asserts that the input instance must have a size not equal to a specified value. /// @@ -416,6 +465,23 @@ public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, stri } } + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(Memory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); + } + } + /// /// Asserts that the input instance must have a size over a specified value. /// @@ -433,6 +499,23 @@ public static void HasSizeOver(ReadOnlyMemory memory, int size, string nam } } + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(Memory memory, int size, string name) + { + if (memory.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); + } + } + /// /// Asserts that the input instance must have a size of at least specified value. /// @@ -450,6 +533,23 @@ public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string } } + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(Memory memory, int size, string name) + { + if (memory.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); + } + } + /// /// Asserts that the input instance must have a size of less than a specified value. /// @@ -467,6 +567,23 @@ public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string } } + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(Memory memory, int size, string name) + { + if (memory.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); + } + } + /// /// Asserts that the input instance must have a size of less than or equal to a specified value. /// @@ -484,6 +601,23 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int siz } } + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Memory memory, int size, string name) + { + if (memory.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); + } + } + /// /// Asserts that the source instance must have the same size of a destination instance. /// @@ -501,6 +635,23 @@ public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destina } } + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Memory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + /// /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// @@ -517,5 +668,22 @@ public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory< ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); } } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs index 2657eddfbc6..b2859643914 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs @@ -24,6 +24,18 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -36,6 +48,18 @@ public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlyMemory memo } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(Memory memory, string name) + { + if (memory.Length != 0) + { + ThrowArgumentException(name, $"Parameter {name} must not be empty"); + } + } + /// /// Throws an when fails. /// @@ -48,6 +72,18 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) + { + if (memory.Length != size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -60,6 +96,18 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory< } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -72,6 +120,18 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory mem } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int size, string name) + { + if (memory.Length <= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -84,6 +144,18 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, int size, string name) + { + if (memory.Length < size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -96,6 +168,18 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) + { + if (memory.Length >= size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -108,6 +192,18 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) + { + if (memory.Length > size) + { + ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + } + } + /// /// Throws an when fails. /// @@ -120,6 +216,18 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory } } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + } + } + /// /// Throws an when fails. /// @@ -131,5 +239,17 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); } } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + } + } } } From 662bb83b1a40bd131982c231e893306ec01affd7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 00:08:15 +0100 Subject: [PATCH 061/123] Minor code refactoring --- Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 336 ----------------- Microsoft.Toolkit/Diagnostics/Guard.Span.cs | 353 ++++++++++++++++++ 2 files changed, 353 insertions(+), 336 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Span.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs index 9910c3e4494..08722efa5fc 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs @@ -14,342 +14,6 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(ReadOnlySpan span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(Span span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(ReadOnlySpan span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(Span span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Span span, int size, string name) - { - if (span.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(Span span, int size, string name) - { - if (span.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(ReadOnlySpan span, int size, string name) - { - if (span.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(Span span, int size, string name) - { - if (span.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) - { - if (span.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(Span span, int size, string name) - { - if (span.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) - { - if (span.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(Span span, int size, string name) - { - if (span.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Span span, int size, string name) - { - if (span.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlySpan source, Span destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Span source, Span destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Span source, Span destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - /// /// Asserts that the input instance must be empty. /// diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Span.cs b/Microsoft.Toolkit/Diagnostics/Guard.Span.cs new file mode 100644 index 00000000000..3213f01d009 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Span.cs @@ -0,0 +1,353 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ReadOnlySpan span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(Span span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ReadOnlySpan span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(Span span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span span, int size, string name) + { + if (span.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(Span span, int size, string name) + { + if (span.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(ReadOnlySpan span, int size, string name) + { + if (span.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(Span span, int size, string name) + { + if (span.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + if (span.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(Span span, int size, string name) + { + if (span.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) + { + if (span.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(Span span, int size, string name) + { + if (span.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The type of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span span, int size, string name) + { + if (span.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The type of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + } +} From 1c334dfd67efa4a7a24dd489e2d2cee0a0a81ebd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 00:09:17 +0100 Subject: [PATCH 062/123] Update file headers --- Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs | 6 +++++- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 6 +++++- 9 files changed, 45 insertions(+), 9 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs index 46566aec005..4ea672e2145 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs index 83a220250fb..6fec24f013c 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs @@ -1,4 +1,8 @@ -using System; +// 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.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs index 762bf5b1128..084942de373 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs @@ -1,4 +1,8 @@ -using System; +// 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.Runtime.CompilerServices; #nullable enable diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs index 37ee27011d2..c7636f2e441 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs index b2859643914..a287fd5d171 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs index d0b5bfdf995..af8ca7105fa 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs index 431ea9722c5..209d5b39c59 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs index 242fefd033f..c7b379e6538 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -1,4 +1,8 @@ -using System; +// 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; diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 43553f6c1e1..3183848280a 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -1,4 +1,8 @@ -using System; +// 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; From eccc2a6b62fa6f8a82d910f333b2af6ebc31015c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 00:27:08 +0100 Subject: [PATCH 063/123] Removed unnecessary methods --- .../Diagnostics/Guard.ThrowExceptions.cs | 56 ------------------- ...ions.cs => ThrowHelper.ThrowExceptions.cs} | 0 2 files changed, 56 deletions(-) delete mode 100644 Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs rename Microsoft.Toolkit/Diagnostics/{ThrowHelper.Exceptions.cs => ThrowHelper.ThrowExceptions.cs} (100%) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs b/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs deleted file mode 100644 index c56181021de..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Guard.ThrowExceptions.cs +++ /dev/null @@ -1,56 +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.Runtime.CompilerServices; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to verify conditions when running code. - /// - public static partial class Guard - { - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with and . - /// This method is marked as to reduce the binary size of the caller. - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentException(string name, string message) - { - throw new ArgumentException(message, name); - } - - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with and . - /// This method is marked as to reduce the binary size of the caller. - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentNullException(string name, string message) - { - throw new ArgumentNullException(name, message); - } - - /// - /// Throws a new . - /// - /// The argument name. - /// The message to include in the exception. - /// Thrown with and . - /// This method is marked as to reduce the binary size of the caller. - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ThrowArgumentOutOfRangeException(string name, string message) - { - throw new ArgumentOutOfRangeException(name, message); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs similarity index 100% rename from Microsoft.Toolkit/Diagnostics/ThrowHelper.Exceptions.cs rename to Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs From 15ef6a3d0127f615593f1f9a16ec0599254cf7bf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:15:59 +0100 Subject: [PATCH 064/123] Added TypeExtensions.ToTypeString extension --- .../Diagnostics/ThrowHelper.Array.cs | 2 +- .../Extensions/TypeExtensions.cs | 93 +++++++++++++++++++ UnitTests/Extensions/Test_TypeExtensions.cs | 40 ++++++++ UnitTests/UnitTests.csproj | 1 + 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Toolkit/Extensions/TypeExtensions.cs create mode 100644 UnitTests/Extensions/Test_TypeExtensions.cs diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs index 4ea672e2145..da0fa99b5cf 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs @@ -22,7 +22,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name} ({typeof(T)}) must be empty, had a size of {array.Length}"); } /// diff --git a/Microsoft.Toolkit/Extensions/TypeExtensions.cs b/Microsoft.Toolkit/Extensions/TypeExtensions.cs new file mode 100644 index 00000000000..ea280edc9e5 --- /dev/null +++ b/Microsoft.Toolkit/Extensions/TypeExtensions.cs @@ -0,0 +1,93 @@ +// 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.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Extensions +{ + /// + /// Helpers for working with types. + /// + public static class TypeExtensions + { + /// + /// The mapping of built-in types to their simple representation. + /// + private static readonly IReadOnlyDictionary BuiltInTypesMap = new Dictionary + { + [typeof(bool)] = "bool", + [typeof(byte)] = "byte", + [typeof(sbyte)] = "sbyte", + [typeof(short)] = "short", + [typeof(ushort)] = "ushort", + [typeof(char)] = "char", + [typeof(int)] = "int", + [typeof(uint)] = "uint", + [typeof(float)] = "float", + [typeof(long)] = "long", + [typeof(ulong)] = "ulong", + [typeof(double)] = "double", + [typeof(decimal)] = "decimal", + [typeof(object)] = "object", + [typeof(string)] = "string" + }; + + /// + /// A thread-safe mapping of precomputed string representation of types. + /// + private static readonly ConditionalWeakTable DisplayNames = new ConditionalWeakTable(); + + /// + /// Returns a simple string representation of a type. + /// + /// The input type. + /// The string representation of . + [Pure] + public static string ToTypeString(this Type type) + { + // Local function to create the formatted string for a given type + static string FormatDisplayString(Type type) + { + // Primitive types use the keyword name + if (BuiltInTypesMap.TryGetValue(type, out string typeName)) + { + return typeName; + } + + // Generic types are displayed as Foo + if (type.IsGenericType && + type.FullName is { } fullName && + fullName.Split('`') is { } tokens && + tokens.Length > 0 && + tokens[0] is { } genericName && + genericName.Length > 0) + { + var typeArguments = type.GetGenericArguments().Select(FormatDisplayString); + + return $"{genericName}<{string.Join(", ", typeArguments)}>"; + } + + // Array types are displayed as Foo[] + if (type.IsArray) + { + var elementType = type.GetElementType(); + var rank = type.GetArrayRank(); + + return $"{FormatDisplayString(elementType)}[{new string(',', rank - 1)}]"; + } + + return type.ToString(); + } + + // Atomically get or build the display string for the current type + return DisplayNames.GetValue(type, FormatDisplayString); + } + } +} diff --git a/UnitTests/Extensions/Test_TypeExtensions.cs b/UnitTests/Extensions/Test_TypeExtensions.cs new file mode 100644 index 00000000000..e99d78016b5 --- /dev/null +++ b/UnitTests/Extensions/Test_TypeExtensions.cs @@ -0,0 +1,40 @@ +// 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.Collections.Generic; +using Microsoft.Toolkit.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTests.Extensions +{ + [TestClass] + public class Test_TypeExtensions + { + [TestCategory("TypeExtensions")] + [TestMethod] + public void Test_TypeExtensions_BuiltInTypes() + { + Assert.AreEqual("bool", typeof(bool).ToTypeString()); + Assert.AreEqual("int", typeof(int).ToTypeString()); + Assert.AreEqual("float", typeof(float).ToTypeString()); + Assert.AreEqual("double", typeof(double).ToTypeString()); + Assert.AreEqual("decimal", typeof(decimal).ToTypeString()); + Assert.AreEqual("object", typeof(object).ToTypeString()); + Assert.AreEqual("string", typeof(string).ToTypeString()); + } + + [TestCategory("TypeExtensions")] + [TestMethod] + public void Test_TypeExtensions_GenericTypes() + { + Assert.AreEqual("int[]", typeof(int[]).ToTypeString()); + Assert.AreEqual(typeof(int[,]).ToTypeString(), "int[,]"); + Assert.AreEqual("System.Span", typeof(Span).ToTypeString()); + Assert.AreEqual("System.Memory", typeof(Memory).ToTypeString()); + Assert.AreEqual("System.Collections.Generic.IEnumerable", typeof(IEnumerable).ToTypeString()); + Assert.AreEqual(typeof(Dictionary>).ToTypeString(), "System.Collections.Generic.Dictionary>"); + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 60d36b809a9..05729126f01 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -135,6 +135,7 @@ + From 317fc990edbfced2eb42405fea62c95cb3efe528 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:28:43 +0100 Subject: [PATCH 065/123] Improved error messages for general Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 6 +-- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 56 ++++++++++---------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 2db43ac14b7..1172f93497f 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -64,7 +64,7 @@ public static void IsNotNull(T? value, string name) { if (value is null) { - ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); + ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); } } @@ -82,7 +82,7 @@ public static void IsNotNull(T? value, string name) { if (value is null) { - ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); + ThrowHelper.ThrowArgumentNullExceptionForIsNotNull(name); } } @@ -289,7 +289,7 @@ public static void IsReferenceEqualTo(T value, T target, string name) { if (!ReferenceEquals(value, target)) { - ThrowHelper.ThrowArgumentExceptionForIsReferenceEqualTo(value, target, name); + ThrowHelper.ThrowArgumentExceptionForIsReferenceEqualTo(name); } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 3183848280a..65c8501ffa3 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -22,10 +22,10 @@ internal static partial class ThrowHelper /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNull(T? value, string name) + public static void ThrowArgumentExceptionForIsNull(T value, string name) where T : class { - ThrowArgumentException(name, $"Parameter {name} must be null, was {value}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be null, was \"{value}\" ({value.GetType().ToTypeString()})"); } /// @@ -35,16 +35,16 @@ public static void ThrowArgumentExceptionForIsNull(T? value, string name) public static void ThrowArgumentExceptionForIsNull(T? value, string name) where T : struct { - ThrowArgumentException(name, $"Parameter {name} must be null, was {value}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T?).ToTypeString()}) must be null, was \"{value}\" ({typeof(T).ToTypeString()})"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentNullExceptionForIsNotNull(string name) + public static void ThrowArgumentNullExceptionForIsNotNull(string name) { - ThrowArgumentNullException(name, $"Parameter {name} must be not null"); + ThrowArgumentNullException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be not null)"); } #pragma warning restore CS0419 @@ -54,7 +54,7 @@ public static void ThrowArgumentNullExceptionForIsNotNull(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsOfType(object value, string name) { - ThrowArgumentException(name, $"Parameter {name} must be of type {typeof(T)}, was {value.GetType()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -63,7 +63,7 @@ public static void ThrowArgumentExceptionForIsOfType(object value, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name) { - ThrowArgumentException(name, $"Parameter {name} must be of type {type}, was {value.GetType()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -72,7 +72,7 @@ public static void ThrowArgumentExceptionForIsOfType(object value, Type type, st [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAssignableToType(object value, string name) { - ThrowArgumentException(name, $"Parameter {name} must be assignable to type {typeof(T)}, was {value.GetType()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -81,7 +81,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name) { - ThrowArgumentException(name, $"Parameter {name} must be assignable to type {type}, was {value.GetType()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -91,7 +91,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) where T : notnull, IEquatable { - ThrowArgumentException(name, $"Parameter {name} must be == {target}, was {value}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be == \"{target}\", was \"{value}\""); } /// @@ -101,7 +101,7 @@ public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, stri public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) where T : notnull, IEquatable { - ThrowArgumentException(name, $"Parameter {name} must be != {target}, was {value}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be != \"{target}\", was \"{value}\""); } /// @@ -111,27 +111,27 @@ public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, s public static void ThrowArgumentExceptionForsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { - ThrowArgumentException(name, $"Parameter {name} is not a bitwise match, was {value.ToHexString()} instead of {target.ToHexString()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) is not a bitwise match, was \"{value.ToHexString()}\" instead of \"{target.ToHexString()}\""); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsReferenceEqualTo(T value, T target, string name) + public static void ThrowArgumentExceptionForIsReferenceEqualTo(string name) where T : class { - ThrowArgumentException(name, $"Parameter {name} must be the same instance as the target object"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be the same instance as the target object"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(T value, T target, string name) + public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name) where T : class { - ThrowArgumentException(name, $"Parameter {name} must not be the same instance as the target object"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must not be the same instance as the target object"); } /// @@ -140,7 +140,7 @@ public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsTrue(string name) { - ThrowArgumentException(name, $"Parameter {name} must be true, was false"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be true, was false"); } /// @@ -149,7 +149,7 @@ public static void ThrowArgumentExceptionForIsTrue(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsFalse(string name) { - ThrowArgumentException(name, $"Parameter {name} must be false, was true"); + ThrowArgumentException(name, $"Parameter \"{name}\" must be false, was true"); } /// @@ -159,7 +159,7 @@ public static void ThrowArgumentExceptionForIsFalse(string name) public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {max}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{max}\", was \"{value}\""); } /// @@ -169,7 +169,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T m public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was \"{value}\""); } /// @@ -179,7 +179,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T v public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was \"{value}\""); } /// @@ -189,7 +189,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was \"{value}\""); } /// @@ -199,7 +199,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo( public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and < {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was \"{value}\""); } /// @@ -209,7 +209,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T mi public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or >= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); } /// @@ -219,7 +219,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be > {minimum} and < {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was \"{value}\""); } /// @@ -229,7 +229,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T mi public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be <= {minimum} or >= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); } /// @@ -239,7 +239,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be >= {minimum} and <= {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was \"{value}\""); } /// @@ -249,7 +249,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T va public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) where T : notnull, IComparable { - ThrowArgumentOutOfRangeException(name, $"Parameter {name} must be < {minimum} or > {maximum}, was {value}"); + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was \"{value}\""); } } } From cb1f3b00fce0601374fcc231bb146a833f232c65 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:37:03 +0100 Subject: [PATCH 066/123] Improved error messages for Enumerable Guard APIs --- .../Diagnostics/Guard.Enumerable.cs | 12 +-- Microsoft.Toolkit/Diagnostics/Guard.cs | 2 +- .../Diagnostics/ThrowHelper.Enumerable.cs | 85 +++++++++++++------ 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs index c17b162ee87..4b492b19d18 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs @@ -76,7 +76,7 @@ public static void IsNotEmpty(ICollection collection, string name) { if (collection.Count == 0) { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(collection, name); } } @@ -92,7 +92,7 @@ public static void IsNotEmpty(IReadOnlyCollection collection, string name) { if (collection.Count == 0) { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(collection, name); } } @@ -108,7 +108,7 @@ public static void IsNotEmpty(IEnumerable enumerable, string name) { if (enumerable.Any()) { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(enumerable, name); } } @@ -176,7 +176,7 @@ public static void HasSizeNotEqualTo(ICollection collection, int size, str { if (collection.Count == size) { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); } } @@ -193,7 +193,7 @@ public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int s { if (collection.Count == size) { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); } } @@ -210,7 +210,7 @@ public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, str { if (enumerable.Count() == size) { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(size, name); + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(enumerable, size, name); } } diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 1172f93497f..a7469f8640d 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -308,7 +308,7 @@ public static void IsReferenceNotEqualTo(T value, T target, string name) { if (ReferenceEquals(value, target)) { - ThrowHelper.ThrowArgumentExceptionForIsReferenceNotEqualTo(value, target, name); + ThrowHelper.ThrowArgumentExceptionForIsReferenceNotEqualTo(name); } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs index 6fec24f013c..4173ed2838f 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs @@ -7,6 +7,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -24,7 +25,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be empty, had a size of {collection.Count}"); } /// @@ -33,7 +34,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ICollection collection [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be empty, had a size of {collection.Count}"); } /// @@ -42,16 +43,34 @@ public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(IEnumerable enumerable, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be empty, had a size of {enumerable.Count()}"); } /// - /// Throws an when , or fail. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(string name) + public static void ThrowArgumentExceptionForIsNotEmpty(ICollection collection, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(IReadOnlyCollection collection, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must not be empty"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(IEnumerable enumerable, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must not be empty"); } /// @@ -60,7 +79,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); } /// @@ -69,7 +88,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); } /// @@ -78,16 +97,34 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized == {size}, had a size of {enumerable.Count()}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized != {size}"); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized != {size}"); } /// - /// Throws an when , or fail. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(int size, string name) + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized != {size}"); } /// @@ -96,7 +133,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(int size, string n [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); } /// @@ -105,7 +142,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ICollection collec [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); } /// @@ -114,7 +151,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized > {size}, had a size of {enumerable.Count()}"); } /// @@ -123,7 +160,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(IEnumerable enumer [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); } /// @@ -132,7 +169,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); } /// @@ -141,7 +178,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized >= {size}, had a size of {enumerable.Count()}"); } /// @@ -150,7 +187,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(IEnumerable enu [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); } /// @@ -159,7 +196,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); } /// @@ -168,7 +205,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollecti [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized < {size}, had a size of {enumerable.Count()}"); } /// @@ -177,7 +214,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(IEnumerable en [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); } /// @@ -186,7 +223,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); } /// @@ -195,7 +232,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {enumerable.Count()}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized <= {size}, had a size of {enumerable.Count()}"); } } } From 23f1504cb381a1d39a2f0e3c3a135ea93da82f7d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:38:19 +0100 Subject: [PATCH 067/123] Improved error messages for stream Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.IO.cs | 6 +++--- Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs index 06228077370..242f139030a 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.IO.cs @@ -26,7 +26,7 @@ public static void CanRead(Stream stream, string name) { if (!stream.CanRead) { - ThrowHelper.ThrowArgumentExceptionForCanRead(name); + ThrowHelper.ThrowArgumentExceptionForCanRead(stream, name); } } @@ -41,7 +41,7 @@ public static void CanWrite(Stream stream, string name) { if (!stream.CanWrite) { - ThrowHelper.ThrowArgumentExceptionForCanWrite(name); + ThrowHelper.ThrowArgumentExceptionForCanWrite(stream, name); } } @@ -56,7 +56,7 @@ public static void CanSeek(Stream stream, string name) { if (!stream.CanSeek) { - ThrowHelper.ThrowArgumentExceptionForCanSeek(name); + ThrowHelper.ThrowArgumentExceptionForCanSeek(stream, name); } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs index c7636f2e441..525048a29e8 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs @@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -21,27 +22,27 @@ internal static partial class ThrowHelper /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForCanRead(string name) + public static void ThrowArgumentExceptionForCanRead(Stream stream, string name) { - ThrowArgumentException(name, $"Stream {name} doesn't support reading"); + ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support reading"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForCanWrite(string name) + public static void ThrowArgumentExceptionForCanWrite(Stream stream, string name) { - ThrowArgumentException(name, $"Stream {name} doesn't support writing"); + ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support writing"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForCanSeek(string name) + public static void ThrowArgumentExceptionForCanSeek(Stream stream, string name) { - ThrowArgumentException(name, $"Stream {name} doesn't support seeking"); + ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support seeking"); } /// @@ -50,7 +51,7 @@ public static void ThrowArgumentExceptionForCanSeek(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name) { - ThrowArgumentException(name, $"Stream {name} must be at start position, was at {stream.Position}"); + ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) must be at start position, was at {stream.Position}"); } } } From 920e69f6377d8cb9811e2ab6e86536f543acfa42 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:41:35 +0100 Subject: [PATCH 068/123] Improved error messages for array Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 2 +- .../Diagnostics/ThrowHelper.Array.cs | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs index c7f4f413341..05b49046775 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs @@ -42,7 +42,7 @@ public static void IsNotEmpty(T[] array, string name) { if (array.Length == 0) { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(array, name); + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs index da0fa99b5cf..f24858478e7 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -22,16 +23,16 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) { - ThrowArgumentException(name, $"Parameter {name} ({typeof(T)}) must be empty, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(T[] array, string name) + public static void ThrowArgumentExceptionForIsNotEmpty(string name) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must not be empty"); } /// @@ -40,7 +41,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(T[] array, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); } /// @@ -49,7 +50,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); } /// @@ -58,7 +59,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); } /// @@ -67,7 +68,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); } /// @@ -76,7 +77,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); } /// @@ -85,7 +86,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int si [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); } /// @@ -94,7 +95,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] arra [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -103,7 +104,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] de [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } } } From 18e661f4d7ff5847b36b3d04b633eb2f300bd61c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:42:41 +0100 Subject: [PATCH 069/123] Improved error messages for string Guard APIs --- .../Diagnostics/ThrowHelper.String.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs index 209d5b39c59..e0360ff327f 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs @@ -22,7 +22,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) { - ThrowArgumentException(name, $"Parameter {name} must be null or empty, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be null or empty, was \"{text}\""); } /// @@ -31,7 +31,7 @@ public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string n [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be null or empty, was {(text is null ? "null" : "empty")}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be null or empty, was {(text is null ? "null" : "empty")}"); } /// @@ -40,7 +40,7 @@ public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, strin [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name) { - ThrowArgumentException(name, $"Parameter {name} must be null or whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be null or whitespace, was \"{text}\""); } /// @@ -49,7 +49,7 @@ public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, str [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); } /// @@ -58,7 +58,7 @@ public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(string text, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be empty, was \"{text}\""); } /// @@ -67,7 +67,7 @@ public static void ThrowArgumentExceptionForIsEmpty(string text, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be empty"); } /// @@ -76,7 +76,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsWhitespace(string text, string name) { - ThrowArgumentException(name, $"Parameter {name} must be whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be whitespace, was \"{text}\""); } /// @@ -85,7 +85,7 @@ public static void ThrowArgumentExceptionForIsWhitespace(string text, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be whitespace, was \"{text}\""); } /// @@ -94,7 +94,7 @@ public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized == {size}, had a size of {text.Length} and was \"{text}\""); } /// @@ -103,7 +103,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized != {size}, was \"{text}\""); } /// @@ -112,7 +112,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized > {size}, had a size of {text.Length} and was \"{text}\""); } /// @@ -121,7 +121,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized >= {size}, had a size of {text.Length} and was \"{text}\""); } /// @@ -130,7 +130,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized < {size}, had a size of {text.Length} and was \"{text}\""); } /// @@ -139,7 +139,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized <= {size}, had a size of {text.Length} and was \"{text}\""); } } } From d00e0e3d8ad1710c22853be7271d42b2c15a83f6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:44:28 +0100 Subject: [PATCH 070/123] Improved error messages for task Guard APIs --- Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs | 2 +- .../Diagnostics/ThrowHelper.Tasks.cs | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs index ddcf9eacec7..846ef8c30f4 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Tasks.cs @@ -163,7 +163,7 @@ public static void HasStatusNotEqualTo(Task task, TaskStatus status, string name { if (task.Status == status) { - ThrowHelper.ThrowArgumentExceptionForHasStatusNotEqualTo(status, name); + ThrowHelper.ThrowArgumentExceptionForHasStatusNotEqualTo(task, status, name); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs index c7b379e6538..0b3a88dd1a7 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -23,7 +24,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be completed, had status {task.Status}"); } /// @@ -32,7 +33,7 @@ public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status}"); } /// @@ -41,7 +42,7 @@ public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status}"); } /// @@ -50,7 +51,7 @@ public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status}"); } /// @@ -59,7 +60,7 @@ public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status}"); } /// @@ -68,7 +69,7 @@ public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status}"); } /// @@ -77,7 +78,7 @@ public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status}"); } /// @@ -86,7 +87,7 @@ public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status}"); } /// @@ -95,16 +96,16 @@ public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name) { - ThrowArgumentException(name, $"Parameter {name} must have status {status}, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasStatusNotEqualTo(TaskStatus status, string name) + public static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name) { - ThrowArgumentException(name, $"Parameter {name} must not have status {status}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not have status {status}"); } } } From 6aeedaab193debe6322bc24832b115449f8f597c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:50:23 +0100 Subject: [PATCH 071/123] Improved error messages for span Guard APIs --- .../Diagnostics/ThrowHelper.Span.cs | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs index af8ca7105fa..b994e8101ea 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -22,7 +23,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be empty, had a size of {span.Length}"); } /// @@ -31,7 +32,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, str [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be empty, had a size of {span.Length}"); } /// @@ -40,7 +41,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Span span, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlySpan span, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must not be empty"); } /// @@ -49,7 +50,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlySpan span, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(Span span, string name) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must not be empty"); } /// @@ -58,7 +59,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(Span span, string n [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized == {size}, had a size of {span.Length}"); } /// @@ -67,7 +68,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized == {size}, had a size of {span.Length}"); } /// @@ -76,7 +77,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized != {size}, had a size of {span.Length}"); } /// @@ -85,7 +86,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized != {size}, had a size of {span.Length}"); } /// @@ -94,7 +95,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, i [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -103,7 +104,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -112,7 +113,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Span span, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -121,7 +122,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -130,7 +131,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized < {size}, had a size of {span.Length}"); } /// @@ -139,7 +140,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized < {size}, had a size of {span.Length}"); } /// @@ -148,7 +149,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized <= {size}, had a size of {span.Length}"); } /// @@ -157,7 +158,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized <= {size}, had a size of {span.Length}"); } /// @@ -166,7 +167,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -175,7 +176,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan so [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -184,7 +185,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -193,7 +194,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized <= {destination.Length}, had a size of {source.Length}"); } } } From acafd8a2022430ef1ab9b352cd8c96350df41b6c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 01:51:47 +0100 Subject: [PATCH 072/123] Improved error messages for memory Guard APIs --- .../Diagnostics/ThrowHelper.Memory.cs | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs index a287fd5d171..65f036c50ac 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -24,7 +25,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be empty, had a size of {memory.Length}"); } } @@ -36,7 +37,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be empty, had a size of {memory.Length}"); } } @@ -48,7 +49,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlyMemory memo { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must not be empty"); } } @@ -60,7 +61,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(Memory memory, stri { if (memory.Length != 0) { - ThrowArgumentException(name, $"Parameter {name} must not be empty"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must not be empty"); } } @@ -72,7 +73,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory { if (memory.Length != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); } } @@ -84,7 +85,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, { if (memory.Length != size) { - ThrowArgumentException(name, $"Parameter {name} must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); } } @@ -96,7 +97,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory< { if (memory.Length == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); } } @@ -108,7 +109,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memor { if (memory.Length == size) { - ThrowArgumentException(name, $"Parameter {name} must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); } } @@ -120,7 +121,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory mem { if (memory.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); } } @@ -132,7 +133,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int { if (memory.Length <= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); } } @@ -144,7 +145,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory { if (memory.Length < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); } } @@ -156,7 +157,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, { if (memory.Length < size) { - ThrowArgumentException(name, $"Parameter {name} must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); } } @@ -168,7 +169,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory { if (memory.Length >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); } } @@ -180,7 +181,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, { if (memory.Length >= size) { - ThrowArgumentException(name, $"Parameter {name} must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); } } @@ -192,7 +193,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly { if (memory.Length > size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); } } @@ -204,7 +205,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory size) { - ThrowArgumentException(name, $"Parameter {name} must be sized <= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); } } @@ -216,7 +217,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } } @@ -228,7 +229,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, { if (source.Length != destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } } @@ -240,7 +241,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly { if (source.Length > destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } } @@ -252,7 +253,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory destination.Length) { - ThrowArgumentException(name, $"The source {name} must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } } } From 8c7757ab2fa63e4988ced16938d62149b799a9ca Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 12:26:34 +0100 Subject: [PATCH 073/123] Improved type string for nullable types --- Microsoft.Toolkit/Extensions/TypeExtensions.cs | 9 ++++++++- UnitTests/Extensions/Test_TypeExtensions.cs | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Extensions/TypeExtensions.cs b/Microsoft.Toolkit/Extensions/TypeExtensions.cs index ea280edc9e5..119c5ec7014 100644 --- a/Microsoft.Toolkit/Extensions/TypeExtensions.cs +++ b/Microsoft.Toolkit/Extensions/TypeExtensions.cs @@ -61,7 +61,7 @@ static string FormatDisplayString(Type type) return typeName; } - // Generic types are displayed as Foo + // Generic types if (type.IsGenericType && type.FullName is { } fullName && fullName.Split('`') is { } tokens && @@ -71,6 +71,13 @@ tokens[0] is { } genericName && { var typeArguments = type.GetGenericArguments().Select(FormatDisplayString); + // Nullable types are displayed as T? + if (type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + return $"{typeArguments.First()}?"; + } + + // Standard generic types are displayed as Foo return $"{genericName}<{string.Join(", ", typeArguments)}>"; } diff --git a/UnitTests/Extensions/Test_TypeExtensions.cs b/UnitTests/Extensions/Test_TypeExtensions.cs index e99d78016b5..bedd3a3d86d 100644 --- a/UnitTests/Extensions/Test_TypeExtensions.cs +++ b/UnitTests/Extensions/Test_TypeExtensions.cs @@ -29,6 +29,8 @@ public void Test_TypeExtensions_BuiltInTypes() [TestMethod] public void Test_TypeExtensions_GenericTypes() { + Assert.AreEqual("int?", typeof(int?).ToTypeString()); + Assert.AreEqual("System.DateTime?", typeof(DateTime?).ToTypeString()); Assert.AreEqual("int[]", typeof(int[]).ToTypeString()); Assert.AreEqual(typeof(int[,]).ToTypeString(), "int[,]"); Assert.AreEqual("System.Span", typeof(Span).ToTypeString()); From c2fd97c98c6b9045d452713dec81f7443490323c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 12:37:39 +0100 Subject: [PATCH 074/123] Improved type string for value tuple types --- Microsoft.Toolkit/Extensions/TypeExtensions.cs | 16 +++++++++++++++- UnitTests/Extensions/Test_TypeExtensions.cs | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Extensions/TypeExtensions.cs b/Microsoft.Toolkit/Extensions/TypeExtensions.cs index 119c5ec7014..b893e3d5743 100644 --- a/Microsoft.Toolkit/Extensions/TypeExtensions.cs +++ b/Microsoft.Toolkit/Extensions/TypeExtensions.cs @@ -72,11 +72,25 @@ tokens[0] is { } genericName && var typeArguments = type.GetGenericArguments().Select(FormatDisplayString); // Nullable types are displayed as T? - if (type.GetGenericTypeDefinition() == typeof(Nullable<>)) + var genericType = type.GetGenericTypeDefinition(); + if (genericType == typeof(Nullable<>)) { return $"{typeArguments.First()}?"; } + // ValueTuple types are displayed as (T1, T2) + if (genericType == typeof(ValueTuple<>) || + genericType == typeof(ValueTuple<,>) || + genericType == typeof(ValueTuple<,,>) || + genericType == typeof(ValueTuple<,,,>) || + genericType == typeof(ValueTuple<,,,,>) || + genericType == typeof(ValueTuple<,,,,,>) || + genericType == typeof(ValueTuple<,,,,,,>) || + genericType == typeof(ValueTuple<,,,,,,,>)) + { + return $"({string.Join(", ", typeArguments)})"; + } + // Standard generic types are displayed as Foo return $"{genericName}<{string.Join(", ", typeArguments)}>"; } diff --git a/UnitTests/Extensions/Test_TypeExtensions.cs b/UnitTests/Extensions/Test_TypeExtensions.cs index bedd3a3d86d..fe920c86640 100644 --- a/UnitTests/Extensions/Test_TypeExtensions.cs +++ b/UnitTests/Extensions/Test_TypeExtensions.cs @@ -31,6 +31,8 @@ public void Test_TypeExtensions_GenericTypes() { Assert.AreEqual("int?", typeof(int?).ToTypeString()); Assert.AreEqual("System.DateTime?", typeof(DateTime?).ToTypeString()); + Assert.AreEqual("(int, float)", typeof((int, float)).ToTypeString()); + Assert.AreEqual("(double?, string, int)?", typeof((double?, string, int)?).ToTypeString()); Assert.AreEqual("int[]", typeof(int[]).ToTypeString()); Assert.AreEqual(typeof(int[,]).ToTypeString(), "int[,]"); Assert.AreEqual("System.Span", typeof(Span).ToTypeString()); From 8facb7e682f0efe1926333597a8fe70765b98269 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 24 Feb 2020 12:49:56 +0100 Subject: [PATCH 075/123] Minor performance improvement --- Microsoft.Toolkit/Extensions/TypeExtensions.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit/Extensions/TypeExtensions.cs b/Microsoft.Toolkit/Extensions/TypeExtensions.cs index b893e3d5743..c012d085daf 100644 --- a/Microsoft.Toolkit/Extensions/TypeExtensions.cs +++ b/Microsoft.Toolkit/Extensions/TypeExtensions.cs @@ -56,9 +56,9 @@ public static string ToTypeString(this Type type) static string FormatDisplayString(Type type) { // Primitive types use the keyword name - if (BuiltInTypesMap.TryGetValue(type, out string typeName)) + if (BuiltInTypesMap.TryGetValue(type, out string? typeName)) { - return typeName; + return typeName!; } // Generic types @@ -107,8 +107,11 @@ tokens[0] is { } genericName && return type.ToString(); } - // Atomically get or build the display string for the current type - return DisplayNames.GetValue(type, FormatDisplayString); + /* Atomically get or build the display string for the current type. + * Manually create a static lambda here to enable caching of the generated closure. + * This is a workaround for the missing caching for method group conversions, and should + * be removed once this issue is resolved: https://github.com/dotnet/roslyn/issues/5835. */ + return DisplayNames.GetValue(type, t => FormatDisplayString(t)); } } } From 2952e23ead884710edcca11e5724b2c40f3fbe02 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 00:45:14 +0100 Subject: [PATCH 076/123] Added numeric comparison T4 file --- .../Diagnostics/Generated/Guard.Numeric.tt | 242 ++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 19 ++ 2 files changed, 261 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt new file mode 100644 index 00000000000..40e2b289582 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt @@ -0,0 +1,242 @@ +<#@ template language="C#" #> +<#@ assembly name="System.Core" #> + +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { +<# +foreach (var type in new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }) +{ +#> + /// + /// 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(<#=type#> value, <#=type#> target, string name) + { + if (value != target) + { + 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(<#=type#> value, <#=type#> target, string name) + { + if (value == target) + { + 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(<#=type#> value, <#=type#> maximum, string name) + { + if (value >= maximum) + { + 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(<#=type#> value, <#=type#> maximum, string name) + { + if (value > maximum) + { + 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(<#=type#> value, <#=type#> minimum, string name) + { + if (value <= minimum) + { + 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(<#=type#> value, <#=type#> minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value < minimum || value > maximum) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The type of input values to compare. + /// 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) + { + if (value >= minimum && value <= maximum) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); + } + } +<# +} +#> + } +} diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index cb0dcd79d28..a19e81a35e2 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -20,4 +20,23 @@ + + + TextTemplatingFileGenerator + Guard.Numeric.cs + + + + + + + + + + True + True + Guard.Numeric.tt + + + \ No newline at end of file From 4991a5cac80feaf6bf9bf6984e3ed765356e948d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 00:47:13 +0100 Subject: [PATCH 077/123] Updated .gitignore to skip generated files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ed0e87e25e0..f63fe7164e0 100644 --- a/.gitignore +++ b/.gitignore @@ -225,3 +225,6 @@ msbuild.binlog *.project.lock.json /build/tools/** !/build/tools/packages.config + +# T4 generated files +*/Diagnostics/Generated/*.cs From 714cf313ffa2671b55671a6c78e1ec11f9918490 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 00:51:26 +0100 Subject: [PATCH 078/123] Moved comparison Guard APIs to separate file --- .../Diagnostics/Guard.Comparable.cs | 337 ++++++++++++++++++ Microsoft.Toolkit/Diagnostics/Guard.cs | 320 ----------------- 2 files changed, 337 insertions(+), 320 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs new file mode 100644 index 00000000000..65c2d13e09b --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs @@ -0,0 +1,337 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input value must be equal to a specified value. + /// + /// The type of input values to compare. + /// 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 IsEqualTo(T value, T target, string name) + where T : notnull, IEquatable + { + if (!value.Equals(target)) + { + ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); + } + } + + /// + /// Asserts that the input value must be not equal to a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T target, string name) + where T : notnull, IEquatable + { + if (value.Equals(target)) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); + } + } + + /// + /// Asserts that the input value must be a bitwise match with a specified value. + /// + /// The type of input values to compare. + /// The input value to test. + /// The target value to test for. + /// The name of the input parameter being tested. + /// Thrown if is not a bitwise match for . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBitwiseEqualTo(T value, T target, string name) + where T : unmanaged + { + /* Include some fast paths if the input type is of size 1, 2, 4 or 8. + * In those cases, just reinterpret the bytes as values of an integer type, + * and compare them directly, which is much faster than having explicitly + * loop through every single byte. This also allows for more expressive error + * messages, since the entire input values can be expressed as hexadecimal values. + * The conditional branches below are known at compile time by the JIT compiler, + * so that only the right one will actually be translated into native code. */ + if (typeof(T) == typeof(byte) || + typeof(T) == typeof(sbyte) || + typeof(T) == typeof(bool)) + { + byte valueByte = Unsafe.As(ref value); + byte targetByte = Unsafe.As(ref target); + + if (valueByte != targetByte) + { + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); + } + } + else if (typeof(T) == typeof(ushort) || + typeof(T) == typeof(short) || + typeof(T) == typeof(char)) + { + ushort valueUShort = Unsafe.As(ref value); + ushort targetUShort = Unsafe.As(ref target); + + if (valueUShort != targetUShort) + { + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); + } + } + else if (typeof(T) == typeof(uint) || + typeof(T) == typeof(int) || + typeof(T) == typeof(float)) + { + uint valueUInt = Unsafe.As(ref value); + uint targetUInt = Unsafe.As(ref target); + + if (valueUInt != targetUInt) + { + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); + } + } + else if (typeof(T) == typeof(ulong) || + typeof(T) == typeof(long) || + typeof(T) == typeof(double)) + { + ulong valueULong = Unsafe.As(ref value); + ulong targetULong = Unsafe.As(ref target); + + if (valueULong != targetULong) + { + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); + } + } + else + { + ref byte valueRef = ref Unsafe.As(ref value); + ref byte targetRef = ref Unsafe.As(ref target); + int bytesCount = Unsafe.SizeOf(); + + for (int i = 0; i < bytesCount; i++) + { + byte valueByte = Unsafe.Add(ref valueRef, i); + byte targetByte = Unsafe.Add(ref targetRef, i); + + if (valueByte != targetByte) + { + ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); + } + } + } + } + + /// + /// Asserts that the input value must be less than a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(maximum) >= 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name); + } + } + + /// + /// Asserts that the input value must be less than or equal to a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(maximum) > 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); + } + } + + /// + /// Asserts that the input value must be greater than a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T minimum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) <= 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); + } + } + + /// + /// Asserts that the input value must be greater than or equal to a specified value. + /// + /// The type of input values to compare. + /// 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(T value, T minimum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); + } + } + + /// + /// Asserts that the input value must be in a given range. + /// + /// The type of input values to compare. + /// 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) >= 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must not be in a given range. + /// + /// The type of input values to compare. + /// 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) < 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The type of input values to compare. + /// 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The type of input values to compare. + /// 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) > 0 && value.CompareTo(maximum) < 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must be in a given interval. + /// + /// The type of input values to compare. + /// 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); + } + } + + /// + /// Asserts that the input value must not be in a given interval. + /// + /// The type of input values to compare. + /// 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) <= 0) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index a7469f8640d..6c7a54b7358 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -150,130 +150,6 @@ public static void IsAssignableToType(object value, Type type, string name) } } - /// - /// Asserts that the input value must be equal to a specified value. - /// - /// The type of input values to compare. - /// 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 IsEqualTo(T value, T target, string name) - where T : notnull, IEquatable - { - if (!value.Equals(target)) - { - ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); - } - } - - /// - /// Asserts that the input value must be not equal to a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T target, string name) - where T : notnull, IEquatable - { - if (value.Equals(target)) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); - } - } - - /// - /// Asserts that the input value must be a bitwise match with a specified value. - /// - /// The type of input values to compare. - /// The input value to test. - /// The target value to test for. - /// The name of the input parameter being tested. - /// Thrown if is not a bitwise match for . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsBitwiseEqualTo(T value, T target, string name) - where T : unmanaged - { - /* Include some fast paths if the input type is of size 1, 2, 4 or 8. - * In those cases, just reinterpret the bytes as values of an integer type, - * and compare them directly, which is much faster than having explicitly - * loop through every single byte. This also allows for more expressive error - * messages, since the entire input values can be expressed as hexadecimal values. - * The conditional branches below are known at compile time by the JIT compiler, - * so that only the right one will actually be translated into native code. */ - if (typeof(T) == typeof(byte) || - typeof(T) == typeof(sbyte) || - typeof(T) == typeof(bool)) - { - byte valueByte = Unsafe.As(ref value); - byte targetByte = Unsafe.As(ref target); - - if (valueByte != targetByte) - { - ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); - } - } - else if (typeof(T) == typeof(ushort) || - typeof(T) == typeof(short) || - typeof(T) == typeof(char)) - { - ushort valueUShort = Unsafe.As(ref value); - ushort targetUShort = Unsafe.As(ref target); - - if (valueUShort != targetUShort) - { - ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); - } - } - else if (typeof(T) == typeof(uint) || - typeof(T) == typeof(int) || - typeof(T) == typeof(float)) - { - uint valueUInt = Unsafe.As(ref value); - uint targetUInt = Unsafe.As(ref target); - - if (valueUInt != targetUInt) - { - ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); - } - } - else if (typeof(T) == typeof(ulong) || - typeof(T) == typeof(long) || - typeof(T) == typeof(double)) - { - ulong valueULong = Unsafe.As(ref value); - ulong targetULong = Unsafe.As(ref target); - - if (valueULong != targetULong) - { - ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); - } - } - else - { - ref byte valueRef = ref Unsafe.As(ref value); - ref byte targetRef = ref Unsafe.As(ref target); - int bytesCount = Unsafe.SizeOf(); - - for (int i = 0; i < bytesCount; i++) - { - byte valueByte = Unsafe.Add(ref valueRef, i); - byte targetByte = Unsafe.Add(ref targetRef, i); - - if (valueByte != targetByte) - { - ThrowHelper.ThrowArgumentExceptionForsBitwiseEqualTo(value, target, name); - } - } - } - } - /// /// Asserts that the input value must be the same instance as the target value. /// @@ -341,201 +217,5 @@ public static void IsFalse(bool value, string name) ThrowHelper.ThrowArgumentExceptionForIsFalse(name); } } - - /// - /// Asserts that the input value must be less than a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T max, string name) - where T : notnull, IComparable - { - if (value.CompareTo(max) >= 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, max, name); - } - } - - /// - /// Asserts that the input value must be less than or equal to a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(maximum) > 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); - } - } - - /// - /// Asserts that the input value must be greater than a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T minimum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) <= 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); - } - } - - /// - /// Asserts that the input value must be greater than or equal to a specified value. - /// - /// The type of input values to compare. - /// 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(T value, T minimum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) < 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); - } - } - - /// - /// Asserts that the input value must be in a given range. - /// - /// The type of input values to compare. - /// 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 >= . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsInRange(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) >= 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name); - } - } - - /// - /// Asserts that the input value must not be in a given range. - /// - /// The type of input values to compare. - /// 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 < . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotInRange(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) < 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name); - } - } - - /// - /// Asserts that the input value must be in a given interval. - /// - /// The type of input values to compare. - /// 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 >= . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsBetween(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) <= 0 || value.CompareTo(maximum) >= 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name); - } - } - - /// - /// Asserts that the input value must not be in a given interval. - /// - /// The type of input values to compare. - /// 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 < . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotBetween(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) > 0 && value.CompareTo(maximum) < 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name); - } - } - - /// - /// Asserts that the input value must be in a given interval. - /// - /// The type of input values to compare. - /// 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 > . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) < 0 || value.CompareTo(maximum) > 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name); - } - } - - /// - /// Asserts that the input value must not be in a given interval. - /// - /// The type of input values to compare. - /// 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 <= . - /// The method is generic to avoid boxing the parameters, if they are value types. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) - where T : notnull, IComparable - { - if (value.CompareTo(minimum) >= 0 && value.CompareTo(maximum) <= 0) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); - } - } } } From fa8cc56eb306151bc4a2fe8fbe94470542ab859c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 00:51:47 +0100 Subject: [PATCH 079/123] Renamed template file, fixed incorrect XML doc --- .../{Guard.Numeric.tt => Guard.Comparable.Numeric.tt} | 1 - Microsoft.Toolkit/Microsoft.Toolkit.csproj | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) rename Microsoft.Toolkit/Diagnostics/Generated/{Guard.Numeric.tt => Guard.Comparable.Numeric.tt} (99%) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt similarity index 99% rename from Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt rename to Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 40e2b289582..2922e433695 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -220,7 +220,6 @@ foreach (var type in new[] { "byte", "sbyte", "short", "ushort", "char", "int", /// /// Asserts that the input value must not be in a given interval. /// - /// The type of input values to compare. /// The input value to test. /// The inclusive minimum value that is accepted. /// The inclusive maximum value that is accepted. diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index a19e81a35e2..1d92f82fa6f 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -21,9 +21,9 @@ - + TextTemplatingFileGenerator - Guard.Numeric.cs + Guard.Comparable.Numeric.cs @@ -32,10 +32,10 @@ - + True True - Guard.Numeric.tt + Guard.Comparable.Numeric.tt From effb2ed8a0d349eab5bda003d3f0bed9a95fffbf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 01:11:29 +0100 Subject: [PATCH 080/123] Fixed missing blank line --- .../Generated/Guard.Comparable.Numeric.tt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 2922e433695..090c544b212 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -1,6 +1,5 @@ <#@ template language="C#" #> <#@ assembly name="System.Core" #> - // 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. @@ -10,7 +9,6 @@ * ===================== */ using System; -using System.Diagnostics; using System.Runtime.CompilerServices; #nullable enable @@ -23,8 +21,19 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# -foreach (var type in new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }) +var types = new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }; +for (int i = 0; i < types.Length; i++) { + // Insert a blank line after the first type, to avoid having methods without a blank line in between + if (i > 0) + { +#> + +<# + } + + // Get the current type and generate the specialized APIs + var type = types[i]; #> /// /// Asserts that the input value must be equal to a specified value. From e2e5171f2cd2dec08271478706f469bdd0259357 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 01:20:21 +0100 Subject: [PATCH 081/123] Removed unnecessary type parameters --- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 65c8501ffa3..a2adafb01e6 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -89,7 +89,6 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) - where T : notnull, IEquatable { ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be == \"{target}\", was \"{value}\""); } @@ -99,7 +98,6 @@ public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, stri /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) - where T : notnull, IEquatable { ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be != \"{target}\", was \"{value}\""); } @@ -157,7 +155,6 @@ public static void ThrowArgumentExceptionForIsFalse(string name) /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{max}\", was \"{value}\""); } @@ -167,7 +164,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T m /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was \"{value}\""); } @@ -177,7 +173,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T v /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was \"{value}\""); } @@ -187,7 +182,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was \"{value}\""); } @@ -197,7 +191,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo( /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was \"{value}\""); } @@ -207,7 +200,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T mi /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); } @@ -217,7 +209,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was \"{value}\""); } @@ -227,7 +218,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T mi /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); } @@ -237,7 +227,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was \"{value}\""); } @@ -247,7 +236,6 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T va /// [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) - where T : notnull, IComparable { ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was \"{value}\""); } From 4482217c3f27614a56987a24e371a8396d11d779 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 02:51:42 +0100 Subject: [PATCH 082/123] Moved enumerable Guard APIs to separate file --- .../Diagnostics/Generated/Guard.Collection.tt | 246 ++++++++++ .../Generated/Guard.Comparable.Numeric.tt | 2 +- Microsoft.Toolkit/Diagnostics/Guard.Array.cs | 185 -------- .../Diagnostics/Guard.Enumerable.cs | 421 ------------------ Microsoft.Toolkit/Diagnostics/Guard.Memory.cs | 353 --------------- Microsoft.Toolkit/Diagnostics/Guard.Span.cs | 353 --------------- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 9 + 7 files changed, 256 insertions(+), 1313 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt delete mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Array.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Memory.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Span.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt new file mode 100644 index 00000000000..10e83bf5aab --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -0,0 +1,246 @@ +<#@ template language="C#" #> +<#@ assembly name="System.Core" #> +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { +<# +(string Type, string XmlType, string Name, string Count, string DestinationType, string Cast)[] items = +{ + ("Span", "", "span", "Length", "Span", ""), + ("ReadOnlySpan", "", "span", "Length", "Span", ""), + ("Memory", "", "memory", "Length", "Memory", ""), + ("ReadOnlyMemory", "", "memory", "Length", "Memory", ""), + ("T[]", " array", "array", "Length", "T[]", ""), + ("List", "", "list", "Count", "List", "(ICollection)"), + ("ICollection", "", "collection", "Count", "ICollection", ""), + ("IReadOnlyCollection", "", "collection", "Count", "ICollection", ""), +}; +for (int i = 0; i < items.Length; i++) +{ + // Insert a blank line after the first item + if (i > 0) + { +#> + +<# + } + + // Get the current item and generate the specialized APIs + var item = items[i]; +#> + /// + /// Asserts that the input <#=item.XmlType#> instance must be empty. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(<#=item.Type#> <#=item.Name#>, string name) + { + if (<#=item.Name#>.<#=item.Count#> != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must not be empty. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(<#=item.Type#> <#=item.Name#>, string name) + { + if (<#=item.Name#>.<#=item.Count#> == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size of a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size not equal to a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size over a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size of less than a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the input <#=item.XmlType#> instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input <#=item.XmlType#> instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + if (<#=item.Name#>.<#=item.Count#> > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name); + } + } + + /// + /// Asserts that the source <#=item.XmlType#> instance must have the same size of a destination <#=item.XmlType#> instance. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The source <#=item.XmlType#> instance to check the size for. + /// The destination <#=item.XmlType#> instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + if (source.<#=item.Count#> != destination.<#=item.Count#>) + { +<# + if (item.Count == "Count") + { +#> + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Count#>, name); +<# + } + else + { +#> + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name); +<# + } +#> + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination <#=item.XmlType#> instance. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The source <#=item.XmlType#> instance to check the size for. + /// The destination <#=item.XmlType#> instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + if (source.<#=item.Count#> > destination.<#=item.Count#>) + { +<# + if (item.Count == "Count") + { +#> + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Count#>, name); +<# + } + else + { +#> + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name); +<# + } +#> + } + } +<# +} +#> + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 090c544b212..aee7707cf0b 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -24,7 +24,7 @@ namespace Microsoft.Toolkit.Diagnostics var types = new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }; for (int i = 0; i < types.Length; i++) { - // Insert a blank line after the first type, to avoid having methods without a blank line in between + // Insert a blank line after the first type if (i > 0) { #> diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Array.cs b/Microsoft.Toolkit/Diagnostics/Guard.Array.cs deleted file mode 100644 index 05b49046775..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Guard.Array.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.Runtime.CompilerServices; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to verify conditions when running code. - /// - public static partial class Guard - { - /// - /// Asserts that the input array instance must be empty. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(T[] array, string name) - { - if (array.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(array, name); - } - } - - /// - /// Asserts that the input array instance must not be empty. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(T[] array, string name) - { - if (array.Length == 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); - } - } - - /// - /// Asserts that the input array instance must have a size of a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(T[] array, int size, string name) - { - if (array.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(array, size, name); - } - } - - /// - /// Asserts that the input array instance must have a size not equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(T[] array, int size, string name) - { - if (array.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(array, size, name); - } - } - - /// - /// Asserts that the input array instance must have a size over a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(T[] array, int size, string name) - { - if (array.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(array, size, name); - } - } - - /// - /// Asserts that the input array instance must have a size of at least or equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(T[] array, int size, string name) - { - if (array.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(array, size, name); - } - } - - /// - /// Asserts that the input array instance must have a size of less than a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(T[] array, int size, string name) - { - if (array.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(array, size, name); - } - } - - /// - /// Asserts that the input array instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input array instance. - /// The input array instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) - { - if (array.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(array, size, name); - } - } - - /// - /// Asserts that the source array instance must have the same size of a destination array instance. - /// - /// The type of items in the input array instance. - /// The source array instance to check the size for. - /// The destination array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(T[] source, T[] destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination array instance. - /// - /// The type of items in the input array instance. - /// The source array instance to check the size for. - /// The destination array instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs deleted file mode 100644 index 4b492b19d18..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Guard.Enumerable.cs +++ /dev/null @@ -1,421 +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.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to verify conditions when running code. - /// - public static partial class Guard - { - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(ICollection collection, string name) - { - if (collection.Count != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); - } - } - - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(IReadOnlyCollection collection, string name) - { - if (collection.Count != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); - } - } - - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(IEnumerable enumerable, string name) - { - if (!enumerable.Any()) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(enumerable, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(ICollection collection, string name) - { - if (collection.Count == 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(collection, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(IReadOnlyCollection collection, string name) - { - if (collection.Count == 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(collection, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(IEnumerable enumerable, string name) - { - if (enumerable.Any()) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(enumerable, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ICollection collection, int size, string name) - { - if (collection.Count != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(enumerable, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(ICollection collection, int size, string name) - { - if (collection.Count == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(enumerable, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(ICollection collection, int size, string name) - { - if (collection.Count <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(enumerable, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ICollection collection, int size, string name) - { - if (collection.Count < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(enumerable, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ICollection collection, int size, string name) - { - if (collection.Count >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(enumerable, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ICollection collection, int size, string name) - { - if (collection.Count > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) - { - if (collection.Count > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) - { - if (enumerable.Count() > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(enumerable, size, name); - } - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs b/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs deleted file mode 100644 index 08722efa5fc..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Guard.Memory.cs +++ /dev/null @@ -1,353 +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.Runtime.CompilerServices; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to verify conditions when running code. - /// - public static partial class Guard - { - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(ReadOnlyMemory memory, string name) - { - if (memory.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); - } - } - - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(Memory memory, string name) - { - if (memory.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(ReadOnlyMemory memory, string name) - { - if (memory.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(memory, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(Memory memory, string name) - { - if (memory.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(memory, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Memory memory, int size, string name) - { - if (memory.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(Memory memory, int size, string name) - { - if (memory.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(Memory memory, int size, string name) - { - if (memory.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(Memory memory, int size, string name) - { - if (memory.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(Memory memory, int size, string name) - { - if (memory.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Memory memory, int size, string name) - { - if (memory.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Memory source, Memory destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Span.cs b/Microsoft.Toolkit/Diagnostics/Guard.Span.cs deleted file mode 100644 index 3213f01d009..00000000000 --- a/Microsoft.Toolkit/Diagnostics/Guard.Span.cs +++ /dev/null @@ -1,353 +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.Runtime.CompilerServices; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to verify conditions when running code. - /// - public static partial class Guard - { - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(ReadOnlySpan span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsEmpty(Span span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(ReadOnlySpan span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must not be empty. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is == 0. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotEmpty(Span span, string name) - { - if (span.Length != 0) - { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(span, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is != . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Span span, int size, string name) - { - if (span.Length != size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size not equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is == . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeNotEqualTo(Span span, int size, string name) - { - if (span.Length == size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(ReadOnlySpan span, int size, string name) - { - if (span.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size over a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeOver(Span span, int size, string name) - { - if (span.Length <= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) - { - if (span.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of at least specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is < . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeAtLeast(Span span, int size, string name) - { - if (span.Length < size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) - { - if (span.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is >= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThan(Span span, int size, string name) - { - if (span.Length >= size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - if (span.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); - } - } - - /// - /// Asserts that the input instance must have a size of less than or equal to a specified value. - /// - /// The type of items in the input instance. - /// The input instance to check the size for. - /// The target size to test. - /// The name of the input parameter being tested. - /// Thrown if the size of is > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Span span, int size, string name) - { - if (span.Length > size) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(ReadOnlySpan source, Span destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have the same size of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is != the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeEqualTo(Span source, Span destination, string name) - { - if (source.Length != destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - - /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. - /// - /// The type of items in the input instance. - /// The source instance to check the size for. - /// The destination instance to check the size for. - /// The name of the input parameter being tested. - /// Thrown if the size of is > the one of . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void HasSizeLessThanOrEqualTo(Span source, Span destination, string name) - { - if (source.Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); - } - } - } -} diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 1d92f82fa6f..0e714adb1a9 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -25,6 +25,10 @@ TextTemplatingFileGenerator Guard.Comparable.Numeric.cs + + TextTemplatingFileGenerator + Guard.Collection.cs + @@ -37,6 +41,11 @@ True Guard.Comparable.Numeric.tt + + True + True + Guard.Collection.tt + \ No newline at end of file From 94872b9b7179880fa56deb93911c8a585e351261 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 13:01:22 +0100 Subject: [PATCH 083/123] Code refactoring in the enumerable T4 template --- .../Diagnostics/Generated/Guard.Collection.tt | 46 +++++------ .../Diagnostics/Generated/TypeInfo.ttinclude | 76 +++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 9 +++ 3 files changed, 102 insertions(+), 29 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 10e83bf5aab..be0b02a3e94 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -1,5 +1,4 @@ -<#@ template language="C#" #> -<#@ assembly name="System.Core" #> +<#@include file="TypeInfo.ttinclude" #> // 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. @@ -22,18 +21,7 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# -(string Type, string XmlType, string Name, string Count, string DestinationType, string Cast)[] items = -{ - ("Span", "", "span", "Length", "Span", ""), - ("ReadOnlySpan", "", "span", "Length", "Span", ""), - ("Memory", "", "memory", "Length", "Memory", ""), - ("ReadOnlyMemory", "", "memory", "Length", "Memory", ""), - ("T[]", " array", "array", "Length", "T[]", ""), - ("List", "", "list", "Count", "List", "(ICollection)"), - ("ICollection", "", "collection", "Count", "ICollection", ""), - ("IReadOnlyCollection", "", "collection", "Count", "ICollection", ""), -}; -for (int i = 0; i < items.Length; i++) + for (int i = 0; i < EnumerableTypes.Count; i++) { // Insert a blank line after the first item if (i > 0) @@ -44,7 +32,7 @@ for (int i = 0; i < items.Length; i++) } // Get the current item and generate the specialized APIs - var item = items[i]; + var item = EnumerableTypes[i]; #> /// /// Asserts that the input <#=item.XmlType#> instance must be empty. @@ -56,7 +44,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsEmpty(<#=item.Type#> <#=item.Name#>, string name) { - if (<#=item.Name#>.<#=item.Count#> != 0) + if (<#=item.Name#>.<#=item.Size#> != 0) { ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name); } @@ -72,7 +60,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotEmpty(<#=item.Type#> <#=item.Name#>, string name) { - if (<#=item.Name#>.<#=item.Count#> == 0) + if (<#=item.Name#>.<#=item.Size#> == 0) { ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); } @@ -89,7 +77,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> != size) + if (<#=item.Name#>.<#=item.Size#> != size) { ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name); } @@ -106,7 +94,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> == size) + if (<#=item.Name#>.<#=item.Size#> == size) { ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name); } @@ -123,7 +111,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeOver(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> <= size) + if (<#=item.Name#>.<#=item.Size#> <= size) { ThrowHelper.ThrowArgumentExceptionForHasSizeOver(<#=item.Cast#><#=item.Name#>, size, name); } @@ -140,7 +128,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeAtLeast(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> < size) + if (<#=item.Name#>.<#=item.Size#> < size) { ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Cast#><#=item.Name#>, size, name); } @@ -157,7 +145,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> >= size) + if (<#=item.Name#>.<#=item.Size#> >= size) { ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name); } @@ -174,7 +162,7 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - if (<#=item.Name#>.<#=item.Count#> > size) + if (<#=item.Name#>.<#=item.Size#> > size) { ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name); } @@ -191,13 +179,13 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - if (source.<#=item.Count#> != destination.<#=item.Count#>) + if (source.<#=item.Size#> != destination.<#=item.Size#>) { <# - if (item.Count == "Count") + if (item.HasCountProperty) { #> - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Count#>, name); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name); <# } else @@ -221,13 +209,13 @@ for (int i = 0; i < items.Length; i++) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void HasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - if (source.<#=item.Count#> > destination.<#=item.Count#>) + if (source.<#=item.Size#> > destination.<#=item.Size#>) { <# - if (item.Count == "Count") + if (item.HasCountProperty) { #> - ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Count#>, name); + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name); <# } else diff --git a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude new file mode 100644 index 00000000000..04812e00a2c --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude @@ -0,0 +1,76 @@ +<#@ template language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Collections.Generic" #> +<#+ + /// + /// A model representing the info on an enumerable type + /// + sealed class EnumerableTypeInfo + { + public EnumerableTypeInfo( + string type, + string xmlType, + string name, + string size, + string destinationType, + string cast) + { + Type = type; + XmlType = xmlType; + Name = name; + Size = size; + DestinationType = destinationType; + Cast = cast; + } + + /// + /// Gets the name of the current type + /// + public string Type { get; } + + /// + /// Gets the XML-formatted name of the current type (eg. with {T} instead of <T>) + /// + public string XmlType { get; } + + /// + /// Gets the variable name to use + /// + public string Name { get; } + + /// + /// Gets the name of the property to use to retrieve the length of the current type + /// + public string Size { get; } + + /// + /// Gets whether or not the current type has a "Count" property + /// + public bool HasCountProperty => Size == "Count"; + + /// + /// Gets the name of the destination type, when comparing counts across different collections + /// + public string DestinationType { get; } + + /// + /// Gets the (optional) casting to resolve the diamond problem between different interfaces + /// + public string Cast { get; } + } + + /// + /// Gets the list of available enumerable types to generate APIs for + /// + static readonly IReadOnlyList EnumerableTypes = new[] + { + new EnumerableTypeInfo("Span", "", "span", "Length", "Span", ""), + new EnumerableTypeInfo("ReadOnlySpan", "", "span", "Length", "Span", ""), + new EnumerableTypeInfo("Memory", "", "memory", "Length", "Memory", ""), + new EnumerableTypeInfo("ReadOnlyMemory", "", "memory", "Length", "Memory", ""), + new EnumerableTypeInfo("T[]", " array", "array", "Length", "T[]", ""), + new EnumerableTypeInfo("List", "", "list", "Count", "List", "(ICollection)"), + new EnumerableTypeInfo("ICollection", "", "collection", "Count", "ICollection", ""), + new EnumerableTypeInfo("IReadOnlyCollection", "", "collection", "Count", "ICollection", ""), + }; +#> \ No newline at end of file diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 0e714adb1a9..f45c058f9b5 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -29,6 +29,10 @@ TextTemplatingFileGenerator Guard.Collection.cs + + TextTemplatingFileGenerator + TypeInfo.cs + @@ -46,6 +50,11 @@ True Guard.Collection.tt + + True + True + TypeInfo.ttinclude + \ No newline at end of file From 756c8424afd8f960fc2d582d27bee9e548acd5fd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 13:02:53 +0100 Subject: [PATCH 084/123] Renamed a file --- .../{Guard.Comparable.cs => Guard.Comparable.Generic.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Microsoft.Toolkit/Diagnostics/{Guard.Comparable.cs => Guard.Comparable.Generic.cs} (100%) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs similarity index 100% rename from Microsoft.Toolkit/Diagnostics/Guard.Comparable.cs rename to Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs From 0ecef12b7201ce01718ea4ead08c2774d1d86b30 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 13:09:43 +0100 Subject: [PATCH 085/123] Added empty template for ThrowHelper.Collection --- .../Diagnostics/Generated/Guard.Collection.tt | 2 +- .../Generated/ThrowHelper.Collection.tt | 43 +++++++++++++++++++ Microsoft.Toolkit/Microsoft.Toolkit.csproj | 9 ++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index be0b02a3e94..34f01b78ba1 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -21,7 +21,7 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# - for (int i = 0; i < EnumerableTypes.Count; i++) +for (int i = 0; i < EnumerableTypes.Count; i++) { // Insert a blank line after the first item if (i > 0) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt new file mode 100644 index 00000000000..2da67d6d421 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -0,0 +1,43 @@ +<#@include file="TypeInfo.ttinclude" #> +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { +<# +for (int i = 0; i < EnumerableTypes.Count; i++) +{ + // Insert a blank line after the first item + if (i > 0) +{ +#> + +<# +} + + // Get the current item and generate the specialized APIs + var item = EnumerableTypes[i]; +#> + // TODO +<# +} +#> + } +} diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index f45c058f9b5..d0f3fe61447 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -29,6 +29,10 @@ TextTemplatingFileGenerator Guard.Collection.cs + + TextTemplatingFileGenerator + ThrowHelper.Collection.cs + TextTemplatingFileGenerator TypeInfo.cs @@ -50,6 +54,11 @@ True Guard.Collection.tt + + True + True + ThrowHelper.Collection.tt + True True From e093427c6877cbae1ffe03145a53aa7a3f712141 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 13:29:28 +0100 Subject: [PATCH 086/123] Code refactoring in the T4 templates --- .../Diagnostics/Generated/Guard.Collection.tt | 19 ++++------------ .../Generated/Guard.Comparable.Numeric.tt | 21 ++++-------------- .../Generated/ThrowHelper.Collection.tt | 22 ++++++------------- .../Diagnostics/Generated/TypeInfo.ttinclude | 20 +++++++++++++++++ 4 files changed, 35 insertions(+), 47 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 34f01b78ba1..ca5a47f806e 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -1,8 +1,7 @@ -<#@include file="TypeInfo.ttinclude" #> -// Licensed to the .NET Foundation under one or more agreements. +// 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. - +<#@include file="TypeInfo.ttinclude" #> /* ======================== * Auto generated file * ===================== */ @@ -21,18 +20,8 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# -for (int i = 0; i < EnumerableTypes.Count; i++) +GenerateTextForItems(EnumerableTypes, item => { - // Insert a blank line after the first item - if (i > 0) - { -#> - -<# - } - - // Get the current item and generate the specialized APIs - var item = EnumerableTypes[i]; #> /// /// Asserts that the input <#=item.XmlType#> instance must be empty. @@ -228,7 +217,7 @@ for (int i = 0; i < EnumerableTypes.Count; i++) } } <# -} +}); #> } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index aee7707cf0b..746d89012c5 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -1,9 +1,7 @@ -<#@ template language="C#" #> -<#@ assembly name="System.Core" #> -// Licensed to the .NET Foundation under one or more agreements. +// 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. - +<#@include file="TypeInfo.ttinclude" #> /* ======================== * Auto generated file * ===================== */ @@ -21,19 +19,8 @@ namespace Microsoft.Toolkit.Diagnostics public static partial class Guard { <# -var types = new[] { "byte", "sbyte", "short", "ushort", "char", "int", "uint", "float", "long", "ulong", "double", "decimal" }; -for (int i = 0; i < types.Length; i++) +GenerateTextForItems(NumericTypes, type => { - // Insert a blank line after the first type - if (i > 0) - { -#> - -<# - } - - // Get the current type and generate the specialized APIs - var type = types[i]; #> /// /// Asserts that the input value must be equal to a specified value. @@ -244,7 +231,7 @@ for (int i = 0; i < types.Length; i++) } } <# -} +}); #> } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 2da67d6d421..2fe82138d08 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -1,15 +1,17 @@ -<#@include file="TypeInfo.ttinclude" #> -// Licensed to the .NET Foundation under one or more agreements. +// 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. - +<#@include file="TypeInfo.ttinclude" #> /* ======================== * Auto generated file * ===================== */ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; #nullable enable @@ -22,22 +24,12 @@ namespace Microsoft.Toolkit.Diagnostics internal static partial class ThrowHelper { <# -for (int i = 0; i < EnumerableTypes.Count; i++) -{ - // Insert a blank line after the first item - if (i > 0) +GenerateTextForItems(EnumerableTypes, item => { -#> - -<# -} - - // Get the current item and generate the specialized APIs - var item = EnumerableTypes[i]; #> // TODO <# -} +}); #> } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude index 04812e00a2c..ac532148096 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude +++ b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude @@ -73,4 +73,24 @@ new EnumerableTypeInfo("ICollection", "", "collection", "Count", "ICollection", ""), new EnumerableTypeInfo("IReadOnlyCollection", "", "collection", "Count", "ICollection", ""), }; + + /// + /// 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" }; + + /// + /// Generates text for a given sequence of items, automatically adding the necessary spacing + /// + void GenerateTextForItems(IReadOnlyList items, Action factory) + { + for (int i = 0; i < items.Count; i++) + { + // Insert a blank line after the first item + if (i > 0) WriteLine(""); + + // Invoke the factory with the current item + factory(items [i]); + } + } #> \ No newline at end of file From 9db9cd0bcd923f74b9ddcc2d63b992a71c58c49a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 15:36:54 +0100 Subject: [PATCH 087/123] Switched collection throw helpers to T4 generation --- .../Diagnostics/Generated/Guard.Collection.tt | 21 +- .../Generated/ThrowHelper.Collection.tt | 82 +++++- .../Diagnostics/ThrowHelper.Array.cs | 110 -------- .../ThrowHelper.Collection.Generic.cs | 54 ++++ .../Diagnostics/ThrowHelper.Enumerable.cs | 238 ---------------- .../Diagnostics/ThrowHelper.Memory.cs | 260 ------------------ .../Diagnostics/ThrowHelper.Span.cs | 200 -------------- 7 files changed, 154 insertions(+), 811 deletions(-) delete mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs delete mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index ca5a47f806e..c52e1e17d40 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -51,7 +51,26 @@ GenerateTextForItems(EnumerableTypes, item => { if (<#=item.Name#>.<#=item.Size#> == 0) { - ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); +<# + if (item.Type == "Span") + { +#> + ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan(name); +<# + } + else if (item.Type == "ReadOnlySpan") + { +#> + ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(name); +<# + } + else + { +#> + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name); +<# + } +#> } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 2fe82138d08..73ad9bf5959 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -9,7 +9,6 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Toolkit.Extensions; @@ -27,7 +26,86 @@ namespace Microsoft.Toolkit.Diagnostics GenerateTextForItems(EnumerableTypes, item => { #> - // TODO + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(<#=item.Type#> <#=item.Name#>, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized != {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized > {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized >= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized < {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + } <# }); #> diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs deleted file mode 100644 index f24858478e7..00000000000 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Array.cs +++ /dev/null @@ -1,110 +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; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to throw exceptions - /// - [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] - internal static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs new file mode 100644 index 00000000000..3cfc7b60e27 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + /// This method is needed because can't be used as a generic type parameter. + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmptyWithSpan(string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must not be empty"); + } + + /// + /// Throws an when fails. + /// + /// This method is needed because can't be used as a generic type parameter. + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must not be empty"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEmpty(string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must not be empty"); + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs deleted file mode 100644 index 4173ed2838f..00000000000 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Enumerable.cs +++ /dev/null @@ -1,238 +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.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Runtime.CompilerServices; -using Microsoft.Toolkit.Extensions; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to throw exceptions - /// - [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] - internal static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be empty, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be empty, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(IEnumerable enumerable, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be empty, had a size of {enumerable.Count()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(ICollection collection, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(IReadOnlyCollection collection, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(IEnumerable enumerable, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized == {size}, had a size of {enumerable.Count()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized != {size}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized != {size}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized != {size}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized > {size}, had a size of {enumerable.Count()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized >= {size}, had a size of {enumerable.Count()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized < {size}, had a size of {enumerable.Count()}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({collection.GetType().ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IEnumerable enumerable, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({enumerable.GetType().ToTypeString()}) must be sized <= {size}, had a size of {enumerable.Count()}"); - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs deleted file mode 100644 index 65f036c50ac..00000000000 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Memory.cs +++ /dev/null @@ -1,260 +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; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to throw exceptions - /// - [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] - internal static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) - { - if (memory.Length != 0) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be empty, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) - { - if (memory.Length != 0) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be empty, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlyMemory memory, string name) - { - if (memory.Length != 0) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must not be empty"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(Memory memory, string name) - { - if (memory.Length != 0) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must not be empty"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length != size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) - { - if (memory.Length != size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length == size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) - { - if (memory.Length == size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length <= size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int size, string name) - { - if (memory.Length <= size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length < size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, int size, string name) - { - if (memory.Length < size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length >= size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) - { - if (memory.Length >= size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) - { - if (memory.Length > size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) - { - if (memory.Length > size) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({memory.GetType().ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - if (source.Length != destination.Length) - { - ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, Memory destination, string name) - { - if (source.Length != destination.Length) - { - ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) - { - if (source.Length > destination.Length) - { - ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); - } - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) - { - if (source.Length > destination.Length) - { - ThrowArgumentException(name, $"The source \"{name}\" ({source.GetType().ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); - } - } - } -} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs deleted file mode 100644 index b994e8101ea..00000000000 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Span.cs +++ /dev/null @@ -1,200 +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; - -#nullable enable - -namespace Microsoft.Toolkit.Diagnostics -{ - /// - /// Helper methods to throw exceptions - /// - [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] - internal static partial class ThrowHelper - { - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be empty, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be empty, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(ReadOnlySpan span, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEmpty(Span span, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must not be empty"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized == {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized == {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized != {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized != {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized >= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized < {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized < {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized <= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized <= {size}, had a size of {span.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized == {destination.Length}, had a size of {source.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized == {destination.Length}, had a size of {source.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" (System.ReadOnlySpan<{typeof(T).ToTypeString()}>) must be sized <= {destination.Length}, had a size of {source.Length}"); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) - { - ThrowArgumentException(name, $"The source \"{name}\" (System.Span<{typeof(T).ToTypeString()}>) must be sized <= {destination.Length}, had a size of {source.Length}"); - } - } -} From f6e3248a331b7fba5ede30820e123cb3001e8306 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 15:39:31 +0100 Subject: [PATCH 088/123] Code refactoring --- .../ThrowHelper.Comparable.Generic.cs | 128 ++++++++++++++++++ Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 108 --------------- 2 files changed, 128 insertions(+), 108 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs new file mode 100644 index 00000000000..596c6fd69bb --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs @@ -0,0 +1,128 @@ +// 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; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be == \"{target}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be != \"{target}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{max}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was \"{value}\""); + } + + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was \"{value}\""); + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index a2adafb01e6..d53069fdc10 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -84,24 +84,6 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ ThrowArgumentException(name, $"Parameter \"{name}\" must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be == \"{target}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) - { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be != \"{target}\", was \"{value}\""); - } - /// /// Throws an when fails. /// @@ -149,95 +131,5 @@ public static void ThrowArgumentExceptionForIsFalse(string name) { ThrowArgumentException(name, $"Parameter \"{name}\" must be false, was true"); } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{max}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was \"{value}\""); - } - - /// - /// Throws an when fails. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) - { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was \"{value}\""); - } } } From fcc3c050455f63f187446be740465cdd1486a0e5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 16:42:26 +0100 Subject: [PATCH 089/123] Removed incorrect file header --- .../Diagnostics/ThrowHelper.Collection.Generic.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs index 3cfc7b60e27..72c87ed3312 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs @@ -2,10 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -/* ======================== - * Auto generated file - * ===================== */ - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; From 760972f613ff2c7ba332af19ef4f29ca6d2dbeaf Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Mar 2020 16:59:59 +0100 Subject: [PATCH 090/123] Added new Guard.IsInRangeFor API --- .../Diagnostics/Generated/Guard.Collection.tt | 17 +++++++++++++++++ .../Generated/ThrowHelper.Collection.tt | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index c52e1e17d40..04983e5e44c 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -235,6 +235,23 @@ GenerateTextForItems(EnumerableTypes, item => #> } } + + /// + /// Asserts that the input index is valid for a given <#=item.XmlType#> instance. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input index to be used to access . + /// The input <#=item.XmlType#> instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name); + } + } <# }); #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 73ad9bf5959..d88877f820d 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -106,6 +106,15 @@ GenerateTextForItems(EnumerableTypes, item => { ThrowArgumentException(name, $"The source \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {<#=item.Name#>.<#=item.Size#>} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index}"); + } <# }); #> From 8d6cc8a64bbe48294c4374865f0904f443f4f17f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 3 Mar 2020 11:57:28 +0100 Subject: [PATCH 091/123] Updated .gitignore, added generated files to source control --- .gitignore | 3 - .../Diagnostics/Generated/Guard.Collection.cs | 1502 ++++++++++ .../Generated/Guard.Comparable.Numeric.cs | 2529 +++++++++++++++++ .../Generated/ThrowHelper.Collection.cs | 745 +++++ 4 files changed, 4776 insertions(+), 3 deletions(-) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs diff --git a/.gitignore b/.gitignore index f63fe7164e0..ed0e87e25e0 100644 --- a/.gitignore +++ b/.gitignore @@ -225,6 +225,3 @@ msbuild.binlog *.project.lock.json /build/tools/** !/build/tools/packages.config - -# T4 generated files -*/Diagnostics/Generated/*.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs new file mode 100644 index 00000000000..cbb08d495b1 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs @@ -0,0 +1,1502 @@ +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(Span span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(Span span, string name) + { + if (span.Length == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span span, int size, string name) + { + if (span.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(Span span, int size, string name) + { + if (span.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(Span span, int size, string name) + { + if (span.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(Span span, int size, string name) + { + if (span.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(Span span, int size, string name) + { + if (span.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span span, int size, string name) + { + if (span.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Span source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, Span span, string name) + { + if ((uint)index >= (uint)span.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, span, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ReadOnlySpan span, string name) + { + if (span.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(span, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ReadOnlySpan span, string name) + { + if (span.Length == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(ReadOnlySpan span, int size, string name) + { + if (span.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + if (span.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlySpan span, int size, string name) + { + if (span.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(span, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + if (span.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(span, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, ReadOnlySpan span, string name) + { + if ((uint)index >= (uint)span.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, span, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(Memory memory, string name) + { + if (memory.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(Memory memory, string name) + { + if (memory.Length == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty>(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Memory memory, int size, string name) + { + if (memory.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(Memory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(Memory memory, int size, string name) + { + if (memory.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(Memory memory, int size, string name) + { + if (memory.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(Memory memory, int size, string name) + { + if (memory.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Memory memory, int size, string name) + { + if (memory.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(Memory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, Memory memory, string name) + { + if ((uint)index >= (uint)memory.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, memory, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(memory, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ReadOnlyMemory memory, string name) + { + if (memory.Length == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty>(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(memory, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + if (memory.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(memory, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, ReadOnlyMemory memory, string name) + { + if ((uint)index >= (uint)memory.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, memory, name); + } + } + + /// + /// Asserts that the input array instance must be empty. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(T[] array, string name) + { + if (array.Length != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(array, name); + } + } + + /// + /// Asserts that the input array instance must not be empty. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(T[] array, string name) + { + if (array.Length == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(name); + } + } + + /// + /// Asserts that the input array instance must have a size of a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] array, int size, string name) + { + if (array.Length != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(array, size, name); + } + } + + /// + /// Asserts that the input array instance must have a size not equal to a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(T[] array, int size, string name) + { + if (array.Length == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(array, size, name); + } + } + + /// + /// Asserts that the input array instance must have a size over a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(T[] array, int size, string name) + { + if (array.Length <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(array, size, name); + } + } + + /// + /// Asserts that the input array instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(T[] array, int size, string name) + { + if (array.Length < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(array, size, name); + } + } + + /// + /// Asserts that the input array instance must have a size of less than a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(T[] array, int size, string name) + { + if (array.Length >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(array, size, name); + } + } + + /// + /// Asserts that the input array instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input array instance. + /// The input array instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + if (array.Length > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(array, size, name); + } + } + + /// + /// Asserts that the source array instance must have the same size of a destination array instance. + /// + /// The item of items in the input array instance. + /// The source array instance to check the size for. + /// The destination array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(T[] source, T[] destination, string name) + { + if (source.Length != destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination array instance. + /// + /// The item of items in the input array instance. + /// The source array instance to check the size for. + /// The destination array instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + if (source.Length > destination.Length) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, destination, name); + } + } + + /// + /// Asserts that the input index is valid for a given array instance. + /// + /// The item of items in the input array instance. + /// The input index to be used to access . + /// The input array instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, T[] array, string name) + { + if ((uint)index >= (uint)array.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, array, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(List list, string name) + { + if (list.Count != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty((ICollection)list, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(List list, string name) + { + if (list.Count == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty>(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(List list, int size, string name) + { + if (list.Count != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo((ICollection)list, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(List list, int size, string name) + { + if (list.Count == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo((ICollection)list, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(List list, int size, string name) + { + if (list.Count <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver((ICollection)list, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(List list, int size, string name) + { + if (list.Count < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast((ICollection)list, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(List list, int size, string name) + { + if (list.Count >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan((ICollection)list, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(List list, int size, string name) + { + if (list.Count > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo((ICollection)list, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(List source, List destination, string name) + { + if (source.Count != destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo((ICollection)source, destination.Count, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(List source, List destination, string name) + { + if (source.Count > destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo((ICollection)source, destination.Count, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, List list, string name) + { + if ((uint)index >= (uint)list.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, (ICollection)list, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(ICollection collection, string name) + { + if (collection.Count != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(ICollection collection, string name) + { + if (collection.Count == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty>(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ICollection collection, int size, string name) + { + if (collection.Count != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(ICollection collection, int size, string name) + { + if (collection.Count == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(ICollection collection, int size, string name) + { + if (collection.Count <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(ICollection collection, int size, string name) + { + if (collection.Count < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(ICollection collection, int size, string name) + { + if (collection.Count >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ICollection collection, int size, string name) + { + if (collection.Count > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(ICollection source, ICollection destination, string name) + { + if (source.Count != destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination.Count, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) + { + if (source.Count > destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination.Count, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, ICollection collection, string name) + { + if ((uint)index >= (uint)collection.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, collection, name); + } + } + + /// + /// Asserts that the input instance must be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsEmpty(IReadOnlyCollection collection, string name) + { + if (collection.Count != 0) + { + ThrowHelper.ThrowArgumentExceptionForIsEmpty(collection, name); + } + } + + /// + /// Asserts that the input instance must not be empty. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is == 0. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotEmpty(IReadOnlyCollection collection, string name) + { + if (collection.Count == 0) + { + ThrowHelper.ThrowArgumentExceptionForIsNotEmpty>(name); + } + } + + /// + /// Asserts that the input instance must have a size of a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is != . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count != size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size not equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is == . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count == size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size over a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is <= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeOver(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count <= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeOver(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of at least or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is < . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeAtLeast(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count < size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is >= . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThan(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count >= size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(collection, size, name); + } + } + + /// + /// Asserts that the input instance must have a size of less than or equal to a specified value. + /// + /// The item of items in the input instance. + /// The input instance to check the size for. + /// The target size to test. + /// The name of the input parameter being tested. + /// Thrown if the size of is > . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + if (collection.Count > size) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(collection, size, name); + } + } + + /// + /// Asserts that the source instance must have the same size of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is != the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + if (source.Count != destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination.Count, name); + } + } + + /// + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// + /// The item of items in the input instance. + /// The source instance to check the size for. + /// The destination instance to check the size for. + /// The name of the input parameter being tested. + /// Thrown if the size of is > the one of . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void HasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + if (source.Count > destination.Count) + { + ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, destination.Count, name); + } + } + + /// + /// Asserts that the input index is valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is not valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRangeFor(int index, IReadOnlyCollection collection, string name) + { + if ((uint)index >= (uint)collection.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, collection, name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs new file mode 100644 index 00000000000..cc9a9c90a9e --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs @@ -0,0 +1,2529 @@ +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// 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(byte value, byte target, string name) + { + if (value != target) + { + 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(byte value, byte target, string name) + { + if (value == target) + { + 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(byte value, byte maximum, string name) + { + if (value >= maximum) + { + 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(byte value, byte maximum, string name) + { + if (value > maximum) + { + 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(byte value, byte minimum, string name) + { + if (value <= minimum) + { + 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(byte value, byte minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(byte value, byte minimum, byte maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(byte value, byte minimum, byte maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(byte value, byte minimum, byte maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(byte value, byte minimum, byte maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(byte value, byte minimum, byte maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(byte value, byte minimum, byte maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(sbyte value, sbyte target, string name) + { + if (value != target) + { + 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(sbyte value, sbyte target, string name) + { + if (value == target) + { + 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(sbyte value, sbyte maximum, string name) + { + if (value >= maximum) + { + 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(sbyte value, sbyte maximum, string name) + { + if (value > maximum) + { + 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(sbyte value, sbyte minimum, string name) + { + if (value <= minimum) + { + 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(sbyte value, sbyte minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(sbyte value, sbyte minimum, sbyte maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(short value, short target, string name) + { + if (value != target) + { + 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(short value, short target, string name) + { + if (value == target) + { + 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(short value, short maximum, string name) + { + if (value >= maximum) + { + 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(short value, short maximum, string name) + { + if (value > maximum) + { + 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(short value, short minimum, string name) + { + if (value <= minimum) + { + 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(short value, short minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(short value, short minimum, short maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(short value, short minimum, short maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(short value, short minimum, short maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(short value, short minimum, short maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(short value, short minimum, short maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(short value, short minimum, short maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(ushort value, ushort target, string name) + { + if (value != target) + { + 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(ushort value, ushort target, string name) + { + if (value == target) + { + 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(ushort value, ushort maximum, string name) + { + if (value >= maximum) + { + 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(ushort value, ushort maximum, string name) + { + if (value > maximum) + { + 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(ushort value, ushort minimum, string name) + { + if (value <= minimum) + { + 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(ushort value, ushort minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(ushort value, ushort minimum, ushort maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(ushort value, ushort minimum, ushort maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(ushort value, ushort minimum, ushort maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(ushort value, ushort minimum, ushort maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(ushort value, ushort minimum, ushort maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(ushort value, ushort minimum, ushort maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(char value, char target, string name) + { + if (value != target) + { + 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(char value, char target, string name) + { + if (value == target) + { + 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(char value, char maximum, string name) + { + if (value >= maximum) + { + 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(char value, char maximum, string name) + { + if (value > maximum) + { + 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(char value, char minimum, string name) + { + if (value <= minimum) + { + 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(char value, char minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(char value, char minimum, char maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(char value, char minimum, char maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(char value, char minimum, char maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(char value, char minimum, char maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(char value, char minimum, char maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(char value, char minimum, char maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(int value, int target, string name) + { + if (value != target) + { + 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(int value, int target, string name) + { + if (value == target) + { + 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(int value, int maximum, string name) + { + if (value >= maximum) + { + 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(int value, int maximum, string name) + { + if (value > maximum) + { + 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(int value, int minimum, string name) + { + if (value <= minimum) + { + 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(int value, int minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(int value, int minimum, int maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(int value, int minimum, int maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(int value, int minimum, int maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(int value, int minimum, int maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(int value, int minimum, int maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(int value, int minimum, int maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(uint value, uint target, string name) + { + if (value != target) + { + 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(uint value, uint target, string name) + { + if (value == target) + { + 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(uint value, uint maximum, string name) + { + if (value >= maximum) + { + 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(uint value, uint maximum, string name) + { + if (value > maximum) + { + 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(uint value, uint minimum, string name) + { + if (value <= minimum) + { + 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(uint value, uint minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(uint value, uint minimum, uint maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(uint value, uint minimum, uint maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(uint value, uint minimum, uint maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(uint value, uint minimum, uint maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(uint value, uint minimum, uint maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(uint value, uint minimum, uint maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(float value, float target, string name) + { + if (value != target) + { + 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(float value, float target, string name) + { + if (value == target) + { + 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(float value, float maximum, string name) + { + if (value >= maximum) + { + 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(float value, float maximum, string name) + { + if (value > maximum) + { + 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(float value, float minimum, string name) + { + if (value <= minimum) + { + 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(float value, float minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(float value, float minimum, float maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(float value, float minimum, float maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(float value, float minimum, float maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(float value, float minimum, float maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(float value, float minimum, float maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(float value, float minimum, float maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(long value, long target, string name) + { + if (value != target) + { + 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(long value, long target, string name) + { + if (value == target) + { + 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(long value, long maximum, string name) + { + if (value >= maximum) + { + 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(long value, long maximum, string name) + { + if (value > maximum) + { + 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(long value, long minimum, string name) + { + if (value <= minimum) + { + 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(long value, long minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(long value, long minimum, long maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(long value, long minimum, long maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(long value, long minimum, long maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(long value, long minimum, long maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(long value, long minimum, long maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(long value, long minimum, long maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(ulong value, ulong target, string name) + { + if (value != target) + { + 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(ulong value, ulong target, string name) + { + if (value == target) + { + 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(ulong value, ulong maximum, string name) + { + if (value >= maximum) + { + 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(ulong value, ulong maximum, string name) + { + if (value > maximum) + { + 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(ulong value, ulong minimum, string name) + { + if (value <= minimum) + { + 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(ulong value, ulong minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(ulong value, ulong minimum, ulong maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(ulong value, ulong minimum, ulong maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(ulong value, ulong minimum, ulong maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(ulong value, ulong minimum, ulong maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(ulong value, ulong minimum, ulong maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(ulong value, ulong minimum, ulong maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(double value, double target, string name) + { + if (value != target) + { + 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(double value, double target, string name) + { + if (value == target) + { + 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(double value, double maximum, string name) + { + if (value >= maximum) + { + 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(double value, double maximum, string name) + { + if (value > maximum) + { + 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(double value, double minimum, string name) + { + if (value <= minimum) + { + 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(double value, double minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(double value, double minimum, double maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(double value, double minimum, double maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(double value, double minimum, double maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(double value, double minimum, double maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(double value, double minimum, double maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(double value, double minimum, double maximum, string name) + { + if (value >= minimum && value <= maximum) + { + 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(decimal value, decimal target, string name) + { + if (value != target) + { + 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(decimal value, decimal target, string name) + { + if (value == target) + { + 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(decimal value, decimal maximum, string name) + { + if (value >= maximum) + { + 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(decimal value, decimal maximum, string name) + { + if (value > maximum) + { + 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(decimal value, decimal minimum, string name) + { + if (value <= minimum) + { + 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(decimal value, decimal minimum, string name) + { + if (value < minimum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsInRange(decimal value, decimal minimum, decimal maximum, string name) + { + if (value < minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRange(decimal value, decimal minimum, decimal maximum, string name) + { + if (value >= minimum && value < maximum) + { + 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 >= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetween(decimal value, decimal minimum, decimal maximum, string name) + { + if (value <= minimum || value >= maximum) + { + 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 < . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetween(decimal value, decimal minimum, decimal maximum, string name) + { + if (value > minimum && value < maximum) + { + 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 > . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsBetweenOrEqualTo(decimal value, decimal minimum, decimal maximum, string name) + { + if (value < minimum || value > maximum) + { + 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 <= . + /// The method is generic to avoid boxing the parameters, if they are value types. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotBetweenOrEqualTo(decimal value, decimal minimum, decimal maximum, string name) + { + if (value >= minimum && value <= maximum) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs new file mode 100644 index 00000000000..281370b9203 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs @@ -0,0 +1,745 @@ +// 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. + +/* ======================== + * Auto generated file + * ===================== */ + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Extensions; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Span).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Span).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, Memory destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Memory memory, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {array.Length} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(List list, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized == {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized != {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized > {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized >= {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized < {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List list, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized <= {size}, had a size of {list.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, List destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(List).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List source, List destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(List).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {list.Count} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection source, ICollection destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) + { + ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) + { + ThrowArgumentException(name, $"The source \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + } + + /// + /// Throws an when (or an overload) fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) + { + ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index}"); + } + } +} From e3d245fc38cd91b1041c4ed09c8b0465d8e24561 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 3 Mar 2020 23:38:24 +0100 Subject: [PATCH 092/123] Added tests for Guard.IsInRangeFor --- UnitTests/Diagnostics/Test_Guard.cs | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs index 8bf9a58b55a..de1a464a400 100644 --- a/UnitTests/Diagnostics/Test_Guard.cs +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -391,6 +391,47 @@ public void Test_Guard_IsNotInRange_InnerFail() Guard.IsNotInRange(2, 0, 4, nameof(Test_Guard_IsNotInRange_InnerFail)); } + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsInRangeFor_Ok() + { + Span span = stackalloc int[10]; + + Guard.IsInRangeFor(0, span, nameof(Test_Guard_IsInRangeFor_Ok)); + Guard.IsInRangeFor(4, span, nameof(Test_Guard_IsInRangeFor_Ok)); + Guard.IsInRangeFor(9, span, nameof(Test_Guard_IsInRangeFor_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRangeFor_LowerFail() + { + Span span = stackalloc int[10]; + + Guard.IsInRangeFor(-2, span, nameof(Test_Guard_IsInRangeFor_LowerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRangeFor_EqualFail() + { + Span span = stackalloc int[10]; + + Guard.IsInRangeFor(10, span, nameof(Test_Guard_IsInRangeFor_EqualFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsInRangeFor_HigherFail() + { + Span span = stackalloc int[10]; + + Guard.IsInRangeFor(99, span, nameof(Test_Guard_IsInRangeFor_HigherFail)); + } + [TestCategory("Guard")] [TestMethod] public void Test_Guard_IsBetween_Ok() From d367add05159ba3157e9acfe1b01627e63328f94 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Mar 2020 21:58:47 +0100 Subject: [PATCH 093/123] Improved formatting of values in error messages --- .../Generated/ThrowHelper.Collection.cs | 160 +++++++++--------- .../Generated/ThrowHelper.Collection.tt | 20 +-- .../ThrowHelper.Collection.Generic.cs | 7 +- .../ThrowHelper.Comparable.Generic.cs | 26 +-- .../Diagnostics/ThrowHelper.IO.cs | 8 +- .../Diagnostics/ThrowHelper.String.cs | 28 +-- .../Diagnostics/ThrowHelper.Tasks.cs | 20 +-- .../ThrowHelper.ThrowExceptions.cs | 17 ++ Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 24 +-- 9 files changed, 163 insertions(+), 147 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs index 281370b9203..cabcd9733d3 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs @@ -28,7 +28,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length}"); } /// @@ -37,7 +37,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Span span, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); } /// @@ -46,7 +46,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); } /// @@ -55,7 +55,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, i [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); } /// @@ -64,7 +64,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Span span, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -73,7 +73,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); } /// @@ -82,7 +82,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); } /// @@ -91,7 +91,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Span).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -100,7 +100,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Span).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -109,7 +109,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index}"); } /// @@ -118,7 +118,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length}"); } /// @@ -127,7 +127,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, str [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); } /// @@ -136,7 +136,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); } /// @@ -145,7 +145,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); } /// @@ -154,7 +154,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); } /// @@ -163,7 +163,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); } /// @@ -172,7 +172,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); } /// @@ -181,7 +181,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -190,7 +190,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan so [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -199,7 +199,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index}"); } /// @@ -208,7 +208,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length}"); } /// @@ -217,7 +217,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); } /// @@ -226,7 +226,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); } /// @@ -235,7 +235,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memor [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); } /// @@ -244,7 +244,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); } /// @@ -253,7 +253,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); } /// @@ -262,7 +262,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); } /// @@ -271,7 +271,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory(Memory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -280,7 +280,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(Memory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -289,7 +289,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory(int index, Memory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index}"); } /// @@ -298,7 +298,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length}"); } /// @@ -307,7 +307,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); } /// @@ -316,7 +316,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); } /// @@ -325,7 +325,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory< [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); } /// @@ -334,7 +334,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory mem [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); } /// @@ -343,7 +343,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); } /// @@ -352,7 +352,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); } /// @@ -361,7 +361,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -370,7 +370,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -379,7 +379,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index}"); } /// @@ -388,7 +388,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); } /// @@ -397,7 +397,7 @@ public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); } /// @@ -406,7 +406,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); } /// @@ -415,7 +415,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); } /// @@ -424,7 +424,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); } /// @@ -433,7 +433,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); } /// @@ -442,7 +442,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int si [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); } /// @@ -451,7 +451,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] arra [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); } /// @@ -460,7 +460,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] de [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); } /// @@ -469,7 +469,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] sour [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {array.Length} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {array.Length} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index}"); } /// @@ -478,7 +478,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(List list, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count}"); } /// @@ -487,7 +487,7 @@ public static void ThrowArgumentExceptionForIsEmpty(List list, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized == {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized == {size}, had a size of {list.Count}"); } /// @@ -496,7 +496,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized != {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized != {size}, had a size of {list.Count}"); } /// @@ -505,7 +505,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, i [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized > {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized > {size}, had a size of {list.Count}"); } /// @@ -514,7 +514,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(List list, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized >= {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized >= {size}, had a size of {list.Count}"); } /// @@ -523,7 +523,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized < {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized < {size}, had a size of {list.Count}"); } /// @@ -532,7 +532,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(List).ToTypeString()}) must be sized <= {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized <= {size}, had a size of {list.Count}"); } /// @@ -541,7 +541,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, List destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(List).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); } /// @@ -550,7 +550,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, Li [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List source, List destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(List).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); } /// @@ -559,7 +559,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {list.Count} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {list.Count} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index}"); } /// @@ -568,7 +568,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); } /// @@ -577,7 +577,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ICollection collection [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); } /// @@ -586,7 +586,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); } /// @@ -595,7 +595,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); } /// @@ -604,7 +604,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ICollection collec [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); } /// @@ -613,7 +613,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); } /// @@ -622,7 +622,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); } /// @@ -631,7 +631,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); } /// @@ -640,7 +640,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection sou [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(ICollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); } /// @@ -649,7 +649,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index}"); } /// @@ -658,7 +658,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); } /// @@ -667,7 +667,7 @@ public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); } /// @@ -676,7 +676,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); } /// @@ -685,7 +685,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollec [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); } /// @@ -694,7 +694,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); } /// @@ -703,7 +703,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); } /// @@ -712,7 +712,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollecti [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); } /// @@ -721,7 +721,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); } /// @@ -730,7 +730,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); } /// @@ -739,7 +739,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index d88877f820d..e80f2643437 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -32,7 +32,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(<#=item.Type#> <#=item.Name#>, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -41,7 +41,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -50,7 +50,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized != {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized != {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -59,7 +59,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized > {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized > {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -68,7 +68,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized >= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized >= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -77,7 +77,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized < {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized < {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -86,7 +86,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); } /// @@ -95,7 +95,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); } /// @@ -104,7 +104,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - ThrowArgumentException(name, $"The source \"{name}\" ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); } /// @@ -113,7 +113,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" (int) must be >= 0 and < {<#=item.Name#>.<#=item.Size#>} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {<#=item.Name#>.<#=item.Size#>} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index}"); } <# }); diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs index 72c87ed3312..7f1a57d1b80 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Collection.Generic.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using Microsoft.Toolkit.Extensions; @@ -25,7 +24,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmptyWithSpan(string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(Span).ToTypeString()}) must not be empty"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must not be empty"); } /// @@ -35,7 +34,7 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithSpan(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(ReadOnlySpan).ToTypeString()}) must not be empty"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must not be empty"); } /// @@ -44,7 +43,7 @@ public static void ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan(string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must not be empty"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be empty"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs index 596c6fd69bb..f6ebea0916b 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs @@ -23,7 +23,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be == \"{target}\", was \"{value}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be == {target.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -32,16 +32,16 @@ public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, stri [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be != \"{target}\", was \"{value}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be != {target.ToAssertString()}, was {value.ToAssertString()}"); } /// /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T max, string name) + public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{max}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -50,7 +50,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T m [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -59,7 +59,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T v [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was {value.ToAssertString()}"); } /// @@ -68,7 +68,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was {value.ToAssertString()}"); } /// @@ -77,7 +77,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo( [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -86,7 +86,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -95,7 +95,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -104,7 +104,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -113,7 +113,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was {value.ToAssertString()}"); } /// @@ -122,7 +122,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T va [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was \"{value}\""); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was {value.ToAssertString()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs index 525048a29e8..5dada175c32 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.IO.cs @@ -24,7 +24,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForCanRead(Stream stream, string name) { - ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support reading"); + ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support reading"); } /// @@ -33,7 +33,7 @@ public static void ThrowArgumentExceptionForCanRead(Stream stream, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForCanWrite(Stream stream, string name) { - ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support writing"); + ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support writing"); } /// @@ -42,7 +42,7 @@ public static void ThrowArgumentExceptionForCanWrite(Stream stream, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForCanSeek(Stream stream, string name) { - ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) doesn't support seeking"); + ThrowArgumentException(name, $"Stream {name.ToAssertString()} ({stream.GetType().ToTypeString()}) doesn't support seeking"); } /// @@ -51,7 +51,7 @@ public static void ThrowArgumentExceptionForCanSeek(Stream stream, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAtStartPosition(Stream stream, string name) { - ThrowArgumentException(name, $"Stream \"{name}\" ({stream.GetType().ToTypeString()}) must be at start position, was at {stream.Position}"); + 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/ThrowHelper.String.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs index e0360ff327f..1dcceb6da55 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs @@ -22,7 +22,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be null or empty, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or empty, was {text.ToAssertString()}"); } /// @@ -31,7 +31,7 @@ public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string n [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be null or empty, was {(text is null ? "null" : "empty")}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or empty, was {(text is null ? "null" : "empty")}"); } /// @@ -40,7 +40,7 @@ public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, strin [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be null or whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be null or whitespace, was {text.ToAssertString()}"); } /// @@ -49,7 +49,7 @@ public static void ThrowArgumentExceptionForIsNullOrWhitespace(string? text, str [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}"); } /// @@ -58,7 +58,7 @@ public static void ThrowArgumentExceptionForIsNotNullOrWhitespace(string? text, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(string text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be empty, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be empty, was {text.ToAssertString()}"); } /// @@ -67,7 +67,7 @@ public static void ThrowArgumentExceptionForIsEmpty(string text, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be empty"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be empty"); } /// @@ -76,7 +76,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(string text, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsWhitespace(string text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be whitespace, was {text.ToAssertString()}"); } /// @@ -85,7 +85,7 @@ public static void ThrowArgumentExceptionForIsWhitespace(string text, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must not be whitespace, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not be whitespace, was {text.ToAssertString()}"); } /// @@ -94,7 +94,7 @@ public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized == {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized == {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -103,7 +103,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized != {size}, was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized != {size}, was {text.ToAssertString()}"); } /// @@ -112,7 +112,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized > {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized > {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -121,7 +121,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized >= {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized >= {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -130,7 +130,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized < {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized < {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -139,7 +139,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" (string) must be sized <= {size}, had a size of {text.Length} and was \"{text}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized <= {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs index 0b3a88dd1a7..5013c790aac 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -24,7 +24,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed, had status {task.Status}"); } /// @@ -33,7 +33,7 @@ public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status}"); } /// @@ -42,7 +42,7 @@ public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status}"); } /// @@ -51,7 +51,7 @@ public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status}"); } /// @@ -60,7 +60,7 @@ public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status}"); } /// @@ -69,7 +69,7 @@ public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status}"); } /// @@ -78,7 +78,7 @@ public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status}"); } /// @@ -87,7 +87,7 @@ public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status}"); } /// @@ -96,7 +96,7 @@ public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStatus status, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status}"); } /// @@ -105,7 +105,7 @@ public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStat [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" ({task.GetType().ToTypeString()}) must not have status {status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not have status {status}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs index 084942de373..52e270677e9 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.ThrowExceptions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; #nullable enable @@ -14,6 +15,22 @@ namespace Microsoft.Toolkit.Diagnostics /// internal 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 a new . /// diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index d53069fdc10..8f0c90352c4 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -25,7 +25,7 @@ internal static partial class ThrowHelper public static void ThrowArgumentExceptionForIsNull(T value, string name) where T : class { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be null, was \"{value}\" ({value.GetType().ToTypeString()})"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be null, was {value.ToAssertString()} ({value.GetType().ToTypeString()})"); } /// @@ -35,7 +35,7 @@ public static void ThrowArgumentExceptionForIsNull(T value, string name) public static void ThrowArgumentExceptionForIsNull(T? value, string name) where T : struct { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T?).ToTypeString()}) must be null, was \"{value}\" ({typeof(T).ToTypeString()})"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T?).ToTypeString()}) must be null, was {value.ToAssertString()} ({typeof(T).ToTypeString()})"); } /// @@ -44,7 +44,7 @@ public static void ThrowArgumentExceptionForIsNull(T? value, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentNullExceptionForIsNotNull(string name) { - ThrowArgumentNullException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be not null)"); + ThrowArgumentNullException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be not null)"); } #pragma warning restore CS0419 @@ -54,7 +54,7 @@ public static void ThrowArgumentNullExceptionForIsNotNull(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsOfType(object value, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -63,7 +63,7 @@ public static void ThrowArgumentExceptionForIsOfType(object value, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -72,7 +72,7 @@ public static void ThrowArgumentExceptionForIsOfType(object value, Type type, st [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAssignableToType(object value, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -81,7 +81,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } /// @@ -91,7 +91,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ public static void ThrowArgumentExceptionForsBitwiseEqualTo(T value, T target, string name) where T : unmanaged { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) is not a bitwise match, was \"{value.ToHexString()}\" instead of \"{target.ToHexString()}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) is not a bitwise match, was \"{value.ToHexString()}\" instead of \"{target.ToHexString()}\""); } /// @@ -101,7 +101,7 @@ public static void ThrowArgumentExceptionForsBitwiseEqualTo(T value, T target public static void ThrowArgumentExceptionForIsReferenceEqualTo(string name) where T : class { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must be the same instance as the target object"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be the same instance as the target object"); } /// @@ -111,7 +111,7 @@ public static void ThrowArgumentExceptionForIsReferenceEqualTo(string name) public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name) where T : class { - ThrowArgumentException(name, $"Parameter \"{name}\" ({typeof(T).ToTypeString()}) must not be the same instance as the target object"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be the same instance as the target object"); } /// @@ -120,7 +120,7 @@ public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsTrue(string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be true, was false"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false"); } /// @@ -129,7 +129,7 @@ public static void ThrowArgumentExceptionForIsTrue(string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsFalse(string name) { - ThrowArgumentException(name, $"Parameter \"{name}\" must be false, was true"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true"); } } } From 6d97f335c33f636a6da4e423daa5c5c47c32b385 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Mar 2020 22:34:40 +0100 Subject: [PATCH 094/123] Improved error messages --- .../Generated/ThrowHelper.Collection.cs | 160 +++++++++--------- .../Generated/ThrowHelper.Collection.tt | 20 +-- .../ThrowHelper.Comparable.Generic.cs | 24 +-- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 2 +- 4 files changed, 103 insertions(+), 103 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs index cabcd9733d3..b313c09d548 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs @@ -28,7 +28,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(Span span, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}"); } /// @@ -37,7 +37,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Span span, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -46,7 +46,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -55,7 +55,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Span span, i [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -64,7 +64,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Span span, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -73,7 +73,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -82,7 +82,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Span span, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + 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()}"); } /// @@ -91,7 +91,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -100,7 +100,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Span source, Sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Span).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -109,7 +109,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -118,7 +118,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be empty, had a size of {span.Length.ToAssertString()}"); } /// @@ -127,7 +127,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlySpan span, str [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size equal to {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -136,7 +136,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized != {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size not equal to {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -145,7 +145,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlySpan [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized > {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size over {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -154,7 +154,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlySpan span, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized >= {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size of at least {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -163,7 +163,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlySpan sp [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized < {size}, had a size of {span.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must have a size less than {size}, had a size of {span.Length.ToAssertString()}"); } /// @@ -172,7 +172,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlySpan s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan span, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {size}, had a size of {span.Length}"); + 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()}"); } /// @@ -181,7 +181,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -190,7 +190,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlySpan so [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlySpan source, Span destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlySpan).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -199,7 +199,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {span.Length} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -208,7 +208,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}"); } /// @@ -217,7 +217,7 @@ public static void ThrowArgumentExceptionForIsEmpty(Memory memory, string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -226,7 +226,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -235,7 +235,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(Memory memor [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -244,7 +244,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(Memory memory, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -253,7 +253,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -262,7 +262,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(Memory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + 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()}"); } /// @@ -271,7 +271,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory(Memory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -280,7 +280,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(Memory source, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(Memory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -289,7 +289,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory(int index, Memory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -298,7 +298,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be empty, had a size of {memory.Length.ToAssertString()}"); } /// @@ -307,7 +307,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ReadOnlyMemory memory, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size equal to {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -316,7 +316,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized != {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size not equal to {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -325,7 +325,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ReadOnlyMemory< [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized > {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size over {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -334,7 +334,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ReadOnlyMemory mem [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized >= {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size of at least {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -343,7 +343,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized < {size}, had a size of {memory.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must have a size less than {size}, had a size of {memory.Length.ToAssertString()}"); } /// @@ -352,7 +352,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory memory, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {size}, had a size of {memory.Length}"); + 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()}"); } /// @@ -361,7 +361,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -370,7 +370,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ReadOnlyMemory [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnlyMemory source, Memory destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ReadOnlyMemory).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -379,7 +379,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {memory.Length} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -388,7 +388,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be empty, had a size of {array.Length.ToAssertString()}"); } /// @@ -397,7 +397,7 @@ public static void ThrowArgumentExceptionForIsEmpty(T[] array, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized == {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size equal to {size}, had a size of {array.Length.ToAssertString()}"); } /// @@ -406,7 +406,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized != {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size not equal to {size}, had a size of {array.Length.ToAssertString()}"); } /// @@ -415,7 +415,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(T[] array, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized > {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size over {size}, had a size of {array.Length.ToAssertString()}"); } /// @@ -424,7 +424,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(T[] array, int size, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized >= {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size of at least {size}, had a size of {array.Length.ToAssertString()}"); } /// @@ -433,7 +433,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(T[] array, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized < {size}, had a size of {array.Length}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must have a size less than {size}, had a size of {array.Length.ToAssertString()}"); } /// @@ -442,7 +442,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(T[] array, int si [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] array, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized <= {size}, had a size of {array.Length}"); + 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()}"); } /// @@ -451,7 +451,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] arra [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized == {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -460,7 +460,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(T[] source, T[] de [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] source, T[] destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(T[]).ToTypeString()}) must be sized <= {destination.Length}, had a size of {source.Length}"); + 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()}"); } /// @@ -469,7 +469,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] sour [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {array.Length} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {array.Length.ToAssertString()} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -478,7 +478,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(List list, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be empty, had a size of {list.Count.ToAssertString()}"); } /// @@ -487,7 +487,7 @@ public static void ThrowArgumentExceptionForIsEmpty(List list, string name [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized == {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size equal to {size}, had a size of {list.Count.ToAssertString()}"); } /// @@ -496,7 +496,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized != {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size not equal to {size}, had a size of {list.Count.ToAssertString()}"); } /// @@ -505,7 +505,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(List list, i [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized > {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size over {size}, had a size of {list.Count.ToAssertString()}"); } /// @@ -514,7 +514,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(List list, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized >= {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size of at least {size}, had a size of {list.Count.ToAssertString()}"); } /// @@ -523,7 +523,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized < {size}, had a size of {list.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must have a size less than {size}, had a size of {list.Count.ToAssertString()}"); } /// @@ -532,7 +532,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(List list, int [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List list, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized <= {size}, had a size of {list.Count}"); + 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()}"); } /// @@ -541,7 +541,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, List destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -550,7 +550,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(List source, Li [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List source, List destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(List).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -559,7 +559,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {list.Count} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {list.Count.ToAssertString()} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -568,7 +568,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(ICollection collection, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}"); } /// @@ -577,7 +577,7 @@ public static void ThrowArgumentExceptionForIsEmpty(ICollection collection [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -586,7 +586,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -595,7 +595,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(ICollection [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -604,7 +604,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(ICollection collec [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -613,7 +613,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(ICollection col [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -622,7 +622,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(ICollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + 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()}"); } /// @@ -631,7 +631,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -640,7 +640,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(ICollection sou [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(ICollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -649,7 +649,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index.ToAssertString()}"); } /// @@ -658,7 +658,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection collection, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be empty, had a size of {collection.Count.ToAssertString()}"); } /// @@ -667,7 +667,7 @@ public static void ThrowArgumentExceptionForIsEmpty(IReadOnlyCollection co [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size equal to {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -676,7 +676,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized != {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size not equal to {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -685,7 +685,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(IReadOnlyCollec [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized > {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size over {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -694,7 +694,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(IReadOnlyCollection(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized >= {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size of at least {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -703,7 +703,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized < {size}, had a size of {collection.Count}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must have a size less than {size}, had a size of {collection.Count.ToAssertString()}"); } /// @@ -712,7 +712,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(IReadOnlyCollecti [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection collection, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {size}, had a size of {collection.Count}"); + 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()}"); } /// @@ -721,7 +721,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized == {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -730,7 +730,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(IReadOnlyCollectio [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnlyCollection source, ICollection destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(IReadOnlyCollection).ToTypeString()}) must be sized <= {destination.Count}, had a size of {source.Count}"); + 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()}"); } /// @@ -739,7 +739,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {collection.Count} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index.ToAssertString()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index e80f2643437..27c557fb978 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -32,7 +32,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public 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#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); } /// @@ -41,7 +41,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + 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()}"); } /// @@ -50,7 +50,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized != {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + 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()}"); } /// @@ -59,7 +59,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized > {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {<#=item.Name#>.<#=item.Size#>.ToAssertString()}"); } /// @@ -68,7 +68,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized >= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + 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()}"); } /// @@ -77,7 +77,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized < {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + 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()}"); } /// @@ -86,7 +86,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> <#=item.Name#>, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {size}, had a size of {<#=item.Name#>.<#=item.Size#>}"); + 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()}"); } /// @@ -95,7 +95,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized == {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + 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()}"); } /// @@ -104,7 +104,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Type#> source, <#=item.DestinationType#> destination, string name) { - ThrowArgumentException(name, $"The source {name.ToAssertString()} ({typeof(<#=item.Type#>).ToTypeString()}) must be sized <= {destination.<#=item.Size#>}, had a size of {source.<#=item.Size#>}"); + 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()}"); } /// @@ -113,7 +113,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be >= 0 and < {<#=item.Name#>.<#=item.Size#>} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}"); } <# }); diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs index f6ebea0916b..368781efa33 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs @@ -23,7 +23,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be == {target.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be equal to {target.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -32,7 +32,7 @@ public static void ThrowArgumentExceptionForIsEqualTo(T value, T target, stri [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be != {target.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be equal to {target.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -41,7 +41,7 @@ public static void ThrowArgumentExceptionForIsNotEqualTo(T value, T target, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -50,7 +50,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThan(T value, T m [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T value, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be <= \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -59,7 +59,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(T v [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, T minimum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be > \"{minimum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than {minimum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -68,7 +68,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThan(T value, [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(T value, T minimum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than or equal to {minimum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -77,7 +77,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo( [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and < \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be at least {minimum.ToAssertString()} and less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -86,7 +86,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or >= \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < {minimum.ToAssertString()} or at least {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -95,7 +95,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be > \"{minimum}\" and < \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than {minimum.ToAssertString()} and less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -104,7 +104,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be <= \"{minimum}\" or >= \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than or equal to {minimum.ToAssertString()} or greater than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -113,7 +113,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be >= \"{minimum}\" and <= \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than or equal to {minimum.ToAssertString()} and less than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -122,7 +122,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T va [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < \"{minimum}\" or > \"{maximum}\", was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than {minimum.ToAssertString()} or greater than {maximum.ToAssertString()}, was {value.ToAssertString()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 8f0c90352c4..5e6160955e5 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -91,7 +91,7 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ public static void ThrowArgumentExceptionForsBitwiseEqualTo(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()}\""); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) is not a bitwise match, was <{value.ToHexString()}> instead of <{target.ToHexString()}>"); } /// From ec67152a4f9c7981be467a92aae4c00fd3da9bdc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 4 Mar 2020 22:57:00 +0100 Subject: [PATCH 095/123] More improvements to the error messages --- .../Generated/ThrowHelper.Collection.cs | 16 +++++++-------- .../Generated/ThrowHelper.Collection.tt | 2 +- .../ThrowHelper.Comparable.Generic.cs | 12 +++++------ .../Diagnostics/ThrowHelper.String.cs | 12 +++++------ .../Diagnostics/ThrowHelper.Tasks.cs | 20 +++++++++---------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs index b313c09d548..c588aad29db 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs @@ -109,7 +109,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Span [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, Span span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Span).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -199,7 +199,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlySpan span, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {span.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlySpan).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -289,7 +289,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(Memory(int index, Memory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(Memory).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -379,7 +379,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ReadOnly [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ReadOnlyMemory memory, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {memory.Length.ToAssertString()} to be a valid index for the target collection ({typeof(ReadOnlyMemory).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -469,7 +469,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(T[] sour [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, T[] array, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {array.Length.ToAssertString()} to be a valid index for the target collection ({typeof(T[]).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -559,7 +559,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(List [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, List list, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {list.Count.ToAssertString()} to be a valid index for the target collection ({typeof(List).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -649,7 +649,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(ICollect [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ICollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(ICollection).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } /// @@ -739,7 +739,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(IReadOnl [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, IReadOnlyCollection collection, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {collection.Count.ToAssertString()} to be a valid index for the target collection ({typeof(IReadOnlyCollection).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 27c557fb978..a14d8f53fb8 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -113,7 +113,7 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} (int) must be at least 0 and less than {<#=item.Name#>.<#=item.Size#>.ToAssertString()} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {index.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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()}"); } <# }); diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs index 368781efa33..c8d636b1700 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs @@ -77,7 +77,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo( [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be at least {minimum.ToAssertString()} and less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -86,7 +86,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRange(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be < {minimum.ToAssertString()} or at least {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be in the range given by {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -95,7 +95,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotInRange(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than {minimum.ToAssertString()} and less than {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -104,7 +104,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetween(T value, T mi [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than or equal to {minimum.ToAssertString()} or greater than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must not be between {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -113,7 +113,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsNotBetween(T value, T [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be greater than or equal to {minimum.ToAssertString()} and less than or equal to {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be between or equal to {minimum.ToAssertString()} and {maximum.ToAssertString()}, was {value.ToAssertString()}"); } /// @@ -122,7 +122,7 @@ public static void ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(T va [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(T value, T minimum, T maximum, string name) { - ThrowArgumentOutOfRangeException(name, $"Parameter {name.ToAssertString()} ({typeof(T).ToTypeString()}) must be less than {minimum.ToAssertString()} or greater than {maximum.ToAssertString()}, was {value.ToAssertString()}"); + ThrowArgumentOutOfRangeException(name, $"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/ThrowHelper.String.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs index 1dcceb6da55..496507b6dc1 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.String.cs @@ -94,7 +94,7 @@ public static void ThrowArgumentExceptionForIsNotWhitespace(string text, string [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized == {size}, had a size of {text.Length} and was {text.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size equal to {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -103,7 +103,7 @@ public static void ThrowArgumentExceptionForHasSizeEqualTo(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized != {size}, was {text.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must not have a size equal to {size}, was {text.ToAssertString()}"); } /// @@ -112,7 +112,7 @@ public static void ThrowArgumentExceptionForHasSizeNotEqualTo(string text, int s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized > {size}, had a size of {text.Length} and was {text.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size over {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -121,7 +121,7 @@ public static void ThrowArgumentExceptionForHasSizeOver(string text, int size, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized >= {size}, had a size of {text.Length} and was {text.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size of at least {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -130,7 +130,7 @@ public static void ThrowArgumentExceptionForHasSizeAtLeast(string text, int size [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized < {size}, had a size of {text.Length} and was {text.ToAssertString()}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must have a size less than {size}, had a size of {text.Length} and was {text.ToAssertString()}"); } /// @@ -139,7 +139,7 @@ public static void ThrowArgumentExceptionForHasSizeLessThan(string text, int siz [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(string text, int size, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} (string) must be sized <= {size}, had a size of {text.Length} and was {text.ToAssertString()}"); + 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()}"); } } } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs index 5013c790aac..9fad34ea8d1 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Tasks.cs @@ -24,7 +24,7 @@ internal static partial class ThrowHelper [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed, had status {task.Status.ToAssertString()}"); } /// @@ -33,7 +33,7 @@ public static void ThrowArgumentExceptionForIsCompleted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed, had status {task.Status.ToAssertString()}"); } /// @@ -42,7 +42,7 @@ public static void ThrowArgumentExceptionForIsNotCompleted(Task task, string nam [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be completed successfully, had status {task.Status.ToAssertString()}"); } /// @@ -51,7 +51,7 @@ public static void ThrowArgumentExceptionForIsCompletedSuccessfully(Task task, s [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be completed successfully, had status {task.Status.ToAssertString()}"); } /// @@ -60,7 +60,7 @@ public static void ThrowArgumentExceptionForIsNotCompletedSuccessfully(Task task [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be faulted, had status {task.Status.ToAssertString()}"); } /// @@ -69,7 +69,7 @@ public static void ThrowArgumentExceptionForIsFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be faulted, had status {task.Status.ToAssertString()}"); } /// @@ -78,7 +78,7 @@ public static void ThrowArgumentExceptionForIsNotFaulted(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must be canceled, had status {task.Status.ToAssertString()}"); } /// @@ -87,7 +87,7 @@ public static void ThrowArgumentExceptionForIsCanceled(Task task, string name) [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not be canceled, had status {task.Status.ToAssertString()}"); } /// @@ -96,7 +96,7 @@ public static void ThrowArgumentExceptionForIsNotCanceled(Task task, string name [MethodImpl(MethodImplOptions.NoInlining)] public 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}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must have status {status}, had status {task.Status.ToAssertString()}"); } /// @@ -105,7 +105,7 @@ public static void ThrowArgumentExceptionForHasStatusEqualTo(Task task, TaskStat [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentExceptionForHasStatusNotEqualTo(Task task, TaskStatus status, string name) { - ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not have status {status}"); + ThrowArgumentException(name, $"Parameter {name.ToAssertString()} ({task.GetType().ToTypeString()}) must not have status {status.ToAssertString()}"); } } } From b2c3acb7af2abd3d5c0764a636a7bcb051145e2a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Mar 2020 22:18:54 +0100 Subject: [PATCH 096/123] Minor code tweaks --- Microsoft.Toolkit/Diagnostics/Guard.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 6c7a54b7358..747a9225f14 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -4,6 +4,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; #nullable enable @@ -24,10 +25,11 @@ public static partial class Guard /// The name of the input parameter being tested. /// Thrown if is not . [MethodImpl(MethodImplOptions.AggressiveInlining)] + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119", Justification = "Negated pattern match expression")] public static void IsNull(T? value, string name) where T : class { - if (value != null) + if (!(value is null)) { ThrowHelper.ThrowArgumentExceptionForIsNull(value, name); } @@ -42,10 +44,11 @@ public static void IsNull(T? value, string name) /// Thrown if is not . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] + [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119", Justification = "Negated pattern match expression")] public static void IsNull(T? value, string name) where T : struct { - if (value != null) + if (!(value is null)) { ThrowHelper.ThrowArgumentExceptionForIsNull(value, name); } From 43b22f08406529656be01ffe2300b42d80316e2b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Mar 2020 22:23:38 +0100 Subject: [PATCH 097/123] Added Guard.IsNotOfType APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 32 ++++++++++++++++++++ Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 18 +++++++++++ 2 files changed, 50 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index 747a9225f14..d272748a6c2 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -105,6 +105,22 @@ public static void IsOfType(object value, string name) } } + /// + /// Asserts that the input value is not of a specific type. + /// + /// The type of the input value. + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if is of type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotOfType(object value, string name) + { + if (value.GetType() == typeof(T)) + { + ThrowHelper.ThrowArgumentExceptionForIsNotOfType(value, name); + } + } + /// /// Asserts that the input value is of a specific type. /// @@ -121,6 +137,22 @@ public static void IsOfType(object value, Type type, string name) } } + /// + /// Asserts that the input value is not of a specific type. + /// + /// The input to test. + /// The type to look for. + /// The name of the input parameter being tested. + /// Thrown if the type of is the same as . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotOfType(object value, Type type, string name) + { + if (value.GetType() == type) + { + ThrowHelper.ThrowArgumentExceptionForIsNotOfType(value, type, name); + } + } + /// /// Asserts that the input value can be cast to a specified type. /// diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 5e6160955e5..7f32357e49b 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -57,6 +57,15 @@ public static void ThrowArgumentExceptionForIsOfType(object value, string nam ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. /// @@ -66,6 +75,15 @@ public static void ThrowArgumentExceptionForIsOfType(object value, Type type, st ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. /// From c8af095985f78e099c1052d21d6f29aa2b622431 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Mar 2020 22:25:52 +0100 Subject: [PATCH 098/123] Added Guard.IsNotAssignableToType APIs --- Microsoft.Toolkit/Diagnostics/Guard.cs | 40 ++++++++++++++++++-- Microsoft.Toolkit/Diagnostics/ThrowHelper.cs | 18 +++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.cs b/Microsoft.Toolkit/Diagnostics/Guard.cs index d272748a6c2..a8703a4c04a 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.cs @@ -154,12 +154,12 @@ public static void IsNotOfType(object value, Type type, string name) } /// - /// Asserts that the input value can be cast to a specified type. + /// Asserts that the input value can be assigned to a specified type. /// /// The type to check the input value against. /// The input to test. /// The name of the input parameter being tested. - /// Thrown if can't be cast to type . + /// Thrown if can't be assigned to type . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsAssignableToType(object value, string name) { @@ -170,12 +170,28 @@ public static void IsAssignableToType(object value, string name) } /// - /// Asserts that the input value can be cast to a specified type. + /// Asserts that the input value can't be assigned to a specified type. + /// + /// The type to check the input value against. + /// The input to test. + /// The name of the input parameter being tested. + /// Thrown if can be assigned to type . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotAssignableToType(object value, string name) + { + if (value is T) + { + ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType(value, name); + } + } + + /// + /// Asserts that the input value can be assigned to a specified type. /// /// The input to test. /// The type to look for. /// The name of the input parameter being tested. - /// Thrown if can't be cast to . + /// Thrown if can't be assigned to . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsAssignableToType(object value, Type type, string name) { @@ -185,6 +201,22 @@ public static void IsAssignableToType(object value, Type type, string name) } } + /// + /// Asserts that the input value can't be assigned to a specified type. + /// + /// The input to test. + /// The type to look for. + /// The name of the input parameter being tested. + /// Thrown if can be assigned to . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotAssignableToType(object value, Type type, string name) + { + if (type.IsInstanceOfType(value)) + { + ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType(value, type, name); + } + } + /// /// Asserts that the input value must be the same instance as the target value. /// diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs index 7f32357e49b..5436d9b48b4 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.cs @@ -93,6 +93,15 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. /// @@ -102,6 +111,15 @@ public static void ThrowArgumentExceptionForIsAssignableToType(object value, Typ ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}"); } + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. /// From 7adfa794c9c026f032176822c9c04f80459813c3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 8 Mar 2020 14:18:32 +0100 Subject: [PATCH 099/123] Added Guard.IsDefault APIs --- .../Diagnostics/Guard.Comparable.Generic.cs | 34 +++++++++++++++++++ .../ThrowHelper.Comparable.Generic.cs | 20 +++++++++++ 2 files changed, 54 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs index 65c2d13e09b..59d14a49a75 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs @@ -14,6 +14,40 @@ namespace Microsoft.Toolkit.Diagnostics /// public static partial class Guard { + /// + /// Asserts that the input value is . + /// + /// The type of value type being tested. + /// The input value to test. + /// The name of the input parameter being tested. + /// Thrown if is not . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsDefault(T value, string name) + where T : struct, IEquatable + { + if (!value.Equals(default)) + { + ThrowHelper.ThrowArgumentExceptionForIsDefault(value, name); + } + } + + /// + /// Asserts that the input value is not . + /// + /// The type of value type being tested. + /// The input value to test. + /// The name of the input parameter being tested. + /// Thrown if is . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotDefault(T value, string name) + where T : struct, IEquatable + { + if (value.Equals(default)) + { + ThrowHelper.ThrowArgumentExceptionForIsNotDefault(name); + } + } + /// /// Asserts that the input value must be equal to a specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs index c8d636b1700..0dab8651f21 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Generic.cs @@ -17,6 +17,26 @@ namespace Microsoft.Toolkit.Diagnostics [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] internal static partial class ThrowHelper { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public 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. /// From d4c2d0527757498f555295941f2f42345a910728 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 01:02:49 +0100 Subject: [PATCH 100/123] Fixed some XML docs --- .../Diagnostics/Generated/Guard.Collection.cs | 14 +++++++------- .../Diagnostics/Generated/Guard.Collection.tt | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs index cbb08d495b1..d8c83116aa9 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs @@ -171,7 +171,7 @@ public static void HasSizeEqualTo(Span source, Span destination, string } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. @@ -356,7 +356,7 @@ public static void HasSizeEqualTo(ReadOnlySpan source, Span destination } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. @@ -541,7 +541,7 @@ public static void HasSizeEqualTo(Memory source, Memory destination, st } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. @@ -911,7 +911,7 @@ public static void HasSizeEqualTo(T[] source, T[] destination, string name) } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination array instance. + /// Asserts that the source array instance must have a size of less than or equal to that of a destination array instance. /// /// The item of items in the input array instance. /// The source array instance to check the size for. @@ -1096,7 +1096,7 @@ public static void HasSizeEqualTo(List source, List destination, string } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. @@ -1281,7 +1281,7 @@ public static void HasSizeEqualTo(ICollection source, ICollection desti } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. @@ -1466,7 +1466,7 @@ public static void HasSizeEqualTo(IReadOnlyCollection source, ICollection< } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. + /// Asserts that the source instance must have a size of less than or equal to that of a destination instance. /// /// The item of items in the input instance. /// The source instance to check the size for. diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 04983e5e44c..d2f08a12067 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -207,7 +207,7 @@ GenerateTextForItems(EnumerableTypes, item => } /// - /// Asserts that the source instance must have a size of less than or equal to that of a destination <#=item.XmlType#> instance. + /// Asserts that the source <#=item.XmlType#> instance must have a size of less than or equal to that of a destination <#=item.XmlType#> instance. /// /// The item of items in the input <#=item.XmlType#> instance. /// The source <#=item.XmlType#> instance to check the size for. From afd973edcee4e9e3447d90f87d869d4a7ec14a73 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 01:15:23 +0100 Subject: [PATCH 101/123] Added missing [Pure] attribute --- Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs index cbd13480b74..4596771136a 100644 --- a/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs +++ b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -30,6 +31,7 @@ public static class ValueTypeExtensions /// The input type to format to . /// The input value to format to . /// The hexadecimal representation of , left-padded and ordered as big-endian. + [Pure] [MethodImpl(MethodImplOptions.NoInlining)] public static unsafe string ToHexString(this T value) where T : unmanaged From 96ce238fb2ad29330dd23ddb5f2adfeaf923f29f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 01:17:01 +0100 Subject: [PATCH 102/123] Fixed typo in a comment --- Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs index 59d14a49a75..654caf420e4 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs @@ -100,7 +100,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) { /* Include some fast paths if the input type is of size 1, 2, 4 or 8. * In those cases, just reinterpret the bytes as values of an integer type, - * and compare them directly, which is much faster than having explicitly + * and compare them directly, which is much faster than having to explicitly * loop through every single byte. This also allows for more expressive error * messages, since the entire input values can be expressed as hexadecimal values. * The conditional branches below are known at compile time by the JIT compiler, From cb98a02e37c91d87a05bb253204c4c114cb3c167 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 01:18:33 +0100 Subject: [PATCH 103/123] Removed unnecessary comment after code changes --- Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs index 654caf420e4..4ee0479511b 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs @@ -100,9 +100,7 @@ public static void IsBitwiseEqualTo(T value, T target, string name) { /* Include some fast paths if the input type is of size 1, 2, 4 or 8. * In those cases, just reinterpret the bytes as values of an integer type, - * and compare them directly, which is much faster than having to explicitly - * loop through every single byte. This also allows for more expressive error - * messages, since the entire input values can be expressed as hexadecimal values. + * and compare them directly, which is much faster than having a loop over each byte. * The conditional branches below are known at compile time by the JIT compiler, * so that only the right one will actually be translated into native code. */ if (typeof(T) == typeof(byte) || From 5f25e180b13dc8c939a75ef828d3ddec7905a635 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 01:23:11 +0100 Subject: [PATCH 104/123] Added XML comment for the T4 service --- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index d0f3fe61447..14d1aaafb52 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -39,6 +39,7 @@ + From cf5785c71667b2389e907d22d2ef16db6fa517c2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 12:08:21 +0100 Subject: [PATCH 105/123] Added more comments to ToHexString --- Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs index 4596771136a..64f73732c98 100644 --- a/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs +++ b/Microsoft.Toolkit/Extensions/ValueTypeExtensions.cs @@ -30,7 +30,18 @@ public static class ValueTypeExtensions /// /// The input type to format to . /// The input value to format to . - /// The hexadecimal representation of , left-padded and ordered as big-endian. + /// + /// The hexadecimal representation of (with the '0x' prefix), left-padded to byte boundaries and ordered as big-endian. + /// + /// + /// As a byte (8 bits) is represented by two hexadecimal digits (each representing a group of 4 bytes), each + /// representation will always contain an even number of digits. For instance: + /// + /// Console.WriteLine(1.ToHexString()); // "0x01" + /// Console.WriteLine(((byte)255).ToHexString()); // "0xFF" + /// Console.WriteLine((-1).ToHexString()); // "0xFFFFFFFF" + /// + /// [Pure] [MethodImpl(MethodImplOptions.NoInlining)] public static unsafe string ToHexString(this T value) From 801dc936a7acd94963d0b0467adec15b8dd72c25 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 12:39:38 +0100 Subject: [PATCH 106/123] Suppressed an incorrect code style warning --- UnitTests/Extensions/Test_TypeExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/UnitTests/Extensions/Test_TypeExtensions.cs b/UnitTests/Extensions/Test_TypeExtensions.cs index fe920c86640..5acb2392faa 100644 --- a/UnitTests/Extensions/Test_TypeExtensions.cs +++ b/UnitTests/Extensions/Test_TypeExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Microsoft.Toolkit.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -27,6 +28,7 @@ public void Test_TypeExtensions_BuiltInTypes() [TestCategory("TypeExtensions")] [TestMethod] + [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1009", Justification = "Nullable value tuple type")] public void Test_TypeExtensions_GenericTypes() { Assert.AreEqual("int?", typeof(int?).ToTypeString()); From b840201d17edd452314eab49bb3b37e9b96fce36 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 12:44:40 +0100 Subject: [PATCH 107/123] Added more tests for Guard.IsInRange --- UnitTests/Diagnostics/Test_Guard.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs index de1a464a400..4c8717d5872 100644 --- a/UnitTests/Diagnostics/Test_Guard.cs +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -339,6 +339,10 @@ public void Test_Guard_IsInRange_Ok() Guard.IsInRange(0, 0, 2, nameof(Test_Guard_IsInRange_Ok)); Guard.IsInRange(3.14f, 0, 10, nameof(Test_Guard_IsInRange_Ok)); Guard.IsInRange(1, 0, 3.14f, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(1, -50, 2, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(-44, -44, 0, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(3.14f, -float.Epsilon, 22, nameof(Test_Guard_IsInRange_Ok)); + Guard.IsInRange(1, int.MinValue, int.MaxValue, nameof(Test_Guard_IsInRange_Ok)); } [TestCategory("Guard")] From 818c1eb524fa3fa98993c65d1a5c572c23b7556f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 15:18:00 +0100 Subject: [PATCH 108/123] Added Guard.IsNotInRangeFor APIs --- .../Diagnostics/Generated/Guard.Collection.cs | 136 ++++++++++++++++++ .../Diagnostics/Generated/Guard.Collection.tt | 17 +++ .../Generated/ThrowHelper.Collection.cs | 72 ++++++++++ .../Generated/ThrowHelper.Collection.tt | 9 ++ UnitTests/Diagnostics/Test_Guard.cs | 31 ++++ 5 files changed, 265 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs index d8c83116aa9..346241db8cc 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs @@ -204,6 +204,23 @@ public static void IsInRangeFor(int index, Span span, string name) } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, Span span, string name) + { + if ((uint)index < (uint)span.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, span, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -389,6 +406,23 @@ public static void IsInRangeFor(int index, ReadOnlySpan span, string name) } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, ReadOnlySpan span, string name) + { + if ((uint)index < (uint)span.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, span, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -574,6 +608,23 @@ public static void IsInRangeFor(int index, Memory memory, string name) } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, Memory memory, string name) + { + if ((uint)index < (uint)memory.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, memory, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -759,6 +810,23 @@ public static void IsInRangeFor(int index, ReadOnlyMemory memory, string n } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, ReadOnlyMemory memory, string name) + { + if ((uint)index < (uint)memory.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, memory, name); + } + } + /// /// Asserts that the input array instance must be empty. /// @@ -944,6 +1012,23 @@ public static void IsInRangeFor(int index, T[] array, string name) } } + /// + /// Asserts that the input index is not valid for a given array instance. + /// + /// The item of items in the input array instance. + /// The input index to be used to access . + /// The input array instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, T[] array, string name) + { + if ((uint)index < (uint)array.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, array, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -1129,6 +1214,23 @@ public static void IsInRangeFor(int index, List list, string name) } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, List list, string name) + { + if ((uint)index < (uint)list.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, (ICollection)list, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -1314,6 +1416,23 @@ public static void IsInRangeFor(int index, ICollection collection, string } } + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, ICollection collection, string name) + { + if ((uint)index < (uint)collection.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, collection, name); + } + } + /// /// Asserts that the input instance must be empty. /// @@ -1498,5 +1617,22 @@ public static void IsInRangeFor(int index, IReadOnlyCollection collection, ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, collection, name); } } + + /// + /// Asserts that the input index is not valid for a given instance. + /// + /// The item of items in the input instance. + /// The input index to be used to access . + /// The input instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, IReadOnlyCollection collection, string name) + { + if ((uint)index < (uint)collection.Count) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, collection, name); + } + } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index d2f08a12067..671c2db5d21 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -252,6 +252,23 @@ GenerateTextForItems(EnumerableTypes, item => ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name); } } + + /// + /// Asserts that the input index is not valid for a given <#=item.XmlType#> instance. + /// + /// The item of items in the input <#=item.XmlType#> instance. + /// The input index to be used to access . + /// The input <#=item.XmlType#> instance to use to validate . + /// The name of the input parameter being tested. + /// Thrown if is valid to access . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void IsNotInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>) + { + ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name); + } + } <# }); #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs index c588aad29db..e40c0d94fe5 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs @@ -112,6 +112,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Span span, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -202,6 +211,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlySpan span, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -292,6 +310,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, Memory memory, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -382,6 +409,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ReadOnlyMemory memory, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -472,6 +508,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, T[] array, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -562,6 +607,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, List list, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -652,6 +706,15 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, ICollection collection, string name) + { + ThrowArgumentOutOfRangeException(name, $"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. /// @@ -741,5 +804,14 @@ public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor(int index, { ThrowArgumentOutOfRangeException(name, $"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)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, IReadOnlyCollection collection, string name) + { + ThrowArgumentOutOfRangeException(name, $"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()}"); + } } } diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index a14d8f53fb8..4aaac7f607a 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -115,6 +115,15 @@ GenerateTextForItems(EnumerableTypes, item => { ThrowArgumentOutOfRangeException(name, $"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. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) + { + ThrowArgumentOutOfRangeException(name, $"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()}"); + } <# }); #> diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs index 4c8717d5872..af71a1c90df 100644 --- a/UnitTests/Diagnostics/Test_Guard.cs +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -436,6 +436,37 @@ public void Test_Guard_IsInRangeFor_HigherFail() Guard.IsInRangeFor(99, span, nameof(Test_Guard_IsInRangeFor_HigherFail)); } + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsNotInRangeFor_Ok() + { + Span span = stackalloc int[10]; + + Guard.IsNotInRangeFor(-2, span, nameof(Test_Guard_IsNotInRangeFor_Ok)); + Guard.IsNotInRangeFor(10, span, nameof(Test_Guard_IsNotInRangeFor_Ok)); + Guard.IsNotInRangeFor(2222, span, nameof(Test_Guard_IsNotInRangeFor_Ok)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotInRangeFor_LowerFail() + { + Span span = stackalloc int[10]; + + Guard.IsNotInRangeFor(0, span, nameof(Test_Guard_IsNotInRangeFor_LowerFail)); + } + + [TestCategory("Guard")] + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void Test_Guard_IsNotInRangeFor_MiddleFail() + { + Span span = stackalloc int[10]; + + Guard.IsNotInRangeFor(6, span, nameof(Test_Guard_IsNotInRangeFor_MiddleFail)); + } + [TestCategory("Guard")] [TestMethod] public void Test_Guard_IsBetween_Ok() From caad2f7792c29f83163212b19045a3d3d43d05af Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 16:00:58 +0100 Subject: [PATCH 109/123] Added readme file for the T4 templates --- Microsoft.Toolkit/Diagnostics/Generated/Guard.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/Generated/Guard.md diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md new file mode 100644 index 00000000000..77e07b7a376 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md @@ -0,0 +1,15 @@ +# T4 templates and generated APIs + +This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.cs` files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (more info is available [here](https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019)). + +## Why is this needed? + +There are a few reasons why T4 templates are used for the `Guard` class: + + - Especially when a large number of similar overloads are available for the same APIs, using templates makes it much easier to maintain code and spot mistakes, as the actual number of lines of code to review is much smaller: it's just the code in each template! + - Using type-specific overloads instead of generic methods can result in faster code. For instance, T4 templates are used to generate overloads for comparison APIs (eg. `Guard.IsGreaterThan(int, int, string)`). This results in more compact and optimized code as opposed to a generic method using `where T : IComparable` as type constraint. + - In some cases, using generic methods just isn't possible. For instance, types like `Span` and `ReadOnlySpan` can't be used as generic type parameters, and even if that had been possible, they don't implement an interface we could use in the generic type constraint. Using T4 templates solves this issue, as we can just have specialized method for each supported type or collection type. + + ## How to make changes + +If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.cs` file automatically. Don't make changes to those generated `.cs` files directly, as those will be overwritten as soon as their source template is updated. \ No newline at end of file From 7134f9877cf7e677d1199c724388f63ced19a736 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 16:14:22 +0100 Subject: [PATCH 110/123] Changed generated files extension to .g.cs to avoid conflicts --- .../{Guard.Collection.cs => Guard.Collection.g.cs} | 0 .../Diagnostics/Generated/Guard.Collection.tt | 3 ++- ...able.Numeric.cs => Guard.Comparable.Numeric.g.cs} | 0 .../Generated/Guard.Comparable.Numeric.tt | 3 ++- ...per.Collection.cs => ThrowHelper.Collection.g.cs} | 0 .../Diagnostics/Generated/ThrowHelper.Collection.tt | 3 ++- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 12 ++++++------ 7 files changed, 12 insertions(+), 9 deletions(-) rename Microsoft.Toolkit/Diagnostics/Generated/{Guard.Collection.cs => Guard.Collection.g.cs} (100%) rename Microsoft.Toolkit/Diagnostics/Generated/{Guard.Comparable.Numeric.cs => Guard.Comparable.Numeric.g.cs} (100%) rename Microsoft.Toolkit/Diagnostics/Generated/{ThrowHelper.Collection.cs => ThrowHelper.Collection.g.cs} (100%) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.g.cs similarity index 100% rename from Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.cs rename to Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.g.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 671c2db5d21..57354f08025 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -1,4 +1,5 @@ -// Licensed to the .NET Foundation under one or more agreements. +<#@ output extension=".g.cs"#> +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs similarity index 100% rename from Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.cs rename to Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 746d89012c5..b53216bb301 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -1,4 +1,5 @@ -// Licensed to the .NET Foundation under one or more agreements. +<#@ output extension=".g.cs"#> +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs similarity index 100% rename from Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.cs rename to Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.g.cs diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index 4aaac7f607a..df8cc56f719 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -1,4 +1,5 @@ -// Licensed to the .NET Foundation under one or more agreements. +<#@ output extension=".g.cs"#> +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 14d1aaafb52..98b5a2df807 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -23,15 +23,15 @@ TextTemplatingFileGenerator - Guard.Comparable.Numeric.cs + Guard.Comparable.Numeric.g.cs TextTemplatingFileGenerator - Guard.Collection.cs + Guard.Collection.g.cs TextTemplatingFileGenerator - ThrowHelper.Collection.cs + ThrowHelper.Collection.g.cs TextTemplatingFileGenerator @@ -45,17 +45,17 @@ - + True True Guard.Comparable.Numeric.tt - + True True Guard.Collection.tt - + True True ThrowHelper.Collection.tt From 01a725711e4b03e2600d980e084633249b40e9ab Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 17:07:20 +0100 Subject: [PATCH 111/123] Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs --- .../Diagnostics/Guard.Comparable.Numeric.cs | 127 ++++++++++++++++++ .../ThrowHelper.Comparable.Numeric.cs | 74 ++++++++++ 2 files changed, 201 insertions(+) create mode 100644 Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs create mode 100644 Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs new file mode 100644 index 00000000000..d4d9b31c833 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs @@ -0,0 +1,127 @@ +// 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.Runtime.CompilerServices; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to verify conditions when running code. + /// + public static partial class Guard + { + /// + /// 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(int value, int target, int 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. */ + if (Math.Abs((double)((long)value - target)) > delta) + { + 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(int value, int target, int delta, string name) + { + if (Math.Abs((double)((long)value - target)) <= delta) + { + 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(float value, float target, float delta, string name) + { + if (Math.Abs(value - target) > delta) + { + 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(float value, float target, float delta, string name) + { + if (Math.Abs(value - target) <= delta) + { + 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(double value, double target, double delta, string name) + { + if (Math.Abs(value - target) > delta) + { + 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(double value, double target, double delta, string name) + { + if (Math.Abs(value - target) <= delta) + { + ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); + } + } + } +} diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs new file mode 100644 index 00000000000..586be6148b4 --- /dev/null +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs @@ -0,0 +1,74 @@ +// 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; + +#nullable enable + +namespace Microsoft.Toolkit.Diagnostics +{ + /// + /// Helper methods to throw exceptions + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618", Justification = "Internal helper methods")] + internal static partial class ThrowHelper + { + /// + /// Throws an when fails. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowArgumentExceptionForIsCloseTo(int value, int target, int 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)] + public static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, int 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)] + public 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)] + public 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)] + public 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)] + public 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()}"); + } + } +} From f33c98496d7338e42f692ac2517150887538c20d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 18:47:16 +0100 Subject: [PATCH 112/123] Added tests for IsCloseTo --- .../Test_Guard.Comparable.Numeric.cs | 100 ++++++++++++++++++ UnitTests/UnitTests.csproj | 1 + 2 files changed, 101 insertions(+) create mode 100644 UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs diff --git a/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs b/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs new file mode 100644 index 00000000000..0c76eb8f8f0 --- /dev/null +++ b/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs @@ -0,0 +1,100 @@ +// 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.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace UnitTests.Diagnostics +{ + public partial class Test_Guard + { + [TestCategory("Guard")] + [TestMethod] + public void Test_Guard_IsCloseToInt_Ok() + { + 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, int 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) + }) + { + bool fail = false; + + try + { + Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToInt_Fail)); + } + catch (ArgumentException) + { + fail = true; + } + + Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}"); + } + } + + [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)); + } + + [TestCategory("Guard")] + [TestMethod] + [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")] + public void Test_Guard_IsCloseToFloat_Fail() + { + foreach (var item in new (float Value, float Target, float Delta)[] + { + (0, 20, 10), + (0, 6, 5), + (0, float.MaxValue, 500), + (-500, -530, 10), + (1000, 800, 100), + (float.MaxValue, float.MaxValue - 10, 7), + (float.MinValue, float.MaxValue, float.MaxValue) + }) + { + bool fail = false; + + try + { + Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToFloat_Fail)); + } + catch (ArgumentException) + { + fail = true; + } + + Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}"); + } + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 05729126f01..b4208dc4fef 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -132,6 +132,7 @@ + From 1b0f2c9dfb5b8656aa5eb468d0a41852d297e689 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 20:47:11 +0100 Subject: [PATCH 113/123] Updated ".g.cs" extension for generated files Co-Authored-By: Michael Hawker MSFT (XAML Llama) --- Microsoft.Toolkit/Diagnostics/Generated/Guard.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md index 77e07b7a376..766e4418ad5 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md @@ -1,6 +1,6 @@ # T4 templates and generated APIs -This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.cs` files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (more info is available [here](https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019)). +This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.g.cs` files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (more info is available [here](https://docs.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2019)). ## Why is this needed? @@ -12,4 +12,4 @@ There are a few reasons why T4 templates are used for the `Guard` class: ## How to make changes -If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.cs` file automatically. Don't make changes to those generated `.cs` files directly, as those will be overwritten as soon as their source template is updated. \ No newline at end of file +If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.cs` file automatically. Don't make changes to those generated `.cs` files directly, as those will be overwritten as soon as their source template is updated. From ef189ba800ed98885a4773b1f448b7cf2e5bb5c0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 20:47:43 +0100 Subject: [PATCH 114/123] Removed extra space (typo) Co-Authored-By: Michael Hawker MSFT (XAML Llama) --- Microsoft.Toolkit/Diagnostics/Generated/Guard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md index 766e4418ad5..65bd29d0996 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md @@ -10,6 +10,6 @@ There are a few reasons why T4 templates are used for the `Guard` class: - Using type-specific overloads instead of generic methods can result in faster code. For instance, T4 templates are used to generate overloads for comparison APIs (eg. `Guard.IsGreaterThan(int, int, string)`). This results in more compact and optimized code as opposed to a generic method using `where T : IComparable` as type constraint. - In some cases, using generic methods just isn't possible. For instance, types like `Span` and `ReadOnlySpan` can't be used as generic type parameters, and even if that had been possible, they don't implement an interface we could use in the generic type constraint. Using T4 templates solves this issue, as we can just have specialized method for each supported type or collection type. - ## How to make changes +## How to make changes If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.cs` file automatically. Don't make changes to those generated `.cs` files directly, as those will be overwritten as soon as their source template is updated. From ac44dadfc511deea7ffbbfa5dc4cbaabcbc7e1b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 21:45:20 +0100 Subject: [PATCH 115/123] More tweakes to the Guard.md file --- Microsoft.Toolkit/Diagnostics/Generated/Guard.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md index 65bd29d0996..7837634f482 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md @@ -12,4 +12,4 @@ There are a few reasons why T4 templates are used for the `Guard` class: ## How to make changes -If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.cs` file automatically. Don't make changes to those generated `.cs` files directly, as those will be overwritten as soon as their source template is updated. +If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.g.cs` file automatically. Don't make changes to those generated `.g.cs` files directly, as those will be overwritten as soon as their source template is updated. From 21115f6a75f3069edcd299d15f65044ae2cd0182 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 21:51:06 +0100 Subject: [PATCH 116/123] Excluded TypeInfo.g.cs file from .ttinclude from checkout --- .gitignore | 3 +++ Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt | 3 +-- .../Diagnostics/Generated/Guard.Comparable.Numeric.tt | 3 +-- .../Diagnostics/Generated/ThrowHelper.Collection.tt | 3 +-- Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude | 3 ++- Microsoft.Toolkit/Microsoft.Toolkit.csproj | 4 ++-- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index ed0e87e25e0..4e9d1eddb81 100644 --- a/.gitignore +++ b/.gitignore @@ -225,3 +225,6 @@ msbuild.binlog *.project.lock.json /build/tools/** !/build/tools/packages.config + +# Generated file from .ttinclude +**/Generated/TypeInfo.g.cs \ No newline at end of file diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 57354f08025..671c2db5d21 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -1,5 +1,4 @@ -<#@ output extension=".g.cs"#> -// Licensed to the .NET Foundation under one or more agreements. +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index b53216bb301..746d89012c5 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -1,5 +1,4 @@ -<#@ output extension=".g.cs"#> -// Licensed to the .NET Foundation under one or more agreements. +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt index df8cc56f719..4aaac7f607a 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/ThrowHelper.Collection.tt @@ -1,5 +1,4 @@ -<#@ output extension=".g.cs"#> -// Licensed to the .NET Foundation under one or more agreements. +// 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. <#@include file="TypeInfo.ttinclude" #> diff --git a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude index ac532148096..04e29500538 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude +++ b/Microsoft.Toolkit/Diagnostics/Generated/TypeInfo.ttinclude @@ -1,6 +1,7 @@ -<#@ template language="C#" #> +<#@ template language="C#"#> <#@ assembly name="System.Core" #> <#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".g.cs"#> <#+ /// /// A model representing the info on an enumerable type diff --git a/Microsoft.Toolkit/Microsoft.Toolkit.csproj b/Microsoft.Toolkit/Microsoft.Toolkit.csproj index 98b5a2df807..011f95377f7 100644 --- a/Microsoft.Toolkit/Microsoft.Toolkit.csproj +++ b/Microsoft.Toolkit/Microsoft.Toolkit.csproj @@ -35,7 +35,7 @@ TextTemplatingFileGenerator - TypeInfo.cs + TypeInfo.g.cs @@ -60,7 +60,7 @@ True ThrowHelper.Collection.tt - + True True TypeInfo.ttinclude From 67fcd478ab4787eb4d57e67019059476b7d2e40b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 22:06:55 +0100 Subject: [PATCH 117/123] Added more info about target ranges for some APIs --- .../Generated/Guard.Comparable.Numeric.g.cs | 360 ++++++++++++++---- .../Generated/Guard.Comparable.Numeric.tt | 30 +- .../Diagnostics/Guard.Comparable.Generic.cs | 30 +- 3 files changed, 336 insertions(+), 84 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs index cc9a9c90a9e..ec0d08fd788 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.g.cs @@ -127,7 +127,10 @@ public static void IsGreaterThanOrEqualTo(byte value, byte minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -145,7 +148,10 @@ public static void IsInRange(byte value, byte minimum, byte maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -163,7 +169,10 @@ public static void IsNotInRange(byte value, byte minimum, byte maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -181,7 +190,10 @@ public static void IsBetween(byte value, byte minimum, byte maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -199,7 +211,10 @@ public static void IsNotBetween(byte value, byte minimum, byte maximum, string n /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -217,7 +232,10 @@ public static void IsBetweenOrEqualTo(byte value, byte minimum, byte maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(byte value, byte minimum, byte maximum, string name) { @@ -336,7 +354,10 @@ public static void IsGreaterThanOrEqualTo(sbyte value, sbyte minimum, string nam /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -354,7 +375,10 @@ public static void IsInRange(sbyte value, sbyte minimum, sbyte maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -372,7 +396,10 @@ public static void IsNotInRange(sbyte value, sbyte minimum, sbyte maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -390,7 +417,10 @@ public static void IsBetween(sbyte value, sbyte minimum, sbyte maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -408,7 +438,10 @@ public static void IsNotBetween(sbyte value, sbyte minimum, sbyte maximum, strin /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -426,7 +459,10 @@ public static void IsBetweenOrEqualTo(sbyte value, sbyte minimum, sbyte maximum, /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(sbyte value, sbyte minimum, sbyte maximum, string name) { @@ -545,7 +581,10 @@ public static void IsGreaterThanOrEqualTo(short value, short minimum, string nam /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -563,7 +602,10 @@ public static void IsInRange(short value, short minimum, short maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -581,7 +623,10 @@ public static void IsNotInRange(short value, short minimum, short maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -599,7 +644,10 @@ public static void IsBetween(short value, short minimum, short maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -617,7 +665,10 @@ public static void IsNotBetween(short value, short minimum, short maximum, strin /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -635,7 +686,10 @@ public static void IsBetweenOrEqualTo(short value, short minimum, short maximum, /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(short value, short minimum, short maximum, string name) { @@ -754,7 +808,10 @@ public static void IsGreaterThanOrEqualTo(ushort value, ushort minimum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -772,7 +829,10 @@ public static void IsInRange(ushort value, ushort minimum, ushort maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -790,7 +850,10 @@ public static void IsNotInRange(ushort value, ushort minimum, ushort maximum, st /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -808,7 +871,10 @@ public static void IsBetween(ushort value, ushort minimum, ushort maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -826,7 +892,10 @@ public static void IsNotBetween(ushort value, ushort minimum, ushort maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -844,7 +913,10 @@ public static void IsBetweenOrEqualTo(ushort value, ushort minimum, ushort maxim /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ushort value, ushort minimum, ushort maximum, string name) { @@ -963,7 +1035,10 @@ public static void IsGreaterThanOrEqualTo(char value, char minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -981,7 +1056,10 @@ public static void IsInRange(char value, char minimum, char maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -999,7 +1077,10 @@ public static void IsNotInRange(char value, char minimum, char maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -1017,7 +1098,10 @@ public static void IsBetween(char value, char minimum, char maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -1035,7 +1119,10 @@ public static void IsNotBetween(char value, char minimum, char maximum, string n /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -1053,7 +1140,10 @@ public static void IsBetweenOrEqualTo(char value, char minimum, char maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(char value, char minimum, char maximum, string name) { @@ -1172,7 +1262,10 @@ public static void IsGreaterThanOrEqualTo(int value, int minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1190,7 +1283,10 @@ public static void IsInRange(int value, int minimum, int maximum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1208,7 +1304,10 @@ public static void IsNotInRange(int value, int minimum, int maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1226,7 +1325,10 @@ public static void IsBetween(int value, int minimum, int maximum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1244,7 +1346,10 @@ public static void IsNotBetween(int value, int minimum, int maximum, string name /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1262,7 +1367,10 @@ public static void IsBetweenOrEqualTo(int value, int minimum, int maximum, strin /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(int value, int minimum, int maximum, string name) { @@ -1381,7 +1489,10 @@ public static void IsGreaterThanOrEqualTo(uint value, uint minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1399,7 +1510,10 @@ public static void IsInRange(uint value, uint minimum, uint maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1417,7 +1531,10 @@ public static void IsNotInRange(uint value, uint minimum, uint maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1435,7 +1552,10 @@ public static void IsBetween(uint value, uint minimum, uint maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1453,7 +1573,10 @@ public static void IsNotBetween(uint value, uint minimum, uint maximum, string n /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1471,7 +1594,10 @@ public static void IsBetweenOrEqualTo(uint value, uint minimum, uint maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(uint value, uint minimum, uint maximum, string name) { @@ -1590,7 +1716,10 @@ public static void IsGreaterThanOrEqualTo(float value, float minimum, string nam /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1608,7 +1737,10 @@ public static void IsInRange(float value, float minimum, float maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1626,7 +1758,10 @@ public static void IsNotInRange(float value, float minimum, float maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1644,7 +1779,10 @@ public static void IsBetween(float value, float minimum, float maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1662,7 +1800,10 @@ public static void IsNotBetween(float value, float minimum, float maximum, strin /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1680,7 +1821,10 @@ public static void IsBetweenOrEqualTo(float value, float minimum, float maximum, /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(float value, float minimum, float maximum, string name) { @@ -1799,7 +1943,10 @@ public static void IsGreaterThanOrEqualTo(long value, long minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -1817,7 +1964,10 @@ public static void IsInRange(long value, long minimum, long maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -1835,7 +1985,10 @@ public static void IsNotInRange(long value, long minimum, long maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -1853,7 +2006,10 @@ public static void IsBetween(long value, long minimum, long maximum, string name /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -1871,7 +2027,10 @@ public static void IsNotBetween(long value, long minimum, long maximum, string n /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -1889,7 +2048,10 @@ public static void IsBetweenOrEqualTo(long value, long minimum, long maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(long value, long minimum, long maximum, string name) { @@ -2008,7 +2170,10 @@ public static void IsGreaterThanOrEqualTo(ulong value, ulong minimum, string nam /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2026,7 +2191,10 @@ public static void IsInRange(ulong value, ulong minimum, ulong maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2044,7 +2212,10 @@ public static void IsNotInRange(ulong value, ulong minimum, ulong maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2062,7 +2233,10 @@ public static void IsBetween(ulong value, ulong minimum, ulong maximum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2080,7 +2254,10 @@ public static void IsNotBetween(ulong value, ulong minimum, ulong maximum, strin /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2098,7 +2275,10 @@ public static void IsBetweenOrEqualTo(ulong value, ulong minimum, ulong maximum, /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(ulong value, ulong minimum, ulong maximum, string name) { @@ -2217,7 +2397,10 @@ public static void IsGreaterThanOrEqualTo(double value, double minimum, string n /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2235,7 +2418,10 @@ public static void IsInRange(double value, double minimum, double maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2253,7 +2439,10 @@ public static void IsNotInRange(double value, double minimum, double maximum, st /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2271,7 +2460,10 @@ public static void IsBetween(double value, double minimum, double maximum, strin /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2289,7 +2481,10 @@ public static void IsNotBetween(double value, double minimum, double maximum, st /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2307,7 +2502,10 @@ public static void IsBetweenOrEqualTo(double value, double minimum, double maxim /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(double value, double minimum, double maximum, string name) { @@ -2426,7 +2624,10 @@ public static void IsGreaterThanOrEqualTo(decimal value, decimal minimum, string /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { @@ -2444,7 +2645,10 @@ public static void IsInRange(decimal value, decimal minimum, decimal maximum, st /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { @@ -2462,7 +2666,10 @@ public static void IsNotInRange(decimal value, decimal minimum, decimal maximum, /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { @@ -2480,7 +2687,10 @@ public static void IsBetween(decimal value, decimal minimum, decimal maximum, st /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { @@ -2498,7 +2708,10 @@ public static void IsNotBetween(decimal value, decimal minimum, decimal maximum, /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { @@ -2516,7 +2729,10 @@ public static void IsBetweenOrEqualTo(decimal value, decimal minimum, decimal ma /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(decimal value, decimal minimum, decimal maximum, string name) { diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt index 746d89012c5..3cb7ee32093 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Comparable.Numeric.tt @@ -131,7 +131,10 @@ GenerateTextForItems(NumericTypes, type => /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { @@ -149,7 +152,10 @@ GenerateTextForItems(NumericTypes, type => /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { @@ -167,7 +173,10 @@ GenerateTextForItems(NumericTypes, type => /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { @@ -185,7 +194,10 @@ GenerateTextForItems(NumericTypes, type => /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { @@ -203,7 +215,10 @@ GenerateTextForItems(NumericTypes, type => /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { @@ -221,7 +236,10 @@ GenerateTextForItems(NumericTypes, type => /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name) { diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs index 4ee0479511b..1c974232ecd 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Generic.cs @@ -255,7 +255,10 @@ public static void IsGreaterThanOrEqualTo(T value, T minimum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable @@ -275,7 +278,10 @@ public static void IsInRange(T value, T minimum, T maximum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable @@ -295,7 +301,10 @@ public static void IsNotInRange(T value, T minimum, T maximum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable @@ -315,7 +324,10 @@ public static void IsBetween(T value, T minimum, T maximum, string name) /// The exclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable @@ -335,7 +347,10 @@ public static void IsNotBetween(T value, T minimum, T maximum, string name) /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable @@ -355,7 +370,10 @@ public static void IsBetweenOrEqualTo(T value, T minimum, T maximum, string n /// The inclusive maximum value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . - /// The method is generic to avoid boxing the parameters, if they are value types. + /// + /// 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(T value, T minimum, T maximum, string name) where T : notnull, IComparable From 41fea84172629f566aa090034ce3036ec6151312 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 22:18:18 +0100 Subject: [PATCH 118/123] Added IsCloseTo overloads for long type --- .../Diagnostics/Guard.Comparable.Numeric.cs | 34 +++++++++++++++++++ .../ThrowHelper.Comparable.Numeric.cs | 18 ++++++++++ 2 files changed, 52 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs index d4d9b31c833..f3f76ba55e2 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs @@ -56,6 +56,40 @@ public static void IsNotCloseTo(int value, int target, int delta, string 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(long value, long target, long delta, string name) + { + if (Math.Abs((decimal)value - target) > delta) + { + 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(long value, long target, long delta, string name) + { + if (Math.Abs((decimal)value - target) <= delta) + { + ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); + } + } + /// /// Asserts that the input value must be within a given distance from a specified value. /// diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs index 586be6148b4..0afc8b548a4 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs @@ -35,6 +35,24 @@ public static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, 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)] + public static void ThrowArgumentExceptionForIsCloseTo(long value, long target, long 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)] + public static void ThrowArgumentExceptionForIsNotCloseTo(long value, long target, long 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. /// From 132d240ded5d271e1e92a28c5647fefa0800b06d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 22:26:24 +0100 Subject: [PATCH 119/123] Minor code tweaks --- .../Diagnostics/Guard.Comparable.Numeric.cs | 27 +++++++++++-------- .../ThrowHelper.Comparable.Numeric.cs | 16 +++++------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs index f3f76ba55e2..712b5fa6784 100644 --- a/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs +++ b/Microsoft.Toolkit/Diagnostics/Guard.Comparable.Numeric.cs @@ -23,7 +23,7 @@ public static partial class Guard /// The name of the input parameter being tested. /// Thrown if ( - ) > . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsCloseTo(int value, int target, int delta, string name) + 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. @@ -32,8 +32,11 @@ public static void IsCloseTo(int value, int target, int delta, string name) * 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. */ - if (Math.Abs((double)((long)value - target)) > delta) + * 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) { ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name); } @@ -48,9 +51,9 @@ public static void IsCloseTo(int value, int target, int delta, string name) /// The name of the input parameter being tested. /// Thrown if ( - ) <= . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotCloseTo(int value, int target, int delta, string name) + public static void IsNotCloseTo(int value, int target, uint delta, string name) { - if (Math.Abs((double)((long)value - target)) <= delta) + if ((uint)Math.Abs((double)((long)value - target)) <= delta) { ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); } @@ -64,10 +67,12 @@ public static void IsNotCloseTo(int value, int target, int delta, string name) /// The maximum distance to allow between and . /// The name of the input parameter being tested. /// Thrown if ( - ) > . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsCloseTo(long value, long target, long delta, string name) + [MethodImpl(MethodImplOptions.NoInlining)] + public static void IsCloseTo(long value, long target, ulong delta, string name) { - if (Math.Abs((decimal)value - target) > delta) + /* 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) { ThrowHelper.ThrowArgumentExceptionForIsCloseTo(value, target, delta, name); } @@ -81,10 +86,10 @@ public static void IsCloseTo(long value, long target, long delta, string name) /// The maximum distance to allow between and . /// The name of the input parameter being tested. /// Thrown if ( - ) <= . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void IsNotCloseTo(long value, long target, long delta, string name) + [MethodImpl(MethodImplOptions.NoInlining)] + public static void IsNotCloseTo(long value, long target, ulong delta, string name) { - if (Math.Abs((decimal)value - target) <= delta) + if ((ulong)Math.Abs((decimal)value - target) <= delta) { ThrowHelper.ThrowArgumentExceptionForIsNotCloseTo(value, target, delta, name); } diff --git a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs index 0afc8b548a4..ab05318ad6c 100644 --- a/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs +++ b/Microsoft.Toolkit/Diagnostics/ThrowHelper.Comparable.Numeric.cs @@ -18,37 +18,37 @@ namespace Microsoft.Toolkit.Diagnostics internal static partial class ThrowHelper { /// - /// Throws an when fails. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsCloseTo(int value, int target, int delta, string name) + public 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. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotCloseTo(int value, int target, int delta, string name) + public 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. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsCloseTo(long value, long target, long delta, string name) + public 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. + /// Throws an when fails. /// [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentExceptionForIsNotCloseTo(long value, long target, long delta, string name) + public 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()}"); } From f17aacd5a1e0f58df35536fb5d15bf9f4dcb0fcd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 11 Mar 2020 22:41:46 +0100 Subject: [PATCH 120/123] Added comment to describe (uint) cast range check trick --- .../Diagnostics/Generated/Guard.Collection.tt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt index 671c2db5d21..37430fced50 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt @@ -247,6 +247,13 @@ GenerateTextForItems(EnumerableTypes, item => [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsInRangeFor(int index, <#=item.Type#> <#=item.Name#>, string name) { +<# + /* Here we're leveraging the fact that signed integers are represented + * in 2-complement to perform the bounds check with a single compare operation. + * This is the same trick used throughout CoreCLR as well. + * For more info and code sample, see the original conversation here: + * https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835 */ +#> if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>) { ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name); From 949134be6382f35e1e1402a8b83017336ae42dc9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Mar 2020 01:49:25 +0100 Subject: [PATCH 121/123] Added a nore about the .g.cs files being checked in --- Microsoft.Toolkit/Diagnostics/Generated/Guard.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md index 7837634f482..6b381a62b90 100644 --- a/Microsoft.Toolkit/Diagnostics/Generated/Guard.md +++ b/Microsoft.Toolkit/Diagnostics/Generated/Guard.md @@ -13,3 +13,5 @@ There are a few reasons why T4 templates are used for the `Guard` class: ## How to make changes If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.g.cs` file automatically. Don't make changes to those generated `.g.cs` files directly, as those will be overwritten as soon as their source template is updated. + +Note that all the `.g.cs` files are checked in into the repository, so if you do make changes to a template file, make sure to also include the updated `.g.cs` file in your commits. From 51b45e2dd7ff8687c0d2d5ed457628d6251e090a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Mar 2020 23:27:49 +0100 Subject: [PATCH 122/123] Fixed IsCloseTo tests --- UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs b/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs index 0c76eb8f8f0..a2df22682cd 100644 --- a/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs +++ b/UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs @@ -28,7 +28,7 @@ public void Test_Guard_IsCloseToInt_Ok() [SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")] public void Test_Guard_IsCloseToInt_Fail() { - foreach (var item in new (int Value, int Target, int Delta)[] + foreach (var item in new (int Value, int Target, uint Delta)[] { (0, 20, 10), (0, 6, 5), @@ -78,7 +78,7 @@ public void Test_Guard_IsCloseToFloat_Fail() (0, float.MaxValue, 500), (-500, -530, 10), (1000, 800, 100), - (float.MaxValue, float.MaxValue - 10, 7), + (float.MaxValue, float.MaxValue / 2, 7), (float.MinValue, float.MaxValue, float.MaxValue) }) { From 244da990ee3458d91bcaa8013bb064b529e85d39 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Mar 2020 23:29:10 +0100 Subject: [PATCH 123/123] Fixed IsNull tests --- UnitTests/Diagnostics/Test_Guard.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitTests/Diagnostics/Test_Guard.cs b/UnitTests/Diagnostics/Test_Guard.cs index af71a1c90df..935c4e2a868 100644 --- a/UnitTests/Diagnostics/Test_Guard.cs +++ b/UnitTests/Diagnostics/Test_Guard.cs @@ -21,7 +21,7 @@ public void Test_Guard_IsNull_Ok() [TestCategory("Guard")] [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_ClassFail() { Guard.IsNull(new object(), nameof(Test_Guard_IsNull_ClassFail)); @@ -29,7 +29,7 @@ public void Test_Guard_IsNull_ClassFail() [TestCategory("Guard")] [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] + [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_StructFail() { Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail));