-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test cases for read and write VarInt, removed VarLong as it is …
…not being used, WriteVarInt now expects int instead of uint (reflected this change within Minecraft.cs).
- Loading branch information
1 parent
fae8bb0
commit a35adb7
Showing
3 changed files
with
101 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,105 @@ | ||
using MCQuery; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace MCQueryTests | ||
{ | ||
[TestClass] | ||
public class UtilitiesTest | ||
{ | ||
[TestMethod] | ||
public void ReadVarIntTest() | ||
public void WriteVarIntTest() | ||
{ | ||
byte[] samplePacketLength = new byte[] { 0x81, 0x01 }; // VarInt(129) == 0x81, 0x01 | ||
byte[] samplePacketId = new byte[] { 0x00 }; // VarInt(0) == 0x00 | ||
byte[] sampleResponseLength = new byte[] { 0x7F }; // VarInt(127) == 0x7F | ||
|
||
bool cond1, cond2, cond3; | ||
int[] testCases = new int[9] | ||
{ | ||
0, | ||
1, | ||
2, | ||
127, | ||
128, | ||
255, | ||
2147483647, | ||
-1, | ||
-2147483648 | ||
}; | ||
byte[][] testCasesResults = new byte[9][] | ||
{ | ||
new byte[] { 0x00 }, // VarInt(0) | ||
new byte[] { 0x01 }, // VarInt(1) | ||
new byte[] { 0x02 }, // VarInt(2) | ||
new byte[] { 0x7F }, // VarInt(127) | ||
new byte[] { 0x80, 0x01 }, // VarInt(128) | ||
new byte[] { 0xFF, 0x01 }, // VarInt(255) | ||
new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 }, // VarInt(2147483647) | ||
new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, }, // VarInt(-1) | ||
new byte[] { 0x80, 0x80, 0x80, 0x80, 0x08 } // VarInt(-2147483648) | ||
}; | ||
|
||
using (MemoryStream memory = new MemoryStream(samplePacketLength)) | ||
using (MemoryStream memory = new MemoryStream()) | ||
{ | ||
int result = MCQuery.Utilities.ReadVarInt(memory); | ||
cond1 = result == 129; | ||
for (int i = 0; i < testCases.Length; ++i) | ||
{ | ||
memory.SetLength(0); // Reset stream | ||
Utilities.WriteVarInt(memory, testCases[i]); // Write integer (VarInt) to stream | ||
byte[] result = memory.ToArray(); | ||
if (!testCasesResults[i].SequenceEqual(result)) | ||
Assert.Fail($"Expected {BitConverter.ToString(testCasesResults[i])}, got {BitConverter.ToString(result)}"); | ||
} | ||
} | ||
} | ||
|
||
using (MemoryStream memory = new MemoryStream(samplePacketId)) | ||
[TestMethod] | ||
public void ReadVarIntTest() | ||
{ | ||
byte[][] testCases = new byte[9][] | ||
{ | ||
int result = MCQuery.Utilities.ReadVarInt(memory); | ||
cond2 = result == 0; | ||
} | ||
new byte[] { 0x00 }, // VarInt(0) | ||
new byte[] { 0x01 }, // VarInt(1) | ||
new byte[] { 0x02 }, // VarInt(2) | ||
new byte[] { 0x7F }, // VarInt(127) | ||
new byte[] { 0x80, 0x01 }, // VarInt(128) | ||
new byte[] { 0xFF, 0x01 }, // VarInt(255) | ||
new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 }, // VarInt(2147483647) | ||
new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, }, // VarInt(-1) | ||
new byte[] { 0x80, 0x80, 0x80, 0x80, 0x08 } // VarInt(-2147483648) | ||
}; | ||
int[] testCasesResults = new int[9] | ||
{ | ||
0, | ||
1, | ||
2, | ||
127, | ||
128, | ||
255, | ||
2147483647, | ||
-1, | ||
-2147483648 | ||
}; | ||
|
||
using (MemoryStream memory = new MemoryStream(sampleResponseLength)) | ||
using (MemoryStream memory = new MemoryStream()) | ||
{ | ||
int result = MCQuery.Utilities.ReadVarInt(memory); | ||
cond3 = result == 127; | ||
for (int i = 0; i < testCases.Length; ++i) | ||
{ | ||
int result = ReadVarIntHelper(memory, testCases[i]); | ||
if (result != testCasesResults[i]) | ||
Assert.Fail($"Expected {testCasesResults[i]}, got {result}."); | ||
} | ||
} | ||
} | ||
|
||
Assert.IsTrue(cond1 && cond2 && cond3); | ||
private int ReadVarIntHelper(Stream stream, byte[] data) | ||
{ | ||
StreamHelper(stream, data); // Setup stream for testing | ||
return Utilities.ReadVarInt(stream); // Get VarInt from stream | ||
} | ||
|
||
private void StreamHelper(Stream stream, byte[] data) | ||
{ | ||
stream.SetLength(0); // Reset stream | ||
stream.Write(data); // Write test bytes | ||
stream.Seek(0, SeekOrigin.Begin); // Go back to the start for reading | ||
} | ||
} | ||
} |