From 298614d9b9e6e34d3fc52d38e2e2b1cf4570d98a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Aug 2022 01:11:34 +0200 Subject: [PATCH] Minor refactoring to unit tests --- .../ComputeSharp.D2D1.Tests/EndToEndTests.cs | 93 ++----------------- .../Helpers/D2D1TestRunner.cs | 84 ++++++++++++++++- 2 files changed, 88 insertions(+), 89 deletions(-) diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs index 02a5da36..8a25a156 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs @@ -1,10 +1,5 @@ -using System; -using System.IO; -using System.Reflection; -using System.Runtime.CompilerServices; -using ComputeSharp.D2D1.Tests.Effects; +using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; -using ComputeSharp.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; using SixLabors.ImageSharp; @@ -23,19 +18,19 @@ public class EndToEndTests [TestMethod] public unsafe void Invert() { - RunAndCompareShader(new InvertEffect(), null, "Landscape.png", "Landscape_Inverted.png"); + D2D1TestRunner.RunAndCompareShader(new InvertEffect(), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] public unsafe void InvertWithThreshold() { - RunAndCompareShader(new InvertWithThresholdEffect(1), null, "Landscape.png", "Landscape_Inverted.png"); + D2D1TestRunner.RunAndCompareShader(new InvertWithThresholdEffect(1), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] public unsafe void Pixelate() { - RunAndCompareShader( + D2D1TestRunner.RunAndCompareShader( new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 840, 16)), static () => new PixelateEffect(), "Landscape.png", @@ -45,88 +40,12 @@ public class EndToEndTests [TestMethod] public unsafe void ZonePlate() { - RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png"); + D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), null, 1280, 720, "ZonePlate.png"); } [TestMethod] public unsafe void CheckerboardClip() { - RunAndCompareShader(new CheckerboardClipEffect(1280, 840, 32), null, "Landscape.png", "Landscape_CheckerboardClip.png"); - } - - /// - /// Executes a pixel shader and compares the expected results. - /// - /// The type of pixel shader to run. - /// A custom factory for the effect. - /// The name of the source image. - /// The name of the expected result image. - /// The name of the destination image to save results to. - /// The shader to run. - private static void RunAndCompareShader( - in T shader, - Func>? transformMapperFactory, - string originalFileName, - string expectedFileName, - [CallerMemberName] string destinationFileName = "") - where T : unmanaged, ID2D1PixelShader - { - string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); - string temporaryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "temp"); - - _ = Directory.CreateDirectory(temporaryPath); - - string originalPath = Path.Combine(assetsPath, originalFileName); - string expectedPath = Path.Combine(assetsPath, expectedFileName); - string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); - - // Run the shader - D2D1TestRunner.ExecutePixelShaderAndCompareResults( - in shader, - transformMapperFactory, - originalPath, - destinationPath); - - // Compare the results - TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); - } - - /// - /// Executes a pixel shader and compares the expected results. - /// - /// The type of pixel shader to run. - /// A custom factory for the effect. - /// The resulting width. - /// The resulting height. - /// The name of the expected result image. - /// The name of the destination image to save results to. - /// The shader to run. - private static void RunAndCompareShader( - in T shader, - Func>? transformMapperFactory, - int width, - int height, - string expectedFileName, - [CallerMemberName] string destinationFileName = "") - where T : unmanaged, ID2D1PixelShader - { - string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); - string temporaryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "temp"); - - _ = Directory.CreateDirectory(temporaryPath); - - string expectedPath = Path.Combine(assetsPath, expectedFileName); - string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); - - // Run the shader - D2D1TestRunner.ExecutePixelShaderAndCompareResults( - in shader, - transformMapperFactory, - width, - height, - destinationPath); - - // Compare the results - TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + D2D1TestRunner.RunAndCompareShader(new CheckerboardClipEffect(1280, 840, 32), null, "Landscape.png", "Landscape_CheckerboardClip.png"); } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index 65de58db..a03bfaf5 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -1,5 +1,9 @@ using System; +using System.IO; +using System.Reflection; +using System.Runtime.CompilerServices; using ComputeSharp.D2D1.Interop; +using ComputeSharp.Tests.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -10,6 +14,82 @@ namespace ComputeSharp.D2D1.Tests.Helpers; /// internal static class D2D1TestRunner { + /// + /// Executes a pixel shader and compares the expected results. + /// + /// The type of pixel shader to run. + /// A custom factory for the effect. + /// The name of the source image. + /// The name of the expected result image. + /// The name of the destination image to save results to. + /// The shader to run. + public static void RunAndCompareShader( + in T shader, + Func>? transformMapperFactory, + string originalFileName, + string expectedFileName, + [CallerMemberName] string destinationFileName = "") + where T : unmanaged, ID2D1PixelShader + { + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + string temporaryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "temp"); + + _ = Directory.CreateDirectory(temporaryPath); + + string originalPath = Path.Combine(assetsPath, originalFileName); + string expectedPath = Path.Combine(assetsPath, expectedFileName); + string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); + + // Run the shader + ExecutePixelShaderAndSaveToFile( + in shader, + transformMapperFactory, + originalPath, + destinationPath); + + // Compare the results + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + } + + /// + /// Executes a pixel shader and compares the expected results. + /// + /// The type of pixel shader to run. + /// A custom factory for the effect. + /// The resulting width. + /// The resulting height. + /// The name of the expected result image. + /// The name of the destination image to save results to. + /// The shader to run. + public static void RunAndCompareShader( + in T shader, + Func>? transformMapperFactory, + int width, + int height, + string expectedFileName, + [CallerMemberName] string destinationFileName = "") + where T : unmanaged, ID2D1PixelShader + { + string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); + string temporaryPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "temp"); + + _ = Directory.CreateDirectory(temporaryPath); + + string expectedPath = Path.Combine(assetsPath, expectedFileName); + string destinationPath = Path.Combine(temporaryPath, $"{destinationFileName}.png"); + + // Run the shader + ExecutePixelShaderAndSaveToFile( + in shader, + transformMapperFactory, + width, + height, + destinationPath); + + // Compare the results + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + } + /// /// Executes a pixel shader to produce an image. /// @@ -18,7 +98,7 @@ internal static class D2D1TestRunner /// A custom factory for the effect. /// The source path for the image to run the shader on. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndCompareResults( + public static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, Func>? transformMapperFactory, string sourcePath, @@ -58,7 +138,7 @@ internal static class D2D1TestRunner /// The resulting height. /// A custom factory for the effect. /// The destination path for the result. - public static unsafe void ExecutePixelShaderAndCompareResults( + public static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, Func>? transformMapperFactory, int width,