Improved Buffer<T> unit tests, minor bug fixes

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-30 14:02:46 +01:00
1 親 48a717f596
コミット 2db612a7c6
3ファイル変更
+221 -99

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

+2
ファイルの表示
@@ -114,6 +114,8 @@ namespace ComputeSharp.Graphics.Buffers.Abstract
[Pure]
public T[] GetData(int offset, int count)
{
Guard.IsGreaterThanOrEqualTo(count, 0, nameof(count));
T[] data = GC.AllocateUninitializedArray<T>(count);
GetData(data.AsSpan(), offset);
+143 -99
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using ComputeSharp.Graphics.Buffers.Abstract;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -10,139 +11,182 @@ namespace ComputeSharp.Tests
public class BufferTests
{
[TestMethod]
public void ReadWriteBufferGetSetDataFromArray()
[DataRow(typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_Uninitialized(Type bufferType)
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using Buffer<float> buffer = Gpu.Default.AllocateBuffer<float>(bufferType, 128);
Assert.IsNotNull(buffer);
Assert.AreEqual(buffer.Length, 128);
Assert.AreSame(buffer.GraphicsDevice, Gpu.Default);
}
[TestMethod]
[DataRow(typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_FromArray(Type bufferType)
{
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
using Buffer<float> buffer = Gpu.Default.AllocateBuffer(bufferType, data);
Assert.IsNotNull(buffer);
Assert.AreEqual(buffer.Length, 128);
Assert.AreSame(buffer.GraphicsDevice, Gpu.Default);
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer(array);
float[] result = buffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(result));
Assert.IsNotNull(result);
Assert.AreEqual(data.Length, result.Length);
Assert.IsTrue(data.SequenceEqual(result));
}
[TestMethod]
public void ReadOnlyBufferGetSetDataFromArray()
[DataRow(typeof(ConstantBuffer<>), typeof(ConstantBuffer<>))]
[DataRow(typeof(ConstantBuffer<>), typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ConstantBuffer<>), typeof(ReadWriteBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>), typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>), typeof(ReadWriteBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>), typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>), typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>), typeof(ReadWriteBuffer<>))]
public void AllocateBuffer_FromBuffer(Type sourceType, Type destinationType)
{
float[] data = Enumerable.Range(0, 128).Select(static i => (float)i).ToArray();
using Buffer<float> source = Gpu.Default.AllocateBuffer(sourceType, data);
using Buffer<float> destination = Gpu.Default.AllocateBuffer(destinationType, source);
Assert.IsNotNull(destination);
Assert.AreEqual(destination.Length, 128);
Assert.AreSame(destination.GraphicsDevice, Gpu.Default);
float[] result = destination.GetData();
Assert.IsNotNull(result);
Assert.AreEqual(data.Length, result.Length);
Assert.IsTrue(data.SequenceEqual(result));
}
[TestMethod]
[DataRow(typeof(ConstantBuffer<>))]
[DataRow(typeof(ReadOnlyBuffer<>))]
[DataRow(typeof(ReadWriteBuffer<>))]
[ExpectedException(typeof(ObjectDisposedException))]
public void UsageAfterDispose(Type bufferType)
{
using Buffer<float> buffer = Gpu.Default.AllocateBuffer<float>(bufferType, 128);
buffer.Dispose();
_ = buffer.GetData();
}
[TestMethod]
[DataRow(typeof(ConstantBuffer<>), 0, 4096)]
[DataRow(typeof(ConstantBuffer<>), 128, 512)]
[DataRow(typeof(ConstantBuffer<>), 2048, 2048)]
[DataRow(typeof(ReadOnlyBuffer<>), 0, 4096)]
[DataRow(typeof(ReadOnlyBuffer<>), 128, 512)]
[DataRow(typeof(ReadOnlyBuffer<>), 2048, 2048)]
[DataRow(typeof(ReadWriteBuffer<>), 0, 4096)]
[DataRow(typeof(ReadWriteBuffer<>), 128, 512)]
[DataRow(typeof(ReadWriteBuffer<>), 2048, 2048)]
public void GetData_RangeToArray_Ok(Type bufferType, int offset, int count)
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ConstantBuffer<float> buffer = Gpu.Default.AllocateConstantBuffer(array);
float[] result = buffer.GetData();
using Buffer<float> buffer = Gpu.Default.AllocateBuffer(bufferType, array);
Assert.IsTrue(array.AsSpan().ContentEquals(result));
float[] result = buffer.GetData(offset, count);
Assert.IsNotNull(result);
Assert.AreEqual(result.Length, count);
Assert.IsTrue(array.AsSpan(offset, count).SequenceEqual(result));
}
[TestMethod]
public void ReadWriteBufferGetSetDataFromReadWriteBuffer()
{
float[] array = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
using ReadWriteBuffer<float> destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer);
float[] sourceResult = sourceBuffer.GetData();
float[] destinationResult = destinationBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
}
[TestMethod]
public void ReadWriteBufferGetSetDataFromReadOnlyBuffer()
[DataRow(typeof(ConstantBuffer<>), -1, 128)]
[DataRow(typeof(ConstantBuffer<>), 8192, 128)]
[DataRow(typeof(ConstantBuffer<>), 512, 4096)]
[DataRow(typeof(ConstantBuffer<>), 512, -128)]
[DataRow(typeof(ReadOnlyBuffer<>), -1, 128)]
[DataRow(typeof(ReadOnlyBuffer<>), 8192, 128)]
[DataRow(typeof(ReadOnlyBuffer<>), 512, 4096)]
[DataRow(typeof(ReadOnlyBuffer<>), 512, -128)]
[DataRow(typeof(ReadWriteBuffer<>), -1, 128)]
[DataRow(typeof(ReadWriteBuffer<>), 8192, 128)]
[DataRow(typeof(ReadWriteBuffer<>), 512, 4096)]
[DataRow(typeof(ReadWriteBuffer<>), 512, -128)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToArray_Fail(Type bufferType, int offset, int count)
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
using ReadWriteBuffer<float> destinationBuffer = Gpu.Default.AllocateReadWriteBuffer(sourceBuffer);
using Buffer<float> buffer = Gpu.Default.AllocateBuffer(bufferType, array);
float[] sourceResult = sourceBuffer.GetData();
float[] destinationResult = destinationBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
_ = buffer.GetData(offset, count);
}
[TestMethod]
public void ReadOnlyBufferGetSetDataFromReadWriteBuffer()
[DataRow(typeof(ConstantBuffer<>), 0, 0, 4096)]
[DataRow(typeof(ConstantBuffer<>), 512, 0, 2048)]
[DataRow(typeof(ConstantBuffer<>), 0, 512, 2048)]
[DataRow(typeof(ConstantBuffer<>), 127, 1024, 587)]
[DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 4096)]
[DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 2048)]
[DataRow(typeof(ReadOnlyBuffer<>), 0, 512, 2048)]
[DataRow(typeof(ReadOnlyBuffer<>), 127, 1024, 587)]
[DataRow(typeof(ReadWriteBuffer<>), 0, 0, 4096)]
[DataRow(typeof(ReadWriteBuffer<>), 512, 0, 2048)]
[DataRow(typeof(ReadWriteBuffer<>), 0, 512, 2048)]
[DataRow(typeof(ReadWriteBuffer<>), 127, 1024, 587)]
public void GetData_RangeToVoid_Ok(Type bufferType, int destinationOffset, int bufferOffset, int count)
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
using ConstantBuffer<float> destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer);
using Buffer<float> buffer = Gpu.Default.AllocateBuffer(bufferType, array);
float[] sourceResult = sourceBuffer.GetData();
float[] destinationResult = destinationBuffer.GetData();
float[] result = new float[4096];
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
buffer.GetData(result, destinationOffset, bufferOffset, count);
Assert.IsTrue(array.AsSpan(bufferOffset, count).SequenceEqual(result.AsSpan(destinationOffset, count)));
}
[TestMethod]
public void ReadOnlyBufferGetSetDataFromReadOnlyBuffer()
[DataRow(typeof(ConstantBuffer<>), 0, 0, 8196)]
[DataRow(typeof(ConstantBuffer<>), -12, 0, 1024)]
[DataRow(typeof(ConstantBuffer<>), 0, -56, 1024)]
[DataRow(typeof(ConstantBuffer<>), 512, 0, 4096)]
[DataRow(typeof(ConstantBuffer<>), 12, 1024, 3600)]
[DataRow(typeof(ConstantBuffer<>), 12, 1024, -2096)]
[DataRow(typeof(ReadOnlyBuffer<>), 0, 0, 8196)]
[DataRow(typeof(ReadOnlyBuffer<>), -12, 0, 1024)]
[DataRow(typeof(ReadOnlyBuffer<>), 0, -56, 1024)]
[DataRow(typeof(ReadOnlyBuffer<>), 512, 0, 4096)]
[DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, 3600)]
[DataRow(typeof(ReadOnlyBuffer<>), 12, 1024, -2096)]
[DataRow(typeof(ReadWriteBuffer<>), 0, 0, 8196)]
[DataRow(typeof(ReadWriteBuffer<>), -12, 0, 1024)]
[DataRow(typeof(ReadWriteBuffer<>), 0, -56, 1024)]
[DataRow(typeof(ReadWriteBuffer<>), 512, 0, 4096)]
[DataRow(typeof(ReadWriteBuffer<>), 12, 1024, 3600)]
[DataRow(typeof(ReadWriteBuffer<>), 12, 1024, -2096)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetData_RangeToVoid_Fail(Type bufferType, int destinationOffset, int bufferOffset, int count)
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
using ConstantBuffer<float> destinationBuffer = Gpu.Default.AllocateConstantBuffer(sourceBuffer);
using Buffer<float> buffer = Gpu.Default.AllocateBuffer(bufferType, array);
float[] sourceResult = sourceBuffer.GetData();
float[] destinationResult = destinationBuffer.GetData();
float[] result = new float[4096];
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
Assert.IsTrue(array.AsSpan().ContentEquals(destinationResult));
}
[TestMethod]
public void ConstantBufferGetRangeData()
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
float[] sourceResult = sourceBuffer.GetData(128, 100);
Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult));
}
[TestMethod]
public void ConstantBufferSetRangeData()
{
float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
using ConstantBuffer<float> sourceBuffer = Gpu.Default.AllocateConstantBuffer(array);
array.AsSpan(56, 100).Clear();
sourceBuffer.SetData(new float[100], 56);
float[] sourceResult = sourceBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
}
[TestMethod]
public void ReadWriteBufferGetRangeData()
{
float[] array = Enumerable.Range(0, 4096).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
float[] sourceResult = sourceBuffer.GetData(128, 100);
Assert.IsTrue(array.AsSpan(128, 100).ContentEquals(sourceResult));
}
[TestMethod]
public void ReadWriteBufferSetRangeData()
{
float[] array = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
using ReadWriteBuffer<float> sourceBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
array.AsSpan(56, 100).Clear();
sourceBuffer.SetData(new float[100], 56);
float[] sourceResult = sourceBuffer.GetData();
Assert.IsTrue(array.AsSpan().ContentEquals(sourceResult));
buffer.GetData(result, destinationOffset, bufferOffset, count);
}
}
}
+76
ファイルの表示
@@ -0,0 +1,76 @@
using System;
using System.Diagnostics.Contracts;
using ComputeSharp.Graphics;
using ComputeSharp.Graphics.Buffers.Abstract;
namespace ComputeSharp.Tests.Extensions
{
/// <summary>
/// A helper class for testing <see cref="Buffer{T}"/> APIs.
/// </summary>
public static class GraphicsDeviceExtensions
{
/// <summary>
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="length">The length of the buffer to create.</param>
/// <returns>A <see cref="Buffer{T}"/> instance of the requested size.</returns>
[Pure]
public static Buffer<T> AllocateBuffer<T>(this GraphicsDevice device, Type type, int length)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer<T>(length),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer<T>(length),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer<T>(length),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="data">The data to load on the buffer.</param>
/// <returns>A <see cref="Buffer{T}"/> instance of the requested type.</returns>
[Pure]
public static Buffer<T> AllocateBuffer<T>(this GraphicsDevice device, Type type, T[] data)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
/// <summary>
/// Allocates a new <see cref="Buffer{T}"/> instance of the specified type.
/// </summary>
/// <typeparam name="T">The type of items in the buffer.</typeparam>
/// <param name="device">The target <see cref="GraphicsDevice"/> instance to allocate the buffer for.</param>
/// <param name="type">The type of buffer to allocate.</param>
/// <param name="data">The data to load on the buffer.</param>
/// <returns>A <see cref="Buffer{T}"/> instance of the requested type.</returns>
[Pure]
public static Buffer<T> AllocateBuffer<T>(this GraphicsDevice device, Type type, Buffer<T> data)
where T : unmanaged
{
return type switch
{
_ when type == typeof(ConstantBuffer<>) => device.AllocateConstantBuffer(data),
_ when type == typeof(ReadOnlyBuffer<>) => device.AllocateReadOnlyBuffer(data),
_ when type == typeof(ReadWriteBuffer<>) => device.AllocateReadWriteBuffer(data),
_ => throw new ArgumentException($"Invalid type: {type}", nameof(type))
};
}
}
}