Added Texture2D<T> unit tests

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-05 15:49:14 +01:00
1 親 6904416d0b
コミット eef8e41899
4ファイル変更
+202 -4

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

+17 -4
ファイルの表示
@@ -7,14 +7,14 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("Buffers")]
[TestCategory("Buffer")]
public class BufferTests
{
[TestMethod]
[DataRow(typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_Uninitialized(Type bufferType)
public void Allocate_Uninitialized_Ok(Type bufferType)
{
using Buffer<float> buffer = Gpu.Default.AllocateBuffer<float>(bufferType, 128);
@@ -23,11 +23,24 @@ namespace ComputeSharp.Tests
Assert.AreSame(buffer.GraphicsDevice, Gpu.Default);
}
[TestMethod]
[DataRow(typeof(ConstantBuffer<>), -247824)]
[DataRow(typeof(ConstantBuffer<>), -1)]
[DataRow(typeof(ConstantBuffer<>), -247824)]
[DataRow(typeof(ConstantBuffer<>), -1)]
[DataRow(typeof(ConstantBuffer<>), -247824)]
[DataRow(typeof(ConstantBuffer<>), -1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Allocate_Uninitialized_Fail(Type bufferType, int length)
{
using Buffer<float> buffer = Gpu.Default.AllocateBuffer<float>(bufferType, length);
}
[TestMethod]
[DataRow(typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_FromArray(Type bufferType)
public void Allocate_FromArray(Type bufferType)
{
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
@@ -54,7 +67,7 @@ namespace ComputeSharp.Tests
[DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_FromBuffer(Type sourceType, Type destinationType)
public void Allocate_FromBuffer(Type sourceType, Type destinationType)
{
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
+1
ファイルの表示
@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.0.0-preview4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
</ItemGroup>
+43
ファイルの表示
@@ -31,6 +31,27 @@ namespace ComputeSharp.Tests.Extensions
};
}
/// <summary>
/// Allocates a new <see cref="Texture2D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of texture to allocate.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance of the requested size.</returns>
[Pure]
public static Texture2D<T> AllocateTexture2D<T>(this GraphicsDevice device, Type type, int width, int height)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D<T>(width, height),
_ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D<T>(width, height),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
/// </summary>
@@ -52,6 +73,28 @@ namespace ComputeSharp.Tests.Extensions
};
}
/// <summary>
/// Allocates a new <see cref="Texture2D{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the texture.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the texture for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="data">The data to load on the texture.</param>
/// <param name="width">The width of the texture to create.</param>
/// <param name="height">The height of the texture to create.</param>
/// <returns>A <see cref="Texture2D{T}"/> instance of the requested type.</returns>
[Pure]
public static Texture2D<T> AllocateTexture2D<T>(this GraphicsDevice device, Type type, T[] data, int width, int height)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ReadOnlyTexture2D<>) => device.AllocateReadOnlyTexture2D(data, width, height),
_ when type == typeof(ReadWriteTexture2D<>) => device.AllocateReadWriteTexture2D(data, width, height),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
/// </summary>
+141
ファイルの表示
@@ -0,0 +1,141 @@
using System;
using System.Linq;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Tests.Extensions;
using Microsoft.Toolkit.HighPerformance.Memory;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.Tests
{
[TestClass]
[TestCategory("Texture2D")]
public class Texture2DTests
{
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>))]
[DataRow(typeof(ReadWriteTexture2D<>))]
public void Allocate_Uninitialized_Ok(Type textureType)
{
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D<float>(textureType, 128, 128);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 128);
Assert.AreEqual(texture.Height, 128);
Assert.AreSame(texture.GraphicsDevice, Gpu.Default);
}
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>), 128, -14253)]
[DataRow(typeof(ReadOnlyTexture2D<>), 128, -1)]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, -4314)]
[DataRow(typeof(ReadOnlyTexture2D<>), -14, -53)]
[DataRow(typeof(ReadWriteTexture2D<>), 128, -14253)]
[DataRow(typeof(ReadWriteTexture2D<>), 128, -1)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, -4314)]
[DataRow(typeof(ReadWriteTexture2D<>), -14, -53)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Allocate_Uninitialized_Fail(Type textureType, int width, int height)
{
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D<float>(textureType, width, height);
}
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>))]
[DataRow(typeof(ReadWriteTexture2D<>))]
public void Allocate_FromArray(Type textureType)
{
float[] data = Enumerable.Range(0, 128 * 128).Select(static i => (float)i).ToArray();
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D(textureType, data, 128, 128);
Assert.IsNotNull(texture);
Assert.AreEqual(texture.Width, 128);
Assert.AreEqual(texture.Height, 128);
Assert.AreSame(texture.GraphicsDevice, Gpu.Default);
float[,] result = texture.GetData();
Assert.IsNotNull(result);
Assert.AreEqual(data.Length, result.Length);
Assert.IsTrue(data.SequenceEqual(result.Cast<float>()));
}
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>))]
[DataRow(typeof(ReadWriteTexture2D<>))]
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Type textureType)
{
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D<float>(textureType, 10, 10);
texture.Dispose();
_ = texture.GetData();
}
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, 0, 64, 64)]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, 14, 64, 50)]
[DataRow(typeof(ReadOnlyTexture2D<>), 14, 0, 50, 64)]
[DataRow(typeof(ReadOnlyTexture2D<>), 10, 10, 54, 54)]
[DataRow(typeof(ReadOnlyTexture2D<>), 20, 20, 32, 27)]
[DataRow(typeof(ReadOnlyTexture2D<>), 60, 0, 4, 64)]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, 60, 64, 4)]
[DataRow(typeof(ReadOnlyTexture2D<>), 63, 2, 1, 60)]
[DataRow(typeof(ReadOnlyTexture2D<>), 2, 63, 60, 1)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, 0, 64, 64)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, 14, 64, 50)]
[DataRow(typeof(ReadWriteTexture2D<>), 14, 0, 50, 64)]
[DataRow(typeof(ReadWriteTexture2D<>), 10, 10, 54, 54)]
[DataRow(typeof(ReadWriteTexture2D<>), 20, 20, 32, 27)]
[DataRow(typeof(ReadWriteTexture2D<>), 60, 0, 4, 64)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, 60, 64, 4)]
[DataRow(typeof(ReadWriteTexture2D<>), 63, 2, 1, 60)]
[DataRow(typeof(ReadWriteTexture2D<>), 2, 63, 60, 1)]
public void GetData_RangeToVoid_Ok(Type textureType, int x, int y, int width, int height)
{
float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64);
float[] result = new float[width * height];
texture.GetData(result, x, y, width, height);
Span2D<float>
expected = new Span2D<float>(array, 64, 64).Slice(y, x, height, width),
data = new(result, height, width);
CollectionAssert.AreEqual(expected.ToArray(), data.ToArray());
}
[TestMethod]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, -1, 50, 50)]
[DataRow(typeof(ReadOnlyTexture2D<>), -1, 0, 50, 50)]
[DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, -1, 50)]
[DataRow(typeof(ReadOnlyTexture2D<>), 12, 0, 20, -1)]
[DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 20, 50)]
[DataRow(typeof(ReadOnlyTexture2D<>), 12, 20, 60, 20)]
[DataRow(typeof(ReadOnlyTexture2D<>), 80, 20, 40, 20)]
[DataRow(typeof(ReadOnlyTexture2D<>), 0, 80, 40, 20)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, -1, 50, 50)]
[DataRow(typeof(ReadWriteTexture2D<>), -1, 0, 50, 50)]
[DataRow(typeof(ReadWriteTexture2D<>), 12, 0, -1, 50)]
[DataRow(typeof(ReadWriteTexture2D<>), 12, 0, 20, -1)]
[DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 20, 50)]
[DataRow(typeof(ReadWriteTexture2D<>), 12, 20, 60, 20)]
[DataRow(typeof(ReadWriteTexture2D<>), 80, 20, 40, 20)]
[DataRow(typeof(ReadWriteTexture2D<>), 0, 80, 40, 20)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToVoid_Fail(Type textureType, int x, int y, int width, int height)
{
float[] array = Enumerable.Range(0, 4096).Select(static i => (float)i).ToArray();
using Texture2D<float> texture = Gpu.Default.AllocateTexture2D(textureType, array, 64, 64);
float[] result = new float[4096];
texture.GetData(result, x, y, width, height);
}
}
}