Skip to content
Merged
23 changes: 23 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 (Release)

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 (Debug)

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 (Release)

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 (Debug)

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 (Release)

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 (Debug)

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,29 @@
public int Value { get; init; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
[ZeroSerializer]
public struct BadAlignedStructWithPackOne
Comment thread
sator-imaging marked this conversation as resolved.
Outdated
{
public byte A { get; set; }
public short B { get; set; }
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
[ZeroSerializer]
public struct BadAlignedContainerStructWithPackOne
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 byte G { get; set; }
public BadAlignedStructWithPackOne H { get; set; }
public BadAlignedStructWithPackOne I { get; set; }
}

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

[Fact]
public void BadAlignedStructWithPackOneRoundTrip()
{
TestAssert.Equal(3, BadAlignedStructWithPackOneView.RequiredByteLength, nameof(BadAlignedStructWithPackOneView.RequiredByteLength));
TestAssert.Equal(31, BadAlignedContainerStructWithPackOneView.RequiredByteLength, nameof(BadAlignedContainerStructWithPackOneView.RequiredByteLength));

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

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

TestAssert.Equal(31, writtenBytes, nameof(writtenBytes));
Comment thread
sator-imaging marked this conversation as resolved.

var view = new BadAlignedContainerStructWithPackOneView(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, view.G, nameof(view.G));
TestAssert.Equal(foo.H.A, view.H.A, nameof(view.H.A));
TestAssert.Equal(foo.H.B, view.H.B, nameof(view.H.B));
TestAssert.Equal(foo.I.A, view.I.A, nameof(view.I.A));
TestAssert.Equal(foo.I.B, view.I.B, nameof(view.I.B));
Comment thread
sator-imaging marked this conversation as resolved.
}
}
Loading