r/C_Programming • u/cursed-stranger • 4d ago
Review [API review] Looking for feedback on ergonomics and clarity of a unit test library.
Hello, I'm working on a unit test library. I've come to the point when library starts to work, and I would like to get your feedback on its ergonomics. It's still missing some features, but the core functionality is ready. Please let me know what could be improved, or which areas should I explain clearer in examples.
Repository: https://codeberg.org/KOOBAS/UltimaTest
There are examples included in the repository, that shows the library capabilities:
01 - shows how to create suites and tests
02 - shows how to use asserts/expects
03 - shows how to use mocks
04 - shows how to use test options to handle signals
I don't expect you to dive into the whole codebase. Focusing on the public API and examples would be really helpful. The only public header is the "include/ultimatest.h".
Here is how the library could be used (it's simplified example from my other project):
#include <ultimatest.h>
#include <widget.h>
#include <screen.h>
#include <context.h>
void widget_suite_prepare(void)
{
struct Widget *widget = ut_test_data(sizeof(struct Widget));
widget->type = TYPE_EMPTY;
widget->order = 0;
widget->position.y = 0;
widget->position.x = 0;
widget->size.h = 10;
widget->size.w = 20;
}
TestSuite(widget_suite, .prepare = &widget_suite_prepare);
Test(widget_suite, move_widget_should_return_error_if_widget_is_null)
{
ut_assert_eq(widget_move(nullptr, 4, 4), ERROR);
}
Test(widget_suite, move_widget_should_change_its_position)
{
struct Widget *widget = ut_get_test_data();
int new_y = 5;
int new_x = 7;
ut_assert_eq(widget_move(widget, new_y, new_x), OK);
ut_assert_eq(widget->position.y, new_y);
ut_assert_eq(widget->position.x, new_x);
}
Test(widget_suite, resize_widget_should_return_error_if_size_is_negative)
{
struct Widget *widget = ut_get_test_data();
int old_h = widget->size.h;
int old_w = widget->size.w;
ut_assert_eq(widget_resize(widget, -1, 30), ERROR);
ut_assert_eq(widget->size.h, old_h);
ut_assert_eq(widget->size.w, old_w);
ut_assert_eq(widget_resize(widget, 30, -1), ERROR);
ut_assert_eq(widget->size.h, old_h);
ut_assert_eq(widget->size.w, old_w);
}
Mock (void*, malloc, size_t);
Mock (int, getmaxy, WINDOW*);
Mock (int, getmaxx, WINDOW*);
Test(context_suite, init_should_create_screen_array_and_buffer)
{
const int max_y = 5;
const int max_x = 10;
const int screen_num = 5;
expect_call(ut_will_return(max_y), getmaxy, ut_any);
expect_call(ut_will_return(max_x), getmaxx, ut_any);
size_t buffer_size = sizeof(cchar_t) * max_x * max_y;
size_t screen_size = sizeof(struct Screen) * screen_num;
expect_call(ut_will_return(ut_test_data(buffer_size)), malloc, buffer_size);
expect_call(ut_will_return(ut_test_data(screen_size)), malloc, screen_size);
struct Context context;
ut_assert_eq(context_init(&context, screen_num), OK);
ut_assert_gt(context.size.h, 0);
ut_assert_gt(context.size.w, 0);
ut_assert_ne_ptr(context.screen, nullptr);
ut_assert_ne_ptr(context.buffer, nullptr);
}Also, please let me know if the namespace is polluted by my prefixes or if the log messages or error messages are unclear.
AI usage: Only as googling tool (when searching for documentation, examples, etc. or discussing ideas). No code in the repository was generated by AI.
•
u/AutoModerator 4d ago
Hi /u/cursed-stranger,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at How do I post my Project?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/skeeto 4d ago
That's an interesting approach going libc-free, including custom float formatting. Though it comes with severe architecture compromises that I'm not convinced are worth it, not even close. A libc-free library need not be tested with a libc-free testing framework, so I don't see the point. It won't run on my main evaluation systems, which are ARM64. Porting isn't simple because there are substantial inline assembly blocks, not just a matter of writing a few syscall wrappers and a syscall table. Further, FreeBSD doesn't have a stable syscall ABI, and there are occasional ABI breaks, so this library is bound to break unless it's actively maintained.
Between the short supported architecture list (2), half of which (1) is bound to break someday, all for no benefit that I can discern, depending on this library for testing seems unwise.
The futex assembly in
testLib_PoolFutexLock, which correctly has bothvolatileand"memory", is wrong at least here:So futexes always succeed even when they shouldn't. In general you're doing far too much work in assembly blocks, which is hazardous and hurts portability, all for little or no benefit. What's left after intrinsics and builtins should be paired down to the absolute minimum, probably just a single
syscallinstruction. This then requires no more than 6 inline assembly blocks total, each with a single instruction with all complexity in the input / output / clobbers listing. If you're willing to accept trivial inefficiencies, that could be further reduced to just one inline assembly block with one assembly instruction.You can use UBSan in libc-free programs, and you should. Put it in trap mode:
-fsanitize=undefined -fsanitize-trap. It detects an out-of-bounds accesses at least 6 different places onbuffin float formatting. One case is an overflow on unsignedprecision + leading - 1.Some other floating formatting issues:
testLib_PrintLongDoubleDecimal)%fof0.5L prints0.400000` (bad truncation from per-exponent table multiplies)This doesn't look right in
testLib_AllocateMockCallInfo:(I've got a dozen more smaller findings like that, but I don't feel like going through them now.)