kusano fc6ab3
//
kusano fc6ab3
// © Copyright Henrik Ravn 2004
kusano fc6ab3
//
kusano fc6ab3
// Use, modification and distribution are subject to the Boost Software License, Version 1.0.
kusano fc6ab3
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
kusano fc6ab3
//
kusano fc6ab3
kusano fc6ab3
using System;
kusano fc6ab3
using System.Collections;
kusano fc6ab3
using System.IO;
kusano fc6ab3
kusano fc6ab3
// uncomment the define below to include unit tests
kusano fc6ab3
//#define nunit
kusano fc6ab3
#if nunit
kusano fc6ab3
using NUnit.Framework;
kusano fc6ab3
kusano fc6ab3
// Unit tests for the DotZLib class library
kusano fc6ab3
// ----------------------------------------
kusano fc6ab3
//
kusano fc6ab3
// Use this with NUnit 2 from http://www.nunit.org
kusano fc6ab3
//
kusano fc6ab3
kusano fc6ab3
namespace DotZLibTests
kusano fc6ab3
{
kusano fc6ab3
    using DotZLib;
kusano fc6ab3
kusano fc6ab3
    // helper methods
kusano fc6ab3
    internal class Utils
kusano fc6ab3
    {
kusano fc6ab3
        public static bool byteArrEqual( byte[] lhs, byte[] rhs )
kusano fc6ab3
        {
kusano fc6ab3
            if (lhs.Length != rhs.Length)
kusano fc6ab3
                return false;
kusano fc6ab3
            for (int i = lhs.Length-1; i >= 0; --i)
kusano fc6ab3
                if (lhs[i] != rhs[i])
kusano fc6ab3
                    return false;
kusano fc6ab3
            return true;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
    [TestFixture]
kusano fc6ab3
    public class CircBufferTests
kusano fc6ab3
    {
kusano fc6ab3
        #region Circular buffer tests
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void SinglePutGet()
kusano fc6ab3
        {
kusano fc6ab3
            CircularBuffer buf = new CircularBuffer(10);
kusano fc6ab3
            Assert.AreEqual( 0, buf.Size );
kusano fc6ab3
            Assert.AreEqual( -1, buf.Get() );
kusano fc6ab3
kusano fc6ab3
            Assert.IsTrue(buf.Put( 1 ));
kusano fc6ab3
            Assert.AreEqual( 1, buf.Size );
kusano fc6ab3
            Assert.AreEqual( 1, buf.Get() );
kusano fc6ab3
            Assert.AreEqual( 0, buf.Size );
kusano fc6ab3
            Assert.AreEqual( -1, buf.Get() );
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void BlockPutGet()
kusano fc6ab3
        {
kusano fc6ab3
            CircularBuffer buf = new CircularBuffer(10);
kusano fc6ab3
            byte[] arr = {1,2,3,4,5,6,7,8,9,10};
kusano fc6ab3
            Assert.AreEqual( 10, buf.Put(arr,0,10) );
kusano fc6ab3
            Assert.AreEqual( 10, buf.Size );
kusano fc6ab3
            Assert.IsFalse( buf.Put(11) );
kusano fc6ab3
            Assert.AreEqual( 1, buf.Get() );
kusano fc6ab3
            Assert.IsTrue( buf.Put(11) );
kusano fc6ab3
kusano fc6ab3
            byte[] arr2 = (byte[])arr.Clone();
kusano fc6ab3
            Assert.AreEqual( 9, buf.Get(arr2,1,9) );
kusano fc6ab3
            Assert.IsTrue( Utils.byteArrEqual(arr,arr2) );
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        #endregion
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    [TestFixture]
kusano fc6ab3
    public class ChecksumTests
kusano fc6ab3
    {
kusano fc6ab3
        #region CRC32 Tests
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void CRC32_Null()
kusano fc6ab3
        {
kusano fc6ab3
            CRC32Checksum crc32 = new CRC32Checksum();
kusano fc6ab3
            Assert.AreEqual( 0, crc32.Value );
kusano fc6ab3
kusano fc6ab3
            crc32 = new CRC32Checksum(1);
kusano fc6ab3
            Assert.AreEqual( 1, crc32.Value );
kusano fc6ab3
kusano fc6ab3
            crc32 = new CRC32Checksum(556);
kusano fc6ab3
            Assert.AreEqual( 556, crc32.Value );
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void CRC32_Data()
kusano fc6ab3
        {
kusano fc6ab3
            CRC32Checksum crc32 = new CRC32Checksum();
kusano fc6ab3
            byte[] data = { 1,2,3,4,5,6,7 };
kusano fc6ab3
            crc32.Update(data);
kusano fc6ab3
            Assert.AreEqual( 0x70e46888, crc32.Value  );
kusano fc6ab3
kusano fc6ab3
            crc32 = new CRC32Checksum();
kusano fc6ab3
            crc32.Update("penguin");
kusano fc6ab3
            Assert.AreEqual( 0x0e5c1a120, crc32.Value );
kusano fc6ab3
kusano fc6ab3
            crc32 = new CRC32Checksum(1);
kusano fc6ab3
            crc32.Update("penguin");
kusano fc6ab3
            Assert.AreEqual(0x43b6aa94, crc32.Value);
kusano fc6ab3
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
kusano fc6ab3
        #region Adler tests
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Adler_Null()
kusano fc6ab3
        {
kusano fc6ab3
            AdlerChecksum adler = new AdlerChecksum();
kusano fc6ab3
            Assert.AreEqual(0, adler.Value);
kusano fc6ab3
kusano fc6ab3
            adler = new AdlerChecksum(1);
kusano fc6ab3
            Assert.AreEqual( 1, adler.Value );
kusano fc6ab3
kusano fc6ab3
            adler = new AdlerChecksum(556);
kusano fc6ab3
            Assert.AreEqual( 556, adler.Value );
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Adler_Data()
kusano fc6ab3
        {
kusano fc6ab3
            AdlerChecksum adler = new AdlerChecksum(1);
kusano fc6ab3
            byte[] data = { 1,2,3,4,5,6,7 };
kusano fc6ab3
            adler.Update(data);
kusano fc6ab3
            Assert.AreEqual( 0x5b001d, adler.Value  );
kusano fc6ab3
kusano fc6ab3
            adler = new AdlerChecksum();
kusano fc6ab3
            adler.Update("penguin");
kusano fc6ab3
            Assert.AreEqual(0x0bcf02f6, adler.Value );
kusano fc6ab3
kusano fc6ab3
            adler = new AdlerChecksum(1);
kusano fc6ab3
            adler.Update("penguin");
kusano fc6ab3
            Assert.AreEqual(0x0bd602f7, adler.Value);
kusano fc6ab3
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    [TestFixture]
kusano fc6ab3
    public class InfoTests
kusano fc6ab3
    {
kusano fc6ab3
        #region Info tests
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Info_Version()
kusano fc6ab3
        {
kusano fc6ab3
            Info info = new Info();
kusano fc6ab3
            Assert.AreEqual("1.2.8", Info.Version);
kusano fc6ab3
            Assert.AreEqual(32, info.SizeOfUInt);
kusano fc6ab3
            Assert.AreEqual(32, info.SizeOfULong);
kusano fc6ab3
            Assert.AreEqual(32, info.SizeOfPointer);
kusano fc6ab3
            Assert.AreEqual(32, info.SizeOfOffset);
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    [TestFixture]
kusano fc6ab3
    public class DeflateInflateTests
kusano fc6ab3
    {
kusano fc6ab3
        #region Deflate tests
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Deflate_Init()
kusano fc6ab3
        {
kusano fc6ab3
            using (Deflater def = new Deflater(CompressLevel.Default))
kusano fc6ab3
            {
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        private ArrayList compressedData = new ArrayList();
kusano fc6ab3
        private uint adler1;
kusano fc6ab3
kusano fc6ab3
        private ArrayList uncompressedData = new ArrayList();
kusano fc6ab3
        private uint adler2;
kusano fc6ab3
kusano fc6ab3
        public void CDataAvail(byte[] data, int startIndex, int count)
kusano fc6ab3
        {
kusano fc6ab3
            for (int i = 0; i < count; ++i)
kusano fc6ab3
                compressedData.Add(data[i+startIndex]);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Deflate_Compress()
kusano fc6ab3
        {
kusano fc6ab3
            compressedData.Clear();
kusano fc6ab3
kusano fc6ab3
            byte[] testData = new byte[35000];
kusano fc6ab3
            for (int i = 0; i < testData.Length; ++i)
kusano fc6ab3
                testData[i] = 5;
kusano fc6ab3
kusano fc6ab3
            using (Deflater def = new Deflater((CompressLevel)5))
kusano fc6ab3
            {
kusano fc6ab3
                def.DataAvailable += new DataAvailableHandler(CDataAvail);
kusano fc6ab3
                def.Add(testData);
kusano fc6ab3
                def.Finish();
kusano fc6ab3
                adler1 = def.Checksum;
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
kusano fc6ab3
        #region Inflate tests
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Inflate_Init()
kusano fc6ab3
        {
kusano fc6ab3
            using (Inflater inf = new Inflater())
kusano fc6ab3
            {
kusano fc6ab3
            }
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        private void DDataAvail(byte[] data, int startIndex, int count)
kusano fc6ab3
        {
kusano fc6ab3
            for (int i = 0; i < count; ++i)
kusano fc6ab3
                uncompressedData.Add(data[i+startIndex]);
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void Inflate_Expand()
kusano fc6ab3
        {
kusano fc6ab3
            uncompressedData.Clear();
kusano fc6ab3
kusano fc6ab3
            using (Inflater inf = new Inflater())
kusano fc6ab3
            {
kusano fc6ab3
                inf.DataAvailable += new DataAvailableHandler(DDataAvail);
kusano fc6ab3
                inf.Add((byte[])compressedData.ToArray(typeof(byte)));
kusano fc6ab3
                inf.Finish();
kusano fc6ab3
                adler2 = inf.Checksum;
kusano fc6ab3
            }
kusano fc6ab3
            Assert.AreEqual( adler1, adler2 );
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    [TestFixture]
kusano fc6ab3
    public class GZipStreamTests
kusano fc6ab3
    {
kusano fc6ab3
        #region GZipStream test
kusano fc6ab3
        [Test]
kusano fc6ab3
        public void GZipStream_WriteRead()
kusano fc6ab3
        {
kusano fc6ab3
            using (GZipStream gzOut = new GZipStream("gzstream.gz", CompressLevel.Best))
kusano fc6ab3
            {
kusano fc6ab3
                BinaryWriter writer = new BinaryWriter(gzOut);
kusano fc6ab3
                writer.Write("hi there");
kusano fc6ab3
                writer.Write(Math.PI);
kusano fc6ab3
                writer.Write(42);
kusano fc6ab3
            }
kusano fc6ab3
kusano fc6ab3
            using (GZipStream gzIn = new GZipStream("gzstream.gz"))
kusano fc6ab3
            {
kusano fc6ab3
                BinaryReader reader = new BinaryReader(gzIn);
kusano fc6ab3
                string s = reader.ReadString();
kusano fc6ab3
                Assert.AreEqual("hi there",s);
kusano fc6ab3
                double d = reader.ReadDouble();
kusano fc6ab3
                Assert.AreEqual(Math.PI, d);
kusano fc6ab3
                int i = reader.ReadInt32();
kusano fc6ab3
                Assert.AreEqual(42,i);
kusano fc6ab3
            }
kusano fc6ab3
kusano fc6ab3
        }
kusano fc6ab3
        #endregion
kusano fc6ab3
	}
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#endif