diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs index 27feb046f..bc938ca0c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs @@ -2,6 +2,7 @@ { using System; using System.Collections.Generic; + using System.Runtime.CompilerServices; using ImageSharp.PixelFormats; @@ -90,11 +91,13 @@ } } - - private static int GetDifferenceInPixelByteSum(ref Rgba32 expected, ref Rgba32 actual) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int GetDifferenceInPixelByteSum(ref Rgba32 a, ref Rgba32 b) { - return (int)actual.R - (int)expected.R + (int)actual.G - (int)expected.G + (int)actual.B - (int)expected.B - + (int)actual.A - (int)expected.A; + return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static int Diff(byte a, byte b) => Math.Abs(a - b); } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index ce61172e2..728cf4569 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests private const string ActualOutputDirectoryRelativePath = @"tests\Images\ActualOutput"; - private const string ReferenceOutputDirectoryRelativePath = @"tests\Images\ReferenceOutput"; + private const string ReferenceOutputDirectoryRelativePath = @"tests\Images\External\ReferenceOutput"; private static Lazy solutionDirectoryFullPath = new Lazy(GetSolutionDirectoryFullPathImpl); @@ -79,6 +79,6 @@ namespace ImageSharp.Tests internal static string ReferenceOutputDirectoryFullPath => Path.Combine(SolutionDirectoryFullPath, ReferenceOutputDirectoryRelativePath); internal static string GetReferenceOutputFileName(string actualOutputFileName) => - actualOutputFileName.Replace("ActualOutput", "ReferenceOutput"); + actualOutputFileName.Replace("ActualOutput", @"External\ReferenceOutput"); } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index fe70e6cc1..41619a3d2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -6,10 +6,7 @@ namespace ImageSharp.Tests { using System; - using System.Collections.Generic; using System.IO; - using System.Linq; - using System.Reflection; using ImageSharp.PixelFormats; using ImageSharp.Tests.TestUtilities.ImageComparison; @@ -38,7 +35,7 @@ namespace ImageSharp.Tests { return image; } - + // We are running locally then we want to save it out provider.Utility.SaveTestOutputFile( image, @@ -55,10 +52,10 @@ namespace ImageSharp.Tests /// The pixel format /// The image /// The image provider + /// The to use /// Details to be concatenated to the test output file, describing the parameters of the test. /// The extension /// A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size. - /// A custom if exact equlity is not the expected behaviour /// public static Image CompareToReferenceOutput( this Image image, @@ -67,6 +64,36 @@ namespace ImageSharp.Tests string extension = "png", bool grayscale = false) where TPixel : struct, IPixel + { + return CompareToReferenceOutput( + image, + provider, + ImageComparer.Tolerant(), + testOutputDetails, + extension, + grayscale); + } + + /// + /// Compares the image against the expected Reference output, throws an exception if the images are not similar enough. + /// The output file should be named identically to the output produced by . + /// + /// The pixel format + /// The image + /// The image provider + /// The to use + /// Details to be concatenated to the test output file, describing the parameters of the test. + /// The extension + /// A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size. + /// + public static Image CompareToReferenceOutput( + this Image image, + ITestImageProvider provider, + ImageComparer comparer, + object testOutputDetails = null, + string extension = "png", + bool grayscale = false) + where TPixel : struct, IPixel { string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails); @@ -86,11 +113,10 @@ namespace ImageSharp.Tests using (Image referenceImage = Image.Load(referenceOutputFile, ReferenceDecoder.Instance)) { - ImageComparer comparer = ImageComparer.Exact; - comparer.CompareImages(referenceImage, image); + comparer.VerifySimilarity(referenceImage, image); } - + return image; } } -} +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs index 8bb26ad17..799bd822a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs @@ -90,7 +90,7 @@ namespace ImageSharp.Tests } [Theory] - [WithTestPatternImages(100, 100, PixelTypes.Rgba32)] + [WithTestPatternImages(110, 110, PixelTypes.Rgba32)] public void TolerantImageComparer_ApprovesSimilarityBelowTolerance(TestImageProvider provider) where TPixel : struct, IPixel { diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs new file mode 100644 index 000000000..1a5e85646 --- /dev/null +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs @@ -0,0 +1,47 @@ +// ReSharper disable InconsistentNaming +namespace ImageSharp.Tests +{ + using System; + + using ImageSharp.PixelFormats; + + using Xunit; + + public class TestImageExtensionsTests + { + [Theory] + [WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)] + public void CompareToReferenceOutput_WhenReferenceOutputMatches_ShouldNotThrow( + TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + image.CompareToReferenceOutput(provider); + } + } + + [Theory] + [WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)] + public void CompareToReferenceOutput_WhenReferenceOutputDoesNotMatch_Throws( + TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + Assert.ThrowsAny(() => image.CompareToReferenceOutput(provider)); + } + } + + [Theory] + [WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)] + public void CompareToReferenceOutput_WhenReferenceFileMissing_Throws(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage()) + { + Assert.ThrowsAny(() => image.CompareToReferenceOutput(provider)); + } + } + } +} \ No newline at end of file diff --git a/tests/Images/External b/tests/Images/External index 8240bdb29..ec2161042 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 8240bdb291e3476c4e50fef6fe2fccfe4fbd10c4 +Subproject commit ec2161042fe9addeff10fab73b0a3d71172b86a8