Skip to content

Commit

Permalink
add some basic async unit tests for the deflator streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Numpsy committed Aug 3, 2020
1 parent a11665d commit ff559a8
Showing 1 changed file with 119 additions and 44 deletions.
163 changes: 119 additions & 44 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 @@ -15,19 +16,50 @@ namespace ICSharpCode.SharpZipLib.Tests.Base
[TestFixture]
public class InflaterDeflaterTestSuite
{
private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
private void VerifyInflatedData(byte[] original, byte[] buf2, int level, bool zlib)
{
ms.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < original.Length; ++i)
{
if (buf2[i] != original[i])
{
string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib);
if (original.Length < 2048)
{
var builder = new StringBuilder(description);
for (int d = 0; d < original.Length; ++d)
{
builder.AppendFormat("{0} ", original[d]);
}

Assert.Fail(builder.ToString());
}
else
{
Assert.Fail(description);
}
}
}
}

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

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

int currentIndex = 0;
int count = buf2.Length;
return inStream;
}

try
private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
{
byte[] buf2 = new byte[original.Length];

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,40 +70,37 @@ private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
currentIndex += numRead;
count -= numRead;
}
}
catch (Exception ex)
{
Console.WriteLine("Unexpected exception - '{0}'", ex.Message);
throw;
}

if (currentIndex != original.Length)
{
Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex);
Assert.Fail("Lengths different");
Assert.That(currentIndex, Is.EqualTo(original.Length), "Decompressed data must have the same length as the original data");
}

for (int i = 0; i < original.Length; ++i)
VerifyInflatedData(original, buf2, level, zlib);
}

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

using (var inStream = GetInflaterInputStream(ms, zlib))
{
if (buf2[i] != original[i])
{
string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib);
if (original.Length < 2048)
{
var builder = new StringBuilder(description);
for (int d = 0; d < original.Length; ++d)
{
builder.AppendFormat("{0} ", original[d]);
}
int currentIndex = 0;
int count = buf2.Length;

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

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

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

private MemoryStream Deflate(byte[] data, int level, bool zlib)
Expand All @@ -89,35 +118,73 @@ private MemoryStream Deflate(byte[] data, int level, bool zlib)
return memoryStream;
}

private void RandomDeflateInflate(int size, int level, bool 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))
{
outStream.IsStreamOwner = false;
await outStream.WriteAsync(data, 0, data.Length);
await outStream.FlushAsync();
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 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);
}

/// <summary>
/// Basic inflate/deflate test
/// </summary>
[Test]
[Category("Base")]
public void InflateDeflateZlib()
public void InflateDeflateZlib([Range(0, 9)] int level)
{
for (int level = 0; level < 10; ++level)
{
RandomDeflateInflate(100000, level, true);
}
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 @@ -167,12 +234,20 @@ private void TryManyVariants(int level, bool zlib, RunCompress test, byte[] buff
/// </summary>
[Test]
[Category("Base")]
public void InflateDeflateNonZlib()
public void InflateDeflateNonZlib([Range(0, 9)] int level)
{
for (int level = 0; level < 10; ++level)
{
RandomDeflateInflate(100000, level, false);
}
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]
Expand Down

0 comments on commit ff559a8

Please sign in to comment.