Skip to content
Merged
22 changes: 22 additions & 0 deletions tests/SerializationModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}

[ZeroSerializer]
public readonly struct EnumStruct

Check warning on line 79 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Debug)

Struct 'EnumStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization

Check warning on line 79 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Release)

Struct 'EnumStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization
{
public EnumStruct(ByteState byteState, SignedState signedState)
{
Expand Down Expand Up @@ -149,7 +149,7 @@
}

[ZeroSerializer]
public readonly struct SmallFixedStruct

Check warning on line 152 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Debug)

Struct 'SmallFixedStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization

Check warning on line 152 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Release)

Struct 'SmallFixedStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization
{
public SmallFixedStruct(int value)
{
Expand All @@ -160,7 +160,7 @@
}

[ZeroSerializer]
public readonly struct LargeFixedStruct

Check warning on line 163 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Debug)

Struct 'LargeFixedStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization

Check warning on line 163 in tests/SerializationModels.cs

View workflow job for this annotation

GitHub Actions / test (Release)

Struct 'LargeFixedStruct' has a Blittable-compatible field shape; use StructLayout(LayoutKind.Sequential, Pack = 1) to enable raw payload serialization
{
public LargeFixedStruct(long value, ByteState state)
{
Expand Down Expand Up @@ -294,6 +294,28 @@
public int Value { get; init; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
[ZeroSerializer]
public struct Bar
{
public byte A { get; set; }
public short B { get; set; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
[ZeroSerializer]
public struct Foo
Comment thread
sator-imaging marked this conversation as resolved.
Outdated
{
public byte A { get; set; }
public long B { get; set; }
public byte C { get; set; }
public int D { get; set; }
public short E { get; set; }
public double F { get; set; }
public Bar G { get; set; }
public Bar H { get; set; }
Comment thread
sator-imaging marked this conversation as resolved.
Outdated
}

[ZeroSerializer]
public record SimpleCsharpRecord
{
Expand Down
37 changes: 37 additions & 0 deletions tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,41 @@ public void BlittableRecordStructNestedPropertyRoundTrip()
TestAssert.True(viewNulls.Values.IsEmpty, nameof(viewNulls.Values.IsEmpty));
TestAssert.Equal(writtenBytesNulls, viewNulls.GetByteLength(), "Nulls GetByteLength");
}

[Fact]
public void SequentialPackOneFooBarRoundTrip()
{
TestAssert.Equal(3, BarView.RequiredByteLength, nameof(BarView.RequiredByteLength));
TestAssert.Equal(30, FooView.RequiredByteLength, nameof(FooView.RequiredByteLength));

var foo = new Foo
{
A = 0x12,
B = 0x123456789ABCDEF0,
C = 0x34,
D = 0x56789ABC,
E = -1234,
F = 3.141592653589793,
G = new Bar { A = 0xAB, B = 0x5678 },
H = new Bar { A = 0xCD, B = -4321 }
};

var buffer = new byte[FooView.RequiredByteLength];
int writtenBytes = foo.Serialize(buffer);

TestAssert.Equal(30, writtenBytes, nameof(writtenBytes));

var view = new FooView(buffer);

TestAssert.Equal(foo.A, view.A, nameof(view.A));
TestAssert.Equal(foo.B, view.B, nameof(view.B));
TestAssert.Equal(foo.C, view.C, nameof(view.C));
TestAssert.Equal(foo.D, view.D, nameof(view.D));
TestAssert.Equal(foo.E, view.E, nameof(view.E));
TestAssert.Equal(foo.F, view.F, nameof(view.F));
TestAssert.Equal(foo.G.A, view.G.A, nameof(view.G.A));
TestAssert.Equal(foo.G.B, view.G.B, nameof(view.G.B));
TestAssert.Equal(foo.H.A, view.H.A, nameof(view.H.A));
TestAssert.Equal(foo.H.B, view.H.B, nameof(view.H.B));
}
}
Loading