Browse Source

TestImageExtensionsTests + TolerantImageComparer bugfix + update submodule

af/merge-core
Anton Firszov 9 years ago
parent
commit
524456a9bd
  1. 11
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs
  2. 4
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs
  3. 44
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs
  4. 2
      tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs
  5. 47
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageExtensionsTests.cs
  6. 2
      tests/Images/External

11
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);
}
}

4
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<string> solutionDirectoryFullPath = new Lazy<string>(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");
}
}

44
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
/// <typeparam name="TPixel">The pixel format</typeparam>
/// <param name="image">The image</param>
/// <param name="provider">The image provider</param>
/// <param name="comparer">The <see cref="ImageComparer"/> to use</param>
/// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
/// <param name="extension">The extension</param>
/// <param name="grayscale">A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size.</param>
/// <param name="customImageComparer">A custom <see cref="ImageComparer"/> if exact equlity is not the expected behaviour</param>
/// <returns></returns>
public static Image<TPixel> CompareToReferenceOutput<TPixel>(
this Image<TPixel> image,
@ -67,6 +64,36 @@ namespace ImageSharp.Tests
string extension = "png",
bool grayscale = false)
where TPixel : struct, IPixel<TPixel>
{
return CompareToReferenceOutput(
image,
provider,
ImageComparer.Tolerant(),
testOutputDetails,
extension,
grayscale);
}
/// <summary>
/// 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 <see cref="DebugSave{TPixel}(Image{TPixel}, ITestImageProvider, object, string, bool)"/>.
/// </summary>
/// <typeparam name="TPixel">The pixel format</typeparam>
/// <param name="image">The image</param>
/// <param name="provider">The image provider</param>
/// <param name="comparer">The <see cref="ImageComparer"/> to use</param>
/// <param name="testOutputDetails">Details to be concatenated to the test output file, describing the parameters of the test.</param>
/// <param name="extension">The extension</param>
/// <param name="grayscale">A boolean indicating whether we should debug save + compare against a grayscale image, smaller in size.</param>
/// <returns></returns>
public static Image<TPixel> CompareToReferenceOutput<TPixel>(
this Image<TPixel> image,
ITestImageProvider provider,
ImageComparer comparer,
object testOutputDetails = null,
string extension = "png",
bool grayscale = false)
where TPixel : struct, IPixel<TPixel>
{
string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails);
@ -86,11 +113,10 @@ namespace ImageSharp.Tests
using (Image<Rgba32> referenceImage = Image.Load<Rgba32>(referenceOutputFile, ReferenceDecoder.Instance))
{
ImageComparer comparer = ImageComparer.Exact;
comparer.CompareImages(referenceImage, image);
comparer.VerifySimilarity(referenceImage, image);
}
return image;
}
}
}
}

2
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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{

47
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<TPixel>(
TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.CompareToReferenceOutput(provider);
}
}
[Theory]
[WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)]
public void CompareToReferenceOutput_WhenReferenceOutputDoesNotMatch_Throws<TPixel>(
TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
}
}
[Theory]
[WithSolidFilledImages(10, 10, 0, 0, 255, PixelTypes.Rgba32)]
public void CompareToReferenceOutput_WhenReferenceFileMissing_Throws<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
Assert.ThrowsAny<Exception>(() => image.CompareToReferenceOutput(provider));
}
}
}
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 8240bdb291e3476c4e50fef6fe2fccfe4fbd10c4
Subproject commit ec2161042fe9addeff10fab73b0a3d71172b86a8
Loading…
Cancel
Save