mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-09-22 22:56:13 +00:00
Make bitstream tests more robust; add notes
This commit is contained in:
@@ -19,36 +19,43 @@ namespace SabreTools.IO.Test.Streams
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadSingleBitBETest()
|
||||
public void ReadSingleBitTest()
|
||||
{
|
||||
byte[] data = [0b01010101];
|
||||
var stream = new ReadOnlyBitStream(new MemoryStream(data));
|
||||
byte? bit = stream.ReadBit();
|
||||
|
||||
Assert.NotNull(bit);
|
||||
Assert.Equal((byte)0b00000001, bit);
|
||||
Assert.Equal(1, stream.Position);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadBitsLETest()
|
||||
[Theory]
|
||||
[InlineData(4, 0b00000101, 1)]
|
||||
[InlineData(9, 0b10101010_1, 2)]
|
||||
public void ReadBitsLETest(int bits, uint expected, int position)
|
||||
{
|
||||
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
|
||||
var stream = new ReadOnlyBitStream(new MemoryStream(data));
|
||||
uint? bits = stream.ReadBitsLE(4);
|
||||
Assert.NotNull(bits);
|
||||
Assert.Equal((byte)0b00001010, bits); // Transcribed to big-endian
|
||||
Assert.Equal(1, stream.Position);
|
||||
uint? actual = stream.ReadBitsLE(bits);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
Assert.Equal(expected, actual);
|
||||
Assert.Equal(position, stream.Position);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadBitsBETest()
|
||||
[Theory]
|
||||
[InlineData(4, 0b00001010, 1)]
|
||||
[InlineData(9, 0b10101010_1, 2)]
|
||||
public void ReadBitsBETest(int bits, uint expected, int position)
|
||||
{
|
||||
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
|
||||
var stream = new ReadOnlyBitStream(new MemoryStream(data));
|
||||
uint? bits = stream.ReadBitsBE(4);
|
||||
Assert.NotNull(bits);
|
||||
Assert.Equal((byte)0b00001010, bits);
|
||||
Assert.Equal(1, stream.Position);
|
||||
uint? actual = stream.ReadBitsBE(bits);
|
||||
|
||||
Assert.NotNull(actual);
|
||||
Assert.Equal(expected, actual);
|
||||
Assert.Equal(position, stream.Position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ namespace SabreTools.IO.Streams
|
||||
/// Read a multiple bits in little-endian, if possible
|
||||
/// </summary>
|
||||
/// <returns>The next bits encoded in a UInt32, null on error or end of stream</returns>
|
||||
/// <remarks>[76543210] order within a byte, appended to output [76543210]</remarks>
|
||||
public uint? ReadBitsLE(int bits)
|
||||
{
|
||||
uint value = 0;
|
||||
@@ -101,8 +102,8 @@ namespace SabreTools.IO.Streams
|
||||
if (bitValue == null)
|
||||
return null;
|
||||
|
||||
// Add the bit shifted by the current index
|
||||
value += (uint)(bitValue.Value << i);
|
||||
// Append the bit shifted by the current index
|
||||
value |= (uint)(bitValue.Value << i);
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -112,6 +113,7 @@ namespace SabreTools.IO.Streams
|
||||
/// Read a multiple bits in big-endian, if possible
|
||||
/// </summary>
|
||||
/// <returns>The next bits encoded in a UInt32, null on error or end of stream</returns>
|
||||
/// <remarks>[76543210] order within a byte, appended to output [01234567]</remarks>
|
||||
public uint? ReadBitsBE(int bits)
|
||||
{
|
||||
uint value = 0;
|
||||
@@ -122,8 +124,8 @@ namespace SabreTools.IO.Streams
|
||||
if (bitValue == null)
|
||||
return null;
|
||||
|
||||
// Add the bit shifted by the current index
|
||||
value = (value << 1) + bitValue.Value;
|
||||
// Append the bit shifted by the current index
|
||||
value = (value << 1) | bitValue.Value;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
||||
Reference in New Issue
Block a user