From f27d8256e4d71463337e6099ba568ffb958ad73b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 11 Dec 2021 13:42:47 +0100 Subject: [PATCH] Add unit tests for new Save() overloads for IBufferWriter --- tests/ComputeSharp.Tests/ImagingTests.cs | 68 +++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.Tests/ImagingTests.cs b/tests/ComputeSharp.Tests/ImagingTests.cs index 216083bc..867993c9 100644 --- a/tests/ComputeSharp.Tests/ImagingTests.cs +++ b/tests/ComputeSharp.Tests/ImagingTests.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -14,6 +15,7 @@ using SixLabors.ImageSharp.PixelFormats; using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32; using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32; using ImageSharpL8 = SixLabors.ImageSharp.PixelFormats.L8; +using Microsoft.Toolkit.HighPerformance.Buffers; namespace ComputeSharp.Tests; @@ -329,6 +331,28 @@ public class ImagingTests TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveRgba32AsJpeg_ToBufferWriter(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string expectedPath = Path.Combine(path, "city.jpg"); + string actualPath = Path.Combine(path, "city_rgba32_saved.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), expectedPath); + + using (ArrayPoolBufferWriter writer = new()) + { + texture.Save(writer, ImageFormat.Jpeg); + + File.WriteAllBytes(actualPath, writer.WrittenSpan.ToArray()); + } + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); + } + [CombinatorialTestMethod] [AllDevices] [Resource(typeof(ReadOnlyTexture2D<,>))] @@ -374,6 +398,31 @@ public class ImagingTests TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); } + [CombinatorialTestMethod] + [AllDevices] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + public void SaveRgba32AsJpeg_ToBufferWriter_WithReadBackTexture(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging"); + string expectedPath = Path.Combine(path, "city.jpg"); + string actualPath = Path.Combine(path, "city_rgba32_saved.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), expectedPath); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + using (ArrayPoolBufferWriter writer = new()) + { + readback.Save(writer, ImageFormat.Jpeg); + + File.WriteAllBytes(actualPath, writer.WrittenSpan.ToArray()); + } + + TolerantImageComparer.AssertEqual(expectedPath, actualPath, 0.00001023f); + } + [CombinatorialTestMethod] [Device(Device.Warp)] [Resource(typeof(ReadOnlyTexture2D<,>))] @@ -405,7 +454,24 @@ public class ImagingTests texture.CopyTo(readback); - readback.Save(null!, ImageFormat.Jpeg); + readback.Save((Stream)null!, ImageFormat.Jpeg); + } + + [CombinatorialTestMethod] + [Device(Device.Warp)] + [Resource(typeof(ReadOnlyTexture2D<,>))] + [Resource(typeof(ReadWriteTexture2D<,>))] + [ExpectedException(typeof(ArgumentNullException))] + public void SaveRgba32AsJpeg_ToBufferWriter_WithReadBackTexture_WithNullBufferWriter(Device device, Type textureType) + { + string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg"); + + using Texture2D texture = device.Get().LoadTexture2D(textureType, typeof(string), path); + using ReadBackTexture2D readback = device.Get().AllocateReadBackTexture2D(texture.Width, texture.Height); + + texture.CopyTo(readback); + + readback.Save((IBufferWriter)null!, ImageFormat.Jpeg); } [CombinatorialTestMethod]