diff --git a/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs new file mode 100644 index 00000000..54550319 --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/AllDevicesAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with targeting all supported devices. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class AllDevicesAttribute : Attribute + { + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs new file mode 100644 index 00000000..ff68a4aa --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/CombinatorialTestMethodAttribute.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ComputeSharp.Tests.Extensions; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// A custom implementation that also acts as a combinatorial + /// instance. It has dedicated paths for target devices and resource types, and arbitrary inputs for the test methods. + /// + /// + /// Use this attribute in combination with , , + /// or , not other existing MSTest attributes. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class CombinatorialTestMethodAttribute : TestMethodAttribute, ITestDataSource + { + /// + IEnumerable ITestDataSource.GetData(MethodInfo methodInfo) + { + Device[] devices; + + if (methodInfo.GetCustomAttribute() is not null) + { + devices = Enum.GetValues(); + } + else if (methodInfo.GetCustomAttributes().ToArray() is { Length: > 0 } values) + { + devices = values.Select(static value => value.Device).ToArray(); + } + else + { + devices = Array.Empty(); + } + + Type[] resources = methodInfo.GetCustomAttributes().Select(static value => value.Type).ToArray(); + object[][] data = methodInfo.GetCustomAttributes().Select(static value => value.Data).ToArray(); + + if (devices.Length > 0) + { + foreach (Device device in devices) + { + if (resources.Length > 0) + { + foreach (Type type in resources) + { + if (data.Length == 0) + { + yield return new object[] { device, type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device, type }.Concat(items).ToArray(); + } + } + } + } + else + { + if (data.Length == 0) + { + yield return new object[] { device }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { device }.Concat(items).ToArray(); + } + } + } + } + } + else if (resources.Length > 0) + { + foreach (Type type in resources) + { + if (data.Length == 0) + { + yield return new object[] { type }; + } + else + { + foreach (object[] items in data) + { + yield return new object[] { type }.Concat(items).ToArray(); + } + } + } + } + else + { + if (data.Length == 0) + { + yield return Array.Empty(); + } + else + { + foreach (object[] items in data) + { + yield return items; + } + } + } + } + + /// + string ITestDataSource.GetDisplayName(MethodInfo methodInfo, object[] data) + { + return $"{methodInfo.Name} ({string.Join(", ", data)})"; + } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs new file mode 100644 index 00000000..fa99504e --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/DataAttribute.cs @@ -0,0 +1,21 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with adding arbitrary input data. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class DataAttribute : Attribute + { + public DataAttribute(params object[] data) + { + Data = data; + } + + /// + /// Gets the input data for the current attribute. + /// + public object[] Data { get; } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs new file mode 100644 index 00000000..ce13a6a1 --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/DeviceAttribute.cs @@ -0,0 +1,22 @@ +using System; +using ComputeSharp.Tests.Extensions; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with adding a specific input. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public sealed class DeviceAttribute : Attribute + { + public DeviceAttribute(Device device) + { + Device = device; + } + + /// + /// Gets the target to use. + /// + public Device Device { get; } + } +} diff --git a/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs new file mode 100644 index 00000000..afdff7fd --- /dev/null +++ b/tests/ComputeSharp.Tests/Attributes/ResourceAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace ComputeSharp.Tests.Attributes +{ + /// + /// An attribute to use with targeting a specific resource type. + /// + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public sealed class ResourceAttribute : Attribute + { + public ResourceAttribute(Type type) + { + Type = type; + } + + public Type Type { get; } + } +} diff --git a/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs new file mode 100644 index 00000000..0b4d44c5 --- /dev/null +++ b/tests/ComputeSharp.Tests/Extensions/DeviceExtensions.cs @@ -0,0 +1,80 @@ +using System.Linq; +using Microsoft.Toolkit.Diagnostics; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Extensions +{ + /// + /// A helper class for testing APIs. + /// + public static class DeviceExtensions + { + /// + /// The instance for the default discrete device. + /// + private static readonly GraphicsDevice? DiscreteDevice; + + /// + /// The instance for the WARP device. + /// + private static readonly GraphicsDevice WarpDevice; + + /// + /// Initializes and . + /// + static DeviceExtensions() + { + GraphicsDevice defaultDevice = Gpu.Default; + + if (defaultDevice.IsHardwareAccelerated) + { + DiscreteDevice = defaultDevice; + WarpDevice = Gpu.QueryDevices(info => !info.IsHardwareAccelerated).First(); + } + else + { + DiscreteDevice = null; + WarpDevice = defaultDevice; + } + } + + /// + /// Gets a instance matching a specified type. + /// + /// The device to retrieve. + /// The device of the requested type. + /// Thrown when the requested device is not present. + public static GraphicsDevice Get(this Device type) + { + GraphicsDevice? device = type switch + { + Device.Discrete => DiscreteDevice, + Device.Warp => WarpDevice, + _ => ThrowHelper.ThrowArgumentException("Invalid device") + }; + + if (device is null) + { + Assert.Inconclusive(); + } + + return device!; + } + } + + /// + /// Indicates a specific compute device. + /// + public enum Device + { + /// + /// A discrete device. + /// + Discrete, + + /// + /// A WARP device (software fallback). + /// + Warp + } +}