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);
            }
        }
        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 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 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 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]);
        }