diff --git a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs index 77825c61..0c1a5453 100644 --- a/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs +++ b/tests/ComputeSharp.Tests.Internals/DynamicMethodTests.cs @@ -13,24 +13,8 @@ namespace ComputeSharp.Tests.Internals { Func square = DynamicMethod>.New(il => { - il.Emit(OpCodes.Ldarg_1); - il.Emit(OpCodes.Dup); - il.Emit(OpCodes.Mul); - il.Emit(OpCodes.Ret); - }); - - int result = square(2); // result = 2 * 2 - - Assert.IsTrue(result == 4); - } - - public delegate int Square(int x); - - [TestMethod] - public void TestSquare() - { - Square square = DynamicMethod.New(il => - { + // Due to the instance method trick, the first parameter is at offset 1. + // See the docs on DynamicMethod.New for more info on how this works. il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Dup); il.Emit(OpCodes.Mul); diff --git a/tests/ComputeSharp.Tests.Internals/ReadableMemberTests.cs b/tests/ComputeSharp.Tests.Internals/ReadableMemberTests.cs deleted file mode 100644 index 85af34b4..00000000 --- a/tests/ComputeSharp.Tests.Internals/ReadableMemberTests.cs +++ /dev/null @@ -1,71 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Numerics; -using System.Reflection; - -namespace ComputeSharp.Tests.Internals -{ - /// - /// A container for static fields and properties to test - /// - public sealed class StaticMembersContainer - { - /// - /// A field - /// - public static readonly int Field = 7; - - /// - /// A property - /// - public static int Property { get; } = 44; - } - - [TestClass] - [TestCategory("ReadableMembers")] - public class ReadableMemberTests - { - [TestMethod] - public void PropertyGetter() - { - string text = "Hello world"; - - ReadableMember member = typeof(string).GetProperty(nameof(string.Length)); - - object value = member.GetValue(text); - - Assert.IsTrue((int)value == text.Length); - } - - [TestMethod] - public void FieldGetter() - { - Vector4 vector = new Vector4(7, 0, 0, 0); - - ReadableMember member = typeof(Vector4).GetField(nameof(Vector4.X)); - - object value = member.GetValue(vector); - - Assert.IsTrue((int)(float)value == (int)vector.X); - } - - [TestMethod] - public void StaticFieldGetter() - { - ReadableMember member = typeof(StaticMembersContainer).GetField(nameof(StaticMembersContainer.Field), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - - object value = member.GetValue(null); - - Assert.IsTrue((int)value == StaticMembersContainer.Field); - } - - [TestMethod] - public void StaticPropertyGetter() - { - ReadableMember member = typeof(StaticMembersContainer).GetProperty(nameof(StaticMembersContainer.Property), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); - - object value = member.GetValue(null); - - Assert.IsTrue((int)value == StaticMembersContainer.Property); - } - } -} diff --git a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs index 8e5f8fe0..611628d2 100644 --- a/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests.Internals/ShaderCompilerTests.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Shaders.Translation; +using ComputeSharp.Exceptions; +using ComputeSharp.Shaders.Translation; using Microsoft.VisualStudio.TestTools.UnitTesting; using TerraFX.Interop; @@ -9,43 +10,37 @@ namespace ComputeSharp.Tests.Internals public class ShaderCompilerTests { private const string ShaderSource = @" - /* =============== - * AUTO GENERATED - * =============== - * This shader was created by ComputeSharp. - * For info or issues: https://github.com/Sergio0694/ComputeSharp */ - - // Scalar/vector variables cbuffer _ : register(b0) { - uint __x__; // Target X iterations - uint __y__; // Target Y iterations - uint __z__; // Target Z iterations - int width; + uint __x; + uint __y; + uint __z; } - // ReadWriteBuffer buffer ""buffer"" - RWStructuredBuffer buffer : register(u1); + RWStructuredBuffer buffer : register(u0); - // Shader body - [Shader(""compute"")] [NumThreads(32, 1, 1)] - void CSMain(uint3 ids : SV_DispatchThreadId) + void Execute(uint3 ids : SV_DispatchThreadId) { - if (ids.x < __x__ && - ids.y < __y__ && - ids.z < __z__) // Automatic bounds check + if (ids.x < __x && ids.y < __y && ids.z < __z) { - int i = ids.x + ids.y * width; - buffer[i] *= 2; + buffer[ids.x] *= 2; } - } - "; + }"; [TestMethod] - public void CompileTest() + public void CompileTest_Ok() { using ComPtr dxcBlob = ShaderCompiler.CompileShader(ShaderSource); } + + [TestMethod] + [ExpectedException(typeof(HlslCompilationException))] + public void CompileTest_Fail() + { + var faultyShader = ShaderSource.Replace("ids.x", "ids.X"); + + using ComPtr dxcBlob = ShaderCompiler.CompileShader(faultyShader); + } } } diff --git a/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs b/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs index abff9951..82a2a0a1 100644 --- a/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs +++ b/tests/ComputeSharp.Tests.Internals/SourceGeneratorTests.cs @@ -18,33 +18,11 @@ namespace ComputeSharp.Tests.Internals { string source = @" using System.Numerics; - using MyLibrary; using ComputeSharp; namespace ComputeSharp { - public readonly struct ThreadIds - { - public int X => throw null; - public int Y => throw null; - public int Z => throw null; - } - - public interface IComputeShader - { - void Execute((int X, int Y, int Z) ids); - } - } - - namespace MyLibrary - { - public class Buffer - { - } - - public interface IFluff - { - } + public class ReadWriteBuffer { } } namespace MyFancyApp.Sample @@ -55,50 +33,39 @@ namespace ComputeSharp.Tests.Internals public partial class Foo { [AutoConstructor] - public readonly partial struct MyShader : IComputeShader + public readonly partial struct Test { - private readonly Buffer A; - private readonly Vector4 B; - - /// - public void Execute(ThreadIds ids) - { - Vector4 foo = default; - Vector4 bar = default(Vector4); - Vector4 baz = B; - A[ids.X] = (Vector4)foo + bar + baz; - } + private readonly float a; + private readonly Vector2 b; + public readonly ReadWriteBuffer c; + public readonly ReadWriteBuffer d; } } } }"; - string expectedForAutoConstructor = @" + string expected = @" namespace MyFancyApp.Sample { public partial interface IFoo { public partial class Foo { - public readonly partial struct MyShader + public readonly partial struct Test { - public MyShader(MyLibrary.Buffer A, System.Numerics.Vector4 B) + public Test(float a, System.Numerics.Vector2 b, ComputeSharp.ReadWriteBuffer c, ComputeSharp.ReadWriteBuffer d) { - this.A = A; - this.B = B; + this.a = a; + this.b = b; + this.c = c; + this.d = d; } } } } }"; - VerifyGeneratedMethodLines(source, 0, expectedForAutoConstructor); - - string expectedForShaderSource = @" - #pragma warning disable - [assembly: ComputeSharp.IComputeShaderSource(""MyFancyApp.Sample.IFoo`1+Foo+MyShader"", ""Execute"", ""void CSMain(uint3 ids : SV_DispatchThreadId)\r\n{\r\n if (ids.x < __x && ids.y < __y && ids.z < __z)\r\n {\r\n float4 foo = (float4)0;\r\n float4 bar = (float4)0;\r\n float4 baz = B;\r\n A[ids.x] = (float4)foo + bar + baz;\r\n }\r\n}"")]"; - - VerifyGeneratedMethodLines(source, 0, expectedForShaderSource); + VerifyGeneratedMethodLines(source, 0, expected); } ///