Files
libakerror/tests/err_capture.h
Andrew Kesterson 4daa411f3f
All checks were successful
libakerror CI Build / cmake_build (push) Successful in 2m42s
Expand test coverage for error-handling macros and error pool
Add 11 CTest programs and a shared test helper covering gaps left by the
original four tests (which only exercised FAIL/CATCH/HANDLE and CLEANUP):

- err_capture.h: capturing akerr_log_method + NDEBUG-proof AKERR_CHECK so
  tests can assert on message/status/stacktrace content, not just exit codes
- err_success: clean nested return does not break/handle/leak
- err_pool_refcount: 100k raise->catch->handle cycles leak 0 pool slots
- err_handle_default / err_handle_group / err_handle_dispatch: handler routing
- err_pass / err_ignore / err_swallow: PASS, IGNORE, FINISH(e,false)
- err_break_variants: FAIL_*_BREAK and FAIL_*_RETURN
- err_errno: system errno name lookup + "Unknown Error" boundary
- err_custom_handler: override the unhandled-error hook, assert non-fatally

Register tests via a foreach loop in CMakeLists.txt. Ignore build/.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-27 16:18:17 -04:00

85 lines
2.4 KiB
C

#ifndef AKERR_TEST_CAPTURE_H
#define AKERR_TEST_CAPTURE_H
/*
* Shared test helpers for libakerror.
*
* Installs a capturing implementation of akerr_log_method so tests can assert
* on the *content* of log/stacktrace output (messages, status codes, error
* names) instead of relying solely on process exit codes.
*
* Also provides AKERR_CHECK(), a NDEBUG-proof assertion that fails the test by
* returning non-zero from main() (unlike assert(), which is compiled out in
* release builds and would silently turn a test into a no-op).
*/
#include "akerror.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#define AKERR_CAPTURE_BUFSZ 65536
static char akerr_capture_buf[AKERR_CAPTURE_BUFSZ];
static size_t akerr_capture_len = 0;
static void __attribute__((unused)) akerr_capture_logger(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(akerr_capture_buf + akerr_capture_len,
AKERR_CAPTURE_BUFSZ - akerr_capture_len, fmt, ap);
va_end(ap);
if ( n > 0 ) {
akerr_capture_len += (size_t)n;
if ( akerr_capture_len >= AKERR_CAPTURE_BUFSZ ) {
akerr_capture_len = AKERR_CAPTURE_BUFSZ - 1;
}
}
}
static void __attribute__((unused)) akerr_capture_reset(void)
{
akerr_capture_len = 0;
akerr_capture_buf[0] = '\0';
}
/*
* Install the capturing logger. akerr_init() only assigns a default logger when
* akerr_log_method is NULL, and it is idempotent, so calling this either before
* or after the first PREPARE_ERROR keeps our logger in place.
*/
static void __attribute__((unused)) akerr_capture_install(void)
{
akerr_capture_reset();
akerr_log_method = &akerr_capture_logger;
}
/* Count array slots currently checked out of the pool (refcount != 0). */
static int __attribute__((unused)) akerr_slots_in_use(void)
{
int n = 0;
for ( int i = 0; i < AKERR_MAX_ARRAY_ERROR; i++ ) {
if ( AKERR_ARRAY_ERROR[i].refcount != 0 ) {
n++;
}
}
return n;
}
#define AKERR_CHECK(cond) \
do { \
if ( !(cond) ) { \
fprintf(stderr, "CHECK FAILED: %s at %s:%d\n", \
#cond, __FILE__, __LINE__); \
return 1; \
} \
} while ( 0 )
#define AKERR_CHECK_CONTAINS(needle) \
AKERR_CHECK(strstr(akerr_capture_buf, (needle)) != NULL)
#define AKERR_CHECK_NOT_CONTAINS(needle) \
AKERR_CHECK(strstr(akerr_capture_buf, (needle)) == NULL)
#endif // AKERR_TEST_CAPTURE_H