From f608289763eb07ade29cc9434f4fec3b72e1f39e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 2 Dec 2023 20:42:42 +0100 Subject: [PATCH] Add unit tests for value type constructors support --- .../UserDefinedTypeConstructorTests.cs | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/ComputeSharp.Tests/UserDefinedTypeConstructorTests.cs diff --git a/tests/ComputeSharp.Tests/UserDefinedTypeConstructorTests.cs b/tests/ComputeSharp.Tests/UserDefinedTypeConstructorTests.cs new file mode 100644 index 00000000..c1303361 --- /dev/null +++ b/tests/ComputeSharp.Tests/UserDefinedTypeConstructorTests.cs @@ -0,0 +1,130 @@ +using ComputeSharp.Tests.Attributes; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable IDE0008, IDE0009, IDE0290, CA1861 + +namespace ComputeSharp.Tests; + +[TestClass] +public partial class UserDefinedTypeConstructorTests +{ + [CombinatorialTestMethod] + [Device(Device.Warp)] + public void ImplicitParameterlessConstructor(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer([111, 222]); + + device.Get().For(1, new ImplicitParameterlessConstructorShader(buffer)); + + int[] results = buffer.ToArray(); + + CollectionAssert.AreEqual(new[] { 0, 0 }, results); + } + + public struct MyValueTypeWithNoConstructor + { + public int X; + public int Y; + } + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + public readonly partial struct ImplicitParameterlessConstructorShader(ReadWriteBuffer buffer) : IComputeShader + { + public void Execute() + { + MyValueTypeWithNoConstructor valueType = new(); + + buffer[0] = valueType.X; + buffer[1] = valueType.Y; + } + } + + [CombinatorialTestMethod] + [Device(Device.Warp)] + public void ExplicitParameterlessConstructor(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(2); + + device.Get().For(1, new ExplicitParameterlessConstructorShader(buffer)); + + int[] results = buffer.ToArray(); + + CollectionAssert.AreEqual(new[] { 42, 12345 }, results); + } + + public struct MyValueTypeWithParameterlessConstructor + { + public int X; + public int Y; + + public MyValueTypeWithParameterlessConstructor() + { + X = 42; + this.Y = 12345; + } + } + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + public readonly partial struct ExplicitParameterlessConstructorShader(ReadWriteBuffer buffer) : IComputeShader + { + public void Execute() + { + MyValueTypeWithParameterlessConstructor valueType = new(); + + buffer[0] = valueType.X; + buffer[1] = valueType.Y; + } + } + + [CombinatorialTestMethod] + [Device(Device.Warp)] + public void ConstructorWithParameters(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(8); + + device.Get().For(1, new ConstructorWithParametersShader(buffer)); + + int[] results = buffer.ToArray(); + + CollectionAssert.AreEqual(new[] { 42, 123456, 3, 4, 111, 222, 5, 6 }, results); + } + + public struct MyValueTypeWithConstructorWithParameters + { + public int X; + public int Y; + public float2 Z; + + public MyValueTypeWithConstructorWithParameters(int x, int y, float2 z) + { + X = x; + this.Y = y; + this.Z = z; + } + } + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + public readonly partial struct ConstructorWithParametersShader(ReadWriteBuffer buffer) : IComputeShader + { + public void Execute() + { + MyValueTypeWithConstructorWithParameters valueType = new(42, 123456, new float2(3, 4)); + + buffer[0] = valueType.X; + buffer[1] = valueType.Y; + buffer[2] = (int)valueType.Z.X; + buffer[3] = (int)valueType.Z.Y; + + var valueType2 = new MyValueTypeWithConstructorWithParameters(111, 222, new float2(5, 6)); + + buffer[4] = valueType2.X; + buffer[5] = valueType2.Y; + buffer[6] = (int)valueType2.Z.X; + buffer[7] = (int)valueType2.Z.Y; + } + } +} \ No newline at end of file