This commit is contained in:
parent
39ca103842
commit
42d572d857
3 changed files with 268 additions and 12 deletions
73
test/Nyanbyte.Sysnya.CoreTests/Binary/BinaryMapperTests.cs
Normal file
73
test/Nyanbyte.Sysnya.CoreTests/Binary/BinaryMapperTests.cs
Normal file
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Threading.Tasks;
|
||||
using Nyanbyte.Sysnya.Binary;
|
||||
|
||||
namespace Nyanbyte.Sysnya.CoreTests.Binary;
|
||||
|
||||
public class BinaryMapperTests
|
||||
{
|
||||
|
||||
private class Dummy
|
||||
{
|
||||
public short Int16 { get; set; }
|
||||
public ushort UInt16 { get; set; }
|
||||
public int Int32 { get; set; }
|
||||
public uint UInt32 { get; set; }
|
||||
public long Int64 { get; set; }
|
||||
public ulong UInt64 { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Map32Bit()
|
||||
{
|
||||
// Given
|
||||
Span<byte> bytes = stackalloc byte[8];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes, 0xfeedbeef);
|
||||
BinaryPrimitives.WriteInt32BigEndian(bytes[4..], 0xbeef);
|
||||
|
||||
BinaryStructMapperBuilder<Dummy> b = new(0, true);
|
||||
|
||||
// When
|
||||
var mapper = b.BindU32(p => p.UInt32).BindI32(p => p.Int32).Build();
|
||||
var dummy = mapper.Map(bytes.ToArray());
|
||||
|
||||
// Then
|
||||
Assert.Equal(0xfeedbeef, dummy.UInt32);
|
||||
Assert.Equal(0xbeef, dummy.Int32);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MapAll()
|
||||
{
|
||||
// Given
|
||||
Span<byte> bytes = stackalloc byte[28];
|
||||
BinaryPrimitives.WriteUInt16BigEndian(bytes[0..], 0xbabe);
|
||||
BinaryPrimitives.WriteInt16BigEndian(bytes[2..], 0xbf);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(bytes[4..], 0xfeedbeef);
|
||||
BinaryPrimitives.WriteInt32BigEndian(bytes[8..], 0xbeef);
|
||||
BinaryPrimitives.WriteUInt64BigEndian(bytes[12..], 0xbad1dea0babebebe);
|
||||
BinaryPrimitives.WriteInt64BigEndian(bytes[20..], 0x1dea0babebebe);
|
||||
|
||||
BinaryStructMapperBuilder<Dummy> b = new(0, true);
|
||||
|
||||
// When
|
||||
var mapper = b
|
||||
.BindU16(p => p.UInt16).BindI16(p => p.Int16)
|
||||
.BindU32(p => p.UInt32).BindI32(p => p.Int32)
|
||||
.BindU64(p => p.UInt64).BindI64(p => p.Int64)
|
||||
.Build();
|
||||
var dummy = mapper.Map(bytes.ToArray());
|
||||
|
||||
// Then
|
||||
Assert.Equal(0xbabe, dummy.UInt16);
|
||||
Assert.Equal(0xbf, dummy.Int16);
|
||||
Assert.Equal(0xfeedbeef, dummy.UInt32);
|
||||
Assert.Equal(0xbeef, dummy.Int32);
|
||||
Assert.Equal(0xbad1dea0babebebe, dummy.UInt64);
|
||||
Assert.Equal(0x1dea0babebebe, dummy.Int64);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue