Skip to content

Commit 72c0034

Browse files
authored
Merge pull request #85 from paulpach/testcases
Tests for arithmetic operations & BitStream
2 parents 79691a3 + 51f6b21 commit 72c0034

File tree

5 files changed

+611
-98
lines changed

5 files changed

+611
-98
lines changed

MLAPI.Tests/MLAPI.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Compile Include="Test.cs" />
4141
<Compile Include="NetworkingManagerComponents\Binary\BitWriterTest.cs" />
4242
<Compile Include="Data\HashCodeTest.cs" />
43+
<Compile Include="NetworkingManagerComponents\Binary\ArithmeticTest.cs" />
4344
<Compile Include="NetworkingManagerComponents\Binary\BitStreamTest.cs" />
4445
</ItemGroup>
4546
<ItemGroup>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
namespace MLAPI.Tests.NetworkingManagerComponents.Binary
3+
{
4+
using MLAPI.NetworkingManagerComponents.Binary;
5+
using NUnit.Framework;
6+
7+
8+
9+
[TestFixture]
10+
public class ArithmeticTest
11+
{
12+
13+
[Test]
14+
public void testCeil()
15+
{
16+
Assert.That(Arithmetic.CeilingExact(10, 5), Is.EqualTo(2));
17+
Assert.That(Arithmetic.CeilingExact(11, 5), Is.EqualTo(3));
18+
Assert.That(Arithmetic.CeilingExact(-5, 5), Is.EqualTo(-1));
19+
Assert.That(Arithmetic.CeilingExact(-4, 5), Is.EqualTo(-1));
20+
}
21+
22+
[Test]
23+
public void testZigZag()
24+
{
25+
Assert.That(Arithmetic.ZigZagDecode(Arithmetic.ZigZagEncode(1234)), Is.EqualTo(1234));
26+
Assert.That(Arithmetic.ZigZagDecode(Arithmetic.ZigZagEncode(-1)), Is.EqualTo(-1));
27+
Assert.That(Arithmetic.ZigZagDecode(Arithmetic.ZigZagEncode(0)), Is.EqualTo(0));
28+
Assert.That(Arithmetic.ZigZagDecode(Arithmetic.ZigZagEncode(Int64.MaxValue)), Is.EqualTo(Int64.MaxValue));
29+
Assert.That(Arithmetic.ZigZagDecode(Arithmetic.ZigZagEncode(Int64.MinValue)), Is.EqualTo(Int64.MinValue));
30+
}
31+
}
32+
}

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace MLAPI.Tests.NetworkingManagerComponents.Binary
33
{
44
using MLAPI.NetworkingManagerComponents.Binary;
55
using NUnit.Framework;
6-
6+
using static MLAPI.NetworkingManagerComponents.Binary.BitStream;
77

88
[TestFixture]
99
public class BitStreamTest
@@ -12,40 +12,30 @@ public class BitStreamTest
1212
[Test]
1313
public void TestEmptyStream()
1414
{
15-
1615
BitStream bitStream = new BitStream(new byte[100]);
1716
// ideally an empty stream should take no space
18-
Assert.That(bitStream.Length, Is.EqualTo(0));
17+
Assert.That(bitStream.Length, Is.EqualTo(100));
1918
}
2019

2120
[Test]
2221
public void TestBool()
2322
{
24-
2523
BitStream bitStream = new BitStream(new byte[100]);
2624
bitStream.WriteBit(true);
2725

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));
26+
// we only wrote 1 bit, so the size should be as small as possible
27+
// which is 1 byte, regardless of how big the buffer is
28+
Assert.That(bitStream.Length, Is.EqualTo(100));
3129
}
3230

3331
[Test]
3432
public void TestGrow()
3533
{
36-
// stream should grow to accomodate input
34+
// stream should not grow when given a buffer
3735
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-
36+
Assert.That(
37+
() => { bitStream.WriteInt64(long.MaxValue); },
38+
Throws.TypeOf<CapacityException>());
4939
}
5040

5141
[Test]
@@ -55,30 +45,30 @@ public void TestInOutBool()
5545

5646
BitStream outStream = new BitStream(buffer);
5747
outStream.WriteBit(true);
58-
outStream.Flush();
48+
outStream.WriteBit(false);
49+
outStream.WriteBit(true);
5950

6051

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

6354
BitStream inStream = new BitStream(buffer);
64-
//bool result = inStream.ReadBit();
65-
66-
Assert.Fail("There is no read bit method");
6755

56+
Assert.That(inStream.ReadBit(), Is.True);
57+
Assert.That(inStream.ReadBit(), Is.False);
58+
Assert.That(inStream.ReadBit(), Is.True);
6859
}
6960

7061

7162
[Test]
7263
public void TestInOutPacked64Bit()
7364
{
7465
byte[] buffer = new byte[100];
75-
66+
7667
long someNumber = 1469598103934656037;
7768

7869

7970
BitStream outStream = new BitStream(buffer);
8071
outStream.WriteInt64Packed(someNumber);
81-
outStream.Flush();
8272

8373

8474
// the bit should now be stored in the buffer, lets see if it comes out
@@ -99,15 +89,15 @@ public void TestInOutBytes()
9989

10090
BitStream outStream = new BitStream(buffer);
10191
outStream.WriteByte(someNumber);
102-
outStream.Flush();
10392

10493

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

10796
BitStream inStream = new BitStream(buffer);
108-
//byte result = inStream.ReadByte();
97+
Assert.That(inStream.ReadByte(), Is.EqualTo(someNumber));
10998

110-
Assert.Fail("Read byte should return byte, but it returns int");
99+
// wtf this is standard behaviour
100+
//Assert.Fail("Read byte should return byte, but it returns int");
111101
}
112102

113103
[Test]
@@ -120,7 +110,6 @@ public void TestInOutInt16()
120110

121111
BitStream outStream = new BitStream(buffer);
122112
outStream.WriteInt16(someNumber);
123-
outStream.Flush();
124113

125114

126115
// the bit should now be stored in the buffer, lets see if it comes out
@@ -141,7 +130,6 @@ public void TestInOutInt32()
141130

142131
BitStream outStream = new BitStream(buffer);
143132
outStream.WriteInt32(someNumber);
144-
outStream.Flush();
145133

146134

147135
// the bit should now be stored in the buffer, lets see if it comes out
@@ -163,7 +151,6 @@ public void TestInOutMultiple()
163151
BitStream outStream = new BitStream(buffer);
164152
outStream.WriteInt16(someNumber);
165153
outStream.WriteInt16(someNumber2);
166-
outStream.Flush();
167154

168155

169156
// the bit should now be stored in the buffer, lets see if it comes out
@@ -175,5 +162,39 @@ public void TestInOutMultiple()
175162
Assert.That(result, Is.EqualTo(someNumber));
176163
Assert.That(result2, Is.EqualTo(someNumber2));
177164
}
165+
166+
[Test]
167+
public void TestLength()
168+
{
169+
BitStream inStream = new BitStream(4);
170+
Assert.That(inStream.Length, Is.EqualTo(0));
171+
inStream.WriteByte(1);
172+
Assert.That(inStream.Length, Is.EqualTo(1));
173+
inStream.WriteByte(2);
174+
Assert.That(inStream.Length, Is.EqualTo(2));
175+
inStream.WriteByte(3);
176+
Assert.That(inStream.Length, Is.EqualTo(3));
177+
inStream.WriteByte(4);
178+
Assert.That(inStream.Length, Is.EqualTo(4));
179+
}
180+
181+
[Test]
182+
public void TestCapacityGrowth()
183+
{
184+
BitStream inStream = new BitStream(4);
185+
Assert.That(inStream.Capacity, Is.EqualTo(4));
186+
187+
inStream.WriteByte(1);
188+
inStream.WriteByte(2);
189+
inStream.WriteByte(3);
190+
inStream.WriteByte(4);
191+
inStream.WriteByte(5);
192+
193+
// buffer should grow and the reported length
194+
// should not waste any space
195+
// note MemoryStream makes a distinction between Length and Capacity
196+
Assert.That(inStream.Length, Is.EqualTo(5));
197+
Assert.That(inStream.Capacity, Is.GreaterThanOrEqualTo(5));
198+
}
178199
}
179200
}

MLAPI/NetworkingManagerComponents/Binary/Arithmetic.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,38 @@
22
{
33
public static class Arithmetic
44
{
5+
// Sign bits for different data types
6+
public const long SIGN_BIT_64 = -9223372036854775808;
7+
public const int SIGN_BIT_32 = -2147483648;
8+
public const short SIGN_BIT_16 = -32768;
9+
public const sbyte SIGN_BIT_8 = -128;
10+
11+
// Ceiling function that doesn't deal with floating point values
512
public static ulong CeilingExact(ulong u1, ulong u2) => (u1 / u2) + (u1 % u2 == 0 ? 0UL : 1UL);
6-
public static long CeilingExact(long u1, long u2) => (u1 / u2) + (u1 % u2 == 0 ? 0L : 1L);
13+
public static long CeilingExact(long u1, long u2) => (u1 / u2) + (u1 % u2 == 0 ? 0L : 1L | (((u1 & SIGN_BIT_64) ^ (u2 & SIGN_BIT_64))>>62));
714
public static uint CeilingExact(uint u1, uint u2) => (u1 / u2) + (u1 % u2 == 0 ? 0U : 1U);
8-
public static int CeilingExact(int u1, int u2) => (u1 / u2) + (u1 % u2 == 0 ? 0 : 1);
15+
public static int CeilingExact(int u1, int u2) => (u1 / u2) + (u1 % u2 == 0 ? 0 : 1 | (((u1 & SIGN_BIT_32) ^ (u2 & SIGN_BIT_32)) >> 30));
916
public static ushort CeilingExact(ushort u1, ushort u2) => (ushort)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1));
10-
public static short CeilingExact(short u1, short u2) => (short)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1));
17+
public static short CeilingExact(short u1, short u2) => (short)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1 | (((u1 & SIGN_BIT_32) ^ (u2 & SIGN_BIT_32)) >> 30)));
1118
public static byte CeilingExact(byte u1, byte u2) => (byte)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1));
12-
public static sbyte CeilingExact(sbyte u1, sbyte u2) => (sbyte)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1));
19+
public static sbyte CeilingExact(sbyte u1, sbyte u2) => (sbyte)((u1 / u2) + (u1 % u2 == 0 ? 0 : 1 | (((u1 & SIGN_BIT_32) ^ (u2 & SIGN_BIT_32)) >> 30)));
20+
21+
// ZigZag
1322
public static ulong ZigZagEncode(long value) => (ulong)((value >> 63) ^ (value << 1));
1423
public static long ZigZagDecode(ulong value) => (((long)(value >> 1) & 0x7FFFFFFFFFFFFFFFL) ^ ((long)(value << 63) >> 63));
24+
25+
// Var int helper stuff
26+
public static int VarIntSize(ulong value) =>
27+
value <= 240 ? 1 :
28+
value <= 2287 ? 2 :
29+
value <= 67823 ? 3 :
30+
value <= 16777215 ? 4 :
31+
value <= 4294967295 ? 5 :
32+
value <= 1099511627775 ? 6 :
33+
value <= 281474976710655 ? 7 :
34+
value <= 72057594037927935 ? 8 :
35+
9;
36+
37+
internal static long Div8Ceil(ulong value) => (long)((value >> 3) + ((value & 1UL) | ((value >> 1) & 1UL) | ((value >> 2) & 1UL)));
1538
}
1639
}

0 commit comments

Comments
 (0)