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