From e2faab5aedc4652cd8798ac2c4aa487153827c46 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 15:28:09 +0100 Subject: [PATCH] Add unit tests for normal texture clearing --- .../ComputeSharp.Tests/ComputeContextTests.cs | 132 +++++++++++++++++- .../ComputeSharp.Tests/Helpers/TestHelper.cs | 34 +++++ 2 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Helpers/TestHelper.cs diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index c0b48044..9e747b73 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -2,6 +2,8 @@ using System.Linq; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests; @@ -10,6 +12,112 @@ namespace ComputeSharp.Tests; [TestCategory("ComputeContext")] public partial class ComputeContextTests { + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(int))] + [Data(typeof(Int2))] + [Data(typeof(Int3))] + [Data(typeof(Int4))] + [Data(typeof(uint))] + [Data(typeof(UInt2))] + [Data(typeof(UInt3))] + [Data(typeof(UInt4))] + [Data(typeof(float))] + [Data(typeof(Float2))] + [Data(typeof(Float3))] + [Data(typeof(Float4))] + [Data(typeof(Bgra32))] + [Data(typeof(Rgba32))] + [Data(typeof(Rgba64))] + [Data(typeof(R8))] + [Data(typeof(R16))] + [Data(typeof(Rg16))] + [Data(typeof(Rg32))] + public void Clear_ReadWriteTexture2D(Device device, Type type) + { + static void Test(Device device) + where T : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear(texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, type, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(int))] + [Data(typeof(Int2))] + [Data(typeof(Int3))] + [Data(typeof(Int4))] + [Data(typeof(uint))] + [Data(typeof(UInt2))] + [Data(typeof(UInt3))] + [Data(typeof(UInt4))] + [Data(typeof(float))] + [Data(typeof(Float2))] + [Data(typeof(Float3))] + [Data(typeof(Float4))] + [Data(typeof(Bgra32))] + [Data(typeof(Rgba32))] + [Data(typeof(Rgba64))] + [Data(typeof(R8))] + [Data(typeof(R16))] + [Data(typeof(Rg16))] + [Data(typeof(Rg32))] + public void Clear_ReadWriteTexture3D(Device device, Type type) + { + static void Test(Device device) + where T : unmanaged + { + if (!device.Get().IsReadWriteTexture3DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear(texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, type, device); + } + [CombinatorialTestMethod] [AllDevices] public void For_Batched(Device device) @@ -108,7 +216,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void Barrier_WithoutPreviousDispatches_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); @@ -123,7 +231,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Barrier_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); @@ -135,8 +243,22 @@ public partial class ComputeContextTests Assert.Fail(); } + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public void DefaultContext_Clear_ThrowsException(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); + + using ComputeContext context = default; + + context.Clear(buffer); + + Assert.Fail(); + } + [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_For_ThrowsException() { using ComputeContext context = default; @@ -148,7 +270,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_ForEach_ThrowsException(Device device) { using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); @@ -161,7 +283,7 @@ public partial class ComputeContextTests } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Dispose_ThrowsException() { using ComputeContext context = default; diff --git a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs new file mode 100644 index 00000000..3601bd20 --- /dev/null +++ b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs @@ -0,0 +1,34 @@ +using System; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Helpers; + +/// +/// A helper class for test methods. +/// +public static class TestHelper +{ + /// + /// Runs a generic test methods with the specified arguments. + /// + /// A instance wrapping the target method. + /// The type argument to use to invoke . + /// The arguments to use to invoke . + public static void Run(Delegate @delegate, Type type, params object[] arguments) + { + if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true }) + { + Assert.Fail("The input delegate must point to a generic static method"); + } + + try + { + @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type).Invoke(null, arguments); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } +}