From 2fcfaafc63d805fd20bf2aa8eb1004f06c81032d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 26 Dec 2023 16:20:47 +0100 Subject: [PATCH] Add incrementality unit tests for all generators --- ...Test_D2DPixelShaderDescriptorGenerator.cs} | 2 +- ...lShaderDescriptorGenerator_Diagnostics.cs} | 2 +- ...haderDescriptorGenerator_Incrementality.cs | 150 ++++++++++++++++++ ...haderDescriptorGenerator_Incrementality.cs | 98 ++++++++++++ 4 files changed, 250 insertions(+), 2 deletions(-) rename tests/ComputeSharp.D2D1.Tests.SourceGenerators/{Test_D2DPixelShaderSourceGenerator.cs => Test_D2DPixelShaderDescriptorGenerator.cs} (99%) rename tests/ComputeSharp.D2D1.Tests.SourceGenerators/{Test_D2DPixelShaderSourceGenerator_Diagnostics.cs => Test_D2DPixelShaderDescriptorGenerator_Diagnostics.cs} (96%) create mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs create mode 100644 tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator_Incrementality.cs diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs similarity index 99% rename from tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs rename to tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index d72bf52e..e9ce7e4a 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -6,7 +6,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests.SourceGenerators; [TestClass] -public class Test_D2DPixelShaderSourceGenerator +public class Test_D2DPixelShaderDescriptorGenerator { [TestMethod] public async Task SimpleShader() diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Diagnostics.cs similarity index 96% rename from tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs rename to tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Diagnostics.cs index 3a1347e8..caf81758 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Diagnostics.cs @@ -5,7 +5,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.Tests.SourceGenerators; [TestClass] -public class Test_D2DPixelShaderSourceGenerator_Diagnostics +public class Test_D2DPixelShaderDescriptorGenerator_Diagnostics { [TestMethod] public void MissingD2DRequiresDoublePrecisionSupportAttribute() diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs new file mode 100644 index 00000000..f2d97272 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Incrementality.cs @@ -0,0 +1,150 @@ +using ComputeSharp.D2D1.SourceGenerators; +using ComputeSharp.Tests.SourceGenerators.Helpers; +using Microsoft.CodeAnalysis; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests.SourceGenerators; + +[TestClass] +public class Test_D2DPixelShaderDescriptorGenerator_Incrementality +{ + [TestMethod] + public void ModifiedStatement_ModifiesOutput() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + public float4 Execute() + { + // Some dummy comment + return 42; + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Modified); + } + + [TestMethod] + public void AddedComment_DoesNotModifyOutput() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + public float4 Execute() + { + return 0; + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(0)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + public float4 Execute() + { + // Some dummy comment + return 0; + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Unchanged, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Cached, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Cached); + } + + [TestMethod] + public void AddedRequiresDoublePrecisionSupport_DoesNotModifyOutput() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(1)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader(double factor) : ID2D1PixelShader + { + public float4 Execute() + { + return (float4)((double4)D2D.GetInput(0) * factor); + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + [D2DInputCount(1)] + [D2DRequiresDoublePrecisionSupport] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader(double factor) : ID2D1PixelShader + { + public float4 Execute() + { + return (float4)((double4)D2D.GetInput(0) * factor); + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: IncrementalStepRunReason.Modified, + outputReason: IncrementalStepRunReason.Unchanged, + diagnosticsSourceReason: IncrementalStepRunReason.Modified, + sourceReason: IncrementalStepRunReason.Cached); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator_Incrementality.cs b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator_Incrementality.cs new file mode 100644 index 00000000..e2726fc1 --- /dev/null +++ b/tests/ComputeSharp.Tests.SourceGenerators/Test_ComputeShaderDescriptorGenerator_Incrementality.cs @@ -0,0 +1,98 @@ +using ComputeSharp.SourceGenerators; +using ComputeSharp.Tests.SourceGenerators.Helpers; +using Microsoft.CodeAnalysis; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.SourceGenerators; + +[TestClass] +public class Test_ComputeShaderDescriptorGenerator_Incrementality +{ + [TestMethod] + public void AddedStatement_ModifiesOutput() + { + const string source = """ + using ComputeSharp; + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct MyShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + public void Execute() + { + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct MyShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + public void Execute() + { + buffer[ThreadIds.X] *= 2.0; + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Modified, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Modified, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Modified); + } + + [TestMethod] + public void AddedComment_DoesNotModifyOutput() + { + const string source = """ + using ComputeSharp; + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct MyShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + public void Execute() + { + } + } + """; + + const string updatedSource = """ + using ComputeSharp; + + [ThreadGroupSize(DefaultThreadGroupSizes.X)] + [GeneratedComputeShaderDescriptor] + internal readonly partial struct MyShader : IComputeShader + { + private readonly ReadWriteBuffer buffer; + + public void Execute() + { + // Some dummy comment + } + } + """; + + CSharpGeneratorTest.VerifyIncrementalSteps( + source, + updatedSource, + executeReason: IncrementalStepRunReason.Unchanged, + diagnosticsReason: null, + outputReason: IncrementalStepRunReason.Cached, + diagnosticsSourceReason: null, + sourceReason: IncrementalStepRunReason.Cached); + } +} \ No newline at end of file