Skip to content

Commit

Permalink
Initial Check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsomers committed Jan 3, 2016
1 parent 9a44513 commit ddbd84a
Show file tree
Hide file tree
Showing 104 changed files with 9,576 additions and 0 deletions.
43 changes: 43 additions & 0 deletions LightUnitTests/BaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using LsMsgPack;
using NUnit.Framework;
using System.IO;

namespace LsMsgPackUnitTests {
public class MsgPackTests {

public static MsgPackItem RoundTripTest<Typ, T>(T value, int expectedLength, MsgPackTypeId expectedMsgPackType, sbyte extensionType=0) where Typ : MsgPackItem {

MsgPackItem item;
if(typeof(Typ) == typeof(MpExt)) {
item = new MpExt() {
Value = value,
TypeSpecifier = extensionType
};
} else
item = MsgPackItem.Pack(value);
byte[] buffer = item.ToBytes();
Type expectedType = typeof(Typ);
Type foundType = item.GetType();

MsgPackTypeId resultType = item.TypeId;
bool typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
Assert.IsTrue(typesAreEqual, string.Concat("Expected packed type of ", expectedMsgPackType, " but received the type ", resultType));
Assert.True(foundType == expectedType, string.Concat("Expected type of ", expectedType.ToString(), " but received the type ", foundType.ToString()));
if(expectedLength>=0) Assert.AreEqual(expectedLength, buffer.Length, string.Concat("Expected a packed length of ", expectedLength.ToString(), " bytes but got ", buffer.Length.ToString(), " bytes."));

MsgPackItem recreate = MsgPackItem.Unpack(new MemoryStream(buffer));

resultType = recreate.TypeId;
typesAreEqual = (expectedMsgPackType & resultType) == expectedMsgPackType;
Assert.IsTrue(typesAreEqual, string.Concat("Expected unpacked type of ", expectedMsgPackType, " but received the type ", resultType));

T ret = recreate.GetTypedValue<T>();

Assert.AreEqual(value, ret, "The returned value ", ret, " differs from the input value ", value);

return recreate;
}

}
}
74 changes: 74 additions & 0 deletions LightUnitTests/LightUnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{42154423-A1C1-4C36-BA9B-30FD0E6BF9D2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LightUnitTests</RootNamespace>
<AssemblyName>LightUnitTests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.6.2.12296, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\NUnit 2.6.2\bin\framework\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<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.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BaseTest.cs" />
<Compile Include="MpArrayTest.cs" />
<Compile Include="MpBinTest.cs" />
<Compile Include="MpBoolTest.cs" />
<Compile Include="MpExtTest.cs" />
<Compile Include="MpFloatTest.cs" />
<Compile Include="MpIntTest.cs" />
<Compile Include="MpMapTest.cs" />
<Compile Include="MpNullTest.cs" />
<Compile Include="MpStringTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerializingObjects.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LsMsgPackL\LsMsgPackL.csproj">
<Project>{601dbb96-b626-4bb0-bbe8-21eeda5f24e8}</Project>
<Name>LsMsgPackL</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
66 changes: 66 additions & 0 deletions LightUnitTests/MpArrayTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using LsMsgPack;
using NUnit.Framework;

namespace LsMsgPackUnitTests {
[TestFixture]
public class MpArrayTest {

private static Randomizer rnd = new Randomizer();

[TestCase(0, 1, MsgPackTypeId.MpArray4)]
[TestCase(1, 2, MsgPackTypeId.MpArray4)]
[TestCase(15, 16, MsgPackTypeId.MpArray4)]
[TestCase(200, 203, MsgPackTypeId.MpArray16)]
[TestCase(256, 259, MsgPackTypeId.MpArray16)]
[TestCase(ushort.MaxValue, ushort.MaxValue + 3, MsgPackTypeId.MpArray16)]
[TestCase(ushort.MaxValue + 1, ushort.MaxValue + 6, MsgPackTypeId.MpArray32)]
// [TestCase(0x7FEFFFF9, 0x7FEFFFF9 + 6, MsgPackTypeId.MpArray32)] // Out of memory on my machine
public void ArrayLengths(int length, int expectedBytes, MsgPackTypeId expedctedType) {
object[] test = new object[length];
int additionalBytes = FillArrayWithRandomNumbers(test);
additionalBytes -= test.Length;
MsgPackItem item = MsgPackTests.RoundTripTest<MpArray, object[]>(test, expectedBytes + additionalBytes, expedctedType);

object[] ret = item.GetTypedValue<object[]>();

Assert.AreEqual(length, ret.Length, string.Concat("Expected ", length, " items but got ", ret.Length, " items in the array."));
for(int t = ret.Length - 1; t >= 0; t--) {
Assert.AreEqual(test[t], ret[t], string.Concat("Expected ", test[t], " but got ", ret[t], " at index ", t));
}
}

private int FillArrayWithRandomNumbers(object[] test) {
int addSize = 0;
for(int t = test.Length - 1; t >= 0; t--) {
int newNr = rnd.Next();
addSize += new MpInt() { Value = newNr }.ToBytes().Length;
test[t] = newNr;
}
return addSize;
}

[TestCase(40)]
public void AssortedMix(int expectedLength) {
object[] items = new object[] {
true,
false,
null,
128,
-30,
5.2d,
900.1f,
"Hallo!",
new byte[]{1,2,3 },
new object[] { true,null,"yes"}
};
MsgPackItem item = MsgPackTests.RoundTripTest<MpArray, object[]>(items, expectedLength, MsgPackTypeId.MpArray4);

object[] ret = item.GetTypedValue<object[]>();

Assert.AreEqual(items.Length, ret.Length, string.Concat("Expected ", items.Length, " items but got ", ret.Length, " items in the array."));
for(int t = ret.Length - 1; t >= 0; t--) {
Assert.AreEqual(items[t], ret[t], string.Concat("Expected ", items[t], " but got ", ret[t], " at index ", t));
}
}
}
}
23 changes: 23 additions & 0 deletions LightUnitTests/MpBinTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using LsMsgPack;
using NUnit.Framework;

namespace LsMsgPackUnitTests {
[TestFixture]
public class MpBinTest {

[TestCase(0, 2, MsgPackTypeId.MpBin8)]
[TestCase(1, 3, MsgPackTypeId.MpBin8)]
[TestCase(32, 34, MsgPackTypeId.MpBin8)]
[TestCase(255, 257, MsgPackTypeId.MpBin8)]
[TestCase(256, 259, MsgPackTypeId.MpBin16)]
[TestCase(ushort.MaxValue, ushort.MaxValue + 3, MsgPackTypeId.MpBin16)]
[TestCase(ushort.MaxValue + 1, ushort.MaxValue + 6, MsgPackTypeId.MpBin32)]
// [TestCase(0x7FEFFFF9, 0x7FEFFFF9 + 6, MsgPackTypeId.MpBin32)] // Out of memory on my machine
public void BinaryLengths(int length, int expectedBytes, MsgPackTypeId expedctedType) {
Randomizer rnd = new Randomizer();
byte[] test = new byte[length];
rnd.NextBytes(test);
MsgPackTests.RoundTripTest<MpBin, byte[]>(test, expectedBytes, expedctedType);
}
}
}
23 changes: 23 additions & 0 deletions LightUnitTests/MpBoolTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using LsMsgPack;

namespace LsMsgPackUnitTests {
[TestFixture]
public class MpBoolTest {

[Test]
public void RoundTripTestFalse() {
MsgPackTests.RoundTripTest<MpBool,bool>(false, 1, MsgPackTypeId.MpBoolFalse);
}

[Test]
public void RoundTripTestTrue() {
MsgPackTests.RoundTripTest<MpBool,bool>(true, 1, MsgPackTypeId.MpBoolTrue);
}

}
}
32 changes: 32 additions & 0 deletions LightUnitTests/MpExtTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using LsMsgPack;
using NUnit.Framework;

namespace LsMsgPackUnitTests {
[TestFixture]
public class MpExtTest {

[TestCase(0, 3, MsgPackTypeId.MpExt8)]
[TestCase(1, 3, MsgPackTypeId.MpFExt1)]
[TestCase(2, 4, MsgPackTypeId.MpFExt2)]
[TestCase(3, 6, MsgPackTypeId.MpExt8)]
[TestCase(4, 6, MsgPackTypeId.MpFExt4)]
[TestCase(5, 8, MsgPackTypeId.MpExt8)]
[TestCase(7, 10, MsgPackTypeId.MpExt8)]
[TestCase(8, 10, MsgPackTypeId.MpFExt8)]
[TestCase(9, 12, MsgPackTypeId.MpExt8)]
[TestCase(15, 18, MsgPackTypeId.MpExt8)]
[TestCase(16, 18, MsgPackTypeId.MpExt16)]
[TestCase(17, 20, MsgPackTypeId.MpExt8)]
[TestCase(255, 258, MsgPackTypeId.MpExt8)]
[TestCase(256, 260, MsgPackTypeId.MpExt16)]
[TestCase(ushort.MaxValue, ushort.MaxValue + 4, MsgPackTypeId.MpExt16)]
[TestCase(ushort.MaxValue+1, ushort.MaxValue + 7, MsgPackTypeId.MpExt32)]
public void BinaryLengths(int length, int expectedBytes, MsgPackTypeId expedctedType) {
Randomizer rnd = new Randomizer();
byte[] test = new byte[length];
rnd.NextBytes(test);
MsgPackTests.RoundTripTest<MpExt, byte[]>(test, expectedBytes, expedctedType, (sbyte)(rnd.Next(255) - 128));
}

}
}
35 changes: 35 additions & 0 deletions LightUnitTests/MpFloatTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using LsMsgPack;
using NUnit.Framework;

namespace LsMsgPackUnitTests {
[TestFixture]
public class MpFloatTest {

[TestCase(0f)]
[TestCase(1f)]
[TestCase(-1f)]
[TestCase(float.MinValue)]
[TestCase(float.MaxValue)]
[TestCase(float.NegativeInfinity)]
[TestCase(float.PositiveInfinity)]
[TestCase(float.Epsilon)]
[TestCase(-float.Epsilon)]
public void RoundTripFloat32(float value) {
MsgPackTests.RoundTripTest<MpFloat, float>(value, 5, MsgPackTypeId.MpFloat);
}

[TestCase(0d)]
[TestCase(1d)]
[TestCase(-1d)]
[TestCase(double.MinValue)]
[TestCase(double.MaxValue)]
[TestCase(double.NegativeInfinity)]
[TestCase(double.PositiveInfinity)]
[TestCase(double.Epsilon)]
[TestCase(-double.Epsilon)]
public void RoundTripFloat64(double value) {
MsgPackTests.RoundTripTest<MpFloat, double>(value, 9, MsgPackTypeId.MpDouble);
}

}
}
Loading

0 comments on commit ddbd84a

Please sign in to comment.