mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-25 18:23:00 +02:00
csharp: add BoxingPacker tests
This commit is contained in:
85
csharp/msgpack.tests/BoxingPackerTests.cs
Normal file
85
csharp/msgpack.tests/BoxingPackerTests.cs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#define NUNIT
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
#if !NUNIT
|
||||||
|
#error Currently, Not Supported
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
#else
|
||||||
|
using NUnit.Framework;
|
||||||
|
using TestClass = NUnit.Framework.TestFixtureAttribute;
|
||||||
|
using TestMethod = NUnit.Framework.TestAttribute;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace msgpack.tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class BoxingPackerTests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void NullTest ()
|
||||||
|
{
|
||||||
|
BoxingPacker packer = new BoxingPacker ();
|
||||||
|
Assert.IsNull (packer.Unpack (packer.Pack (null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void PrimitiveTypeTest ()
|
||||||
|
{
|
||||||
|
BoxingPacker packer = new BoxingPacker ();
|
||||||
|
RoundtripTest<int> (packer, 12345);
|
||||||
|
RoundtripTest<ulong> (packer, 1234567890123456789UL);
|
||||||
|
RoundtripTest<double> (packer, Math.PI);
|
||||||
|
RoundtripTest<bool> (packer, true);
|
||||||
|
RoundtripTest<bool> (packer, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ArrayTest ()
|
||||||
|
{
|
||||||
|
BoxingPacker packer = new BoxingPacker ();
|
||||||
|
RoundtripTest<object[]> (packer, new object[0]);
|
||||||
|
RoundtripTest<object[]> (packer, new object[]{
|
||||||
|
int.MinValue, int.MaxValue, 1234567890123456789UL, ulong.MaxValue,
|
||||||
|
float.MinValue, float.MaxValue, float.Epsilon, float.NaN, float.PositiveInfinity, float.NegativeInfinity,
|
||||||
|
double.MinValue, double.MaxValue, double.Epsilon, double.NaN, double.PositiveInfinity, double.NegativeInfinity,
|
||||||
|
null, true, false, new object[] {
|
||||||
|
new object[] {1, 2, 3},
|
||||||
|
new object[] {Math.PI, true}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void MapTest ()
|
||||||
|
{
|
||||||
|
BoxingPacker packer = new BoxingPacker ();
|
||||||
|
Dictionary<object, object> dic = new Dictionary<object,object> ();
|
||||||
|
Dictionary<object, object> dic2 = new Dictionary<object,object> ();
|
||||||
|
RoundtripTest<IDictionary<object,object>> (packer, dic);
|
||||||
|
|
||||||
|
dic2.Add (123, 456);
|
||||||
|
dic2.Add (234, 567);
|
||||||
|
dic2.Add (345, 678);
|
||||||
|
|
||||||
|
dic.Add (0, 0.123);
|
||||||
|
dic.Add (Math.PI, true);
|
||||||
|
dic.Add (false, new object[] {1, 2, 3});
|
||||||
|
dic.Add (1, new Dictionary<object,object> (dic2));
|
||||||
|
RoundtripTest<IDictionary<object,object>> (packer, dic);
|
||||||
|
|
||||||
|
dic[1] = ((Dictionary<object,object>)dic[1]).ToArray ();
|
||||||
|
Assert.AreEqual (dic, packer.Unpack (packer.Pack (dic.ToArray ())));
|
||||||
|
}
|
||||||
|
|
||||||
|
void RoundtripTest<T> (BoxingPacker packer, T obj)
|
||||||
|
{
|
||||||
|
T obj2 = (T)packer.Unpack (packer.Pack (obj));
|
||||||
|
Assert.AreEqual (obj, obj2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,12 +36,15 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
|
<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="nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="AssemblyInfo.cs" />
|
<Compile Include="AssemblyInfo.cs" />
|
||||||
|
<Compile Include="BoxingPackerTests.cs" />
|
||||||
<Compile Include="ReaderWriterTests.cs" />
|
<Compile Include="ReaderWriterTests.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -21,6 +21,14 @@ namespace msgpack
|
|||||||
Pack (writer, o);
|
Pack (writer, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] Pack (object o)
|
||||||
|
{
|
||||||
|
using (MemoryStream ms = new MemoryStream ()) {
|
||||||
|
Pack (ms, o);
|
||||||
|
return ms.ToArray ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Pack (MsgPackWriter writer, object o)
|
void Pack (MsgPackWriter writer, object o)
|
||||||
{
|
{
|
||||||
if (o == null) {
|
if (o == null) {
|
||||||
@@ -86,6 +94,18 @@ namespace msgpack
|
|||||||
return Unpack (reader);
|
return Unpack (reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public object Unpack (byte[] buf, int offset, int size)
|
||||||
|
{
|
||||||
|
using (MemoryStream ms = new MemoryStream (buf, offset, size)) {
|
||||||
|
return Unpack (ms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public object Unpack (byte[] buf)
|
||||||
|
{
|
||||||
|
return Unpack (buf, 0, buf.Length);
|
||||||
|
}
|
||||||
|
|
||||||
object Unpack (MsgPackReader reader)
|
object Unpack (MsgPackReader reader)
|
||||||
{
|
{
|
||||||
if (!reader.Read ())
|
if (!reader.Read ())
|
||||||
|
|||||||
Reference in New Issue
Block a user