mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-25 00:15:15 +00:00
fmt
This commit is contained in:
@@ -11,88 +11,88 @@ namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class SharpCompressStreamTests
|
||||
{
|
||||
private static void createData(MemoryStream ms)
|
||||
private static void createData(MemoryStream ms)
|
||||
{
|
||||
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8, true))
|
||||
{
|
||||
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8, true))
|
||||
{
|
||||
//write offset every 4 bytes - easy to test position
|
||||
for (int i = 0; i < ms.Length; i += 4)
|
||||
{
|
||||
bw.Write(i);
|
||||
}
|
||||
}
|
||||
ms.Position = 0;
|
||||
//write offset every 4 bytes - easy to test position
|
||||
for (int i = 0; i < ms.Length; i += 4)
|
||||
{
|
||||
bw.Write(i);
|
||||
}
|
||||
}
|
||||
ms.Position = 0;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BufferReadTest()
|
||||
{
|
||||
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
|
||||
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
{
|
||||
createData(ms);
|
||||
|
||||
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 (with 0xf000 remaining in the buffer)
|
||||
Assert.Equal(0x1000, ms.Position); //initial seek + full buffer read
|
||||
|
||||
scs.Read(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
|
||||
scs.Read(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
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BufferReadTest()
|
||||
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);
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
{
|
||||
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
|
||||
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
{
|
||||
createData(ms);
|
||||
createData(ms);
|
||||
|
||||
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
|
||||
{
|
||||
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 (with 0xf000 remaining in the buffer)
|
||||
Assert.Equal(0x1000, ms.Position); //initial seek + full buffer read
|
||||
scs.Read(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 the size of the buffer not what was requested
|
||||
|
||||
scs.Read(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.Read(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
|
||||
|
||||
scs.Seek(0x500, SeekOrigin.Begin); //seek before the buffer start
|
||||
scs.Read(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
|
||||
}
|
||||
}
|
||||
//rewind the buffer
|
||||
stack.Rewind(0x1000); //rewind buffer back by 0x1000 bytes
|
||||
|
||||
ArrayPool<byte>.Shared.Return(data);
|
||||
ArrayPool<byte>.Shared.Return(test);
|
||||
//repeat the previous test
|
||||
scs.Read(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
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BufferReadAndSeekTest()
|
||||
{
|
||||
byte[] data = ArrayPool<byte>.Shared.Rent(0x100000);
|
||||
byte[] test = ArrayPool<byte>.Shared.Rent(0x1000);
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
{
|
||||
createData(ms);
|
||||
|
||||
using (SharpCompressStream scs = new SharpCompressStream(ms, true, false, 0x10000))
|
||||
{
|
||||
IStreamStack stack = (IStreamStack)scs;
|
||||
|
||||
scs.Read(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 the size of the buffer not what was requested
|
||||
|
||||
scs.Read(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
|
||||
|
||||
//repeat the previous test
|
||||
scs.Read(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
|
||||
}
|
||||
}
|
||||
|
||||
ArrayPool<byte>.Shared.Return(data);
|
||||
ArrayPool<byte>.Shared.Return(test);
|
||||
}
|
||||
ArrayPool<byte>.Shared.Return(data);
|
||||
ArrayPool<byte>.Shared.Return(test);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user