Hi everyone, I’m currently switching one of my library’s build system from make to Zig. So far so good, everything work as expected, now since the library is building fine, I figure it’s time to use Zig unit testing framework for making sure everything work as expected. The only problem I’ve found is that, the process of converting C types to Zig types is pretty verbose, and hinders a lot of the readability of my test case, so I’ve decided to build two simple functions to make the conversion between ?*anyopaque amd slice :
const clib = @cImport({
@cInclude("clib.h");
});
pub fn sliceFromAnyopaque(c_ptr: ?*anyopaque, comptime child_type: type) [:0]child_type {
const slc: [*c]child_type = @as([*c]child_type, @alignCast(@ptrCast(c_ptr.?)));
const sp = std.mem.span(slc);
return (sp);
}
pub fn anyopaqueFromSlice(slice: anytype) ?*anyopaque {
return (@as(?*anyopaque, @alignCast(@ptrCast(slice.ptr))));
}
test "heap_allocator : test1" {
const heap: *HeapAllocator = clib.heap_init();
defer _ = clib.heap_deinit(heap);
const ptr = sliceFromAnyopaque(heap.alloc.?(heap, 32), u8);
defer _ = heap.dealloc.?(heap, anyopaqueFromSlice(ptr));
}
This is what I came up with, and I was wondering if there was a better or cleaner way to do that, and if the logic make sense ?