don't use pools in tests

This commit is contained in:
Adam Hathcock
2025-10-31 11:39:57 +00:00
parent 1e22b47fe1
commit c53ca372f2
3 changed files with 72 additions and 114 deletions

View File

@@ -581,7 +581,7 @@ public class LzmaStreamTests
false
);
var buffer = ArrayPool<byte>.Shared.Rent(1024);
var buffer = new byte[1024];
long totalRead = 0;
while (totalRead < decompressedSize)
{
@@ -597,6 +597,5 @@ public class LzmaStreamTests
break;
}
}
ArrayPool<byte>.Shared.Return(buffer);
}
}

View File

@@ -28,148 +28,113 @@ public class SharpCompressStreamAsyncTests
[Fact]
public async Task BufferReadAsyncTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
try
byte[] data = new byte[0x100000];
byte[] test = new byte[0x1000];
using (MemoryStream ms = new MemoryStream(data))
{
using (MemoryStream ms = new MemoryStream(data))
CreateData(ms);
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
CreateData(ms);
IStreamStack stack = (IStreamStack)scs;
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
IStreamStack stack = (IStreamStack)scs;
scs.Seek(0x1000, SeekOrigin.Begin);
Assert.Equal(0x1000, scs.Position); // position in the SharpCompressionStream
Assert.Equal(0x1000, ms.Position); // initial seek + full buffer read
scs.Seek(0x1000, SeekOrigin.Begin);
Assert.Equal(0x1000, scs.Position); // position in the SharpCompressionStream
Assert.Equal(0x1000, ms.Position); // initial seek + full buffer read
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x11000, ms.Position); // seek plus read bytes
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x11000, ms.Position); // seek plus read bytes
scs.Seek(0x500, SeekOrigin.Begin); // seek before the buffer start
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x500 to 0x1500
Assert.Equal(0x1500, scs.Position); // stream has correct position
Assert.True(data.Skip(0x500).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10500, ms.Position); // seek plus read bytes
}
scs.Seek(0x500, SeekOrigin.Begin); // seek before the buffer start
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x500 to 0x1500
Assert.Equal(0x1500, scs.Position); // stream has correct position
Assert.True(data.Skip(0x500).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10500, ms.Position); // seek plus read bytes
}
}
finally
{
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test);
}
}
[Fact]
public async Task BufferReadAndSeekAsyncTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
try
byte[] data = new byte[0x100000];
byte[] test = new byte[0x1000];
using (MemoryStream ms = new MemoryStream(data))
{
using (MemoryStream ms = new MemoryStream(data))
CreateData(ms);
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
CreateData(ms);
IStreamStack stack = (IStreamStack)scs;
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
IStreamStack stack = (IStreamStack)scs;
await scs.ReadAsync(test, 0, test.Length); // read bytes 0 to 0x1000
Assert.True(data.Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x1000, scs.Position); // stream has correct position
Assert.Equal(0x10000, ms.Position); // moved the base stream on by buffer size
await scs.ReadAsync(test, 0, test.Length); // read bytes 0 to 0x1000
Assert.True(data.Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x1000, scs.Position); // stream has correct position
Assert.Equal(0x10000, ms.Position); // moved the base stream on by buffer size
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10000, ms.Position); // the base stream has not moved
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10000, ms.Position); // the base stream has not moved
// rewind the buffer
stack.Rewind(0x1000); // rewind buffer back by 0x1000 bytes
// rewind the buffer
stack.Rewind(0x1000); // rewind buffer back by 0x1000 bytes
// repeat the previous test
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10000, ms.Position); // the base stream has not moved
}
// repeat the previous test
await scs.ReadAsync(test, 0, test.Length); // read bytes 0x1000 to 0x2000
Assert.Equal(0x2000, scs.Position); // stream has correct position
Assert.True(data.Skip(test.Length).Take(test.Length).SequenceEqual(test)); // is the data correct
Assert.Equal(0x10000, ms.Position); // the base stream has not moved
}
}
finally
{
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test);
}
}
[Fact]
public async Task MultipleAsyncReadsTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
byte[] test1 = ArrayPool<byte>.Shared.Rent(0x800);
byte[] test2 = ArrayPool<byte>.Shared.Rent(0x800);
try
byte[] data = new byte[0x100000];
byte[] test1 = new byte[0x800];
byte[] test2 = new byte[0x800];
using (MemoryStream ms = new MemoryStream(data))
{
using (MemoryStream ms = new MemoryStream(data))
CreateData(ms);
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
CreateData(ms);
// Read first chunk
await scs.ReadAsync(test1, 0, test1.Length);
Assert.Equal(0x800, scs.Position);
Assert.True(data.Take(test1.Length).SequenceEqual(test1)); // first read is correct
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
// Read first chunk
await scs.ReadAsync(test1, 0, test1.Length);
Assert.Equal(0x800, scs.Position);
Assert.True(data.Take(test1.Length).SequenceEqual(test1)); // first read is correct
// Read second chunk
await scs.ReadAsync(test2, 0, test2.Length);
Assert.Equal(0x1000, scs.Position);
Assert.True(data.Skip(test1.Length).Take(test2.Length).SequenceEqual(test2)); // second read is correct
}
// Read second chunk
await scs.ReadAsync(test2, 0, test2.Length);
Assert.Equal(0x1000, scs.Position);
Assert.True(data.Skip(test1.Length).Take(test2.Length).SequenceEqual(test2)); // second read is correct
}
}
finally
{
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test1);
ArrayPool<byte>.Shared.Return(test2);
}
}
[Fact]
public async Task LargeBufferAsyncReadTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x200000);
byte[] test = ArrayPool<byte>.Shared.Rent(0x8000);
try
byte[] data = new byte[0x200000];
byte[] test = new byte[0x8000];
using (MemoryStream ms = new MemoryStream(data))
{
using (MemoryStream ms = new MemoryStream(data))
{
CreateData(ms);
CreateData(ms);
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
{
for (int i = 0; i < 10; i++)
{
for (int i = 0; i < 10; i++)
{
await scs.ReadAsync(test, 0, test.Length);
long expectedPosition = (long)(i + 1) * test.Length;
Assert.Equal(expectedPosition, scs.Position);
Assert.True(
data.Skip(i * test.Length).Take(test.Length).SequenceEqual(test)
);
}
await scs.ReadAsync(test, 0, test.Length);
long expectedPosition = (long)(i + 1) * test.Length;
Assert.Equal(expectedPosition, scs.Position);
Assert.True(data.Skip(i * test.Length).Take(test.Length).SequenceEqual(test));
}
}
}
finally
{
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test);
}
}
}

View File

@@ -27,8 +27,8 @@ public class SharpCompressStreamTests
[Fact]
public void BufferReadTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
byte[] data = new byte[0x100000];
byte[] test = new byte[0x1000];
using (MemoryStream ms = new MemoryStream(data))
{
createData(ms);
@@ -53,16 +53,13 @@ public class SharpCompressStreamTests
Assert.Equal(0x10500, ms.Position); //seek plus read bytes
}
}
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test);
}
[Fact]
public void BufferReadAndSeekTest()
{
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
byte[] data = new byte[0x100000];
byte[] test = new byte[0x1000];
using (MemoryStream ms = new MemoryStream(data))
{
createData(ms);
@@ -91,8 +88,5 @@ public class SharpCompressStreamTests
Assert.Equal(0x10000, ms.Position); //the base stream has not moved
}
}
ArrayPool<byte>.Shared.Return(data);
ArrayPool<byte>.Shared.Return(test);
}
}