From ed953c6bf17fc8ca8c336d6211235088b2534843 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 16 Dec 2023 17:52:59 +0100 Subject: [PATCH] Add diagnostic tests for double precision attribute --- ...DPixelShaderSourceGenerator_Diagnostics.cs | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs new file mode 100644 index 00000000..4a6169d8 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderSourceGenerator_Diagnostics.cs @@ -0,0 +1,121 @@ +extern alias Core; +extern alias D2D1; + +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using Basic.Reference.Assemblies; +using ComputeSharp.D2D1.SourceGenerators; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests.SourceGenerators; + +[TestClass] +public class Test_D2DPixelShaderSourceGenerator_DIagnostics +{ + [TestMethod] + public void MissingD2DRequiresDoublePrecisionSupportAttribute() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + namespace MyNamespace; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + private readonly float time; + + public float4 Execute() + { + return (float)(time * 2.0); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPSD2D0080"); + } + + [TestMethod] + public void UnnecessaryD2DRequiresDoublePrecisionSupportAttribute() + { + const string source = """ + using ComputeSharp; + using ComputeSharp.D2D1; + using float4 = global::ComputeSharp.Float4; + + namespace MyNamespace; + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DRequiresDoublePrecisionSupport] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct MyShader : ID2D1PixelShader + { + private readonly float time; + + public float4 Execute() + { + return (float)(time * 2.0f); + } + } + """; + + VerifyGeneratedDiagnostics(source, "CMPSD2D0081"); + } + + /// + /// Verifies the output of a source generator. + /// + /// The input source to process. + /// The expected diagnostics ids to be generated. + /// The task for the operation. + private static void VerifyGeneratedDiagnostics(string source, params string[] diagnosticsIds) + { + // Get all assembly references for the .NET TFM and ComputeSharp + IEnumerable metadataReferences = + [ + .. Net80.References.All, + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Hlsl).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location) + ]; + + // Parse the source text (C# 12) + SyntaxTree sourceTree = CSharpSyntaxTree.ParseText( + source, + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12)); + + // Create the original compilation + CSharpCompilation compilation = CSharpCompilation.Create( + "original", + [sourceTree], + metadataReferences, + new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true)); + + // Create the generator driver with the D2D shader generator + GeneratorDriver driver = CSharpGeneratorDriver.Create(new D2DPixelShaderDescriptorGenerator()).WithUpdatedParseOptions((CSharpParseOptions)sourceTree.Options); + + // Run all source generators on the input source code + _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); + + Dictionary diagnosticMap = diagnostics.DistinctBy(diagnostic => diagnostic.Id).ToDictionary(diagnostic => diagnostic.Id); + + // Check that the diagnostics match + Assert.IsTrue(diagnosticMap.Keys.ToHashSet().SetEquals(diagnosticsIds), $"Diagnostics didn't match. {string.Join(", ", diagnosticMap.Values)}"); + + // If the compilation was supposed to succeed, ensure that no further errors were generated + if (diagnosticsIds.Length == 0) + { + // Compute diagnostics for the final compiled output (just include errors) + List outputCompilationDiagnostics = outputCompilation.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error).ToList(); + + Assert.IsTrue(outputCompilationDiagnostics.Count == 0, $"resultingIds: {string.Join(", ", outputCompilationDiagnostics)}"); + } + } +} \ No newline at end of file