Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 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,27 @@
public int Value { get; init; }
}

[ZeroSerializer]
public class DuplicateInstanceContainer
{
public SharedClassItem? Foo { get; set; }
public SharedClassItem? Bar { get; set; }
public SharedClassItem? Baz { get; set; }
}

[ZeroSerializer]
public class SharedClassItem
{
public int Value { get; set; }
public SharedClassNested? Nested { get; set; }
}

[ZeroSerializer]
public class SharedClassNested
{
public int NestedValue { get; set; }
}

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

[Fact]
public void SharedReferenceInstancesRoundTrip()
{
var sharedNested = new SharedClassNested { NestedValue = 42 };
var sharedItem = new SharedClassItem { Value = 100, Nested = sharedNested };
var distinctItemWithSharedNested = new SharedClassItem { Value = 200, Nested = sharedNested };

// Foo and Bar share SharedClassItem instance; Baz has a distinct SharedClassItem instance but shares the same SharedClassNested instance
var container = new DuplicateInstanceContainer
{
Foo = sharedItem,
Bar = sharedItem,
Baz = distinctItemWithSharedNested,
};

var buffer = new byte[256];
int writtenBytes = container.Serialize(buffer);
var view = new DuplicateInstanceContainerView(buffer.AsMemory(0, writtenBytes));

TestAssert.Equal(100, view.Foo.Value, nameof(view.Foo.Value));
TestAssert.Equal(100, view.Bar.Value, nameof(view.Bar.Value));
TestAssert.Equal(200, view.Baz.Value, nameof(view.Baz.Value));

TestAssert.Equal(42, view.Foo.Nested.NestedValue, nameof(view.Foo.Nested.NestedValue));
TestAssert.Equal(42, view.Bar.Nested.NestedValue, nameof(view.Bar.Nested.NestedValue));
TestAssert.Equal(42, view.Baz.Nested.NestedValue, nameof(view.Baz.Nested.NestedValue));

TestAssert.Equal(writtenBytes, view.GetByteLength(), "SharedReferenceInstances GetByteLength");
}
}
Loading