コード例 #1
0
ファイル: BitStream.cs プロジェクト: FineRedMist/Compression
 /// <summary>
 /// Initializes a new instance of the <see cref="OneOddSock.IO.BitStream"/> class
 /// with backing <paramref name="stream"/>.  The stream will be closed only if
 /// <paramref name="leaveOpen"/> is false.
 /// </summary>
 protected BitStream(Stream stream, bool leaveOpen)
 {
     _baseStream = stream;
     _leaveOpen = leaveOpen;
     _bitBuffer = new BitRingBuffer();
     _conversionBuffer = new byte[0x100];
 }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OneOddSock.IO.BitStream"/> class
 /// with backing <paramref name="stream"/>.  The stream will be closed only if
 /// <paramref name="leaveOpen"/> is false.
 /// </summary>
 protected BitStream(Stream stream, bool leaveOpen)
 {
     _baseStream       = stream;
     _leaveOpen        = leaveOpen;
     _bitBuffer        = new BitRingBuffer();
     _conversionBuffer = new byte[0x100];
 }
コード例 #3
0
 public void Eight_Bits_Written()
 {
     var streamer = new BitRingBuffer(0x100);
     for (int i = 0; i < 8; ++i)
     {
         streamer.Write(true);
     }
     Assert.AreEqual(0x100 - 1, streamer.AvailableBytes);
     Assert.AreEqual(1, streamer.UsedBytes);
     Assert.AreEqual(1, streamer.LengthBytes);
     Assert.AreEqual(8, streamer.LengthBits);
     Assert.AreEqual(0x100*8 - 8, streamer.AvailableBits);
 }
コード例 #4
0
 public void Almost_Full()
 {
     var streamer = new BitRingBuffer(0x100);
     for (int i = 0; i < 0x100*8 - 1; ++i)
     {
         streamer.Write(true);
     }
     Assert.AreEqual(0, streamer.AvailableBytes);
     Assert.AreEqual(0x100, streamer.UsedBytes);
     Assert.AreEqual(0x100 - 1, streamer.LengthBytes);
     Assert.AreEqual(0x100*8 - 1, streamer.LengthBits);
     Assert.AreEqual(1, streamer.AvailableBits);
 }
コード例 #5
0
 public void EnsureIndexWrap()
 {
     var streamer = new BitRingBuffer(2);
     streamer.Write(0x3c);
     for (int i = 0; i < 7; ++i)
     {
         streamer.Write(true);
     }
     byte result = streamer.ReadByte();
     Assert.AreEqual((byte) 0x3c, result);
     streamer.Write(false); // End of buffer
     streamer.Write(true); // Wrap
     result = streamer.ReadByte();
     Assert.AreEqual((byte) 0xfe, result);
     Assert.AreEqual(true, streamer.ReadBoolean());
 }
コード例 #6
0
 public void InitialState()
 {
     var streamer = new BitRingBuffer(0x100);
     Assert.AreEqual(0x100, streamer.AvailableBytes);
     Assert.AreEqual(0, streamer.UsedBytes);
     Assert.AreEqual(0, streamer.LengthBytes);
     Assert.AreEqual(0, streamer.LengthBits);
     Assert.AreEqual(0x100*8, streamer.AvailableBits);
 }
コード例 #7
0
 public void WriteByteArray()
 {
     var streamer = new BitRingBuffer(0x100);
     var buffer = new byte[0x100];
     for (int i = 0; i < 0x100; ++i)
     {
         buffer[i] = (byte) (((i + 123)*(i + 7))%256);
     }
     streamer.Write(buffer, 0, 0x100);
     var result = new byte[0x100];
     streamer.ReadBytes(result, 0, 0x100);
     CollectionAssert.AreEqual(buffer, result);
 }
コード例 #8
0
 public void TwoBits_Written()
 {
     var streamer = new BitRingBuffer(0x100);
     streamer.Write(true);
     streamer.Write(false);
     Assert.AreEqual(0x100 - 1, streamer.AvailableBytes);
     Assert.AreEqual(1, streamer.UsedBytes);
     Assert.AreEqual(0, streamer.LengthBytes);
     Assert.AreEqual(2, streamer.LengthBits);
     Assert.AreEqual(0x100*8 - 2, streamer.AvailableBits);
 }
コード例 #9
0
 public void ReadOneBit_After_Full()
 {
     var streamer = new BitRingBuffer(0x100);
     for (int i = 0; i < 0x100*8; ++i)
     {
         streamer.Write(true);
     }
     streamer.ReadBoolean();
     Assert.AreEqual(0, streamer.AvailableBytes);
     Assert.AreEqual(0x100, streamer.UsedBytes);
     Assert.AreEqual(0x100 - 1, streamer.LengthBytes);
     Assert.AreEqual(0x100*8 - 1, streamer.LengthBits);
     Assert.AreEqual(1, streamer.AvailableBits);
 }
コード例 #10
0
        public void ReadFullBuffer_BitByBit()
        {
            var streamer = new BitRingBuffer(0x100);
            for (int i = 0; i < 0x100*8; ++i)
            {
                streamer.Write((i*(i%7))%2 == 1);
            }

            for (int i = 0; i < 0x100*8; ++i)
            {
                Assert.AreEqual((i*(i%7))%2 == 1, streamer.ReadBoolean());
            }
        }
コード例 #11
0
 public void ReadByte_BitByBit()
 {
     byte b = 0xc7; // 11000111
     var values = new[] {false, true, true, false, false, false, true, true, true};
     var streamer = new BitRingBuffer(0x100);
     streamer.Write(false);
     streamer.Write(b);
     for (int i = 0; i < 8; ++i)
     {
         Assert.AreEqual(values[i], streamer.ReadBoolean());
     }
 }
コード例 #12
0
 public void Overwrite_Bit()
 {
     var streamer = new BitRingBuffer(0x100);
     for (int i = 0; i < 0x100*8 + 1; ++i)
     {
         streamer.Write(true);
     }
 }
コード例 #13
0
 public void Overread_Bit()
 {
     var streamer = new BitRingBuffer(0x100);
     streamer.ReadBoolean();
 }