Skip to content

Commit

Permalink
PR #457: add basic async unit tests for the inflator/deflator streams
Browse files Browse the repository at this point in the history
* add some basic async unit tests for the deflator streams

Co-authored-by: nils måsén <[email protected]>
  • Loading branch information
Numpsy and piksel authored Feb 6, 2021
1 parent 33da791 commit 1193672
Showing 1 changed file with 121 additions and 40 deletions.
161 changes: 121 additions & 40 deletions test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace ICSharpCode.SharpZipLib.Tests.Base
{
Expand All @@ -17,17 +18,13 @@ public class InflaterDeflaterTestSuite
{
private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
{
ms.Seek(0, SeekOrigin.Begin);

var inflater = new Inflater(!zlib);
var inStream = new InflaterInputStream(ms, inflater);
byte[] buf2 = new byte[original.Length];

int currentIndex = 0;
int count = buf2.Length;

try
using (var inStream = GetInflaterInputStream(ms, zlib))
{
int currentIndex = 0;
int count = buf2.Length;

while (true)
{
int numRead = inStream.Read(buf2, currentIndex, count);
Expand All @@ -38,19 +35,106 @@ private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
currentIndex += numRead;
count -= numRead;
}

Assert.That(currentIndex, Is.EqualTo(original.Length), "Decompressed data must have the same length as the original data");
}
catch (Exception ex)

VerifyInflatedData(original, buf2, level, zlib);
}

private MemoryStream Deflate(byte[] data, int level, bool zlib)
{
var memoryStream = new MemoryStream();

var deflater = new Deflater(level, !zlib);
using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater))
{
outStream.IsStreamOwner = false;
outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();
}
return memoryStream;
}

private static byte[] GetRandomTestData(int size)
{
byte[] buffer = new byte[size];
var rnd = new Random();
rnd.NextBytes(buffer);

return buffer;
}

private void RandomDeflateInflate(int size, int level, bool zlib)
{
byte[] buffer = GetRandomTestData(size);

MemoryStream ms = Deflate(buffer, level, zlib);
Inflate(ms, buffer, level, zlib);
}

private static InflaterInputStream GetInflaterInputStream(Stream compressedStream, bool zlib)
{
compressedStream.Seek(0, SeekOrigin.Begin);

var inflater = new Inflater(!zlib);
var inStream = new InflaterInputStream(compressedStream, inflater);

return inStream;
}

private async Task InflateAsync(MemoryStream ms, byte[] original, int level, bool zlib)
{
byte[] buf2 = new byte[original.Length];

using (var inStream = GetInflaterInputStream(ms, zlib))
{
Console.WriteLine("Unexpected exception - '{0}'", ex.Message);
throw;
int currentIndex = 0;
int count = buf2.Length;

while (true)
{
int numRead = await inStream.ReadAsync(buf2, currentIndex, count);
if (numRead <= 0)
{
break;
}
currentIndex += numRead;
count -= numRead;
}

Assert.That(currentIndex, Is.EqualTo(original.Length), "Decompressed data must have the same length as the original data");
}

if (currentIndex != original.Length)
VerifyInflatedData(original, buf2, level, zlib);
}

private async Task<MemoryStream> DeflateAsync(byte[] data, int level, bool zlib)
{
var memoryStream = new MemoryStream();

var deflater = new Deflater(level, !zlib);
using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater))
{
Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex);
Assert.Fail("Lengths different");
outStream.IsStreamOwner = false;
await outStream.WriteAsync(data, 0, data.Length);
await outStream.FlushAsync();
outStream.Finish();
}
return memoryStream;
}

private async Task RandomDeflateInflateAsync(int size, int level, bool zlib)
{
byte[] buffer = GetRandomTestData(size);

MemoryStream ms = await DeflateAsync(buffer, level, zlib);
await InflateAsync(ms, buffer, level, zlib);
}

private void VerifyInflatedData(byte[] original, byte[] buf2, int level, bool zlib)
{
for (int i = 0; i < original.Length; ++i)
{
if (buf2[i] != original[i])
Expand All @@ -74,31 +158,6 @@ private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
}
}

private MemoryStream Deflate(byte[] data, int level, bool zlib)
{
var memoryStream = new MemoryStream();

var deflater = new Deflater(level, !zlib);
using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater))
{
outStream.IsStreamOwner = false;
outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();
}
return memoryStream;
}

private void RandomDeflateInflate(int size, int level, bool zlib)
{
byte[] buffer = new byte[size];
var rnd = new Random();
rnd.NextBytes(buffer);

MemoryStream ms = Deflate(buffer, level, zlib);
Inflate(ms, buffer, level, zlib);
}

/// <summary>
/// Basic inflate/deflate test
/// </summary>
Expand All @@ -109,12 +168,23 @@ public void InflateDeflateZlib([Range(0, 9)] int level)
RandomDeflateInflate(100000, level, true);
}

/// <summary>
/// Basic async inflate/deflate test
/// </summary>
[Test]
[Category("Base")]
[Category("Async")]
public async Task InflateDeflateZlibAsync([Range(0, 9)] int level)
{
await RandomDeflateInflateAsync(100000, level, true);
}

private delegate void RunCompress(byte[] buffer);

private int runLevel;
private bool runZlib;
private long runCount;
private Random runRandom = new Random(5);
private readonly Random runRandom = new Random(5);

private void DeflateAndInflate(byte[] buffer)
{
Expand Down Expand Up @@ -169,6 +239,17 @@ public void InflateDeflateNonZlib([Range(0, 9)] int level)
RandomDeflateInflate(100000, level, false);
}

/// <summary>
/// Basic async inflate/deflate test
/// </summary>
[Test]
[Category("Base")]
[Category("Async")]
public async Task InflateDeflateNonZlibAsync([Range(0, 9)] int level)
{
await RandomDeflateInflateAsync(100000, level, false);
}


[Test]
[Category("Base")]
Expand Down

0 comments on commit 1193672

Please sign in to comment.