/// <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]; }
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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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()); } }
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()); } }
public void Overwrite_Bit() { var streamer = new BitRingBuffer(0x100); for (int i = 0; i < 0x100*8 + 1; ++i) { streamer.Write(true); } }
public void Overread_Bit() { var streamer = new BitRingBuffer(0x100); streamer.ReadBoolean(); }