This commit is contained in:
Lani Aung
2024-09-29 16:09:18 -06:00
parent 6dbc577566
commit 9c44ec0156
2 changed files with 22 additions and 25 deletions
+19 -22
View File
@@ -29,16 +29,7 @@ public class BitStreamTests
var stream = new MemoryStream(bytes);
var bitStream = new BitStream(stream);
var list = new List<int>();
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());
var list = GetRandomNumbersSummingTo(32);
foreach (var num in list)
{
@@ -50,32 +41,38 @@ public class BitStreamTests
[Test]
public void TestReadLong()
{
var random = (ulong)Random.Shared.NextInt64();
var random = (uint)Random.Shared.Next();
var bytes = BitConverter.GetBytes(random);
var stream = new MemoryStream(bytes);
var bitStream = new BitStream(stream);
var list = new List<int>();
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 list = GetRandomNumbersSummingTo(32);
var runningTotal = 0;
foreach (var num in list)
{
var temp = random;
temp <<= runningTotal;
temp >>= 64 - num;
temp >>= 32 - num;
Console.WriteLine($"Original value: {random:b}, bit shifted number: {temp:b}");
var value = bitStream.ReadBits(num);
Assert.That(value, Is.EqualTo(temp), $"{num} bits match failed");
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;
}
}
+3 -3
View File
@@ -34,8 +34,9 @@ public class BitStream : Stream
if (_bitBufferCount <= 24)
while (_bitBufferCount < 32 && _stream.Position < _stream.Length)
{
_bitBuffer <<= 8;
_bitBuffer |= (uint)_stream.ReadByte();
var local = (uint)_stream.ReadByte();
local <<= _bitBufferCount;
_bitBuffer |= local;
_bitBufferCount += 8;
}
@@ -43,7 +44,6 @@ public class BitStream : Stream
buffer >>= 32 - bitLength;
#if DEBUG
Console.WriteLine($"BitStream buffer: {_bitBuffer:b}, extracted number: {buffer:b}");
_stream.Position = 0;
#endif
_bitBuffer <<= bitLength;
Position += bitLength;