Merge pull request #11 from Sergio0694/feature_unit-tests

Unit tests
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2019-08-13 10:49:36 +02:00
コミット eef3214611
10ファイル変更
+315

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

+2
ファイルの表示
@@ -103,6 +103,8 @@ namespace ComputeSharp.Shaders.Translation
{
if (FieldsInfo.Count > 0) throw new InvalidOperationException("Shader fields already loaded");
if (ShaderFields.Any(fieldInfo => fieldInfo.IsStatic)) throw new InvalidOperationException("Empty shader body");
List<DescriptorRange> descriptorRanges = new List<DescriptorRange>();
List<GraphicsResource> readWriteBuffers = new List<GraphicsResource>();
int readWriteBuffersCount = 0;
+13
ファイルの表示
@@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.NetCore.Tests", "..\tests\ComputeSharp.NetCore.Tests\ComputeSharp.NetCore.Tests.csproj", "{E91F4D24-922A-4921-A8D7-9C45A143C45D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -58,12 +62,21 @@ Global
{7A48A85D-8866-4DED-A404-653ED09BF396}.Release|Any CPU.Build.0 = Release|Any CPU
{7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.ActiveCfg = Release|Any CPU
{7A48A85D-8866-4DED-A404-653ED09BF396}.Release|x64.Build.0 = Release|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.ActiveCfg = Debug|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Debug|x64.Build.0 = Debug|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|Any CPU.Build.0 = Release|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.ActiveCfg = Release|Any CPU
{E91F4D24-922A-4921-A8D7-9C45A143C45D}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7A48A85D-8866-4DED-A404-653ED09BF396} = {0ED8F632-5E17-46BE-8CC3-B14A82D4AEB1}
{E91F4D24-922A-4921-A8D7-9C45A143C45D} = {F8EFBB27-4EE2-4463-A75B-7EFDFB55D0F7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4664C5E3-0340-4E22-BCFD-98AAEDF5F2DC}
+1
ファイルの表示
@@ -14,6 +14,7 @@
<PackageProjectUrl>https://github.com/Sergio0694/ComputeSharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/Sergio0694/ComputeSharp</RepositoryUrl>
<PackageTags>dotnet net netcore netstandard csharp library graphics shader hlsl compute gpu</PackageTags>
<PackageIconUrl>https://i.imgur.com/oo8dA0q.png</PackageIconUrl>
</PropertyGroup>
<ItemGroup>
+36
ファイルの表示
@@ -0,0 +1,36 @@
using System;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
{
[TestClass]
[TestCategory("Buffers")]
public class BufferTests
{
[TestMethod]
public void ReadWriteBufferGetSetData()
{
float[] array = new float[4096];
array.AsSpan().FillRandom();
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer(array);
float[] result = buffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(result));
}
[TestMethod]
public void ReadOnlyBufferGetSetData()
{
float[] array = new float[4096];
array.AsSpan().FillRandom();
using ReadOnlyBuffer<float> buffer = Gpu.Default.AllocateConstantBuffer(array);
float[] result = buffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(result));
}
}
}
+19
ファイルの表示
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\ComputeSharp\ComputeSharp.csproj" />
</ItemGroup>
</Project>
+54
ファイルの表示
@@ -0,0 +1,54 @@
using System;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace ComputeSharp.NetCore.Tests.Extensions
{
/// <summary>
/// An helper <see langword="class"/> with methods to process fixed-size buffers
/// </summary>
public static class SpanExtensions
{
/// <summary>
/// Fills an input <see cref="Span{T}"/> instance with random <see langword="float"/> values
/// </summary>
/// <param name="span">The target <see cref="Span{T}"/> to fill</param>
public static void FillRandom(this Span<float> span)
{
if (span.Length == 0) return;
var l = span.Length;
ref var rspan = ref span.GetPinnableReference();
for (var i = 0; i < l; i++)
Unsafe.Add(ref rspan, i) = (float)ConcurrentRandom.Instance.NextDouble();
}
/// <summary>
/// The maximum threshold to validate two <see langword="float"/> values as being equal
/// </summary>
private const float FloatThreshold = 0.00001f;
/// <summary>
/// Checks whether or not the two input <see cref="Span{T}"/> instances have the same content
/// </summary>
/// <param name="a">The first <see cref="Span{T}"/> to check</param>
/// <param name="b">The second <see cref="Span{T}"/> to check</param>
[Pure]
public static bool ContentEquals(this Span<float> a, Span<float> b)
{
if (a.Length != b.Length) return false;
if (a.Length == 0) return true;
var l = a.Length;
ref var ra = ref a.GetPinnableReference();
ref var rb = ref b.GetPinnableReference();
for (var i = 0; i < l; i++)
if (Math.Abs(Unsafe.Add(ref ra, i) - Unsafe.Add(ref rb, i)) > FloatThreshold)
return false;
return true;
}
}
}
+28
ファイルの表示
@@ -0,0 +1,28 @@
using System.Runtime.CompilerServices;
using System.Threading;
namespace System
{
/// <summary>
/// A thread-safe <see cref="Random"/> wrapper class
/// </summary>
public static class ConcurrentRandom
{
// Shared seed
private static int _Seed = Environment.TickCount;
/// <summary>
/// The <see cref="ThreadLocal{T}"/> object that provides the <see cref="Random"/> instances
/// </summary>
private static readonly ThreadLocal<Random> _Instance = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref _Seed)));
/// <summary>
/// Gets a thread-safe <see cref="Random"/> instance
/// </summary>
public static Random Instance
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _Instance.Value;
}
}
}
+19
ファイルの表示
@@ -0,0 +1,19 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
{
[TestClass]
[TestCategory("InvalidShader")]
public class InvalidShaderTests
{
[TestMethod]
public void EmptyShaderCompileError()
{
Action<ThreadIds> action = id => { };
Assert.ThrowsException<InvalidOperationException>(() => Gpu.Default.For(10, action));
}
}
}
+85
ファイルの表示
@@ -0,0 +1,85 @@
using System;
using System.Linq;
using System.Numerics;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
{
[TestClass]
[TestCategory("ShaderClosure")]
public class ShaderClosureTests
{
[TestMethod]
public void LocalScalarAssignToBuffer()
{
float value = 10;
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1);
Action<ThreadIds> action = id => buffer[0] = value;
Gpu.Default.For(1, action);
float[] result = buffer.GetData();
Assert.IsTrue((int)result[0] == (int)value);
}
[TestMethod]
public void LocalKnownVectorAssignToBuffer()
{
Vector4 vector4 = new Vector4(1, 2, 3, 4);
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(4);
Action<ThreadIds> action = id =>
{
if (id.X == 0) buffer[0] = vector4.X;
else if (id.X == 1) buffer[1] = vector4.Y;
else if (id.X == 2) buffer[2] = vector4.Z;
else if (id.X == 3) buffer[3] = vector4.W;
};
Gpu.Default.For(1, action);
float[] result = buffer.GetData();
float[] expected = { 1, 2, 3, 4 };
Assert.IsTrue(result.AsSpan().ContentEquals(expected));
}
[TestMethod]
public void CopyBetweenWriteableBuffers()
{
float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> input = Gpu.Default.AllocateReadWriteBuffer(source);
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(source.Length);
Action<ThreadIds> action = id => buffer[id.X] = input[id.X];
Gpu.Default.For(source.Length, action);
float[] result = buffer.GetData();
Assert.IsTrue(result.AsSpan().ContentEquals(source));
}
[TestMethod]
public void CopyBetweenReadOnlyAndWriteableBuffers()
{
float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
using ReadOnlyBuffer<float> input = Gpu.Default.AllocateConstantBuffer(source);
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(source.Length);
Action<ThreadIds> action = id => buffer[id.X] = input[id.X];
Gpu.Default.For(source.Length, action);
float[] result = buffer.GetData();
Assert.IsTrue(result.AsSpan().ContentEquals(source));
}
}
}
+58
ファイルの表示
@@ -0,0 +1,58 @@
using System;
using System.Linq;
using ComputeSharp.Graphics.Buffers;
using ComputeSharp.NetCore.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.NetCore.Tests
{
[TestClass]
[TestCategory("ShaderDispatch")]
public class ShaderDispatch
{
[TestMethod]
public void WriteToReadWriteBufferDispatch1D()
{
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(100);
Action<ThreadIds> action = id => buffer[id.X] = id.X;
Gpu.Default.For(100, action);
float[] array = buffer.GetData();
float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
Assert.IsTrue(array.AsSpan().ContentEquals(expected));
}
[TestMethod]
public void WriteToReadWriteBufferDispatch2D()
{
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(100);
Action<ThreadIds> action = id => buffer[id.X + id.Y * 10] = id.X + id.Y * 10;
Gpu.Default.For(10, 10, action);
float[] array = buffer.GetData();
float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
Assert.IsTrue(array.AsSpan().ContentEquals(expected));
}
[TestMethod]
public void WriteToReadWriteBufferDispatch3D()
{
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(1000);
Action<ThreadIds> action = id => buffer[id.X + id.Y * 10 + id.Z * 100] = id.X + id.Y * 10 + id.Z * 100;
Gpu.Default.For(10, 10, 10, action);
float[] array = buffer.GetData();
float[] expected = Enumerable.Range(0, 1000).Select(i => (float)i).ToArray();
Assert.IsTrue(array.AsSpan().ContentEquals(expected));
}
}
}