1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-22 19:18:47 +00:00

Add managed<->native marshalling tests

This commit is contained in:
Oliver Booth 2022-12-04 14:14:04 +00:00
parent 20e5eebf47
commit 9a2b841e96
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 77 additions and 0 deletions

View File

@ -6,6 +6,8 @@
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>

View File

@ -0,0 +1,75 @@
using System.Diagnostics;
using System.Text;
using VpSharp.Internal;
namespace VpSharp.Tests;
[TestClass]
public class MarshellerTests
{
private static readonly Utf8StringToNative ManagedToNativeMarshaller = new();
private static readonly Utf8StringToManaged NativeToManagedMarshaller = new();
private static readonly Random Random = new();
[TestMethod]
public unsafe void MarshalNativeToManaged_ShouldReturnPointerToUtf8Bytes_GivenString()
{
string value = GenerateRandomString();
byte* pointer = GetBytePointer(value, out int count);
string expected = Encoding.UTF8.GetString(pointer, count);
var actual = (string)NativeToManagedMarshaller.MarshalNativeToManaged((nint)pointer);
Assert.AreEqual(expected, actual);
CleanUpNativeData(pointer);
}
[TestMethod]
public unsafe void MarshalManagedToNative_ShouldReturnPointerToUtf8Bytes_GivenString()
{
string value = GenerateRandomString();
byte* pointer = GetBytePointer(value, out int count);
string result = Encoding.UTF8.GetString(pointer, count);
Trace.WriteLine($"Test string: {value}");
Assert.AreEqual(value, result);
CleanUpNativeData(pointer);
}
private static unsafe void CleanUpNativeData(byte* pointer)
{
ManagedToNativeMarshaller.CleanUpNativeData((nint)pointer);
}
private static string GenerateRandomString(int length = 50)
{
var builder = new StringBuilder();
for (var index = 0; index < length; index++)
{
int mode = Random.Next(0, 3);
switch (mode)
{
case 0:
builder.Append((char)Random.Next('A', 'Z'));
break;
case 1:
builder.Append((char)Random.Next('a', 'z'));
break;
default:
builder.Append((char)Random.Next('0', '9'));
break;
}
}
return builder.ToString();
}
private static unsafe byte* GetBytePointer(string input, out int byteCount)
{
var marshaller = new Utf8StringToNative();
nint buffer = marshaller.MarshalManagedToNative(input);
byteCount = Encoding.UTF8.GetByteCount(input);
return (byte*)buffer.ToPointer();
}
}