Exemplo n.º 1
0
        public void TestUnalignedRead()
        {
            BitStreamList lst = new BitStreamList();
            var           bs1 = new BitStream();

            bs1.WriteBits(0xaa, 12);
            lst.Add(bs1);
            var bs2a = new BitStream();

            bs2a.WriteBits(0x7, 3);
            lst.Add(bs2a);
            var bs2b = new BitStream();

            bs2b.WriteBits(0x7, 4);
            lst.Add(bs2b);
            var bs3 = new BitStream();

            bs3.WriteBits(0x10a, 18);
            lst.Add(bs3);

            lst.SeekBits(10, SeekOrigin.Begin);
            Assert.AreEqual(37, lst.LengthBits);

            var buf = new byte[4];
            int len = lst.Read(buf, 0, buf.Length);

            Assert.AreEqual(3, len);             // 2bit + 7bit + 18bit = 27bit = 3 bytes
            Assert.AreEqual(37, lst.LengthBits);
            Assert.AreEqual(34, lst.PositionBits);
            Assert.AreEqual(4, lst.Length);
            Assert.AreEqual(4, lst.Position);

            // 00 00 10 10 10 10 11 10 11 10 00 00 00 00 10 00 01 01 0
            //                10 11 10 11 10 00 00 00 00 10 00 01
            //                0xbb        0x80        0x21
            Assert.AreEqual(new byte[] { 0xbb, 0x80, 0x21, 0x00 }, buf);

            lst.SeekBits(0, SeekOrigin.Begin);
            int   bits = (int)lst[0].LengthBits + 1;
            ulong tmp;
            int   cnt = lst.ReadBits(out tmp, bits);

            Assert.AreEqual(bits, cnt);
        }
Exemplo n.º 2
0
        public void TestList()
        {
            BitStreamList lst = new BitStreamList();

            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))));
            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("World"))));

            Assert.AreEqual(0, lst.Position);
            Assert.AreEqual(0, lst.PositionBits);

            Assert.AreEqual(10, lst.Length);
            Assert.AreEqual(80, lst.LengthBits);

            long ret = lst.SeekBits(12, SeekOrigin.Begin);

            Assert.AreEqual(12, ret);
            Assert.AreEqual(1, lst.Position);
            Assert.AreEqual(12, lst.PositionBits);

            ret = lst.Seek(2, SeekOrigin.Current);
            Assert.AreEqual(3, ret);
            Assert.AreEqual(3, lst.Position);
            Assert.AreEqual(28, lst.PositionBits);

            ret = lst.SeekBits(-12, SeekOrigin.End);
            Assert.AreEqual(68, ret);
            Assert.AreEqual(8, lst.Position);
            Assert.AreEqual(68, lst.PositionBits);

            ulong bits;
            int   b = lst.ReadBits(out bits, 15);

            Assert.AreEqual(12, b);
            Assert.AreEqual(0xc64, bits);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);

            lst.Add(new BitStream(new MemoryStream(Encoding.ASCII.GetBytes("!"))));
            Assert.AreEqual(10, lst.Position);
            Assert.AreEqual(80, lst.PositionBits);
            Assert.AreEqual(11, lst.Length);
            Assert.AreEqual(88, lst.LengthBits);

            ret = lst.SeekBits(-12, SeekOrigin.End);
            Assert.AreEqual(76, ret);
            Assert.AreEqual(9, lst.Position);
            Assert.AreEqual(76, lst.PositionBits);

            b = lst.ReadBits(out bits, 15);
            Assert.AreEqual(12, b);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);
            Assert.AreEqual(0x421, bits);

            var buf = new byte[3];

            b = lst.Read(buf, 0, buf.Length);
            Assert.AreEqual(0, b);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);

            lst.Seek(-3, SeekOrigin.End);
            b = lst.Read(buf, 0, buf.Length);
            Assert.AreEqual(3, b);
            Assert.AreEqual(Encoding.ASCII.GetBytes("ld!"), buf);
            Assert.AreEqual(lst.Length, lst.Position);
            Assert.AreEqual(lst.LengthBits, lst.PositionBits);
        }