Skip to content

Commit

Permalink
(#354) CPtr, FPtr: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 12, 2023
1 parent 244b6bd commit 8ed11c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Cesium.Runtime.Tests/PtrTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Cesium.Runtime.Tests;

public unsafe class PtrTests
{
[Fact]
public void CPtrTests()
{
CPtr v = (void*)0x1234;
Assert.Equal(0x1234L, (long)v.AsPtr());
Assert.Equal(0x1234L, (long)v.AsPtr<byte>());

Assert.Equal(sizeof(long), sizeof(CPtr));

CPtr<int> t = (int*)0x2345;
Assert.Equal(0x2345L, (long)t.AsPtr());
Assert.Equal((IntPtr)0x2345, t.AsIntPtr());
Assert.Equal(0x2345L, (long)t.AsPtr<byte>());

Assert.Equal(sizeof(long), sizeof(CPtr<int>));
}

[Fact]
public void FPtrTests()
{
var a = new FPtr<Action>((void*)0x1234);
Assert.Equal(0x1234L, (long)a.AsPtr());
Assert.Equal(sizeof(long), sizeof(FPtr<Action>));
}
}
4 changes: 2 additions & 2 deletions Cesium.Runtime/CPtr.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Cesium.Runtime;

/// <summary>A class encapsulating an opaque pointer (aka <code>void*</code> in C).</summary>
public unsafe readonly struct CPtr
public readonly unsafe struct CPtr
{
private readonly long _value;

Expand All @@ -19,7 +19,7 @@ private CPtr(long value)

/// <summary>A class encapsulating an object pointer.</summary>
/// <typeparam name="T">Type this pointer may be resolved to.</typeparam>
public unsafe readonly struct CPtr<T> where T : unmanaged
public readonly unsafe struct CPtr<T> where T : unmanaged
{
private readonly long _value;

Expand Down
14 changes: 14 additions & 0 deletions Cesium.Runtime/FPtr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Cesium.Runtime;

/// <summary>A class encapsulating a C function pointer.</summary>
public readonly unsafe struct FPtr<TDelegate> where TDelegate : Delegate // TODO: Think about vararg and empty parameter list encoding.
{
private readonly long _value;

public FPtr(void* ptr)
{
_value = (long)ptr;
}

public void* AsPtr() => (void*)_value;
}
2 changes: 1 addition & 1 deletion Cesium.Runtime/StringFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Cesium.Runtime;
/// <summary>
/// Functions declared in the string.h
/// </summary>
public unsafe static class StringFunctions
public static unsafe class StringFunctions
{
public static uint StrLen(CPtr<byte> str)
{
Expand Down

0 comments on commit 8ed11c6

Please sign in to comment.