Skip to content

Commit ae03d7e

Browse files
Merge pull request #86 from paulpach/bitstreamtest
Add bitstream tests
2 parents 5f8c306 + 5fc678c commit ae03d7e

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

MLAPI.Tests/MLAPI.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@
3333
<HintPath>..\packages\NUnit.2.6.6\lib\nunit.framework.dll</HintPath>
3434
</Reference>
3535
<Reference Include="System" />
36+
<Reference Include="nunit.framework">
37+
<HintPath>..\packages\NUnit.2.6.6\lib\nunit.framework.dll</HintPath>
38+
</Reference>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<Compile Include="Data\FieldTypeTests.cs" />
3942
<Compile Include="Test.cs" />
4043
<Compile Include="NetworkingManagerComponents\Binary\BitWriterTest.cs" />
4144
<Compile Include="Data\HashCodeTest.cs" />
45+
<Compile Include="NetworkingManagerComponents\Binary\BitStreamTest.cs" />
4246
</ItemGroup>
4347
<ItemGroup>
4448
<None Include="packages.config" />
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
namespace MLAPI.Tests.NetworkingManagerComponents.Binary
3+
{
4+
using MLAPI.NetworkingManagerComponents.Binary;
5+
using NUnit.Framework;
6+
7+
8+
[TestFixture]
9+
public class BitStreamTest
10+
{
11+
12+
[Test]
13+
public void TestEmptyStream()
14+
{
15+
16+
BitStream bitStream = new BitStream(new byte[100]);
17+
// ideally an empty stream should take no space
18+
Assert.That(bitStream.Length, Is.EqualTo(0));
19+
}
20+
21+
[Test]
22+
public void TestBool()
23+
{
24+
25+
BitStream bitStream = new BitStream(new byte[100]);
26+
bitStream.WriteBit(true);
27+
28+
// this is failing, I just wrote something, how is it possible
29+
// that the length is still 0?
30+
Assert.That(bitStream.Length, Is.EqualTo(1));
31+
}
32+
33+
[Test]
34+
public void TestGrow()
35+
{
36+
// stream should grow to accomodate input
37+
BitStream bitStream = new BitStream(new byte[0]);
38+
bitStream.WriteInt64(long.MaxValue);
39+
40+
}
41+
42+
[Test]
43+
public void TestGrow2()
44+
{
45+
// stream should grow to accomodate input
46+
BitStream bitStream = new BitStream(new byte[1]);
47+
bitStream.WriteInt64(long.MaxValue);
48+
49+
}
50+
51+
[Test]
52+
public void TestInOutBool()
53+
{
54+
byte[] buffer = new byte[100];
55+
56+
BitStream outStream = new BitStream(buffer);
57+
outStream.WriteBit(true);
58+
outStream.Flush();
59+
60+
61+
// the bit should now be stored in the buffer, lets see if it comes out
62+
63+
BitStream inStream = new BitStream(buffer);
64+
//bool result = inStream.ReadBit();
65+
66+
Assert.Fail("There is no read bit method");
67+
68+
}
69+
70+
71+
[Test]
72+
public void TestInOutPacked64Bit()
73+
{
74+
byte[] buffer = new byte[100];
75+
76+
long someNumber = 1469598103934656037;
77+
78+
79+
BitStream outStream = new BitStream(buffer);
80+
outStream.WriteInt64Packed(someNumber);
81+
outStream.Flush();
82+
83+
84+
// the bit should now be stored in the buffer, lets see if it comes out
85+
86+
BitStream inStream = new BitStream(buffer);
87+
long result = inStream.ReadInt64Packed();
88+
89+
Assert.That(result, Is.EqualTo(someNumber));
90+
}
91+
92+
[Test]
93+
public void TestInOutBytes()
94+
{
95+
byte[] buffer = new byte[100];
96+
97+
byte someNumber = 0xff;
98+
99+
100+
BitStream outStream = new BitStream(buffer);
101+
outStream.WriteByte(someNumber);
102+
outStream.Flush();
103+
104+
105+
// the bit should now be stored in the buffer, lets see if it comes out
106+
107+
BitStream inStream = new BitStream(buffer);
108+
//byte result = inStream.ReadByte();
109+
110+
Assert.Fail("Read byte should return byte, but it returns int");
111+
}
112+
113+
[Test]
114+
public void TestInOutInt16()
115+
{
116+
byte[] buffer = new byte[100];
117+
118+
short someNumber = 23223;
119+
120+
121+
BitStream outStream = new BitStream(buffer);
122+
outStream.WriteInt16(someNumber);
123+
outStream.Flush();
124+
125+
126+
// the bit should now be stored in the buffer, lets see if it comes out
127+
128+
BitStream inStream = new BitStream(buffer);
129+
short result = inStream.ReadInt16();
130+
131+
Assert.That(result, Is.EqualTo(someNumber));
132+
}
133+
134+
[Test]
135+
public void TestInOutInt32()
136+
{
137+
byte[] buffer = new byte[100];
138+
139+
int someNumber = 23234223;
140+
141+
142+
BitStream outStream = new BitStream(buffer);
143+
outStream.WriteInt32(someNumber);
144+
outStream.Flush();
145+
146+
147+
// the bit should now be stored in the buffer, lets see if it comes out
148+
149+
BitStream inStream = new BitStream(buffer);
150+
int result = inStream.ReadInt32();
151+
152+
Assert.That(result, Is.EqualTo(someNumber));
153+
}
154+
155+
[Test]
156+
public void TestInOutMultiple()
157+
{
158+
byte[] buffer = new byte[100];
159+
160+
short someNumber = -12423;
161+
short someNumber2 = 9322;
162+
163+
BitStream outStream = new BitStream(buffer);
164+
outStream.WriteInt16(someNumber);
165+
outStream.WriteInt16(someNumber2);
166+
outStream.Flush();
167+
168+
169+
// the bit should now be stored in the buffer, lets see if it comes out
170+
171+
BitStream inStream = new BitStream(buffer);
172+
short result = inStream.ReadInt16();
173+
short result2 = inStream.ReadInt16();
174+
175+
Assert.That(result, Is.EqualTo(someNumber));
176+
Assert.That(result2, Is.EqualTo(someNumber2));
177+
}
178+
}
179+
}

MLAPI/MLAPI.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<SpecificVersion>False</SpecificVersion>
8181
<HintPath>..\Libraries\Unity\UnityEngine.dll</HintPath>
8282
</Reference>
83+
<Reference Include="IntXLib">
84+
<HintPath>..\packages\IntX.1.0.1.0\lib\net20\IntXLib.dll</HintPath>
85+
</Reference>
8386
</ItemGroup>
8487
<ItemGroup>
8588
<Compile Include="Attributes\BinaryIgnore.cs" />

0 commit comments

Comments
 (0)