Exemple #1
0
        public void TestStreamDecompression()
        {
            MemoryStream memStream = new MemoryStream();
            Random rnd = new Random();

            byte[] original;
            byte[] decompressed;

            bool match;

            PatternCompressor compressor = new PatternCompressor
            {
                CompressedBuffer = new byte[4 * TotalTestSampleSize],
                CompressionStrength = 31
            };

            PatternDecompressor decompressor = new PatternDecompressor();

            for (int i = 0; i < TotalTestSampleSize; i++)
            {
                uint value = (uint)(rnd.NextDouble() * 100000);
                memStream.Write(BitConverter.GetBytes(value), 0, 4);
                compressor.Compress(value);
            }

            original = memStream.ToArray();
            memStream = new MemoryStream();
            decompressor.AugmentBuffer(compressor.CompressedBuffer, compressor.CompressedBufferLength);

            for (int i = 0; i < TotalTestSampleSize; i++)
            {
                uint value;
                decompressor.Decompress(out value);
                memStream.Write(BitConverter.GetBytes(value), 0, 4);
            }

            decompressed = memStream.ToArray();
            match = original.Length == decompressed.Length;
            for (int i = 0; match && i < original.Length; i++)
            {
                match = original[i] == decompressed[i];
            }

            Assert.AreEqual(original.Length, decompressed.Length);
            Assert.IsTrue(match);
        }
Exemple #2
0
        public void TestDecompressionOfStreamCompression()
        {
            MemoryStream memStream = new MemoryStream();
            Random rnd = new Random();

            byte[] original;
            byte[] decompressed;
            int decompressedLen;

            bool match;

            PatternCompressor compressor = new PatternCompressor
                {
                    CompressedBuffer = new byte[4 * TotalTestSampleSize],
                    CompressionStrength = 31
                };

            for (int i = 0; i < TotalTestSampleSize; i++)
            {
                uint value = (uint)(rnd.NextDouble() * 100000);
                memStream.Write(BitConverter.GetBytes(value), 0, 4);
                compressor.Compress(value);
            }

            original = memStream.ToArray();
            decompressed = new byte[PatternDecompressor.MaximumSizeDecompressed(compressor.CompressedBufferLength)];
            Buffer.BlockCopy(compressor.CompressedBuffer, 0, decompressed, 0, compressor.CompressedBufferLength);
            decompressedLen = PatternDecompressor.DecompressBuffer(decompressed, 0, compressor.CompressedBufferLength, decompressed.Length);

            match = decompressedLen == original.Length;
            for (int i = 0; match && i < decompressedLen; i++)
            {
                match = decompressed[i] == original[i];
            }

            Assert.AreEqual(original.Length, decompressedLen);
            Assert.IsTrue(match);
        }
Exemple #3
0
        public void TestStreamCompression()
        {
            const int compressionStrength = 31;

            MemoryStream buffer = new MemoryStream();
            Random rnd = new Random();

            PatternCompressor compressor = new PatternCompressor
            {
                CompressedBuffer = new byte[4 * TotalTestSampleSize],
                CompressionStrength = compressionStrength
            };

            byte[] arrayOfInts;
            int bufferLength;
            int dataLength;
            int compressedLen;

            //bool match;

            for (int i = 0; i < TotalTestSampleSize; i++)
            {
                uint value = (uint)(rnd.NextDouble() * 100000);
                buffer.Write(BitConverter.GetBytes(value), 0, 4);
                compressor.Compress(value);
            }

            // Add one byte of extra space to accommodate compression algorithm
            buffer.WriteByte(0xFF);

            arrayOfInts = buffer.ToArray();
            bufferLength = arrayOfInts.Length;
            dataLength = bufferLength - 1;
            compressedLen = PatternCompressor.CompressBuffer(arrayOfInts, 0, dataLength, bufferLength, compressionStrength);

            // Compressed arrays do not match. This is because the streaming compression
            // searches the back buffer queue starting from index 0, regardless of the
            // index of the start of the queue. The static method searches from the start
            // of the queue and wraps around in a circular fashion. This discrepancy does
            // not affect decompression.
            //
            //match = compressedLen == compressor.CompressedBufferLength;
            //for (int i = 0; match && i < compressedLen; i++)
            //{
            //    match = arrayOfInts[i] == compressor.CompressedBuffer[i];
            //}

            Assert.AreEqual(compressedLen, compressor.CompressedBufferLength);
            //Assert.IsTrue(match);
        }