mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 21:18:23 +01:00
Add MsgPackReader/MsgPackWriter unit tests
This commit is contained in:
parent
0b45e9442b
commit
8949551c2e
330
csharp/msgpack.tests/ReaderWriterTests.cs
Normal file
330
csharp/msgpack.tests/ReaderWriterTests.cs
Normal file
@ -0,0 +1,330 @@
|
|||||||
|
#define NUNIT
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
#if !NUNIT
|
||||||
|
#error Currently, Not Supported
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
#else
|
||||||
|
using NUnit.Framework;
|
||||||
|
using TestClass = NUnit.Framework.TestFixtureAttribute;
|
||||||
|
using TestInitialize = NUnit.Framework.SetUpAttribute;
|
||||||
|
using TestCleanup = NUnit.Framework.TearDownAttribute;
|
||||||
|
using TestMethod = NUnit.Framework.TestAttribute;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace msgpack.tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ReaderWriterTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void SignedNumberTest ()
|
||||||
|
{
|
||||||
|
long[] nums = new long[]{
|
||||||
|
/* positive fixnum */
|
||||||
|
0, 1, 126, 127,
|
||||||
|
|
||||||
|
/* negative fixnum */
|
||||||
|
-1, -2, -31, -32,
|
||||||
|
|
||||||
|
/* int8 */
|
||||||
|
-128, -33,
|
||||||
|
|
||||||
|
/* int16 */
|
||||||
|
-32768, -129, 128, 32767,
|
||||||
|
|
||||||
|
/* int32 */
|
||||||
|
-2147483648, -32769, 32768, 2147483647,
|
||||||
|
|
||||||
|
/* int64 */
|
||||||
|
-9223372036854775808, -2147483649, 2147483648, 9223372036854775807
|
||||||
|
};
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
/* positive fixnum */
|
||||||
|
0, 1, 126, 127,
|
||||||
|
|
||||||
|
/* negative fixnum */
|
||||||
|
0xff, 0xfe, 0xe1, 0xe0,
|
||||||
|
|
||||||
|
/* int8 */
|
||||||
|
0xd0, 0x80,
|
||||||
|
0xd0, 0xdf,
|
||||||
|
|
||||||
|
/* int16 */
|
||||||
|
0xd1, 0x80, 0x00,
|
||||||
|
0xd1, 0xff, 0x7f,
|
||||||
|
0xd1, 0x00, 0x80,
|
||||||
|
0xd1, 0x7f, 0xff,
|
||||||
|
|
||||||
|
/* int32 */
|
||||||
|
0xd2, 0x80, 0x00, 0x00, 0x00,
|
||||||
|
0xd2, 0xff, 0xff, 0x7f, 0xff,
|
||||||
|
0xd2, 0x00, 0x00, 0x80, 0x00,
|
||||||
|
0xd2, 0x7f, 0xff, 0xff, 0xff,
|
||||||
|
|
||||||
|
/* int64 */
|
||||||
|
0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0xd3, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff, 0xff,
|
||||||
|
0xd3, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
|
||||||
|
0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
for (int i = 0; i < nums.Length; i ++) {
|
||||||
|
writer.Write (nums[i]);
|
||||||
|
}
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
for (int i = 0; i < nums.Length; i ++) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.IsSigned () || reader.IsSigned64 ());
|
||||||
|
if (reader.IsSigned64 ()) {
|
||||||
|
Assert.AreEqual (nums[i], reader.ValueSigned64);
|
||||||
|
} else {
|
||||||
|
Assert.AreEqual (nums[i], reader.ValueSigned);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void UnsignedNumberTest ()
|
||||||
|
{
|
||||||
|
ulong[] nums = new ulong[]{
|
||||||
|
/* uint8 */
|
||||||
|
128, byte.MaxValue,
|
||||||
|
|
||||||
|
/* uint16 */
|
||||||
|
byte.MaxValue + 1, ushort.MaxValue,
|
||||||
|
|
||||||
|
/* uint32 */
|
||||||
|
ushort.MaxValue + 1U, uint.MaxValue,
|
||||||
|
|
||||||
|
/* uint64 */
|
||||||
|
uint.MaxValue + 1UL, ulong.MaxValue
|
||||||
|
};
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
/* int8 */
|
||||||
|
0xcc, 0x80,
|
||||||
|
0xcc, 0xff,
|
||||||
|
|
||||||
|
/* int16 */
|
||||||
|
0xcd, 0x01, 0x00,
|
||||||
|
0xcd, 0xff, 0xff,
|
||||||
|
|
||||||
|
/* int32 */
|
||||||
|
0xce, 0x00, 0x01, 0x00, 0x00,
|
||||||
|
0xce, 0xff, 0xff, 0xff, 0xff,
|
||||||
|
|
||||||
|
/* int64 */
|
||||||
|
0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
for (int i = 0; i < nums.Length; i ++) {
|
||||||
|
writer.Write (nums[i]);
|
||||||
|
}
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
for (int i = 0; i < nums.Length; i ++) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.IsUnsigned () || reader.IsUnsigned64 ());
|
||||||
|
if (reader.IsUnsigned64 ()) {
|
||||||
|
Assert.AreEqual (nums[i], reader.ValueUnsigned64);
|
||||||
|
} else {
|
||||||
|
Assert.AreEqual (nums[i], reader.ValueUnsigned);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void NilTest ()
|
||||||
|
{
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
0xc0
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
writer.WriteNil ();
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.Type == TypePrefixes.Nil);
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void BooleanTest ()
|
||||||
|
{
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
0xc3, 0xc2
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
writer.Write (true);
|
||||||
|
writer.Write (false);
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.Type == TypePrefixes.True);
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.Type == TypePrefixes.False);
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void FloatingPointTest ()
|
||||||
|
{
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
0xca, 0x40, 0x49, 0x0f, 0xdb,
|
||||||
|
0xcb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
writer.Write ((float)Math.PI);
|
||||||
|
writer.Write (Math.PI);
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.Type == TypePrefixes.Float);
|
||||||
|
Assert.AreEqual ((float)Math.PI, reader.ValueFloat);
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.Type == TypePrefixes.Double);
|
||||||
|
Assert.AreEqual (Math.PI, reader.ValueDouble);
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void RawTest ()
|
||||||
|
{
|
||||||
|
Random rnd = new Random ();
|
||||||
|
|
||||||
|
byte[][] rawList = new byte[][] {
|
||||||
|
new byte[0],
|
||||||
|
new byte[1],
|
||||||
|
new byte[31],
|
||||||
|
new byte[32],
|
||||||
|
new byte[65535],
|
||||||
|
new byte[65536]
|
||||||
|
};
|
||||||
|
byte[][] headerSizeList = new byte[][] {
|
||||||
|
new byte[] {0xa0},
|
||||||
|
new byte[] {0xa1},
|
||||||
|
new byte[] {0xbf},
|
||||||
|
new byte[] {0xda, 0x00, 0x20},
|
||||||
|
new byte[] {0xda, 0xff, 0xff},
|
||||||
|
new byte[] {0xdb, 0x00, 0x01, 0x00, 0x00},
|
||||||
|
};
|
||||||
|
byte[] expectedBytes = new byte[131135 + 1 * 3 + 3 * 2 + 5];
|
||||||
|
for (int i = 0, offset = 0; i < rawList.Length; i ++) {
|
||||||
|
rnd.NextBytes (rawList[i]);
|
||||||
|
|
||||||
|
Buffer.BlockCopy (headerSizeList[i], 0, expectedBytes, offset, headerSizeList[i].Length);
|
||||||
|
offset += headerSizeList[i].Length;
|
||||||
|
Buffer.BlockCopy (rawList[i], 0, expectedBytes, offset, rawList[i].Length);
|
||||||
|
offset += rawList[i].Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
for (int i = 0; i < rawList.Length; i ++)
|
||||||
|
writer.Write (rawList[i]);
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
for (int i = 0; i < rawList.Length; i ++) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.AreEqual (rawList[i].Length, (int)reader.Length);
|
||||||
|
byte[] tmp = new byte[(int)reader.Length];
|
||||||
|
reader.ReadValueRaw (tmp, 0, tmp.Length);
|
||||||
|
Assert.AreEqual (rawList[i], tmp);
|
||||||
|
}
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ArrayMapRawHeaderTest ()
|
||||||
|
{
|
||||||
|
int[] list = new int[] {
|
||||||
|
0, 1, 15, 16, 31, 32, 65535, 65536
|
||||||
|
};
|
||||||
|
byte[] expectedBytes = new byte[] {
|
||||||
|
/* size=0 */
|
||||||
|
0x90, /* array */
|
||||||
|
0x80, /* map */
|
||||||
|
0xa0, /* raw */
|
||||||
|
|
||||||
|
/* size=1 */
|
||||||
|
0x91,
|
||||||
|
0x81,
|
||||||
|
0xa1,
|
||||||
|
|
||||||
|
/* size=15 */
|
||||||
|
0x9f,
|
||||||
|
0x8f,
|
||||||
|
0xaf,
|
||||||
|
|
||||||
|
/* size=16 */
|
||||||
|
0xdc, 0x00, 0x10, /* array16 */
|
||||||
|
0xde, 0x00, 0x10, /* map16 */
|
||||||
|
0xb0, /* fix raw */
|
||||||
|
|
||||||
|
/* size=31 */
|
||||||
|
0xdc, 0x00, 0x1f, /* array16 */
|
||||||
|
0xde, 0x00, 0x1f, /* map16 */
|
||||||
|
0xbf, /* fix raw */
|
||||||
|
|
||||||
|
/* size=32 */
|
||||||
|
0xdc, 0x00, 0x20, /* array16 */
|
||||||
|
0xde, 0x00, 0x20, /* map16 */
|
||||||
|
0xda, 0x00, 0x20, /* raw16 */
|
||||||
|
|
||||||
|
/* size=65535 */
|
||||||
|
0xdc, 0xff, 0xff, /* array16 */
|
||||||
|
0xde, 0xff, 0xff, /* map16 */
|
||||||
|
0xda, 0xff, 0xff, /* raw16 */
|
||||||
|
|
||||||
|
/* size=65536 */
|
||||||
|
0xdd, 0x00, 0x01, 0x00, 0x00, /* array32 */
|
||||||
|
0xdf, 0x00, 0x01, 0x00, 0x00, /* map32 */
|
||||||
|
0xdb, 0x00, 0x01, 0x00, 0x00, /* raw32 */
|
||||||
|
};
|
||||||
|
|
||||||
|
Test (delegate (MsgPackWriter writer) {
|
||||||
|
for (int i = 0; i < list.Length; i ++) {
|
||||||
|
writer.WriteArrayHeader (list[i]);
|
||||||
|
writer.WriteMapHeader (list[i]);
|
||||||
|
writer.WriteRawHeader (list[i]);
|
||||||
|
}
|
||||||
|
}, delegate (MsgPackReader reader) {
|
||||||
|
for (int i = 0; i < list.Length; i ++) {
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.IsArray ());
|
||||||
|
Assert.AreEqual (list[i], (int)reader.Length);
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.IsMap ());
|
||||||
|
Assert.AreEqual (list[i], (int)reader.Length);
|
||||||
|
Assert.IsTrue (reader.Read ());
|
||||||
|
Assert.IsTrue (reader.IsRaw ());
|
||||||
|
Assert.AreEqual (list[i], (int)reader.Length);
|
||||||
|
}
|
||||||
|
}, expectedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
delegate void WriteDelegate (MsgPackWriter writer);
|
||||||
|
delegate void ReadDelegate (MsgPackReader reader);
|
||||||
|
void Test (WriteDelegate writeProc, ReadDelegate readProc, byte[] expectedBytes)
|
||||||
|
{
|
||||||
|
byte[] raw;
|
||||||
|
using (MemoryStream ms = new MemoryStream ()) {
|
||||||
|
MsgPackWriter writer = new MsgPackWriter (ms);
|
||||||
|
writeProc (writer);
|
||||||
|
raw = ms.ToArray ();
|
||||||
|
}
|
||||||
|
Assert.AreEqual (expectedBytes, raw, "pack failed");
|
||||||
|
using (MemoryStream ms = new MemoryStream (raw)) {
|
||||||
|
MsgPackReader reader = new MsgPackReader (ms);
|
||||||
|
readProc (reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
@ -12,13 +12,14 @@
|
|||||||
<AssemblyName>msgpack.tests</AssemblyName>
|
<AssemblyName>msgpack.tests</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
<DebugType>full</DebugType>
|
<DebugType>full</DebugType>
|
||||||
<Optimize>false</Optimize>
|
<Optimize>false</Optimize>
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>TRACE;DEBUG</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
@ -34,16 +35,14 @@
|
|||||||
<StartupObject />
|
<StartupObject />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
|
||||||
|
<Reference Include="nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
|
||||||
<Reference Include="Microsoft.CSharp" />
|
|
||||||
<Reference Include="System.Data" />
|
|
||||||
<Reference Include="System.Xml" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="AssemblyInfo.cs" />
|
<Compile Include="AssemblyInfo.cs" />
|
||||||
|
<Compile Include="ReaderWriterTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\msgpack\msgpack.csproj">
|
<ProjectReference Include="..\msgpack\msgpack.csproj">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user