Browse Source

Merge pull request #1225 from SixLabors/af/FixedCapacityPooledMemoryStream-use-allocator

Tests for FixedCapacityPooledMemoryStream
pull/1574/head
James Jackson-South 6 years ago
committed by GitHub
parent
commit
4f5fc8e441
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/ImageSharp/IO/FixedCapacityPooledMemoryStream.cs
  2. 42
      tests/ImageSharp.Tests/IO/FixedCapacityPooledMemoryStreamTests.cs

13
src/ImageSharp/IO/FixedCapacityPooledMemoryStream.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.IO;
using SixLabors.ImageSharp.Memory;
@ -29,6 +30,18 @@ namespace SixLabors.ImageSharp.IO
/// <inheritdoc/>
public override long Length { get; }
/// <inheritdoc/>
public override bool TryGetBuffer(out ArraySegment<byte> buffer)
{
if (this.isDisposed)
{
throw new ObjectDisposedException(this.GetType().Name);
}
buffer = new ArraySegment<byte>(this.buffer.Array, 0, this.buffer.Length());
return true;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{

42
tests/ImageSharp.Tests/IO/FixedCapacityPooledMemoryStreamTests.cs

@ -0,0 +1,42 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Tests.Memory;
using Xunit;
namespace SixLabors.ImageSharp.Tests.IO
{
public class FixedCapacityPooledMemoryStreamTests
{
private readonly TestMemoryAllocator memoryAllocator = new TestMemoryAllocator();
[Theory]
[InlineData(1)]
[InlineData(512)]
public void RentsManagedBuffer(int length)
{
MemoryStream ms = this.memoryAllocator.AllocateFixedCapacityMemoryStream(length);
Assert.Equal(length, this.memoryAllocator.AllocationLog.Single().Length);
ms.Dispose();
Assert.Equal(1, this.memoryAllocator.ReturnLog.Count);
}
[Theory]
[InlineData(42)]
[InlineData(2999)]
public void UsesRentedBuffer(int length)
{
using MemoryStream ms = this.memoryAllocator.AllocateFixedCapacityMemoryStream(length);
ms.TryGetBuffer(out ArraySegment<byte> buffer);
byte[] array = buffer.Array;
Assert.Equal(array.GetHashCode(), this.memoryAllocator.AllocationLog.Single().HashCodeOfBuffer);
ms.Write(new byte[] { 123 });
Assert.Equal(123, array[0]);
}
}
}
Loading…
Cancel
Save