2 // © Copyright Henrik Ravn 2004
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)
9 using System
.Collections
;
12 // uncomment the define below to include unit tests
15 using NUnit
.Framework
;
17 // Unit tests for the DotZLib class library
18 // ----------------------------------------
20 // Use this with NUnit 2 from http://www.nunit.org
23 namespace DotZLibTests
30 public static bool byteArrEqual( byte[] lhs
, byte[] rhs
)
32 if (lhs
.Length
!= rhs
.Length
)
34 for (int i
= lhs
.Length
-1; i
>= 0; --i
)
44 public class CircBufferTests
46 #region Circular buffer tests
48 public void SinglePutGet()
50 CircularBuffer buf
= new CircularBuffer(10);
51 Assert
.AreEqual( 0, buf
.Size
);
52 Assert
.AreEqual( -1, buf
.Get() );
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() );
62 public void BlockPutGet()
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) );
72 byte[] arr2
= (byte[])arr
.Clone();
73 Assert
.AreEqual( 9, buf
.Get(arr2
,1,9) );
74 Assert
.IsTrue( Utils
.byteArrEqual(arr
,arr2
) );
81 public class ChecksumTests
85 public void CRC32_Null()
87 CRC32Checksum crc32
= new CRC32Checksum();
88 Assert
.AreEqual( 0, crc32
.Value
);
90 crc32
= new CRC32Checksum(1);
91 Assert
.AreEqual( 1, crc32
.Value
);
93 crc32
= new CRC32Checksum(556);
94 Assert
.AreEqual( 556, crc32
.Value
);
98 public void CRC32_Data()
100 CRC32Checksum crc32
= new CRC32Checksum();
101 byte[] data
= { 1,2,3,4,5,6,7 }
;
103 Assert
.AreEqual( 0x70e46888, crc32
.Value
);
105 crc32
= new CRC32Checksum();
106 crc32
.Update("penguin");
107 Assert
.AreEqual( 0x0e5c1a120, crc32
.Value
);
109 crc32
= new CRC32Checksum(1);
110 crc32
.Update("penguin");
111 Assert
.AreEqual(0x43b6aa94, crc32
.Value
);
119 public void Adler_Null()
121 AdlerChecksum adler
= new AdlerChecksum();
122 Assert
.AreEqual(0, adler
.Value
);
124 adler
= new AdlerChecksum(1);
125 Assert
.AreEqual( 1, adler
.Value
);
127 adler
= new AdlerChecksum(556);
128 Assert
.AreEqual( 556, adler
.Value
);
132 public void Adler_Data()
134 AdlerChecksum adler
= new AdlerChecksum(1);
135 byte[] data
= { 1,2,3,4,5,6,7 }
;
137 Assert
.AreEqual( 0x5b001d, adler
.Value
);
139 adler
= new AdlerChecksum();
140 adler
.Update("penguin");
141 Assert
.AreEqual(0x0bcf02f6, adler
.Value
);
143 adler
= new AdlerChecksum(1);
144 adler
.Update("penguin");
145 Assert
.AreEqual(0x0bd602f7, adler
.Value
);
152 public class InfoTests
156 public void Info_Version()
158 Info info
= new Info();
159 Assert
.AreEqual("1.2.11", 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
);
169 public class DeflateInflateTests
171 #region Deflate tests
173 public void Deflate_Init()
175 using (Deflater def
= new Deflater(CompressLevel
.Default
))
180 private ArrayList compressedData
= new ArrayList();
183 private ArrayList uncompressedData
= new ArrayList();
186 public void CDataAvail(byte[] data
, int startIndex
, int count
)
188 for (int i
= 0; i
< count
; ++i
)
189 compressedData
.Add(data
[i
+startIndex
]);
193 public void Deflate_Compress()
195 compressedData
.Clear();
197 byte[] testData
= new byte[35000];
198 for (int i
= 0; i
< testData
.Length
; ++i
)
201 using (Deflater def
= new Deflater((CompressLevel
)5))
203 def
.DataAvailable
+= new DataAvailableHandler(CDataAvail
);
206 adler1
= def
.Checksum
;
211 #region Inflate tests
213 public void Inflate_Init()
215 using (Inflater inf
= new Inflater())
220 private void DDataAvail(byte[] data
, int startIndex
, int count
)
222 for (int i
= 0; i
< count
; ++i
)
223 uncompressedData
.Add(data
[i
+startIndex
]);
227 public void Inflate_Expand()
229 uncompressedData
.Clear();
231 using (Inflater inf
= new Inflater())
233 inf
.DataAvailable
+= new DataAvailableHandler(DDataAvail
);
234 inf
.Add((byte[])compressedData
.ToArray(typeof(byte)));
236 adler2
= inf
.Checksum
;
238 Assert
.AreEqual( adler1
, adler2
);
244 public class GZipStreamTests
246 #region GZipStream test
248 public void GZipStream_WriteRead()
250 using (GZipStream gzOut
= new GZipStream("gzstream.gz", CompressLevel
.Best
))
252 BinaryWriter writer
= new BinaryWriter(gzOut
);
253 writer
.Write("hi there");
254 writer
.Write(Math
.PI
);
258 using (GZipStream gzIn
= new GZipStream("gzstream.gz"))
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
);