From a0c1787d1049515bafadd32e869ab0dc80fc955f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 30 Dec 2021 21:43:35 +0100 Subject: [PATCH] Add unit tests for async APIs --- .../ComputeSharp.Tests/ComputeContextTests.cs | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 3ddaf53f..38e74a87 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading.Tasks; using ComputeSharp.__Internals; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; @@ -433,6 +434,91 @@ public partial class ComputeContextTests } } + [CombinatorialTestMethod] + [AllDevices] + public async Task ForAndForEach_Async_Batched(Device device) + { + int[] array = Enumerable.Range(0, 1024).ToArray(); + + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(array); + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); + + await using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.For(512, new OffsetComputeShader(buffer, 0)); + context.For(64, new OffsetComputeShader(buffer, 512)); + context.ForEach(texture, default(ClearPixelShader)); + context.For(299, new OffsetComputeShader(buffer, 576)); + context.Barrier(texture); + context.ForEach(texture, new ColorPixelShader(new float4(0.22f, 0.44f, 0.66f, 0.88f))); + context.For(149, new OffsetComputeShader(buffer, 875)); + } + + void Validate() + { + // Buffer test + { + int[] result = buffer.ToArray(); + + foreach (ref int x in array.AsSpan()) + { + x *= 2; + } + + CollectionAssert.AreEqual(array, result); + } + + // Texture test + { + Rgba32[,] result = texture.ToArray(); + + foreach (Rgba32 pixel in result) + { + Assert.AreEqual(pixel.R, 56); + Assert.AreEqual(pixel.G, 112); + Assert.AreEqual(pixel.B, 168); + Assert.AreEqual(pixel.A, 224); + } + } + } + + Validate(); + } + + [CombinatorialTestMethod] + [AllDevices] + public async Task ForAndForEach_Async_Batched_Parallel(Device device) + { + async Task TestAsync(Device device) + { + for (int i = 0; i < 20; i++) + { + await ForAndForEach_Async_Batched(device); + } + } + + await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); + } + + [CombinatorialTestMethod] + [AllDevices] + public async Task ForAndForEach_Async_Batched_Parallel_Multiple(Device device) + { + async Task TestAsync(Device device) + { + for (int i = 0; i < 10; i++) + { + Task task1 = ForAndForEach_Async_Batched(device); + Task task2 = ForAndForEach_Async_Batched(device); + Task task3 = ForAndForEach_Async_Batched(device); + + await Task.WhenAll(task1, task2, task3); + } + } + + await Task.WhenAll(Enumerable.Range(0, 64).Select(_ => TestAsync(device))); + } + [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] @@ -448,6 +534,13 @@ public partial class ComputeContextTests Assert.Fail(); } + [TestMethod] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public void DefaultContext_GetGraphicsDevice_ThrowsException() + { + _ = default(ComputeContext).GraphicsDevice; + } + [CombinatorialTestMethod] [AllDevices] [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] @@ -508,6 +601,13 @@ public partial class ComputeContextTests using ComputeContext context = default; } + [TestMethod] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public async Task DefaultContext_DisposeAsync_ThrowsException() + { + await using ComputeContext context = default; + } + [AutoConstructor] internal readonly partial struct OffsetComputeShader : IComputeShader {