Skip to content

Commit 9bdc78b

Browse files
Merged local stuff
2 parents fc63b03 + 72c0034 commit 9bdc78b

File tree

8 files changed

+747
-76
lines changed

8 files changed

+747
-76
lines changed

MLAPI.Tests/MLAPI.Tests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" />
34
<PropertyGroup>
45
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
56
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -29,16 +30,18 @@
2930
<LangVersion>7</LangVersion>
3031
</PropertyGroup>
3132
<ItemGroup>
32-
<Reference Include="nunit.framework, Version=2.6.6.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
33-
<HintPath>..\packages\NUnit.2.6.6\lib\nunit.framework.dll</HintPath>
34-
</Reference>
3533
<Reference Include="System" />
34+
<Reference Include="nunit.framework">
35+
<HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath>
36+
</Reference>
3637
</ItemGroup>
3738
<ItemGroup>
3839
<Compile Include="Data\FieldTypeTests.cs" />
3940
<Compile Include="Test.cs" />
4041
<Compile Include="NetworkingManagerComponents\Binary\BitWriterTest.cs" />
4142
<Compile Include="Data\HashCodeTest.cs" />
43+
<Compile Include="NetworkingManagerComponents\Binary\ArithmeticTest.cs" />
44+
<Compile Include="NetworkingManagerComponents\Binary\BitStreamTest.cs" />
4245
</ItemGroup>
4346
<ItemGroup>
4447
<None Include="packages.config" />
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+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
using System;
2+
namespace MLAPI.Tests.NetworkingManagerComponents.Binary
3+
{
4+
using MLAPI.NetworkingManagerComponents.Binary;
5+
using NUnit.Framework;
6+
using static MLAPI.NetworkingManagerComponents.Binary.BitStream;
7+
8+
[TestFixture]
9+
public class BitStreamTest
10+
{
11+
12+
[Test]
13+
public void TestEmptyStream()
14+
{
15+
BitStream bitStream = new BitStream(new byte[100]);
16+
// ideally an empty stream should take no space
17+
Assert.That(bitStream.Length, Is.EqualTo(100));
18+
}
19+
20+
[Test]
21+
public void TestBool()
22+
{
23+
BitStream bitStream = new BitStream(new byte[100]);
24+
bitStream.WriteBit(true);
25+
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));
29+
}
30+
31+
[Test]
32+
public void TestGrow()
33+
{
34+
// stream should not grow when given a buffer
35+
BitStream bitStream = new BitStream(new byte[0]);
36+
Assert.That(
37+
() => { bitStream.WriteInt64(long.MaxValue); },
38+
Throws.TypeOf<CapacityException>());
39+
}
40+
41+
[Test]
42+
public void TestInOutBool()
43+
{
44+
byte[] buffer = new byte[100];
45+
46+
BitStream outStream = new BitStream(buffer);
47+
outStream.WriteBit(true);
48+
outStream.WriteBit(false);
49+
outStream.WriteBit(true);
50+
51+
52+
// the bit should now be stored in the buffer, lets see if it comes out
53+
54+
BitStream inStream = new BitStream(buffer);
55+
56+
Assert.That(inStream.ReadBit(), Is.True);
57+
Assert.That(inStream.ReadBit(), Is.False);
58+
Assert.That(inStream.ReadBit(), Is.True);
59+
}
60+
61+
62+
[Test]
63+
public void TestInOutPacked64Bit()
64+
{
65+
byte[] buffer = new byte[100];
66+
67+
long someNumber = 1469598103934656037;
68+
69+
70+
BitStream outStream = new BitStream(buffer);
71+
outStream.WriteInt64Packed(someNumber);
72+
73+
74+
// the bit should now be stored in the buffer, lets see if it comes out
75+
76+
BitStream inStream = new BitStream(buffer);
77+
long result = inStream.ReadInt64Packed();
78+
79+
Assert.That(result, Is.EqualTo(someNumber));
80+
}
81+
82+
[Test]
83+
public void TestInOutBytes()
84+
{
85+
byte[] buffer = new byte[100];
86+
87+
byte someNumber = 0xff;
88+
89+
90+
BitStream outStream = new BitStream(buffer);
91+
outStream.WriteByte(someNumber);
92+
93+
94+
// the bit should now be stored in the buffer, lets see if it comes out
95+
96+
BitStream inStream = new BitStream(buffer);
97+
Assert.That(inStream.ReadByte(), Is.EqualTo(someNumber));
98+
99+
// wtf this is standard behaviour
100+
//Assert.Fail("Read byte should return byte, but it returns int");
101+
}
102+
103+
[Test]
104+
public void TestInOutInt16()
105+
{
106+
byte[] buffer = new byte[100];
107+
108+
short someNumber = 23223;
109+
110+
111+
BitStream outStream = new BitStream(buffer);
112+
outStream.WriteInt16(someNumber);
113+
114+
115+
// the bit should now be stored in the buffer, lets see if it comes out
116+
117+
BitStream inStream = new BitStream(buffer);
118+
short result = inStream.ReadInt16();
119+
120+
Assert.That(result, Is.EqualTo(someNumber));
121+
}
122+
123+
[Test]
124+
public void TestInOutInt32()
125+
{
126+
byte[] buffer = new byte[100];
127+
128+
int someNumber = 23234223;
129+
130+
131+
BitStream outStream = new BitStream(buffer);
132+
outStream.WriteInt32(someNumber);
133+
134+
135+
// the bit should now be stored in the buffer, lets see if it comes out
136+
137+
BitStream inStream = new BitStream(buffer);
138+
int result = inStream.ReadInt32();
139+
140+
Assert.That(result, Is.EqualTo(someNumber));
141+
}
142+
143+
[Test]
144+
public void TestInOutMultiple()
145+
{
146+
byte[] buffer = new byte[100];
147+
148+
short someNumber = -12423;
149+
short someNumber2 = 9322;
150+
151+
BitStream outStream = new BitStream(buffer);
152+
outStream.WriteInt16(someNumber);
153+
outStream.WriteInt16(someNumber2);
154+
155+
156+
// the bit should now be stored in the buffer, lets see if it comes out
157+
158+
BitStream inStream = new BitStream(buffer);
159+
short result = inStream.ReadInt16();
160+
short result2 = inStream.ReadInt16();
161+
162+
Assert.That(result, Is.EqualTo(someNumber));
163+
Assert.That(result2, Is.EqualTo(someNumber2));
164+
}
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+
}
199+
}
200+
}

MLAPI.Tests/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="NUnit" version="2.6.6" targetFramework="net35" />
3+
<package id="NUnit" version="3.10.1" targetFramework="net35" />
44
<package id="OpenCover" version="4.6.519" targetFramework="net35" />
55
</packages>

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" />

MLAPI/NetworkingManagerComponents/Binary/Arithmetic.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
public static class Arithmetic
44
{
55
// Sign bits for different data types
6-
public const long SIGN_BIT_64 = -9223372036854775808;
7-
public const int SIGN_BIT_32 = -2147483648;
6+
public const long SIGN_BIT_64 = -9223372036854775808;
7+
public const int SIGN_BIT_32 = -2147483648;
88
public const short SIGN_BIT_16 = -32768;
9-
public const sbyte SIGN_BIT_8 = -128;
9+
public const sbyte SIGN_BIT_8 = -128;
1010

1111
// Ceiling function that doesn't deal with floating point values
1212
public static ulong CeilingExact(ulong u1, ulong u2) => (u1 / u2) + (u1 % u2 == 0 ? 0UL : 1UL);
13-
public static long CeilingExact(long u1, long u2) => (u1 / u2) + (u1 % u2 == 0 ? 0L : 1L | ((u1&SIGN_BIT_64)^(u2&SIGN_BIT_64)));
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));
1414
public static uint CeilingExact(uint u1, uint u2) => (u1 / u2) + (u1 % u2 == 0 ? 0U : 1U);
15-
public static int CeilingExact(int u1, int u2) => (u1 / u2) + (u1 % u2 == 0 ? 0 : 1 | ((u1 & SIGN_BIT_32) ^ (u2 & SIGN_BIT_32)));
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));
1616
public static ushort CeilingExact(ushort u1, ushort u2) => (ushort)((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))));
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)));
1818
public static byte CeilingExact(byte u1, byte u2) => (byte)((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))));
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)));
2020

2121
// ZigZag
2222
public static ulong ZigZagEncode(long value) => (ulong)((value >> 63) ^ (value << 1));
@@ -33,5 +33,7 @@ public static int VarIntSize(ulong value) =>
3333
value <= 281474976710655 ? 7 :
3434
value <= 72057594037927935 ? 8 :
3535
9;
36+
37+
internal static long Div8Ceil(ulong value) => (long)((value >> 3) + ((value & 1UL) | ((value >> 1) & 1UL) | ((value >> 2) & 1UL)));
3638
}
3739
}

0 commit comments

Comments
 (0)