Skip to content

Commit f511913

Browse files
Added tests
Added constructors Fixed CanRead Fixed CopyFrom
1 parent ed5d7a7 commit f511913

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,98 @@ public void TestInOutBool()
5959
}
6060

6161

62+
[Test]
63+
public void TestIntOutPacked16Bit()
64+
{
65+
short svalue = -31934;
66+
ushort uvalue = 64893;
67+
BitStream outStream = new BitStream();
68+
outStream.WriteInt16Packed(svalue);
69+
outStream.WriteUInt16Packed(uvalue);
70+
71+
BitStream inStream = new BitStream(outStream.GetBuffer());
72+
Assert.That(inStream.ReadInt16Packed(), Is.EqualTo(svalue));
73+
Assert.That(inStream.ReadUInt16Packed(), Is.EqualTo(uvalue));
74+
}
75+
76+
77+
[Test]
78+
public void TestIntOutPacked32Bit()
79+
{
80+
int svalue = -100913642;
81+
uint uvalue = 1467867235;
82+
BitStream outStream = new BitStream();
83+
outStream.WriteInt32Packed(svalue);
84+
outStream.WriteUInt32Packed(uvalue);
85+
86+
BitStream inStream = new BitStream(outStream.GetBuffer());
87+
Assert.That(inStream.ReadInt32Packed(), Is.EqualTo(svalue));
88+
Assert.That(inStream.ReadUInt32Packed(), Is.EqualTo(uvalue));
89+
}
90+
91+
6292
[Test]
6393
public void TestInOutPacked64Bit()
6494
{
6595
byte[] buffer = new byte[100];
6696

67-
long someNumber = 1469598103934656037;
97+
long someNumber = -1469598103934656037;
98+
ulong uNumber = 81246971249124124;
6899

69100

70101
BitStream outStream = new BitStream(buffer);
71102
outStream.WriteInt64Packed(someNumber);
103+
outStream.WriteUInt64Packed(uNumber);
72104

73105

74106
// the bit should now be stored in the buffer, lets see if it comes out
75107

76108
BitStream inStream = new BitStream(buffer);
77109
long result = inStream.ReadInt64Packed();
110+
ulong result1 = inStream.ReadUInt64Packed();
78111

79112
Assert.That(result, Is.EqualTo(someNumber));
113+
Assert.That(result1, Is.EqualTo(uNumber));
80114
}
81115

116+
[Test]
117+
public void TestStreamCopy()
118+
{
119+
BitStream inStream = new BitStream();
120+
BitStream copyFrom = new BitStream();
121+
122+
byte initialValue1 = 56;
123+
byte initialValue2 = 24;
124+
125+
inStream.WriteByte(initialValue1);
126+
inStream.WriteByte(initialValue2);
127+
128+
byte copyValue1 = 27;
129+
byte copyValue2 = 100;
130+
131+
copyFrom.WriteByte(copyValue1);
132+
copyFrom.WriteByte(copyValue2);
133+
134+
inStream.CopyFrom(copyFrom, 2);
135+
136+
BitStream outStream = new BitStream(inStream.ToArray());
137+
138+
Assert.That(outStream.ReadByte(), Is.EqualTo(initialValue1));
139+
Assert.That(outStream.ReadByte(), Is.EqualTo(initialValue2));
140+
Assert.That(outStream.ReadByte(), Is.EqualTo(copyValue1));
141+
Assert.That(outStream.ReadByte(), Is.EqualTo(copyValue2));
142+
}
143+
144+
[Test]
145+
public void TestToArray()
146+
{
147+
BitStream inStream = new BitStream();
148+
inStream.WriteByte(5);
149+
inStream.WriteByte(6);
150+
Assert.That(inStream.ToArray().Length, Is.EqualTo(2));
151+
}
152+
153+
82154
[Test]
83155
public void TestInOutBytes()
84156
{

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ namespace MLAPI.NetworkingManagerComponents.Binary
1111
public sealed class BitStream : Stream
1212
{
1313
const int initialCapacity = 16;
14+
const float initialGrowthFactor = 2.0f;
1415
private byte[] target;
1516

1617
/// <summary>
1718
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
1819
/// </summary>
1920
/// <param name="capacity">Initial capacity of buffer in bytes.</param>
2021
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
21-
public BitStream(int capacity = initialCapacity, float growthFactor = 2.0f)
22+
public BitStream(int capacity, float growthFactor)
2223
{
2324
target = new byte[capacity];
2425
GrowthFactor = growthFactor;
@@ -29,7 +30,17 @@ public BitStream(int capacity = initialCapacity, float growthFactor = 2.0f)
2930
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
3031
/// </summary>
3132
/// <param name="growthFactor">Factor by which buffer should grow when necessary.</param>
32-
public BitStream(float growthFactor = 2.0f) : this(initialCapacity, growthFactor) { }
33+
public BitStream(float growthFactor) : this(initialCapacity, growthFactor) { }
34+
/// <summary>
35+
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
36+
/// </summary>
37+
/// <param name="capacity"></param>
38+
public BitStream(int capacity) : this(capacity, initialGrowthFactor) { }
39+
40+
/// <summary>
41+
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
42+
/// </summary>
43+
public BitStream() : this(initialCapacity, initialGrowthFactor) { }
3344

3445
/// <summary>
3546
/// A stream that supports writing data smaller than a single byte. This stream also has a built-in compression algorithm that can (optionally) be used to write compressed data.
@@ -69,7 +80,7 @@ public BitStream(byte[] target)
6980
/// <summary>
7081
/// Whether or not data can be read from the stream.
7182
/// </summary>
72-
public override bool CanRead => BitPosition < (ulong)target.LongLength;
83+
public override bool CanRead => Position < target.LongLength;
7384

7485
/// <summary>
7586
/// Whether or not seeking is supported by this stream. (Always true)
@@ -632,23 +643,19 @@ public override void WriteByte(byte value)
632643
/// <param name="count">How many bytes to read. Set to value less than one to read until ReadByte returns -1</param>
633644
public void CopyFrom(Stream s, int count = -1)
634645
{
635-
if(s is BitStream b)
636-
{
637-
ulong bytes = b.BitLength >> 3;
638-
Write(b.target, 0, (int)bytes);
639-
if ((b.BitLength & 7) != 0) _WriteBits(b.target[bytes - 1], (int)(b.BitLength & 7));
640-
}
646+
if(s is BitStream b) Write(b.target, 0, count < 0 ? (int)b.Length : count);
641647
else
642648
{
643649
int read;
644650
bool readToEnd = count < 0;
645651
while((readToEnd || count-- > 0) && (read = s.ReadByte()) != -1)
646652
_WriteIntByte(read);
653+
UpdateLength();
647654
}
648-
649-
UpdateLength();
650655
}
651656

657+
// TODO: Implement CopyFrom() for BitStream with bitCount parameter
658+
652659
/// <summary>
653660
/// Update length of data considered to be "written" to the stream.
654661
/// </summary>

0 commit comments

Comments
 (0)