/* * test_helpers.h — minimal test harness for pg_knoe_auth unit tests. * * Provides palloc/pfree/pstrdup/pnstrdup stubs (no PG cluster needed), * a simple PASS/FAIL assertion macro, and a test-runner entry point. * * See docs/plans/junie/pg_knoe_auth-rename-harden-modularize.md §2.5 */ #ifndef TEST_HELPERS_H #define TEST_HELPERS_H #include #include #include #include #include /* ── PG memory stubs ─────────────────────────────────────────────────────── */ static inline void *palloc(size_t n) { return malloc(n); } static inline void *repalloc(void *p, size_t n) { return realloc(p, n); } static inline void pfree(void *p) { free(p); } static inline char *pstrdup(const char *s) { return strdup(s); } static inline char *pnstrdup(const char *s, size_t n) { char *r = malloc(n + 1); memcpy(r, s, n); r[n] = '\0'; return r; } /* ── PG logging stubs ────────────────────────────────────────────────────── */ #define WARNING 0 #define LOG 1 static inline void elog(int level, const char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "[%s] ", level == LOG ? "LOG" : "WARNING"); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); } /* ── TopMemoryContext stub ───────────────────────────────────────────────── */ typedef void *MemoryContext; static MemoryContext TopMemoryContext = NULL; static inline MemoryContext MemoryContextSwitchTo(MemoryContext ctx) { (void) ctx; return NULL; } static inline void *MemoryContextAlloc(MemoryContext ctx, size_t n) { (void) ctx; return malloc(n); } /* ── Test runner ─────────────────────────────────────────────────────────── */ static int _tests_run = 0; static int _tests_failed = 0; #define ASSERT(cond, msg) do { \ _tests_run++; \ if (!(cond)) { \ fprintf(stderr, "FAIL [%s:%d] %s\n", __FILE__, __LINE__, msg); \ _tests_failed++; \ } else { \ fprintf(stdout, "PASS %s\n", msg); \ } \ } while (0) #define TEST_SUMMARY() do { \ printf("\n%d/%d tests passed\n", _tests_run - _tests_failed, _tests_run); \ return _tests_failed ? 1 : 0; \ } while (0) #endif /* TEST_HELPERS_H */