-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve float serialization, add .NET 5 Half support. #77
Open
PJB3005
wants to merge
6
commits into
tomba:master
Choose a base branch
from
space-wizards:20-11-21-floats-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19c22ba
Target netcoreapp3.1 instead of netcoreapp3.0
PJB3005 253923c
Add .NET 5 support.
PJB3005 b7afbfb
Add unit testing project, float tests.
PJB3005 646e77b
Improve float serialization.
PJB3005 915c8de
Add support for .NET 5 Half type.
PJB3005 701fbdc
Fix indentation.
PJB3005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
|
||
namespace NetSerializer.UnitTests | ||
{ | ||
[TestFixture] | ||
[TestOf(typeof(Primitives))] | ||
[Parallelizable(ParallelScope.All)] | ||
public class PrimitivesTest | ||
{ | ||
#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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these two methods be part of the next commit ("Add support for .NET 5 Half type.")?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was kind of debating that as well but figured this would make the diffs cleaner since now all these uint read/write methods are in the same commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasons why I think it should not be part of the commit: