diff --git a/src/SixLabors.Core/Helpers/DebugGuard.cs b/src/SixLabors.Core/Helpers/DebugGuard.cs
index 7850545f34..e836a1bba4 100644
--- a/src/SixLabors.Core/Helpers/DebugGuard.cs
+++ b/src/SixLabors.Core/Helpers/DebugGuard.cs
@@ -178,6 +178,26 @@ namespace SixLabors
}
}
+ ///
+ /// Verifies, that the target span is of same size than the 'other' span.
+ ///
+ /// The element type of the spans
+ /// The target span.
+ /// The 'other' span to compare 'target' to.
+ /// The name of the parameter that is to be checked.
+ ///
+ /// is true
+ ///
+ [Conditional("DEBUG")]
+ public static void MustBeSameSized(Span target, Span other, string parameterName)
+ where T : struct
+ {
+ if (target.Length != other.Length)
+ {
+ throw new ArgumentException("Span-s must be the same size!", parameterName);
+ }
+ }
+
///
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
///
@@ -197,5 +217,45 @@ namespace SixLabors
throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName);
}
}
+
+ ///
+ /// Verifies, that the `target` span has the length of 'minSpan', or longer.
+ ///
+ /// The element type of the spans
+ /// The target span.
+ /// The 'minSpan' span to compare 'target' to.
+ /// The name of the parameter that is to be checked.
+ ///
+ /// is true
+ ///
+ [Conditional("DEBUG")]
+ public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName)
+ where T : struct
+ {
+ if (target.Length < minSpan.Length)
+ {
+ throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName);
+ }
+ }
+
+ ///
+ /// Verifies, that the `target` array has declared the length or longer.
+ ///
+ /// The element type of the spans
+ /// The target array.
+ /// The min length the array must have.
+ /// The name of the parameter that is to be checked.
+ ///
+ /// is true
+ ///
+ [Conditional("DEBUG")]
+ public static void MustBeSizedAtLeast(T[] target, int minLength, string parameterName)
+ where T : struct
+ {
+ if (target.Length < minLength)
+ {
+ throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
+ }
+ }
}
}
\ No newline at end of file
diff --git a/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs b/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs
new file mode 100644
index 0000000000..71b6436ca5
--- /dev/null
+++ b/tests/SixLabors.Core.Tests/Helpers/DebugGuardTests.cs
@@ -0,0 +1,260 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
+// tell this file to enable debug conditional method calls, i.e. all the debug guard calls
+#define DEBUG
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Reflection;
+using Xunit;
+
+namespace SixLabors.Helpers.Tests
+{
+ public class DebugGuardTests
+ {
+ [Fact]
+ public void AllStaticMethodsOnOnDebugGuardHaveDEBUGConditional()
+ {
+ var methods = typeof(DebugGuard).GetTypeInfo().GetMethods()
+ .Where(x => x.IsStatic);
+
+ foreach (var m in methods)
+ {
+ var attribs = m.GetCustomAttributes();
+ Assert.True(attribs.Select(x => x.ConditionString).Contains("DEBUG"), $"Method '{m.Name}' does not have [Conditional(\"DEBUG\")] set.");
+ }
+ }
+
+ [Fact]
+ public void NotNull_TargetNotNull_ThrowsNoException()
+ {
+ DebugGuard.NotNull("test", "myParamName");
+ }
+
+ [Fact]
+ public void NotNull_TargetNull_ThrowsException()
+ {
+ Assert.Throws(() =>
+ {
+ DebugGuard.NotNull(null, "myParamName");
+ });
+ }
+
+
+ [Fact]
+ public void MustBeLessThan_IsLess_ThrowsNoException()
+ {
+ DebugGuard.MustBeLessThan(0, 1, "myParamName");
+ }
+
+ [Theory]
+ [InlineData(2, 1)]
+ [InlineData(1, 1)]
+ public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max)
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeLessThan(value, max, "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains($"Value must be less than {max}."));
+ }
+
+ [Theory]
+ [InlineData(0, 1)]
+ [InlineData(1, 1)]
+ public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max)
+ {
+ DebugGuard.MustBeLessThanOrEqualTo(value, max, "myParamName");
+ }
+
+ [Fact]
+ public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeLessThanOrEqualTo(2, 1, "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains($"Value must be less than or equal to 1."));
+ }
+
+ [Fact]
+ public void MustBeGreaterThan_IsGreater_ThrowsNoException()
+ {
+ DebugGuard.MustBeGreaterThan(2, 1, "myParamName");
+ }
+
+ [Theory]
+ [InlineData(1, 2)]
+ [InlineData(1, 1)]
+ public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min)
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeGreaterThan(value, min, "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains($"Value must be greater than {min}."));
+ }
+
+ [Theory]
+ [InlineData(2, 1)]
+ [InlineData(1, 1)]
+ public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min)
+ {
+ DebugGuard.MustBeGreaterThanOrEqualTo(value, min, "myParamName");
+ }
+
+ [Fact]
+ public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2."));
+ }
+
+ [Fact]
+ public void IsTrue_IsTrue_ThrowsNoException()
+ {
+ DebugGuard.IsTrue(true, "myParamName", "myTestMessage");
+ }
+
+ [Fact]
+ public void IsTrue_IsFalse_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.IsTrue(false, "myParamName", "myTestMessage");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains("myTestMessage"));
+ }
+
+ [Fact]
+ public void IsFalse_IsFalse_ThrowsNoException()
+ {
+ DebugGuard.IsFalse(false, "myParamName", "myTestMessage");
+ }
+
+ [Fact]
+ public void IsFalse_IsTrue_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.IsFalse(true, "myParamName", "myTestMessage");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.True(exception.Message.Contains("myTestMessage"));
+ }
+
+ [Theory]
+ [InlineData(new int[] { 1, 2 }, 1)]
+ [InlineData(new int[] { 1, 2 }, 2)]
+ public void MustBeSizedAtLeast_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength)
+ {
+ DebugGuard.MustBeSizedAtLeast(value, minLength, "myParamName");
+ }
+
+ [Fact]
+ public void MustBeSizedAtLeast_LengthIsLess_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeSizedAtLeast(new int[] { 1, 2 }, 3, "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.Contains($"The size must be at least 3.", exception.Message);
+ }
+
+ [Fact]
+ public void ReadOnlySpan_MustBeSizedAtLeast_LengthIsLess_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[3]), "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.Contains($"Span-s must be at least of length 3!", exception.Message);
+ }
+
+ [Fact]
+ public void Span_MustBeSizedAtLeast_LengthIsLess_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeSizedAtLeast(new Span(new int[2]), new Span(new int[3]), "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.Contains($"Span-s must be at least of length 3!", exception.Message);
+ }
+
+
+ [Fact]
+ public void ReadOnlySpan_MustBeSameSized_LengthIsLess_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeSameSized(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[3]), "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.Contains($"Span-s must be the same size!", exception.Message);
+ }
+
+ [Fact]
+ public void Span_MustBeSameSized_LengthIsLess_ThrowsException()
+ {
+ var exception = Assert.Throws(() =>
+ {
+ DebugGuard.MustBeSameSized(new Span(new int[2]), new Span(new int[3]), "myParamName");
+ });
+
+ Assert.Equal("myParamName", exception.ParamName);
+ Assert.Contains($"Span-s must be the same size!", exception.Message);
+ }
+
+ [Theory]
+ [InlineData(2, 2)]
+ [InlineData(2, 4)]
+ public void ReadOnlySpan_MustBeSizedAtLeast_DoesNotThowException(int leftSize, int rightSize)
+ {
+ DebugGuard.MustBeSizedAtLeast(new ReadOnlySpan(new int[leftSize]), new ReadOnlySpan(new int[rightSize]), "myParamName");
+ }
+
+ [Theory]
+ [InlineData(2, 2)]
+ [InlineData(2, 4)]
+ public void Span_MustBeSizedAtLeast_DoesNotThowException(int leftSize, int rightSize)
+ {
+ DebugGuard.MustBeSizedAtLeast(new Span(new int[leftSize]), new Span(new int[rightSize]), "myParamName");
+ }
+
+ [Fact]
+ public void ReadOnlySpan_MustBeSameSized_LengthIsEqual_DoesNotThrowException()
+ {
+ DebugGuard.MustBeSameSized(new ReadOnlySpan(new int[2]), new ReadOnlySpan(new int[2]), "myParamName");
+ }
+
+ [Fact]
+ public void Span_MustBeSameSized_LengthIsEqual_DoesNotThrowException()
+ {
+ DebugGuard.MustBeSameSized(new Span(new int[2]), new Span(new int[2]), "myParamName");
+ }
+ }
+}