Added new test attributes and extensions

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-16 00:14:00 +01:00
1 親 d7095043d0
コミット 088b46e69c
6ファイル変更
+271

検索条件に一致するファイルはありません

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