Browse Source

Buffer2DTests using a mock MemoryManager

af/merge-core
Anton Firszov 8 years ago
parent
commit
1e78de07f9
  1. 13
      src/ImageSharp/Memory/BasicArrayBuffer.cs
  2. 50
      tests/ImageSharp.Tests/Memory/Buffer2DTests.cs

13
src/ImageSharp/Memory/BasicArrayBuffer.cs

@ -9,16 +9,23 @@ namespace SixLabors.ImageSharp.Memory
internal class BasicArrayBuffer<T> : IBuffer<T> internal class BasicArrayBuffer<T> : IBuffer<T>
where T : struct where T : struct
{ {
public BasicArrayBuffer(T[] array) public BasicArrayBuffer(T[] array, int length)
{ {
DebugGuard.MustBeLessThanOrEqualTo(length, array.Length, nameof(length));
this.Array = array; this.Array = array;
this.Length = length;
}
public BasicArrayBuffer(T[] array)
: this(array, array.Length)
{
} }
public T[] Array { get; } public T[] Array { get; }
public Span<T> Span => this.Array; public int Length { get; }
public int Length => this.Array.Length; public Span<T> Span => this.Array.AsSpan().Slice(0, this.Length);
/// <summary> /// <summary>
/// Returns a reference to specified element of the buffer. /// Returns a reference to specified element of the buffer.

50
tests/ImageSharp.Tests/Memory/Buffer2DTests.cs

@ -26,12 +26,38 @@ namespace SixLabors.ImageSharp.Tests.Memory
} }
} }
private MemoryManager MemoryManager { get; } = new MockMemoryManager();
private class MockMemoryManager : MemoryManager
{
internal override IBuffer<T> Allocate<T>(int length, bool clear)
{
T[] array = new T[length + 42];
if (!clear)
{
Span<byte> data = array.AsSpan().NonPortableCast<T, byte>();
for (int i = 0; i < data.Length; i++)
{
data[i] = 42;
}
}
return new BasicArrayBuffer<T>(array, length);
}
internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, bool clear)
{
throw new NotImplementedException();
}
}
[Theory] [Theory]
[InlineData(7, 42)] [InlineData(7, 42)]
[InlineData(1025, 17)] [InlineData(1025, 17)]
public void Construct(int width, int height) public void Construct(int width, int height)
{ {
using (Buffer2D<TestStructs.Foo> buffer = Configuration.Default.MemoryManager.Allocate2D<TestStructs.Foo>(width, height)) using (Buffer2D<TestStructs.Foo> buffer = this.MemoryManager.Allocate2D<TestStructs.Foo>(width, height))
{ {
Assert.Equal(width, buffer.Width); Assert.Equal(width, buffer.Width);
Assert.Equal(height, buffer.Height); Assert.Equal(height, buffer.Height);
@ -42,16 +68,12 @@ namespace SixLabors.ImageSharp.Tests.Memory
[Fact] [Fact]
public void CreateClean() public void CreateClean()
{ {
for (int i = 0; i < 100; i++) using (Buffer2D<int> buffer = this.MemoryManager.Allocate2D<int>(42, 42, true))
{ {
using (Buffer2D<int> buffer = Configuration.Default.MemoryManager.Allocate2D<int>(42, 42, true)) Span<int> span = buffer.Span;
for (int j = 0; j < span.Length; j++)
{ {
Span<int> span = buffer.Span; Assert.Equal(0, span[j]);
for (int j = 0; j < span.Length; j++)
{
Assert.Equal(0, span[j]);
span[j] = 666;
}
} }
} }
} }
@ -62,7 +84,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(17, 42, 41)] [InlineData(17, 42, 41)]
public void GetRowSpanY(int width, int height, int y) public void GetRowSpanY(int width, int height, int y)
{ {
using (Buffer2D<TestStructs.Foo> buffer = Configuration.Default.MemoryManager.Allocate2D<TestStructs.Foo>(width, height)) using (Buffer2D<TestStructs.Foo> buffer = this.MemoryManager.Allocate2D<TestStructs.Foo>(width, height))
{ {
Span<TestStructs.Foo> span = buffer.GetRowSpan(y); Span<TestStructs.Foo> span = buffer.GetRowSpan(y);
@ -78,7 +100,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(17, 42, 0, 41)] [InlineData(17, 42, 0, 41)]
public void GetRowSpanXY(int width, int height, int x, int y) public void GetRowSpanXY(int width, int height, int x, int y)
{ {
using (Buffer2D<TestStructs.Foo> buffer = Configuration.Default.MemoryManager.Allocate2D<TestStructs.Foo>(width, height)) using (Buffer2D<TestStructs.Foo> buffer = this.MemoryManager.Allocate2D<TestStructs.Foo>(width, height))
{ {
Span<TestStructs.Foo> span = buffer.GetRowSpan(x, y); Span<TestStructs.Foo> span = buffer.GetRowSpan(x, y);
@ -94,13 +116,13 @@ namespace SixLabors.ImageSharp.Tests.Memory
[InlineData(99, 88, 98, 87)] [InlineData(99, 88, 98, 87)]
public void Indexer(int width, int height, int x, int y) public void Indexer(int width, int height, int x, int y)
{ {
using (Buffer2D<TestStructs.Foo> buffer = Configuration.Default.MemoryManager.Allocate2D<TestStructs.Foo>(width, height)) using (Buffer2D<TestStructs.Foo> buffer = this.MemoryManager.Allocate2D<TestStructs.Foo>(width, height))
{ {
Span<TestStructs.Foo> array = buffer.Buffer.Span; Span<TestStructs.Foo> span = buffer.Buffer.Span;
ref TestStructs.Foo actual = ref buffer[x, y]; ref TestStructs.Foo actual = ref buffer[x, y];
ref TestStructs.Foo expected = ref array[y * width + x]; ref TestStructs.Foo expected = ref span[y * width + x];
Assert.True(Unsafe.AreSame(ref expected, ref actual)); Assert.True(Unsafe.AreSame(ref expected, ref actual));
} }

Loading…
Cancel
Save