Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreilly committed Apr 13, 2009
1 parent 62f8838 commit f1a5d55
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 86 deletions.
62 changes: 28 additions & 34 deletions tests/Base/InflaterDeflaterTests.cs
Expand Up @@ -7,7 +7,6 @@


using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;


using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Tests.TestSupport;


Expand Down Expand Up @@ -193,19 +192,17 @@ public void CloseDeflatorWithNestedUsing()
} }


Assert.IsNotNull(tempFile, "No permission to execute this test?"); Assert.IsNotNull(tempFile, "No permission to execute this test?");
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();
}


File.Delete(tempFile); 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();
} }

File.Delete(tempFile);
} }


[Test] [Test]
Expand Down Expand Up @@ -281,33 +278,30 @@ public void CloseInflatorWithNestedUsing()


Assert.IsNotNull(tempFile, "No permission to execute this test?"); Assert.IsNotNull(tempFile, "No permission to execute this test?");


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

using (FileStream diskFile = File.OpenRead(tempFile))
using (InflaterInputStream deflator = new InflaterInputStream(diskFile))
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(); using (FileStream diskFile = File.OpenRead(tempFile))
b.Append(buffer); using (InflaterInputStream deflator = new InflaterInputStream(diskFile))
Assert.AreEqual("Hello", b.ToString()); 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);
} }

File.Delete(tempFile);
} }
} }
} }
96 changes: 44 additions & 52 deletions tests/Zip/ZipTests.cs
Expand Up @@ -165,7 +165,7 @@ protected byte[] MakeInMemoryZip(bool withSeek, params object[] createSpecs)
{ {
MemoryStream ms; MemoryStream ms;


if (withSeek == true) { if (withSeek) {
ms = new MemoryStream(); ms = new MemoryStream();
} }
else { else {
Expand Down Expand Up @@ -211,7 +211,7 @@ protected byte[] MakeInMemoryZip(bool withSeek, params object[] createSpecs)
{ {
MemoryStream ms; MemoryStream ms;


if (withSeek == true) { if (withSeek) {
ms = new MemoryStream(); ms = new MemoryStream();
} }
else { else {
Expand Down Expand Up @@ -758,8 +758,7 @@ public void ReadAndWriteZip64NonSeekable()


using (ZipInputStream zis = new ZipInputStream(msw)) using (ZipInputStream zis = new ZipInputStream(msw))
{ {
ZipEntry ze; while ( zis.GetNextEntry() != null )
while ( (ze = zis.GetNextEntry()) != null )
{ {
int len = 0; int len = 0;
int bufferSize = 1024; int bufferSize = 1024;
Expand Down Expand Up @@ -932,7 +931,7 @@ public void WriteThroughput()


DateTime endTime = DateTime.Now; DateTime endTime = DateTime.Now;
TimeSpan span = endTime - startTime; TimeSpan span = endTime - startTime;
Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024) / span.TotalSeconds); Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024.0) / span.TotalSeconds);
} }


[Test] [Test]
Expand Down Expand Up @@ -962,7 +961,7 @@ public void SingleLargeEntry()


DateTime endTime = DateTime.Now; DateTime endTime = DateTime.Now;
TimeSpan span = endTime - startTime; TimeSpan span = endTime - startTime;
Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024) / span.TotalSeconds); Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024.0) / span.TotalSeconds);
} }


void Reader() void Reader()
Expand Down Expand Up @@ -1080,7 +1079,7 @@ public void NameTooLong()
WindowsNameTransform wnt = new WindowsNameTransform(); WindowsNameTransform wnt = new WindowsNameTransform();
string veryLong = new string('x', 261); string veryLong = new string('x', 261);
try { try {
string transformed = wnt.TransformDirectory(veryLong); wnt.TransformDirectory(veryLong);
Assert.Fail("Expected an exception"); Assert.Fail("Expected an exception");
} }
catch (PathTooLongException) { catch (PathTooLongException) {
Expand Down Expand Up @@ -1194,10 +1193,10 @@ public void TooLong()
ZipNameTransform zt = new ZipNameTransform(); ZipNameTransform zt = new ZipNameTransform();
string veryLong = new string('x', 65536); string veryLong = new string('x', 65536);
try { try {
string transformed = zt.TransformDirectory(veryLong); zt.TransformDirectory(veryLong);
Assert.Fail("Expected an exception"); Assert.Fail("Expected an exception");
} }
catch { catch (PathTooLongException) {
} }
} }


Expand All @@ -1207,7 +1206,7 @@ public void LengthBoundaryOk()
ZipNameTransform zt = new ZipNameTransform(); ZipNameTransform zt = new ZipNameTransform();
string veryLong = "c:\\" + new string('x', 65535); string veryLong = "c:\\" + new string('x', 65535);
try { try {
string transformed = zt.TransformDirectory(veryLong); zt.TransformDirectory(veryLong);
} }
catch { catch {
Assert.Fail("Expected no exception"); Assert.Fail("Expected no exception");
Expand Down Expand Up @@ -1424,8 +1423,7 @@ public void ExerciseGetNextEntry()
using (ZipInputStream inStream = new ZipInputStream(ms)) { using (ZipInputStream inStream = new ZipInputStream(ms)) {
byte[] buffer = new byte[10]; byte[] buffer = new byte[10];


ZipEntry entry; while (inStream.GetNextEntry() != null) {
while ((entry = inStream.GetNextEntry()) != null) {
// Read a portion of the data, so GetNextEntry has some work to do. // Read a portion of the data, so GetNextEntry has some work to do.
inStream.Read(buffer, 0, 10); inStream.Read(buffer, 0, 10);
} }
Expand Down Expand Up @@ -1683,8 +1681,7 @@ public void SkipEncryptedEntriesWithoutSettingPassword()
MemoryStream ms = new MemoryStream(compressedData); MemoryStream ms = new MemoryStream(compressedData);
ZipInputStream inStream = new ZipInputStream(ms); ZipInputStream inStream = new ZipInputStream(ms);


ZipEntry entry; while (inStream.GetNextEntry() != null) {
while ((entry = inStream.GetNextEntry()) != null) {
} }


inStream.Close(); inStream.Close();
Expand Down Expand Up @@ -1905,44 +1902,42 @@ public void MakeLargeZipFile()


Assert.IsNotNull(tempFile, "No permission to execute this test?"); Assert.IsNotNull(tempFile, "No permission to execute this test?");


if (tempFile != null) { const int blockSize = 4096;
const int blockSize = 4096;


byte[] data = new byte[blockSize]; byte[] data = new byte[blockSize];
byte nextValue = 0; byte nextValue = 0;
for (int i = 0; i < blockSize; ++i) { for (int i = 0; i < blockSize; ++i) {
nextValue = ScatterValue(nextValue); nextValue = ScatterValue(nextValue);
data[i] = nextValue; data[i] = nextValue;
} }


tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
Console.WriteLine("Starting at {0}", DateTime.Now); Console.WriteLine("Starting at {0}", DateTime.Now);
try { try {
// MakeZipFile(tempFile, new String[] {"1", "2" }, int.MaxValue, "C1"); // MakeZipFile(tempFile, new String[] {"1", "2" }, int.MaxValue, "C1");
using (FileStream fs = File.Create(tempFile)) { using (FileStream fs = File.Create(tempFile)) {
ZipOutputStream zOut = new ZipOutputStream(fs); ZipOutputStream zOut = new ZipOutputStream(fs);
zOut.SetLevel(4); zOut.SetLevel(4);
const int TargetFiles = 8100; const int TargetFiles = 8100;
for (int i = 0; i < TargetFiles; ++i) { for (int i = 0; i < TargetFiles; ++i) {
ZipEntry e = new ZipEntry(i.ToString()); ZipEntry e = new ZipEntry(i.ToString());
e.CompressionMethod = CompressionMethod.Stored; e.CompressionMethod = CompressionMethod.Stored;


zOut.PutNextEntry(e); zOut.PutNextEntry(e);
for (int block = 0; block < 128; ++block) { for (int block = 0; block < 128; ++block) {
zOut.Write(data, 0, blockSize); zOut.Write(data, 0, blockSize);
}
} }
zOut.Close();
fs.Close();

TestLargeZip(tempFile, TargetFiles);
} }
zOut.Close();
fs.Close();

TestLargeZip(tempFile, TargetFiles);
} }
finally }
{ finally
Console.WriteLine("Starting at {0}", DateTime.Now); {
// File.Delete(tempFile); Console.WriteLine("Starting at {0}", DateTime.Now);
} // File.Delete(tempFile);
} }
} }


Expand Down Expand Up @@ -3458,7 +3453,7 @@ public void FindEntry()
Assert.IsNotNull(tempFile, "No permission to execute this test?"); Assert.IsNotNull(tempFile, "No permission to execute this test?");


tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip");
MakeZipFile(tempFile, new String[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha"); MakeZipFile(tempFile, new string[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha");


using (ZipFile zipFile = new ZipFile(tempFile)) { using (ZipFile zipFile = new ZipFile(tempFile)) {
Assert.AreEqual(3, zipFile.Count, "Expected 1 entry"); Assert.AreEqual(3, zipFile.Count, "Expected 1 entry");
Expand Down Expand Up @@ -3631,8 +3626,6 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect()
{ {
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();


byte[] rawData;

using (ZipFile testFile = new ZipFile(ms)) using (ZipFile testFile = new ZipFile(ms))
{ {
testFile.IsStreamOwner = false; testFile.IsStreamOwner = false;
Expand All @@ -3643,13 +3636,12 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect()
testFile.CommitUpdate(); testFile.CommitUpdate();


Assert.IsTrue(testFile.TestArchive(true)); Assert.IsTrue(testFile.TestArchive(true));
rawData = ms.ToArray();
} }


using (ZipFile testFile = new ZipFile(ms)) using (ZipFile testFile = new ZipFile(ms))
{ {
Assert.IsTrue(testFile.TestArchive(true)); Assert.IsTrue(testFile.TestArchive(true));
testFile.IsStreamOwner = false; testFile.IsStreamOwner = true;


testFile.BeginUpdate(); testFile.BeginUpdate();
testFile.Password = "pwd"; testFile.Password = "pwd";
Expand Down

0 comments on commit f1a5d55

Please sign in to comment.