예제 #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));
            }
        }
예제 #2
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));
            }
        }
 public void BaseStream_IsBitStream()
 {
     var data = new byte[] {0x23, 0x72, 0x64, 0xe3, 0x11, 0xd1, 0x4a, 0x9c, 0xb6, 0x02};
     var stream = new MemoryStream(data);
     BitStream bitStream = new BitStreamWriter(stream);
     var writer = new BitBinaryWriter(bitStream);
     Assert.AreSame(bitStream, writer.BaseStream);
 }
        public void leaveOpen_True()
        {
            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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);
            }
        }
        public void WriteByte()
        {
            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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.WriteByte(0x3c);

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

            Assert.AreEqual(1, stream.Length);
            Assert.AreEqual(1, stream.Position);
            Assert.AreEqual(0x3c, stream.GetBuffer()[0]);
        }
        public void WriteSevenBits()
        {
            int bitCount = 7;
            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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);

                for (int i = 0; i < bitCount; ++i)
                {
                    writer.Write(true);
                }

                Assert.AreEqual((bitCount + 7) >> 3, writer.Length);
                Assert.AreEqual(bitCount, writer.BitLength);
                Assert.AreEqual(bitCount, writer.BitPosition);
                Assert.AreEqual(bitCount >> 3, writer.Position);
            }
        }
        public void WriteBit_Seek_WriteBit()
        {
            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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]);
        }
        public void WriteArray()
        {
            var data = new byte[] {0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65};
            var expected = new byte[] {0x9e, 0x25, 0x8d, 0x2a, 0x11, 0x08, 0x57, 0xc4, 0xcb, 0xb2, 0x80};

            var result = new byte[expected.Length];

            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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);
                writer.Write(data, 0, data.Length);

                Assert.AreEqual(11, writer.Length);
                Assert.AreEqual(81, writer.BitLength);
                Assert.AreEqual(81, writer.BitPosition);
                Assert.AreEqual(10, writer.Position);
            }

            Array.Copy(stream.GetBuffer(), 0, result, 0, expected.Length);
            CollectionAssert.AreEqual(expected, result);
        }
        public void SetLength_SetPosition()
        {
            var stream = new MemoryStream();
            using (var writer = new BitStreamWriter(stream, true))
            {
                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.SetLength(10);
                writer.Write(true);
                writer.Position = 5;
                writer.Write(true);

                Assert.AreEqual(10, writer.Length);
                Assert.AreEqual(80, writer.BitLength);
                Assert.AreEqual(41, writer.BitPosition);
                Assert.AreEqual(5, writer.Position);
            }

            Assert.AreEqual(10, stream.Length);
            Assert.AreEqual(6, stream.Position);
            Assert.AreEqual(0x80, stream.GetBuffer()[0]);
            Assert.AreEqual(0x80, stream.GetBuffer()[5]);
        }
 public void ReadByte_NotSupported()
 {
     var data = new byte[] {0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65};
     var stream = new MemoryStream(data);
     using (var writer = new BitStreamWriter(stream, false))
     {
         writer.ReadByte();
     }
 }
 public void ReadArray_NotSupported()
 {
     var data = new byte[] {0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65};
     var stream = new MemoryStream(data);
     using (var writer = new BitStreamWriter(stream, false))
     {
         var result = new byte[10];
         writer.Read(result, 0, result.Length);
     }
 }