Skip to content

Commit

Permalink
Updated tests:
Browse files Browse the repository at this point in the history
+Fixed possible 'Test host process crashed' due to bug in Conari 1.5
3F/Conari#22

Added new around strings, arrays, and structures
  • Loading branch information
3F committed Nov 2, 2024
1 parent 54a1364 commit cbf1e71
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
89 changes: 88 additions & 1 deletion src/DllExport/UnitedTest/NetfxAssetBasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Linq;
using net.r_eg.Conari;
using net.r_eg.Conari.Native;
using net.r_eg.Conari.PE;
using net.r_eg.Conari.Types;
using net.r_eg.DllExport.UnitedTest._svc;
Expand All @@ -32,6 +33,79 @@ public void AddTest1()
Assert.NotNull(pe.ExportedProcNames.FirstOrDefault(s => s == "add"));
}

[Fact]
public void GetStructExargTest1()
{
using dynamic l = new ConariX(Assets.PrjNetfxRuntime);

try
{
IntPtr ptr = l.getStructExarg<IntPtr>();

using NativeStruct<Exarg> nst = new(ptr);

Assert.Equal(12816, nst.Data.x);
Assert.Equal(".NET DllExport + Conari", nst.Data.str);

// alternative via Native chains
dynamic data = ptr.Native()
.f<int>("x", null)
.f<CharPtr>("str")
.Struct.Access;

Assert.Equal(nst.Data.x, data.x);
Assert.Equal(nst.Data.str, (CharPtr)data.str);
}
finally
{
l.free();
}
}

[Fact]
public void GetUnalignedStructTest1()
{
using dynamic l = new ConariX(Assets.PrjNetfxRuntime);
try
{
IntPtr ptr = l.getUnalignedStruct<IntPtr>();

dynamic data = ptr.Native()
.f<int>("x") // <<< as is, unaligned
.f<CharPtr>("str")
.Struct.Access;

Assert.Equal("unaligned struct via Conari", (CharPtr)data.str);
Assert.Equal(0x1000, data.x);
}
finally
{
l.free();
}
}

[Fact]
public void PassTest1()
{
using dynamic l = new ConariX(Assets.PrjNetfxRuntime);

using NativeString<CharPtr> ns = new("test123");
using NativeStruct<Exarg> nstruct = new(new Exarg(0x3F_0000, ns));

Assert.True(l.pass<bool>(nstruct.Data.x, (CharPtr)ns, nstruct, "hello"));
}

[Fact]
public void PassBufferedTest1()
{
using dynamic l = new ConariX(Assets.PrjNetfxRuntime);

using BufferedString<CharPtr> ns = new("Hello world!");

l.passBuffered((IntPtr)ns);
Assert.Equal("new value", (CharPtr)ns);
}

[Fact]
public void CallmeTest1()
{
Expand Down Expand Up @@ -66,7 +140,10 @@ public void CallmeTest1()
Assert.Equal(upd.x, nr2[0]);
}

TCharPtr r = l.callme<TCharPtr>(ns + " and You 👋", nstruct);
// https://github.com/3F/Conari/issues/22
using NativeString<TCharPtr> nsnew = new($"{ns} and You 👋");

TCharPtr r = l.callme<TCharPtr>(nsnew, nstruct);
Assert.Equal("Рад знакомству 🤝 どうぞよろしく", r);
}
finally
Expand Down Expand Up @@ -105,6 +182,16 @@ public void ThrowExceptionTest1()
);
}

#region optional structures for the convenience

struct Arg { public int x, y; }

struct Exarg(int x, CharPtr str)
{
public int x = x;
public CharPtr str = str;
}

#endregion
}
}
44 changes: 44 additions & 0 deletions src/DllExport/assets/NetfxAsset/Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using net.r_eg.Conari.Native;
using net.r_eg.Conari.Types;

Expand All @@ -22,6 +23,20 @@ public static class Basic
[DllExport]
public static void throwException() => throw new NotImplementedException("こんにちは!");

[DllExport]
public static bool pass(int a, CharPtr cstr, Exarg data, [MarshalAs(UnmanagedType.LPWStr)] string str)
=> (a == 0x3F_0000)
&& (str == "hello")
&& (cstr == "test123")
&& (data.x == a)
&& ((CharPtr)data.str == cstr);

[DllExport]
public static void passBuffered(CharPtr cstr)
{
new NativeString<CharPtr>(cstr.AddressPtr).update("new value");
}

[DllExport]
public static IntPtr callme(TCharPtr str, IntPtr structure)
{
Expand All @@ -40,6 +55,25 @@ public static IntPtr callme(TCharPtr str, IntPtr structure)
return R(new NativeArray<int>(-1, v.x, 1, v.y));
}

[DllExport]
public static IntPtr getStructExarg() => R
(
new NativeStruct<Exarg>(new Exarg
(
0x3210,
R(new NativeString<CharPtr>(".NET DllExport + Conari"))
))
);

[DllExport]
public static IntPtr getUnalignedStruct() => NativeStruct.Make
.f<int>("x") // NOTE this field is not aligned as in getStructExarg() ^v
.f<CharPtr>("str")
.Struct.AddressPtr.Access()
.write(4096)
.write(R(new NativeString<CharPtr>("unaligned struct via Conari")))
.InitialPtr;

[DllExport]
public static void free()
{
Expand All @@ -55,5 +89,15 @@ private static IntPtr R<T>(T input) where T: IDisposable, IMarshalableGeneric
resources.Push(input);
return input.AddressPtr;
}

#region optional structures for the convenience

public struct Exarg(int x, IntPtr str)
{
public int x = x;
public IntPtr str = str;
}

#endregion
}
}
1 change: 1 addition & 0 deletions src/DllExport/assets/NetfxAsset/NetfxAsset.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net472</TargetFramework>
<RootNamespace>NetfxAsset</RootNamespace>
<AssemblyName>NetfxAsset</AssemblyName>
<NoWarn>1701;1702;CS1591;IDE1006</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Conari" Version="$(ConariVersion)" />
Expand Down

0 comments on commit cbf1e71

Please sign in to comment.