1 | //
|
---|
2 | // © Copyright Henrik Ravn 2004
|
---|
3 | //
|
---|
4 | // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
|
---|
5 | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
---|
6 | //
|
---|
7 |
|
---|
8 | using System;
|
---|
9 | using System.Collections;
|
---|
10 | using System.IO;
|
---|
11 |
|
---|
12 | // uncomment the define below to include unit tests
|
---|
13 | //#define nunit
|
---|
14 | #if nunit
|
---|
15 | using NUnit.Framework;
|
---|
16 |
|
---|
17 | // Unit tests for the DotZLib class library
|
---|
18 | // ----------------------------------------
|
---|
19 | //
|
---|
20 | // Use this with NUnit 2 from http://www.nunit.org
|
---|
21 | //
|
---|
22 |
|
---|
23 | namespace DotZLibTests
|
---|
24 | {
|
---|
25 | using DotZLib;
|
---|
26 |
|
---|
27 | // helper methods
|
---|
28 | internal class Utils
|
---|
29 | {
|
---|
30 | public static bool byteArrEqual( byte[] lhs, byte[] rhs )
|
---|
31 | {
|
---|
32 | if (lhs.Length != rhs.Length)
|
---|
33 | return false;
|
---|
34 | for (int i = lhs.Length-1; i >= 0; --i)
|
---|
35 | if (lhs[i] != rhs[i])
|
---|
36 | return false;
|
---|
37 | return true;
|
---|
38 | }
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | [TestFixture]
|
---|
44 | public class CircBufferTests
|
---|
45 | {
|
---|
46 | #region Circular buffer tests
|
---|
47 | [Test]
|
---|
48 | public void SinglePutGet()
|
---|
49 | {
|
---|
50 | CircularBuffer buf = new CircularBuffer(10);
|
---|
51 | Assert.AreEqual( 0, buf.Size );
|
---|
52 | Assert.AreEqual( -1, buf.Get() );
|
---|
53 |
|
---|
54 | Assert.IsTrue(buf.Put( 1 ));
|
---|
55 | Assert.AreEqual( 1, buf.Size );
|
---|
56 | Assert.AreEqual( 1, buf.Get() );
|
---|
57 | Assert.AreEqual( 0, buf.Size );
|
---|
58 | Assert.AreEqual( -1, buf.Get() );
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Test]
|
---|
62 | public void BlockPutGet()
|
---|
63 | {
|
---|
64 | CircularBuffer buf = new CircularBuffer(10);
|
---|
65 | byte[] arr = {1,2,3,4,5,6,7,8,9,10};
|
---|
66 | Assert.AreEqual( 10, buf.Put(arr,0,10) );
|
---|
67 | Assert.AreEqual( 10, buf.Size );
|
---|
68 | Assert.IsFalse( buf.Put(11) );
|
---|
69 | Assert.AreEqual( 1, buf.Get() );
|
---|
70 | Assert.IsTrue( buf.Put(11) );
|
---|
71 |
|
---|
72 | byte[] arr2 = (byte[])arr.Clone();
|
---|
73 | Assert.AreEqual( 9, buf.Get(arr2,1,9) );
|
---|
74 | Assert.IsTrue( Utils.byteArrEqual(arr,arr2) );
|
---|
75 | }
|
---|
76 |
|
---|
77 | #endregion
|
---|
78 | }
|
---|
79 |
|
---|
80 | [TestFixture]
|
---|
81 | public class ChecksumTests
|
---|
82 | {
|
---|
83 | #region CRC32 Tests
|
---|
84 | [Test]
|
---|
85 | public void CRC32_Null()
|
---|
86 | {
|
---|
87 | CRC32Checksum crc32 = new CRC32Checksum();
|
---|
88 | Assert.AreEqual( 0, crc32.Value );
|
---|
89 |
|
---|
90 | crc32 = new CRC32Checksum(1);
|
---|
91 | Assert.AreEqual( 1, crc32.Value );
|
---|
92 |
|
---|
93 | crc32 = new CRC32Checksum(556);
|
---|
94 | Assert.AreEqual( 556, crc32.Value );
|
---|
95 | }
|
---|
96 |
|
---|
97 | [Test]
|
---|
98 | public void CRC32_Data()
|
---|
99 | {
|
---|
100 | CRC32Checksum crc32 = new CRC32Checksum();
|
---|
101 | byte[] data = { 1,2,3,4,5,6,7 };
|
---|
102 | crc32.Update(data);
|
---|
103 | Assert.AreEqual( 0x70e46888, crc32.Value );
|
---|
104 |
|
---|
105 | crc32 = new CRC32Checksum();
|
---|
106 | crc32.Update("penguin");
|
---|
107 | Assert.AreEqual( 0x0e5c1a120, crc32.Value );
|
---|
108 |
|
---|
109 | crc32 = new CRC32Checksum(1);
|
---|
110 | crc32.Update("penguin");
|
---|
111 | Assert.AreEqual(0x43b6aa94, crc32.Value);
|
---|
112 |
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | #region Adler tests
|
---|
117 |
|
---|
118 | [Test]
|
---|
119 | public void Adler_Null()
|
---|
120 | {
|
---|
121 | AdlerChecksum adler = new AdlerChecksum();
|
---|
122 | Assert.AreEqual(0, adler.Value);
|
---|
123 |
|
---|
124 | adler = new AdlerChecksum(1);
|
---|
125 | Assert.AreEqual( 1, adler.Value );
|
---|
126 |
|
---|
127 | adler = new AdlerChecksum(556);
|
---|
128 | Assert.AreEqual( 556, adler.Value );
|
---|
129 | }
|
---|
130 |
|
---|
131 | [Test]
|
---|
132 | public void Adler_Data()
|
---|
133 | {
|
---|
134 | AdlerChecksum adler = new AdlerChecksum(1);
|
---|
135 | byte[] data = { 1,2,3,4,5,6,7 };
|
---|
136 | adler.Update(data);
|
---|
137 | Assert.AreEqual( 0x5b001d, adler.Value );
|
---|
138 |
|
---|
139 | adler = new AdlerChecksum();
|
---|
140 | adler.Update("penguin");
|
---|
141 | Assert.AreEqual(0x0bcf02f6, adler.Value );
|
---|
142 |
|
---|
143 | adler = new AdlerChecksum(1);
|
---|
144 | adler.Update("penguin");
|
---|
145 | Assert.AreEqual(0x0bd602f7, adler.Value);
|
---|
146 |
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 | }
|
---|
150 |
|
---|
151 | [TestFixture]
|
---|
152 | public class InfoTests
|
---|
153 | {
|
---|
154 | #region Info tests
|
---|
155 | [Test]
|
---|
156 | public void Info_Version()
|
---|
157 | {
|
---|
158 | Info info = new Info();
|
---|
159 | Assert.AreEqual("1.2.6", Info.Version);
|
---|
160 | Assert.AreEqual(32, info.SizeOfUInt);
|
---|
161 | Assert.AreEqual(32, info.SizeOfULong);
|
---|
162 | Assert.AreEqual(32, info.SizeOfPointer);
|
---|
163 | Assert.AreEqual(32, info.SizeOfOffset);
|
---|
164 | }
|
---|
165 | #endregion
|
---|
166 | }
|
---|
167 |
|
---|
168 | [TestFixture]
|
---|
169 | public class DeflateInflateTests
|
---|
170 | {
|
---|
171 | #region Deflate tests
|
---|
172 | [Test]
|
---|
173 | public void Deflate_Init()
|
---|
174 | {
|
---|
175 | using (Deflater def = new Deflater(CompressLevel.Default))
|
---|
176 | {
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | private ArrayList compressedData = new ArrayList();
|
---|
181 | private uint adler1;
|
---|
182 |
|
---|
183 | private ArrayList uncompressedData = new ArrayList();
|
---|
184 | private uint adler2;
|
---|
185 |
|
---|
186 | public void CDataAvail(byte[] data, int startIndex, int count)
|
---|
187 | {
|
---|
188 | for (int i = 0; i < count; ++i)
|
---|
189 | compressedData.Add(data[i+startIndex]);
|
---|
190 | }
|
---|
191 |
|
---|
192 | [Test]
|
---|
193 | public void Deflate_Compress()
|
---|
194 | {
|
---|
195 | compressedData.Clear();
|
---|
196 |
|
---|
197 | byte[] testData = new byte[35000];
|
---|
198 | for (int i = 0; i < testData.Length; ++i)
|
---|
199 | testData[i] = 5;
|
---|
200 |
|
---|
201 | using (Deflater def = new Deflater((CompressLevel)5))
|
---|
202 | {
|
---|
203 | def.DataAvailable += new DataAvailableHandler(CDataAvail);
|
---|
204 | def.Add(testData);
|
---|
205 | def.Finish();
|
---|
206 | adler1 = def.Checksum;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | #endregion
|
---|
210 |
|
---|
211 | #region Inflate tests
|
---|
212 | [Test]
|
---|
213 | public void Inflate_Init()
|
---|
214 | {
|
---|
215 | using (Inflater inf = new Inflater())
|
---|
216 | {
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void DDataAvail(byte[] data, int startIndex, int count)
|
---|
221 | {
|
---|
222 | for (int i = 0; i < count; ++i)
|
---|
223 | uncompressedData.Add(data[i+startIndex]);
|
---|
224 | }
|
---|
225 |
|
---|
226 | [Test]
|
---|
227 | public void Inflate_Expand()
|
---|
228 | {
|
---|
229 | uncompressedData.Clear();
|
---|
230 |
|
---|
231 | using (Inflater inf = new Inflater())
|
---|
232 | {
|
---|
233 | inf.DataAvailable += new DataAvailableHandler(DDataAvail);
|
---|
234 | inf.Add((byte[])compressedData.ToArray(typeof(byte)));
|
---|
235 | inf.Finish();
|
---|
236 | adler2 = inf.Checksum;
|
---|
237 | }
|
---|
238 | Assert.AreEqual( adler1, adler2 );
|
---|
239 | }
|
---|
240 | #endregion
|
---|
241 | }
|
---|
242 |
|
---|
243 | [TestFixture]
|
---|
244 | public class GZipStreamTests
|
---|
245 | {
|
---|
246 | #region GZipStream test
|
---|
247 | [Test]
|
---|
248 | public void GZipStream_WriteRead()
|
---|
249 | {
|
---|
250 | using (GZipStream gzOut = new GZipStream("gzstream.gz", CompressLevel.Best))
|
---|
251 | {
|
---|
252 | BinaryWriter writer = new BinaryWriter(gzOut);
|
---|
253 | writer.Write("hi there");
|
---|
254 | writer.Write(Math.PI);
|
---|
255 | writer.Write(42);
|
---|
256 | }
|
---|
257 |
|
---|
258 | using (GZipStream gzIn = new GZipStream("gzstream.gz"))
|
---|
259 | {
|
---|
260 | BinaryReader reader = new BinaryReader(gzIn);
|
---|
261 | string s = reader.ReadString();
|
---|
262 | Assert.AreEqual("hi there",s);
|
---|
263 | double d = reader.ReadDouble();
|
---|
264 | Assert.AreEqual(Math.PI, d);
|
---|
265 | int i = reader.ReadInt32();
|
---|
266 | Assert.AreEqual(42,i);
|
---|
267 | }
|
---|
268 |
|
---|
269 | }
|
---|
270 | #endregion
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | #endif
|
---|