From bcaece6781b1e938c6e070c037bb76e06c9e7259 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sun, 28 Jan 2024 23:21:40 -0800 Subject: [PATCH] Add String Ref Marshaller test --- .../Natives/RefNetworkTableValueMarshaller.cs | 18 +++++++---- .../RefNetworkTableValueMarshallerTest.cs | 31 +++++++++++-------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs b/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs index 4ab89edb..2c7bfa4d 100644 --- a/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs +++ b/src/ntcore/Natives/RefNetworkTableValueMarshaller.cs @@ -136,15 +136,21 @@ public NetworkTableValueMarshaller.NativeNetworkTableValue ToUnmanaged() return m_nativeValue; } - public void Free() + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void Free() { if (m_nativeValue.type == NetworkTableType.StringArray) { - int len = (int)m_nativeValue.data.arrString.size; - for (int i = 0; i < len; i++) - { - NativeMemory.Free(m_nativeValue.data.arrString.arr[i].Str); - } + FreeStringArray(); + } + } + + private readonly void FreeStringArray() + { + int len = (int)m_nativeValue.data.arrString.size; + for (int i = 0; i < len; i++) + { + NativeMemory.Free(m_nativeValue.data.arrString.arr[i].Str); } } } diff --git a/test/ntcore.test/RefNetworkTableValueMarshallerTest.cs b/test/ntcore.test/RefNetworkTableValueMarshallerTest.cs index dea3a582..aa10296a 100644 --- a/test/ntcore.test/RefNetworkTableValueMarshallerTest.cs +++ b/test/ntcore.test/RefNetworkTableValueMarshallerTest.cs @@ -1,6 +1,7 @@ using System.Runtime.CompilerServices; using System.Text; using NetworkTables.Natives; +using WPIUtil.Marshal; using Xunit; namespace NetworkTables.Test; @@ -195,25 +196,29 @@ public unsafe void TestString() [Fact] public unsafe void TestStringArray() { - Span raw = stackalloc byte[3]; - "abc"u8.CopyTo(raw); - void* ptr = Unsafe.AsPointer(ref raw.GetPinnableReference()); - HandleInMarshal(RefNetworkTableValue.MakeRaw(raw), (v, pinned) => + HandleInMarshal(RefNetworkTableValue.MakeStringArray(["abc", "12345"]), (v, pinned) => { - Assert.Equal(NetworkTableType.Raw, v->type); - Assert.Equal((nuint)3, v->data.valueRaw.size); - ReadOnlySpan consumed = new ReadOnlySpan(v->data.valueRaw.data, (int)v->data.valueRaw.size); + Assert.Equal(NetworkTableType.StringArray, v->type); + Assert.Equal((nuint)2, v->data.arrString.size); + + ReadOnlySpan array = new ReadOnlySpan(v->data.arrString.arr, (int)v->data.arrString.size); + Assert.Equal((nuint)3, array[0].Len); + ReadOnlySpan consumed = new ReadOnlySpan(array[0].Str, (int)array[0].Len); Assert.True(consumed.SequenceEqual("abc"u8)); - Assert.True(pinned == ptr); - Assert.True(pinned == v->data.valueRaw.data); + + Assert.Equal((nuint)5, array[1].Len); + consumed = new ReadOnlySpan(array[1].Str, (int)array[1].Len); + Assert.True(consumed.SequenceEqual("12345"u8)); + + Assert.True(pinned == v->data.arrString.arr); }); - HandleInMarshal(RefNetworkTableValue.MakeRaw(new()), (v, pinned) => + HandleInMarshal(RefNetworkTableValue.MakeStringArray(new()), (v, pinned) => { - Assert.Equal(NetworkTableType.Raw, v->type); - Assert.Equal((nuint)0, v->data.valueRaw.size); + Assert.Equal(NetworkTableType.StringArray, v->type); + Assert.Equal((nuint)0, v->data.arrString.size); Assert.True(pinned == null); - Assert.True(v->data.valueRaw.data.IsNull); + Assert.True(v->data.arrString.arr.IsNull); }); } }