UnitTests.cs 7.5 KB

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