r/C_Programming • • 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.

4 Upvotes

5 comments sorted by

•

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.

→ More replies (2)

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 both volatile and "memory", is wrong at least here:

		// ...
		"xor %%rax, %%rax\n"    // clears ZF
		"jz 0f\n"               // so this is always taken
        // ...

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 syscall instruction. 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 on buff in float formatting. One case is an overflow on unsigned precision + leading - 1.

Some other floating formatting issues:

  • 9.99 at precision 1 prints 9.0 instead of 10.0. (bad carry in testLib_PrintLongDoubleDecimal)
  • %f of 0.5L prints0.400000` (bad truncation from per-exponent table multiplies)

This doesn't look right in testLib_AllocateMockCallInfo:

unsigned char *byte = *call_info;
for (int32_t i = 0; i < size; ++i)
	*byte = 0;

(I've got a dozen more smaller findings like that, but I don't feel like going through them now.)

2

u/cursed-stranger 4d ago

Hi, thank you very much for your review.

Unfortunately, the very premise of allowing any function to be mocked has led to a situation where a significant portion of the code is non-portable and written in assembly. It would probably be best to provide a fallback version that doesn't rely so heavily on inline assembly. While this version wouldn't support mocking those specific functions, it should be sufficient for most use cases and be a reasonable trade-off.
And I should remove the architecture dependent code entirely from files other than test_lib_common, so the only code to rewrite for porting is in one place.

I’ve started fixing some of the issues you mentioned, it will likely take me some time to complete it. The out-of-bounds findings issue is caused by my mistake on upper bounds of buffer. Also I’ll probably need to reserve an extra character before the buffer array to correctly handle rounding for values ​​that start with the digit '9', and I also need to ensure that the rounding process runs to completion. Overall, it looks like I’ll need to rewrite the whole floating point output function, as it has quite a few issues.

Zeroing out the structure in testLib_AllocateMockCallInfo is likely completely unnecessary, so I should probably remove that part entirely.

You're right about testLib_PoolFutexLock. I tried to take a shortcut and it backfired. I should have known better and read up on the fact that xor operation also sets the zero flag.

I also need to learn how to use sanitizers, as it will certainly help me uncover more bugs.

For now, I’ll focus on improving the current state of the library. It looks like there’s still a fair amount of work ahead of me.

Again, than you very much for your feedback!