Exemplo n.º 1
0
        public void VerifyCompression_LargeCorpus()
        {
            var    stream     = new MemoryStream();
            var    writer     = new BitStreamWriter(stream, true);
            var    reader     = new BitStreamReader(stream, true);
            string input      = TestResources.RFC5_Text;
            var    state      = new TreeStateStore();
            var    compressor = new StaticHuffman <char>(CharacterFrequencies(input));

            compressor.WriteTable(state.WriteSymbol, state.WriteUInt32);

            foreach (char ch in input)
            {
                compressor.WriteCode(ch, writer.Write);
            }
            writer.Flush();

            state.Reset();

            var decompressor = new StaticHuffman <char>(state.ReadSymbol, state.ReadUInt32);

            stream.Position = 0;

            foreach (char ch in input)
            {
                Assert.AreEqual(ch, decompressor.GetSymbol(reader.ReadBoolean));
            }
        }
Exemplo n.º 2
0
        public void Should_WriteUe()
        {
            var stream      = new BitStream();
            var writer      = new BitStreamWriter(stream);
            var val         = 6300U;
            var bitsWritten = writer.WriteUe(val);

            writer.Flush();
            Assert.AreEqual(24, bitsWritten);
            var bytes = stream.ToArray();

            Assert.AreEqual(new byte[] { 0, 24, 185 }, bytes);
        }
Exemplo n.º 3
0
        public void Should_WriteInt48()
        {
            var stream = new BitStream();
            var writer = new BitStreamWriter(stream);
            var val    = Int48.MaxValue;

            writer.Write(val);
            writer.Flush();
            Assert.AreEqual(6, stream.Length);
            var bytes    = stream.ToArray();
            var valBits  = val.GetBits();
            var byteBits = bytes.GetBits(Int48.BitSize);

            CollectionAssert.AreEqual(valBits, byteBits);
        }
Exemplo n.º 4
0
        public void Should_WriteBit()
        {
            var stream = new BitStream();
            var writer = new BitStreamWriter(stream);

            writer.WriteBit(0);
            writer.WriteBit(1);
            writer.WriteBit(true);
            writer.WriteBit(new Bit(1));
            writer.Flush();
            Assert.AreEqual(1, stream.Length);
            var bytes = stream.ToArray();

            // we wrote 0111 binary, which is 14
            Assert.AreEqual(14, bytes[0]);
        }
Exemplo n.º 5
0
        public void Should_WriteBitsAndBytes()
        {
            var stream = new BitStream();

            stream.AllowUnalignedOperations = true;
            var writer = new BitStreamWriter(stream);
            var val    = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };

            writer.WriteBit(0);
            writer.WriteBit(1);
            writer.Write(val);
            writer.Flush();
            Assert.AreEqual(val.Length + 1, stream.Length);
            var bytes = stream.ToArray();

            CollectionAssert.AreNotEqual(val, bytes);
            CollectionAssert.AreEqual(new byte[] { 254, 7, 48, 171, 2 }, bytes);
        }
Exemplo n.º 6
0
        public void leaveOpen_False()
        {
            var stream = new MemoryStream();

            using (var writer = new BitStreamWriter(stream, false))
            {
                Assert.AreEqual(false, writer.CanRead);
                Assert.AreEqual(true, writer.CanWrite);
                Assert.AreEqual(true, writer.CanSeek);

                Assert.AreEqual(0, writer.Length);
                Assert.AreEqual(0, writer.BitLength);
                Assert.AreEqual(0, writer.BitPosition);
                Assert.AreEqual(0, writer.Position);

                writer.Write(true);

                Assert.AreEqual(1, writer.Length);
                Assert.AreEqual(1, writer.BitLength);
                Assert.AreEqual(1, writer.BitPosition);
                Assert.AreEqual(0, writer.Position);

                writer.Seek(0, SeekOrigin.Begin);

                Assert.AreEqual(1, stream.Length);
                Assert.AreEqual(0, stream.Position);
                Assert.AreEqual(0x80, stream.GetBuffer()[0]);

                writer.Write(false);
            }
            Assert.AreEqual(1, stream.Length);
            Assert.AreEqual(1, stream.Position);
            Assert.AreEqual(0, stream.GetBuffer()[0]);

            using (var writer = new BitStreamWriter(stream, false))
            {
                var data = new byte[] { 0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65 };
                writer.Write(data, 0, data.Length);
                writer.Flush();

                Assert.AreEqual(11, stream.Length);
                Assert.AreEqual(11, stream.Position);
            }
        }
Exemplo n.º 7
0
        public static byte[] Encode(byte[] aData)
        {
            using (MemoryStream data = new MemoryStream())
            {
                BitStreamWriter writer = new BitStreamWriter(data);
                var             leaves = Analyze(aData);
                var             tree   = GenerateTree(leaves);
                tree.Serialize(writer);
                WriteDynamicLength(writer, (ulong)aData.LongLength);
                var lookup = GenerateLookup(leaves);

                for (int i = 0; i < aData.Length; i++)
                {
                    var n = lookup[aData[i]];
                    writer.WriteBits(n.bitPattern, n.bitCount);
                }
                writer.Flush();
                return(data.ToArray());
            }
        }
Exemplo n.º 8
0
        public void VerifyCompression_astrachan_()
        {
            var    stream     = new MemoryStream();
            string input      = "astrachan_";
            var    compressor = new StaticHuffman <char>(CharacterFrequencies(input));

            // Node ids:					     0    1    2    3    4    5    6    7  8     9    10    11    12     13      14
            // Weights:						     1    1    1    1    1    1    1    3  2     2     2     3     4      6      10
            var state = new TreeState(8, '_', 'c', 'h', 'n', 'r', 's', 't', 'a', 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 7, 11,
                                      12, 13);

            compressor.WriteTable(state.WriteSymbol, state.WriteUInt32);
            state.Final();

            var writer = new BitStreamWriter(stream, true);

            foreach (char ch in input)
            {
                compressor.WriteCode(ch, writer.Write);
            }
            writer.Flush();

            state.Reset();
            var decompressor = new StaticHuffman <char>(state.ReadSymbol, state.ReadUInt32);

            state.Final();
            state.Reset();
            decompressor.WriteTable(state.WriteSymbol, state.WriteUInt32);
            state.Final();

            stream.Position = 0;

            var reader = new BitStreamReader(stream, true);

            foreach (char ch in input)
            {
                Assert.AreEqual(ch, decompressor.GetSymbol(reader.ReadBoolean));
            }
        }