-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic support for fixed-point numbers.
- Loading branch information
1 parent
037e660
commit 7d51b01
Showing
19 changed files
with
430 additions
and
130 deletions.
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,50 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Numerics; | ||
|
||
using NUnit.Framework; | ||
|
||
using schema.binary.attributes; | ||
using schema.testing; | ||
using schema.util.asserts; | ||
|
||
|
||
namespace schema.binary; | ||
|
||
public partial class FixedPointTests { | ||
[BinarySchema] | ||
private partial class FixedPointWrapper : IBinaryConvertible { | ||
[FixedPoint(1, 19, 12)] | ||
public float FloatValue { get; set; } | ||
|
||
[FixedPoint(1, 19, 12)] | ||
public double DoubleValue { get; set; } | ||
} | ||
|
||
[Test] | ||
public void TestReadsAsExpected() { | ||
using var br = SchemaMemoryStream.From([123, 456]).GetBinaryReader(); | ||
|
||
var wrapper = br.ReadNew<FixedPointWrapper>(); | ||
Assert.AreEqual(0.0300292969f, wrapper.FloatValue); | ||
Assert.AreEqual(0.111328125, wrapper.DoubleValue); | ||
} | ||
|
||
[Test] | ||
public void TestWritesAsExpected() { | ||
var wrapper = new FixedPointWrapper { | ||
FloatValue = 0.0300292969f, DoubleValue = 0.111328125, | ||
}; | ||
|
||
var bw = new SchemaBinaryWriter(); | ||
wrapper.Write(bw); | ||
|
||
using var ms = new MemoryStream(); | ||
bw.CompleteAndCopyTo(ms); | ||
ms.Position = 0; | ||
|
||
var br = new SchemaBinaryReader(ms); | ||
Assert.AreEqual(123, br.ReadUInt32()); | ||
Assert.AreEqual(456, br.ReadUInt32()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using NUnit.Framework; | ||
|
||
|
||
namespace schema.binary.text; | ||
|
||
internal class FixedPointTests { | ||
[Test] | ||
public void TestMutableFixedPoint_1_19_12() | ||
=> BinarySchemaTestUtil.AssertGenerated( | ||
""" | ||
using System.Numerics; | ||
using schema.binary; | ||
using schema.binary.attributes; | ||
namespace foo.bar; | ||
[BinarySchema] | ||
public partial class Wrapper { | ||
[FixedPoint(1, 19, 12)] | ||
public float SingleField { get; set; } | ||
[FixedPoint(1, 19, 12)] | ||
public double DoubleField { get; set; } | ||
} | ||
""", | ||
""" | ||
using System; | ||
using schema.binary; | ||
using schema.util; | ||
namespace foo.bar; | ||
public partial class Wrapper { | ||
public void Read(IBinaryReader br) { | ||
this.SingleField = BitLogic.ConvertFixedPointToSingle(br.ReadUInt32(), 1, 19, 12); | ||
this.DoubleField = BitLogic.ConvertFixedPointToDouble(br.ReadUInt32(), 1, 19, 12); | ||
} | ||
} | ||
""", | ||
""" | ||
using System; | ||
using schema.binary; | ||
using schema.util; | ||
namespace foo.bar; | ||
public partial class Wrapper { | ||
public void Write(IBinaryWriter bw) { | ||
bw.WriteUInt32(BitLogic.ConvertSingleToFixedPoint(this.SingleField, 1, 19, 12)); | ||
bw.WriteUInt32(BitLogic.ConvertDoubleToFixedPoint(this.DoubleField, 1, 19, 12)); | ||
} | ||
} | ||
"""); | ||
|
||
[Test] | ||
public void TestReadonlyFixedPoint_1_19_12() | ||
=> BinarySchemaTestUtil.AssertGenerated( | ||
""" | ||
using System.Numerics; | ||
using schema.binary; | ||
using schema.binary.attributes; | ||
namespace foo.bar; | ||
[BinarySchema] | ||
public partial class Wrapper { | ||
[FixedPoint(1, 19, 12)] | ||
public float SingleField { get; } | ||
[FixedPoint(1, 19, 12)] | ||
public double DoubleField { get; } | ||
} | ||
""", | ||
""" | ||
using System; | ||
using schema.binary; | ||
using schema.util; | ||
namespace foo.bar; | ||
public partial class Wrapper { | ||
public void Read(IBinaryReader br) { | ||
br.AssertUInt32(BitLogic.ConvertSingleToFixedPoint(this.SingleField, 1, 19, 12)); | ||
br.AssertUInt32(BitLogic.ConvertDoubleToFixedPoint(this.DoubleField, 1, 19, 12)); | ||
} | ||
} | ||
""", | ||
""" | ||
using System; | ||
using schema.binary; | ||
using schema.util; | ||
namespace foo.bar; | ||
public partial class Wrapper { | ||
public void Write(IBinaryWriter bw) { | ||
bw.WriteUInt32(BitLogic.ConvertSingleToFixedPoint(this.SingleField, 1, 19, 12)); | ||
bw.WriteUInt32(BitLogic.ConvertDoubleToFixedPoint(this.DoubleField, 1, 19, 12)); | ||
} | ||
} | ||
"""); | ||
} |
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
Oops, something went wrong.