Fixed?
This commit is contained in:
@@ -29,16 +29,7 @@ public class BitStreamTests
|
|||||||
var stream = new MemoryStream(bytes);
|
var stream = new MemoryStream(bytes);
|
||||||
|
|
||||||
var bitStream = new BitStream(stream);
|
var bitStream = new BitStream(stream);
|
||||||
var list = new List<int>();
|
var list = GetRandomNumbersSummingTo(32);
|
||||||
var sum = 0;
|
|
||||||
while (sum < 32)
|
|
||||||
{
|
|
||||||
var temp = Random.Shared.Next(1, 10);
|
|
||||||
if (sum + temp < 32) list.Add(temp);
|
|
||||||
sum += temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list.Sum() < 32) list.Add(32 - list.Sum());
|
|
||||||
|
|
||||||
foreach (var num in list)
|
foreach (var num in list)
|
||||||
{
|
{
|
||||||
@@ -50,32 +41,38 @@ public class BitStreamTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void TestReadLong()
|
public void TestReadLong()
|
||||||
{
|
{
|
||||||
var random = (ulong)Random.Shared.NextInt64();
|
var random = (uint)Random.Shared.Next();
|
||||||
var bytes = BitConverter.GetBytes(random);
|
var bytes = BitConverter.GetBytes(random);
|
||||||
var stream = new MemoryStream(bytes);
|
var stream = new MemoryStream(bytes);
|
||||||
|
|
||||||
var bitStream = new BitStream(stream);
|
var bitStream = new BitStream(stream);
|
||||||
var list = new List<int>();
|
var list = GetRandomNumbersSummingTo(32);
|
||||||
var sum = 0;
|
|
||||||
while (sum < 64)
|
|
||||||
{
|
|
||||||
var temp = Random.Shared.Next(1, 20);
|
|
||||||
if (sum + temp < 64) list.Add(temp);
|
|
||||||
sum += temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list.Sum() < 64) list.Add(64 - list.Sum());
|
|
||||||
|
|
||||||
var runningTotal = 0;
|
var runningTotal = 0;
|
||||||
foreach (var num in list)
|
foreach (var num in list)
|
||||||
{
|
{
|
||||||
var temp = random;
|
var temp = random;
|
||||||
temp <<= runningTotal;
|
temp <<= runningTotal;
|
||||||
temp >>= 64 - num;
|
temp >>= 32 - num;
|
||||||
Console.WriteLine($"Original value: {random:b}, bit shifted number: {temp:b}");
|
Console.WriteLine($"Original value: {random:b}, bit shifted number: {temp:b}");
|
||||||
var value = bitStream.ReadBits(num);
|
var value = bitStream.ReadBits(num);
|
||||||
Assert.That(value, Is.EqualTo(temp), $"{num} bits match failed");
|
Assert.That(value, Is.EqualTo(temp), $"{num} bits match failed");
|
||||||
runningTotal += num;
|
runningTotal += num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<int> GetRandomNumbersSummingTo(int total)
|
||||||
|
{
|
||||||
|
var list = new List<int>();
|
||||||
|
var sum = 0;
|
||||||
|
while (sum < total)
|
||||||
|
{
|
||||||
|
var temp = Random.Shared.Next(1, total / 3);
|
||||||
|
if (sum + temp < total) list.Add(temp);
|
||||||
|
sum += temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (list.Sum() < total) list.Add(total - list.Sum());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -34,8 +34,9 @@ public class BitStream : Stream
|
|||||||
if (_bitBufferCount <= 24)
|
if (_bitBufferCount <= 24)
|
||||||
while (_bitBufferCount < 32 && _stream.Position < _stream.Length)
|
while (_bitBufferCount < 32 && _stream.Position < _stream.Length)
|
||||||
{
|
{
|
||||||
_bitBuffer <<= 8;
|
var local = (uint)_stream.ReadByte();
|
||||||
_bitBuffer |= (uint)_stream.ReadByte();
|
local <<= _bitBufferCount;
|
||||||
|
_bitBuffer |= local;
|
||||||
_bitBufferCount += 8;
|
_bitBufferCount += 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +44,6 @@ public class BitStream : Stream
|
|||||||
buffer >>= 32 - bitLength;
|
buffer >>= 32 - bitLength;
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.WriteLine($"BitStream buffer: {_bitBuffer:b}, extracted number: {buffer:b}");
|
Console.WriteLine($"BitStream buffer: {_bitBuffer:b}, extracted number: {buffer:b}");
|
||||||
_stream.Position = 0;
|
|
||||||
#endif
|
#endif
|
||||||
_bitBuffer <<= bitLength;
|
_bitBuffer <<= bitLength;
|
||||||
Position += bitLength;
|
Position += bitLength;
|
||||||
|
|||||||
Reference in New Issue
Block a user