Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
jfreilly committed Oct 26, 2006
1 parent 7b57fde commit 85d2a58
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 97 deletions.
241 changes: 154 additions & 87 deletions tests/Base/InflaterDeflaterTests.cs
Expand Up @@ -24,39 +24,52 @@ void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)
Inflater inflater = new Inflater(!zlib);
InflaterInputStream inStream = new InflaterInputStream(ms, inflater);
byte[] buf2 = new byte[original.Length];
int pos = 0;


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

try
{
while (true) {
int numRead = inStream.Read(buf2, pos, 4096);
if (numRead <= 0) {
while (true)
{
int numRead = inStream.Read(buf2, currentIndex, count);
if (numRead <= 0)
{
break;
}
pos += numRead;
currentIndex += numRead;
count -= numRead;
}
}
catch(Exception ex)
{
Console.WriteLine("Unexpected exception - '{0}'", ex.Message);
throw;
}

if ( pos != original.Length ) {
Console.WriteLine("Original {0}, new {1}", original.Length, pos);

if ( currentIndex != original.Length )
{
Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex);
Assert.Fail("Lengths different");
}

for (int i = 0; i < original.Length; ++i) {
if ( buf2[i] != original[i] ) {
string description = string.Format("Difference at {0} lvl {1} zlib {2} ", i, level, zlib);
if ( original.Length < 2048 ) {

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 )
{
StringBuilder builder = new StringBuilder(description);
for (int d = 0; d < original.Length; ++d) {
builder.AppendFormat("{0} ", original[i]);
for (int d = 0; d < original.Length; ++d)
{
builder.AppendFormat("{0} ", original[d]);
}

Assert.Fail(builder.ToString());
}
else {
else
{
Assert.Fail(description);
}
}
Expand All @@ -65,110 +78,152 @@ void Inflate(MemoryStream ms, byte[] original, int level, bool zlib)

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

Deflater deflater = new Deflater(level, !zlib);
DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater);

outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();

return ms;
using ( DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater) )
{
outStream.IsStreamOwner = false;
outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();
}
return memoryStream;
}

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

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

/// <summary>
/// Random inflate/deflate test using zlib headers.
/// Basic inflate/deflate test
/// </summary>
[Test]
[Category("Base")]
public void TestInflateDeflateZlib()
public void InflateDeflateZlib()
{
for (int level = 0; level < 10; ++level) {
for (int level = 0; level < 10; ++level)
{
RandomDeflateInflate(100000, level, true);
}
}
/// <summary>
/// Random inflate/deflate using non-zlib variant
/// </summary>

delegate void RunCompress(byte[] buffer);

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

void DeflateAndInflate(byte[] buffer)
{
++runCount;
MemoryStream ms = Deflate(buffer, runLevel, runZlib);
Inflate(ms, buffer, runLevel, runZlib);
}

void TryVariants(RunCompress test, byte[] buffer, int index)
{
int worker = 0;
while ( worker <= 255 )
{
buffer[index] = (byte)worker;
if ( index < buffer.Length - 1)
{
TryVariants(test, buffer, index + 1);
}
else
{
test(buffer);
}

worker += runRandom.Next(256);
}
}

void TryManyVariants(int level, bool zlib, RunCompress test, byte[] buffer)
{
runLevel = level;
runZlib = zlib;
TryVariants(test, buffer, 0);
}


[Test]
[Category("Base")]
public void TestInflateDeflateNonZlib()
public void SmallBlocks()
{
for (int level = 0; level < 10; ++level) {
RandomDeflateInflate(100000, level, false);
byte[] buffer = new byte[10];
Array.Clear(buffer, 0, buffer.Length);
TryManyVariants(0, false, new RunCompress(DeflateAndInflate), buffer);
}

[Test]
[Category("Base")]
public void FindBug()
{
using ( FileStream fs = File.OpenRead("c:\\tmp\\original.dat") )
{
long readOffset = 0;
long readLength = fs.Length - readOffset;
// readLength -= 5567; // 5568 works 5567 doesnt....

fs.Seek(readOffset, SeekOrigin.Begin);

byte[] original = new byte[readLength];

int bytesRead = fs.Read(original, 0, original.Length);
Assert.AreEqual(bytesRead, original.Length);

MemoryStream ms = Deflate(original, Deflater.BEST_SPEED, true);
Inflate(ms, original, Deflater.BEST_SPEED, true);
}
}

/// <summary>
/// Basic inflate/deflate test
/// </summary>
[Test]
[Category("Base")]
public void TestInflateDeflate()
public void InflateDeflateNonZlib()
{
MemoryStream ms = new MemoryStream();
Deflater deflater = new Deflater(6);
DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater);

byte[] buf = new byte[1000000];
System.Random rnd = new Random();
rnd.NextBytes(buf);

outStream.Write(buf, 0, buf.Length);
outStream.Flush();
outStream.Finish();

ms.Seek(0, SeekOrigin.Begin);

InflaterInputStream inStream = new InflaterInputStream(ms);
byte[] buf2 = new byte[buf.Length];
int pos = 0;
while (true) {
int numRead = inStream.Read(buf2, pos, 4096);
if (numRead <= 0) {
break;
}
pos += numRead;
}

for (int i = 0; i < buf.Length; ++i) {
Assert.AreEqual(buf2[i], buf[i]);
for (int level = 0; level < 10; ++level)
{
RandomDeflateInflate(100000, level, false);
}
}

[Test]
[Category("Base")]
public void CloseDeflatorWithNestedUsing()
{
string tempFile = null;
try {
try
{
tempFile = Path.GetTempPath();
}
catch (SecurityException) {
catch (SecurityException)
{
}

Assert.IsNotNull(tempFile, "No permission to execute this test?");
if (tempFile != null) {
if (tempFile != null)
{
tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");

using (FileStream diskFile = File.Create(tempFile))
using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile))
using (StreamWriter txtFile = new StreamWriter(deflator)) {
using (StreamWriter txtFile = new StreamWriter(deflator))
{
txtFile.Write("Hello");
txtFile.Flush();
}

File.Delete(tempFile);
}
}
Expand All @@ -178,29 +233,41 @@ public void CloseDeflatorWithNestedUsing()
public void CloseInflatorWithNestedUsing()
{
string tempFile = null;
try {
try
{
tempFile = Path.GetTempPath();
}
catch (SecurityException) {
catch (SecurityException)
{
}

Assert.IsNotNull(tempFile, "No permission to execute this test?");
if (tempFile != null) {

if (tempFile != null)
{
tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
using (FileStream diskFile = File.Create(tempFile))
using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile))
using (StreamWriter txtFile = new StreamWriter(deflator)) {
txtFile.Write("Hello");
txtFile.Flush();
using (StreamWriter textWriter = new StreamWriter(deflator))
{
textWriter.Write("Hello");
textWriter.Flush();
}

// This wont actually fail... Test is not valid

using (FileStream diskFile = File.OpenRead(tempFile))
using (InflaterInputStream deflator = new InflaterInputStream(diskFile))
using (StreamReader reader = new StreamReader(deflator)) {
reader.Peek();
using (StreamReader textReader = new StreamReader(deflator))
{
char[] buffer = new char[5];
int readCount = textReader.Read(buffer, 0, 5);
Assert.AreEqual(5, readCount);

StringBuilder b = new StringBuilder();
b.Append(buffer);
Assert.AreEqual("Hello", b.ToString());

}

File.Delete(tempFile);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/SharpZipLibTests.prjx
Expand Up @@ -14,7 +14,7 @@
</Contents>
<References>
<Reference type="Assembly" refto="..\bin\ICSharpCode.SharpZipLib.dll" localcopy="True" />
<Reference type="Assembly" refto="..\..\..\..\SharpDevelop\SharpDevelop\bin\nunit.framework.dll" localcopy="True" />
<Reference type="Assembly" refto="..\Nunit\nunit.framework.dll" localcopy="True" />
</References>
<DeploymentInformation target="" script="" strategy="File" />
<Configuration runwithwarnings="True" name="Debug">
Expand Down
16 changes: 16 additions & 0 deletions tests/SharpZipLibTests.sln
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 9.00
# SharpDevelop 2.0.0.1710
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpZipLibTests", "SharpZipLibTests.csproj", "{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.Build.0 = Release|Any CPU
{4F9BF21E-A7FC-4005-A1C7-A17108F7010D}.Release|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion tests/Tar/TarTests.cs
Expand Up @@ -555,7 +555,7 @@ public void CloningAndUniqueness()

headerD.LinkName = "Something different";

Assert.IsTrue(headerD.LinkName != entryf.TarHeader.LinkName, "Entry headers should be unique");
Assert.AreNotEqual(headerD.LinkName, entryf.TarHeader.LinkName, "Entry headers should be unique");
}
}
}

0 comments on commit 85d2a58

Please sign in to comment.