Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions NetSerializer.UnitTests/HalfTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#if NET5_0
using System;
using System.IO;
using NUnit.Framework;

namespace NetSerializer.UnitTests
{
[TestFixture]
[TestOf(typeof(Primitives))]
[Parallelizable(ParallelScope.All)]
public class HalfTest
{
[Test]
public void Test()
{
var serializer = new Serializer(new[] {typeof(SerializationType)});

var stream = new MemoryStream();
var obj = new SerializationType
{
R = (Half) 0,
G = (Half) 12.34,
B = (Half) 0.1,
A = Half.PositiveInfinity,
};

serializer.Serialize(stream, obj);

stream.Position = 0;

var read = (SerializationType) serializer.Deserialize(stream);

Assert.That(read.R, Is.EqualTo(obj.R));
Assert.That(read.G, Is.EqualTo(obj.G));
Assert.That(read.B, Is.EqualTo(obj.B));
Assert.That(read.A, Is.EqualTo(obj.A));
}

[Serializable]
private class SerializationType
{
public Half R;
public Half G;
public Half B;
public Half A;
}
}
}
#endif
26 changes: 26 additions & 0 deletions NetSerializer.UnitTests/NetSerializer.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netcoreapp3.1;net5.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="0.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetSerializer\NetSerializer.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
130 changes: 130 additions & 0 deletions NetSerializer.UnitTests/PrimitivesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.IO;
using NUnit.Framework;

namespace NetSerializer.UnitTests
{
[TestFixture]
[TestOf(typeof(Primitives))]
[Parallelizable(ParallelScope.All)]
public class PrimitivesTest
{
#if NET5_0
#if NO_UNSAFE
[Ignore("Float and half tests are inacurrate due to rounding when NO_UNSAFE is enabled.")]
#endif
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(123.4)]
[TestCase(0.01)]
[TestCase(double.PositiveInfinity)]
[TestCase(double.NegativeInfinity)]
[TestCase(double.NaN)]
public void TestHalf(double val)
{
// Can't stick Half values in attributes so have to do this.
var half = (Half) val;

var stream = new MemoryStream();
Primitives.WritePrimitive(stream, half);

stream.Position = 0;

Primitives.ReadPrimitive(new ByteStream(stream), out Half read);
Assert.That(read, Is.EqualTo(half));
}
#endif

#if NO_UNSAFE
[Ignore("Float tests are inacurrate due to rounding when NO_UNSAFE is enabled.")]
#endif
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(123.4f)]
[TestCase(0.01f)]
[TestCase(float.PositiveInfinity)]
[TestCase(float.NegativeInfinity)]
[TestCase(float.NaN)]
public void TestSingle(float val)
{
var stream = new MemoryStream();
Primitives.WritePrimitive(stream, val);

stream.Position = 0;

Primitives.ReadPrimitive(new ByteStream(stream), out float read);
Assert.That(read, Is.EqualTo(val));
}

[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(123.4)]
[TestCase(0.01)]
[TestCase(float.PositiveInfinity)]
[TestCase(float.NegativeInfinity)]
[TestCase(float.NaN)]
public void TestDouble(double val)
{
var stream = new MemoryStream();
Primitives.WritePrimitive(stream, val);

stream.Position = 0;

Primitives.ReadPrimitive(new ByteStream(stream), out double read);
Assert.That(read, Is.EqualTo(val));
}

// Stream wrapper that only reads one byte at a time to test the reading code.
private sealed class ByteStream : Stream
{
private readonly Stream _parent;

public ByteStream(Stream parent)
{
_parent = parent;
}

public override void Flush()
{
_parent.Flush();
}

public override long Seek(long offset, SeekOrigin origin)
{
return _parent.Seek(offset, origin);
}

public override void SetLength(long value)
{
_parent.SetLength(value);
}

public override int Read(byte[] buffer, int offset, int count)
{
return _parent.Read(buffer, offset, 1);
}

public override void Write(byte[] buffer, int offset, int count)
{
_parent.Write(buffer, offset, count);
}

public override bool CanRead => _parent.CanRead;

public override bool CanSeek => _parent.CanSeek;

public override bool CanWrite => _parent.CanWrite;

public override long Length => _parent.Length;

public override long Position
{
get => _parent.Position;
set => _parent.Position = value;
}
}
}
}
6 changes: 6 additions & 0 deletions NetSerializer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrimitiveTest", "PrimitiveTest\PrimitiveTest.csproj", "{CBA6D818-4B6A-4A80-95D5-7F5EC3FBB3C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetSerializer.UnitTests", "NetSerializer.UnitTests\NetSerializer.UnitTests.csproj", "{17DC557B-DB9E-464C-A311-5AB83E5684AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{CBA6D818-4B6A-4A80-95D5-7F5EC3FBB3C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBA6D818-4B6A-4A80-95D5-7F5EC3FBB3C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBA6D818-4B6A-4A80-95D5-7F5EC3FBB3C4}.Release|Any CPU.Build.0 = Release|Any CPU
{17DC557B-DB9E-464C-A311-5AB83E5684AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17DC557B-DB9E-464C-A311-5AB83E5684AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17DC557B-DB9E-464C-A311-5AB83E5684AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17DC557B-DB9E-464C-A311-5AB83E5684AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion NetSerializer/NetSerializer.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net45;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net45;netcoreapp3.1;net5.0</TargetFrameworks>
<Description>A very fast and minimal serializer</Description>
<Company>Tomi Valkeinen</Company>
<Copyright>Copyright © 2012-2019 Tomi Valkeinen</Copyright>
Expand Down
Loading