Skip to content

Commit 5e2816b

Browse files
committed
Added further tests for stream ownership. Refactored tests somewhat adding TestSupport classes.
Added threaded zip tests.
1 parent adbe984 commit 5e2816b

File tree

7 files changed

+1164
-74
lines changed

7 files changed

+1164
-74
lines changed

tests/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// You can specify all values by your own or you can build default build and revision
2424
// numbers with the '*' character (the default):
2525

26-
[assembly: AssemblyVersion("0.85.2.329")]
26+
[assembly: AssemblyVersion("0.85.3.346")]
2727

2828
// The following attributes specify the key for the sign of your assembly. See the
2929
// .NET Framework documentation for more information about signing.

tests/Base/InflaterDeflaterTests.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
1010
using ICSharpCode.SharpZipLib.GZip;
1111

12+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
13+
1214
namespace ICSharpCode.SharpZipLib.Tests.Base
1315
{
1416
/// <summary>
@@ -165,7 +167,7 @@ public void SmallBlocks()
165167
}
166168

167169
[Test]
168-
[Explicit]
170+
[Explicit]
169171
[Category("Base")]
170172
[Category("ExcludeFromAutoBuild")]
171173
public void FindBug()
@@ -230,6 +232,64 @@ public void CloseDeflatorWithNestedUsing()
230232
}
231233
}
232234

235+
[Test]
236+
[Category("Base")]
237+
public void DeflatorStreamOwnership()
238+
{
239+
MemoryStreamEx memStream = new MemoryStreamEx();
240+
DeflaterOutputStream s = new DeflaterOutputStream(memStream);
241+
242+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
243+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
244+
245+
s.Close();
246+
247+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
248+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
249+
250+
memStream = new MemoryStreamEx();
251+
s = new DeflaterOutputStream(memStream);
252+
253+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
254+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
255+
256+
s.IsStreamOwner = false;
257+
s.Close();
258+
259+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
260+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
261+
262+
}
263+
264+
[Test]
265+
[Category("Base")]
266+
public void InflatorStreamOwnership()
267+
{
268+
MemoryStreamEx memStream = new MemoryStreamEx();
269+
InflaterInputStream s = new InflaterInputStream(memStream);
270+
271+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
272+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
273+
274+
s.Close();
275+
276+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
277+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
278+
279+
memStream = new MemoryStreamEx();
280+
s = new InflaterInputStream(memStream);
281+
282+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
283+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
284+
285+
s.IsStreamOwner = false;
286+
s.Close();
287+
288+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
289+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
290+
291+
}
292+
233293
[Test]
234294
[Category("Base")]
235295
public void CloseInflatorWithNestedUsing()

tests/GZip/GZipTests.cs

Lines changed: 165 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using System.IO;
3+
using System.Threading;
34

45
using ICSharpCode.SharpZipLib.Zip.Compression;
56
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
67
using ICSharpCode.SharpZipLib.GZip;
78

89
using NUnit.Framework;
910

11+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
12+
1013
namespace ICSharpCode.SharpZipLib.Tests.GZip
1114
{
1215
/// <summary>
@@ -24,21 +27,22 @@ public void TestGZip()
2427
{
2528
MemoryStream ms = new MemoryStream();
2629
GZipOutputStream outStream = new GZipOutputStream(ms);
27-
30+
2831
byte[] buf = new byte[100000];
2932
System.Random rnd = new Random();
3033
rnd.NextBytes(buf);
31-
34+
3235
outStream.Write(buf, 0, buf.Length);
3336
outStream.Flush();
3437
outStream.Finish();
35-
38+
3639
ms.Seek(0, SeekOrigin.Begin);
37-
40+
3841
GZipInputStream inStream = new GZipInputStream(ms);
3942
byte[] buf2 = new byte[buf.Length];
40-
int currentIndex = 0;
41-
int count = buf2.Length;
43+
int currentIndex = 0;
44+
int count = buf2.Length;
45+
4246
while (true) {
4347
int numRead = inStream.Read(buf2, currentIndex, count);
4448
if (numRead <= 0) {
@@ -47,6 +51,8 @@ public void TestGZip()
4751
currentIndex += numRead;
4852
count -= numRead;
4953
}
54+
55+
Assert.AreEqual(0, count);
5056

5157
for (int i = 0; i < buf.Length; ++i) {
5258
Assert.AreEqual(buf2[i], buf[i]);
@@ -57,14 +63,16 @@ public void TestGZip()
5763
/// Writing GZip headers is delayed so that this stream can be used with HTTP/IIS.
5864
/// </summary>
5965
[Test]
66+
[Category("GZip")]
6067
public void DelayedHeaderWriteNoData()
6168
{
6269
MemoryStream ms = new MemoryStream();
6370
Assert.AreEqual(0, ms.Length);
64-
using (GZipOutputStream outStream = new GZipOutputStream(ms))
65-
{
71+
72+
using (GZipOutputStream outStream = new GZipOutputStream(ms)) {
6673
Assert.AreEqual(0, ms.Length);
6774
}
75+
6876
byte[] data = ms.ToArray();
6977

7078
Assert.IsTrue(data.Length > 0);
@@ -74,12 +82,12 @@ public void DelayedHeaderWriteNoData()
7482
/// Writing GZip headers is delayed so that this stream can be used with HTTP/IIS.
7583
/// </summary>
7684
[Test]
85+
[Category("GZip")]
7786
public void DelayedHeaderWriteWithData()
7887
{
7988
MemoryStream ms = new MemoryStream();
8089
Assert.AreEqual(0, ms.Length);
81-
using (GZipOutputStream outStream = new GZipOutputStream(ms))
82-
{
90+
using (GZipOutputStream outStream = new GZipOutputStream(ms)) {
8391
Assert.AreEqual(0, ms.Length);
8492
outStream.WriteByte(45);
8593

@@ -93,19 +101,163 @@ public void DelayedHeaderWriteWithData()
93101
}
94102

95103
[Test]
104+
[Category("GZip")]
96105
public void ZeroLengthInputStream()
97106
{
98107
GZipInputStream gzi = new GZipInputStream(new MemoryStream());
99108
bool exception = false;
100109
try {
101110
gzi.ReadByte();
102111
}
103-
catch
104-
{
112+
catch {
105113
exception = true;
106114
}
107-
115+
108116
Assert.IsTrue(exception, "reading from an empty stream should cause an exception");
109117
}
118+
119+
[Test]
120+
[Category("GZip")]
121+
public void OutputStreamOwnership()
122+
{
123+
MemoryStreamEx memStream = new MemoryStreamEx();
124+
GZipOutputStream s = new GZipOutputStream(memStream);
125+
126+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
127+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
128+
129+
s.Close();
130+
131+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
132+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
133+
134+
memStream = new MemoryStreamEx();
135+
s = new GZipOutputStream(memStream);
136+
137+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
138+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
139+
140+
s.IsStreamOwner = false;
141+
s.Close();
142+
143+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
144+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
145+
}
146+
147+
[Test]
148+
[Category("GZip")]
149+
public void InputStreamOwnership()
150+
{
151+
MemoryStreamEx memStream = new MemoryStreamEx();
152+
GZipInputStream s = new GZipInputStream(memStream);
153+
154+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
155+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
156+
157+
s.Close();
158+
159+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
160+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
161+
162+
memStream = new MemoryStreamEx();
163+
s = new GZipInputStream(memStream);
164+
165+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
166+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
167+
168+
s.IsStreamOwner = false;
169+
s.Close();
170+
171+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
172+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
173+
174+
}
175+
176+
[Test]
177+
[Category("GZip")]
178+
[Category("Long Running")]
179+
public void BigStream()
180+
{
181+
window_ = new WindowedStream(0x3ffff);
182+
outStream_ = new GZipOutputStream(window_);
183+
inStream_ = new GZipInputStream(window_);
184+
185+
long target = 0x10000000;
186+
readTarget_ = writeTarget_ = target;
187+
188+
Thread reader = new Thread(Reader);
189+
reader.Name = "Reader";
190+
reader.Start();
191+
192+
Thread writer = new Thread(Writer);
193+
writer.Name = "Writer";
194+
195+
DateTime startTime = DateTime.Now;
196+
writer.Start();
197+
198+
writer.Join();
199+
reader.Join();
200+
201+
DateTime endTime = DateTime.Now;
202+
203+
TimeSpan span = endTime - startTime;
204+
Console.WriteLine("Time {0} processes {1} KB/Sec", span, (target / 1024) / span.TotalSeconds);
205+
}
206+
207+
void Reader()
208+
{
209+
const int Size = 8192;
210+
int readBytes = 1;
211+
byte[] buffer = new byte[Size];
212+
213+
long passifierLevel = readTarget_ - 0x10000000;
214+
215+
while ( (readTarget_ > 0) && (readBytes > 0) ) {
216+
int count = Size;
217+
if (count > readTarget_) {
218+
count = (int)readTarget_;
219+
}
220+
221+
readBytes = inStream_.Read(buffer, 0, count);
222+
readTarget_ -= readBytes;
223+
224+
if (readTarget_ <= passifierLevel) {
225+
Console.WriteLine("Reader {0} bytes remaining", readTarget_);
226+
passifierLevel = readTarget_ - 0x10000000;
227+
}
228+
}
229+
230+
Assert.IsTrue(window_.IsClosed, "Window should be closed");
231+
232+
// This shouldnt read any data but should read the footer
233+
readBytes = inStream_.Read(buffer, 0, 1);
234+
Assert.AreEqual(0, readBytes, "Stream should be empty");
235+
Assert.AreEqual(0, window_.Length, "Window should be closed");
236+
inStream_.Close();
237+
}
238+
239+
void Writer()
240+
{
241+
const int Size = 8192;
242+
243+
byte[] buffer = new byte[Size];
244+
245+
while (writeTarget_ > 0) {
246+
int thisTime = Size;
247+
if (thisTime > writeTarget_) {
248+
thisTime = (int)writeTarget_;
249+
}
250+
251+
outStream_.Write(buffer, 0, thisTime);
252+
writeTarget_-= thisTime;
253+
}
254+
outStream_.Close();
255+
}
256+
257+
WindowedStream window_;
258+
GZipOutputStream outStream_;
259+
GZipInputStream inStream_;
260+
long readTarget_;
261+
long writeTarget_;
110262
}
111263
}

tests/SharpZipLibTests.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@
4343
<StartArguments>SharpZipLibTests.dll</StartArguments>
4444
</PropertyGroup>
4545
<ItemGroup>
46+
<Reference Include="nunit.framework, Version=2.4.1.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
47+
<SpecificVersion>False</SpecificVersion>
48+
<HintPath>..\..\..\..\..\..\..\..\..\Bin\Nunit-2.4\bin\nunit.framework.dll</HintPath>
49+
</Reference>
4650
<Reference Include="System" />
4751
<Reference Include="System.Data" />
4852
<Reference Include="System.Drawing" />
4953
<Reference Include="System.Windows.Forms" />
5054
<Reference Include="System.Xml" />
51-
<Reference Include="nunit.framework">
52-
<HintPath>..\Nunit\nunit.framework.dll</HintPath>
53-
<Private>True</Private>
54-
</Reference>
5555
</ItemGroup>
5656
<ItemGroup>
5757
<Compile Include="AssemblyInfo.cs" />
58+
<Compile Include="TestSupport\RingBuffer.cs" />
59+
<Compile Include="TestSupport\Streams.cs" />
5860
<Compile Include="Zip\ZipTests.cs" />
5961
<Compile Include="BZip2\Bzip2Tests.cs" />
6062
<Compile Include="GZip\GZipTests.cs" />
@@ -67,8 +69,5 @@
6769
<Name>ICSharpCode.SharpZLib</Name>
6870
</ProjectReference>
6971
</ItemGroup>
70-
<ItemGroup>
71-
<Folder Include="Support\" />
72-
</ItemGroup>
7372
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
7473
</Project>

0 commit comments

Comments
 (0)