root / trunk / hammock / src / net35 / ICSharpCode.SharpZipLib.Silverlight.Tests / Base / InflaterDeflaterTests.cs @ 0eea575a
History | View | Annotate | Download (8.2 kB)
1 |
using System; |
---|---|
2 |
using System.IO; |
3 |
using System.Text; |
4 |
using System.Security; |
5 |
using ICSharpCode.SharpZipLib.Silverlight.Zip.Compression; |
6 |
using ICSharpCode.SharpZipLib.Silverlight.Zip.Compression.Streams; |
7 |
using NUnit.Framework; |
8 |
|
9 |
using ICSharpCode.SharpZipLib.Tests.TestSupport; |
10 |
|
11 |
namespace ICSharpCode.SharpZipLib.Tests.Base |
12 |
{ |
13 |
/// <summary> |
14 |
/// This class contains test cases for Deflater/Inflater streams. |
15 |
/// </summary> |
16 |
[TestFixture] |
17 |
public class InflaterDeflaterTestSuite |
18 |
{ |
19 |
void Inflate(MemoryStream ms, byte[] original, int level, bool zlib) |
20 |
{ |
21 |
ms.Seek(0, SeekOrigin.Begin); |
22 |
|
23 |
Inflater inflater = new Inflater(!zlib); |
24 |
InflaterInputStream inStream = new InflaterInputStream(ms, inflater); |
25 |
byte[] buf2 = new byte[original.Length]; |
26 |
|
27 |
int currentIndex = 0; |
28 |
int count = buf2.Length; |
29 |
|
30 |
try |
31 |
{ |
32 |
while (true) |
33 |
{ |
34 |
int numRead = inStream.Read(buf2, currentIndex, count); |
35 |
if (numRead <= 0) |
36 |
{ |
37 |
break; |
38 |
} |
39 |
currentIndex += numRead; |
40 |
count -= numRead; |
41 |
} |
42 |
} |
43 |
catch(Exception ex) |
44 |
{ |
45 |
Console.WriteLine("Unexpected exception - '{0}'", ex.Message); |
46 |
throw; |
47 |
} |
48 |
|
49 |
if ( currentIndex != original.Length ) |
50 |
{ |
51 |
Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex); |
52 |
Assert.Fail("Lengths different"); |
53 |
} |
54 |
|
55 |
for (int i = 0; i < original.Length; ++i) |
56 |
{ |
57 |
if ( buf2[i] != original[i] ) |
58 |
{ |
59 |
string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib); |
60 |
if ( original.Length < 2048 ) |
61 |
{ |
62 |
StringBuilder builder = new StringBuilder(description); |
63 |
for (int d = 0; d < original.Length; ++d) |
64 |
{ |
65 |
builder.AppendFormat("{0} ", original[d]); |
66 |
} |
67 |
|
68 |
Assert.Fail(builder.ToString()); |
69 |
} |
70 |
else |
71 |
{ |
72 |
Assert.Fail(description); |
73 |
} |
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
MemoryStream Deflate(byte[] data, int level, bool zlib) |
79 |
{ |
80 |
MemoryStream memoryStream = new MemoryStream(); |
81 |
|
82 |
Deflater deflater = new Deflater(level, !zlib); |
83 |
using ( DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater) ) |
84 |
{ |
85 |
outStream.IsStreamOwner = false; |
86 |
outStream.Write(data, 0, data.Length); |
87 |
outStream.Flush(); |
88 |
outStream.Finish(); |
89 |
} |
90 |
return memoryStream; |
91 |
} |
92 |
|
93 |
void RandomDeflateInflate(int size, int level, bool zlib) |
94 |
{ |
95 |
byte[] buffer = new byte[size]; |
96 |
System.Random rnd = new Random(); |
97 |
rnd.NextBytes(buffer); |
98 |
|
99 |
MemoryStream ms = Deflate(buffer, level, zlib); |
100 |
Inflate(ms, buffer, level, zlib); |
101 |
} |
102 |
|
103 |
/// <summary> |
104 |
/// Basic inflate/deflate test |
105 |
/// </summary> |
106 |
[Test] |
107 |
[Category("Base")] |
108 |
public void InflateDeflateZlib() |
109 |
{ |
110 |
for (int level = 0; level < 10; ++level) |
111 |
{ |
112 |
RandomDeflateInflate(100000, level, true); |
113 |
} |
114 |
} |
115 |
|
116 |
delegate void RunCompress(byte[] buffer); |
117 |
|
118 |
int runLevel; |
119 |
bool runZlib; |
120 |
long runCount = 0; |
121 |
Random runRandom = new Random(5); |
122 |
|
123 |
void DeflateAndInflate(byte[] buffer) |
124 |
{ |
125 |
++runCount; |
126 |
MemoryStream ms = Deflate(buffer, runLevel, runZlib); |
127 |
Inflate(ms, buffer, runLevel, runZlib); |
128 |
} |
129 |
|
130 |
void TryVariants(RunCompress test, byte[] buffer, int index) |
131 |
{ |
132 |
int worker = 0; |
133 |
while ( worker <= 255 ) |
134 |
{ |
135 |
buffer[index] = (byte)worker; |
136 |
if ( index < buffer.Length - 1) |
137 |
{ |
138 |
TryVariants(test, buffer, index + 1); |
139 |
} |
140 |
else |
141 |
{ |
142 |
test(buffer); |
143 |
} |
144 |
|
145 |
worker += runRandom.Next(256); |
146 |
} |
147 |
} |
148 |
|
149 |
void TryManyVariants(int level, bool zlib, RunCompress test, byte[] buffer) |
150 |
{ |
151 |
runLevel = level; |
152 |
runZlib = zlib; |
153 |
TryVariants(test, buffer, 0); |
154 |
} |
155 |
|
156 |
|
157 |
[Test] |
158 |
[Category("Base")] |
159 |
public void SmallBlocks() |
160 |
{ |
161 |
byte[] buffer = new byte[10]; |
162 |
Array.Clear(buffer, 0, buffer.Length); |
163 |
TryManyVariants(0, false, new RunCompress(DeflateAndInflate), buffer); |
164 |
} |
165 |
|
166 |
[Test] |
167 |
[Explicit] |
168 |
[Category("Base")] |
169 |
[Category("ExcludeFromAutoBuild")] |
170 |
public void FindBug() |
171 |
{ |
172 |
using ( FileStream fs = File.OpenRead("c:\\tmp\\original.dat") ) |
173 |
{ |
174 |
long readOffset = 0; |
175 |
long readLength = fs.Length - readOffset; |
176 |
// readLength -= 5567; // 5568 works 5567 doesnt.... |
177 |
|
178 |
fs.Seek(readOffset, SeekOrigin.Begin); |
179 |
|
180 |
byte[] original = new byte[readLength]; |
181 |
|
182 |
int bytesRead = fs.Read(original, 0, original.Length); |
183 |
Assert.AreEqual(bytesRead, original.Length); |
184 |
|
185 |
MemoryStream ms = Deflate(original, Deflater.BEST_SPEED, true); |
186 |
Inflate(ms, original, Deflater.BEST_SPEED, true); |
187 |
} |
188 |
} |
189 |
|
190 |
/// <summary> |
191 |
/// Basic inflate/deflate test |
192 |
/// </summary> |
193 |
[Test] |
194 |
[Category("Base")] |
195 |
public void InflateDeflateNonZlib() |
196 |
{ |
197 |
for (int level = 0; level < 10; ++level) |
198 |
{ |
199 |
RandomDeflateInflate(100000, level, false); |
200 |
} |
201 |
} |
202 |
|
203 |
[Test] |
204 |
[Category("Base")] |
205 |
public void CloseDeflatorWithNestedUsing() |
206 |
{ |
207 |
string tempFile = null; |
208 |
try |
209 |
{ |
210 |
tempFile = Path.GetTempPath(); |
211 |
} |
212 |
catch (SecurityException) |
213 |
{ |
214 |
} |
215 |
|
216 |
Assert.IsNotNull(tempFile, "No permission to execute this test?"); |
217 |
if (tempFile != null) |
218 |
{ |
219 |
tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); |
220 |
using (FileStream diskFile = File.Create(tempFile)) |
221 |
using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) |
222 |
using (StreamWriter txtFile = new StreamWriter(deflator)) |
223 |
{ |
224 |
txtFile.Write("Hello"); |
225 |
txtFile.Flush(); |
226 |
} |
227 |
|
228 |
File.Delete(tempFile); |
229 |
} |
230 |
} |
231 |
|
232 |
[Test] |
233 |
[Category("Base")] |
234 |
public void DeflatorStreamOwnership() |
235 |
{ |
236 |
MemoryStreamEx memStream = new MemoryStreamEx(); |
237 |
DeflaterOutputStream s = new DeflaterOutputStream(memStream); |
238 |
|
239 |
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); |
240 |
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); |
241 |
|
242 |
s.Close(); |
243 |
|
244 |
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); |
245 |
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); |
246 |
|
247 |
memStream = new MemoryStreamEx(); |
248 |
s = new DeflaterOutputStream(memStream); |
249 |
|
250 |
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); |
251 |
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); |
252 |
|
253 |
s.IsStreamOwner = false; |
254 |
s.Close(); |
255 |
|
256 |
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); |
257 |
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); |
258 |
|
259 |
} |
260 |
|
261 |
[Test] |
262 |
[Category("Base")] |
263 |
public void InflatorStreamOwnership() |
264 |
{ |
265 |
MemoryStreamEx memStream = new MemoryStreamEx(); |
266 |
InflaterInputStream s = new InflaterInputStream(memStream); |
267 |
|
268 |
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); |
269 |
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); |
270 |
|
271 |
s.Close(); |
272 |
|
273 |
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); |
274 |
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); |
275 |
|
276 |
memStream = new MemoryStreamEx(); |
277 |
s = new InflaterInputStream(memStream); |
278 |
|
279 |
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); |
280 |
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); |
281 |
|
282 |
s.IsStreamOwner = false; |
283 |
s.Close(); |
284 |
|
285 |
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); |
286 |
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); |
287 |
|
288 |
} |
289 |
|
290 |
[Test] |
291 |
[Category("Base")] |
292 |
public void CloseInflatorWithNestedUsing() |
293 |
{ |
294 |
string tempFile = null; |
295 |
try |
296 |
{ |
297 |
tempFile = Path.GetTempPath(); |
298 |
} |
299 |
catch (SecurityException) |
300 |
{ |
301 |
} |
302 |
|
303 |
Assert.IsNotNull(tempFile, "No permission to execute this test?"); |
304 |
|
305 |
if (tempFile != null) |
306 |
{ |
307 |
tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); |
308 |
using (FileStream diskFile = File.Create(tempFile)) |
309 |
using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) |
310 |
using (StreamWriter textWriter = new StreamWriter(deflator)) |
311 |
{ |
312 |
textWriter.Write("Hello"); |
313 |
textWriter.Flush(); |
314 |
} |
315 |
|
316 |
using (FileStream diskFile = File.OpenRead(tempFile)) |
317 |
using (InflaterInputStream deflator = new InflaterInputStream(diskFile)) |
318 |
using (StreamReader textReader = new StreamReader(deflator)) |
319 |
{ |
320 |
char[] buffer = new char[5]; |
321 |
int readCount = textReader.Read(buffer, 0, 5); |
322 |
Assert.AreEqual(5, readCount); |
323 |
|
324 |
StringBuilder b = new StringBuilder(); |
325 |
b.Append(buffer); |
326 |
Assert.AreEqual("Hello", b.ToString()); |
327 |
|
328 |
} |
329 |
|
330 |
File.Delete(tempFile); |
331 |
} |
332 |
} |
333 |
} |
334 |
} |