From 05ac2603e6e8219b4cb588d2e53eaaf016f24672 Mon Sep 17 00:00:00 2001 From: Kazuki Oikawa Date: Sun, 10 Apr 2011 01:06:26 +0900 Subject: [PATCH] csharp: add BoxingPacker tests --- csharp/msgpack.tests/BoxingPackerTests.cs | 85 +++++++++++++++++++++++ csharp/msgpack.tests/msgpack.tests.csproj | 5 +- csharp/msgpack/BoxingPacker.cs | 20 ++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 csharp/msgpack.tests/BoxingPackerTests.cs diff --git a/csharp/msgpack.tests/BoxingPackerTests.cs b/csharp/msgpack.tests/BoxingPackerTests.cs new file mode 100644 index 00000000..da2209dd --- /dev/null +++ b/csharp/msgpack.tests/BoxingPackerTests.cs @@ -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 (packer, 12345); + RoundtripTest (packer, 1234567890123456789UL); + RoundtripTest (packer, Math.PI); + RoundtripTest (packer, true); + RoundtripTest (packer, false); + } + + [TestMethod] + public void ArrayTest () + { + BoxingPacker packer = new BoxingPacker (); + RoundtripTest (packer, new object[0]); + RoundtripTest (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 dic = new Dictionary (); + Dictionary dic2 = new Dictionary (); + RoundtripTest> (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 (dic2)); + RoundtripTest> (packer, dic); + + dic[1] = ((Dictionary)dic[1]).ToArray (); + Assert.AreEqual (dic, packer.Unpack (packer.Pack (dic.ToArray ()))); + } + + void RoundtripTest (BoxingPacker packer, T obj) + { + T obj2 = (T)packer.Unpack (packer.Pack (obj)); + Assert.AreEqual (obj, obj2); + } + } +} diff --git a/csharp/msgpack.tests/msgpack.tests.csproj b/csharp/msgpack.tests/msgpack.tests.csproj index 4a9f2879..e610dd80 100644 --- a/csharp/msgpack.tests/msgpack.tests.csproj +++ b/csharp/msgpack.tests/msgpack.tests.csproj @@ -36,12 +36,15 @@ - + + False + + diff --git a/csharp/msgpack/BoxingPacker.cs b/csharp/msgpack/BoxingPacker.cs index c513a309..c4608503 100644 --- a/csharp/msgpack/BoxingPacker.cs +++ b/csharp/msgpack/BoxingPacker.cs @@ -21,6 +21,14 @@ namespace msgpack 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) { if (o == null) { @@ -86,6 +94,18 @@ namespace msgpack 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) { if (!reader.Read ())