C
C#2y ago
cap5lut

❔ Unit Test | Mocking class with unsafe methods

Hi, I am having trouble writing unit tests, because I dont know how to mock/setup the underlying instance/its methods. The class Silk.NET.OpenCL.CL (https://github.com/dotnet/Silk.NET/blob/main/src/OpenCL/Silk.NET.OpenCL/CL.gen.cs) does not implement an interface and the methods are unsafe and non virtual. This would be one of the methods I need to mock:
public unsafe int GetPlatformIDs(uint num_entries, nint* platforms, out uint num_platforms);
public unsafe int GetPlatformIDs(uint num_entries, nint* platforms, out uint num_platforms);
(instead of out uint num_platforms I could also use uint* num_platforms, if that matters) I tried using Moq, but I run into errors for the null pointer and count parameter:
unsafe
{
var cl = new Mock<CL>();
cl.Setup(instance => instance.GetPlatformIDs(0, null, out var count)).Returns(0);
// ....
}
unsafe
{
var cl = new Mock<CL>();
cl.Setup(instance => instance.GetPlatformIDs(0, null, out var count)).Returns(0);
// ....
}
CS1944: An expression tree may not contain an unsafe pointer operation CS8198: An expression tree may not contain an out argument variable declaration
( I tried also other libraries like Pose, but they all use System.Linq.Expressions.Expression so I always ran into the same errors) Any idea how to do that?
3 Replies
Sergio
Sergio2y ago
Use some non-unsafe helper method to do that, and use that one in the test expression?
cap5lut
cap5lutOP2y ago
I would like to write a test for this method:
public static IEnumerable<ICLPlatform> GetPlatforms(this CL cl)
{
unsafe
{
// get the platform count
var result = cl.GetPlatformIDs(0, null, out var count);
if (result != (int)ErrorCodes.Success) CLException.Throw(nameof(cl.GetPlatformIDs), result, "Could not get platform IDs");
if (count == 0) return Enumerable.Empty<CLPlatform>();

// get all platform IDs
var platforms = new nint[count];
fixed (nint* dst = platforms)
{
result = cl.GetPlatformIDs(count, dst, null);
if (result != (int)ErrorCodes.Success) CLException.Throw(nameof(cl.GetPlatformIDs), result, "Could not get platform IDs");
}

// map to CLPlatforms
return platforms.Select(platformId => new CLPlatform(cl, platformId));
}
}
public static IEnumerable<ICLPlatform> GetPlatforms(this CL cl)
{
unsafe
{
// get the platform count
var result = cl.GetPlatformIDs(0, null, out var count);
if (result != (int)ErrorCodes.Success) CLException.Throw(nameof(cl.GetPlatformIDs), result, "Could not get platform IDs");
if (count == 0) return Enumerable.Empty<CLPlatform>();

// get all platform IDs
var platforms = new nint[count];
fixed (nint* dst = platforms)
{
result = cl.GetPlatformIDs(count, dst, null);
if (result != (int)ErrorCodes.Success) CLException.Throw(nameof(cl.GetPlatformIDs), result, "Could not get platform IDs");
}

// map to CLPlatforms
return platforms.Select(platformId => new CLPlatform(cl, platformId));
}
}
guess that would mean i would have to use the mentioned non-unsafe helper method in GetPlatforms() then as well right?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server