Files
neoLegacy-mac/tests/support/gr_strokepath_test_support.c
2026-07-17 21:15:30 -05:00

232 lines
7.4 KiB
C

/*
* Host harness for the reconstructed gr_strokepath translation unit.
*
* gr_strokepath owns the path-stroking / stroke-antialiasing renderer. It
* exports iggy_stroke_path, iggy_stroke_path_for_antialiasing and
* iggy_stroke_path_free, and owns exactly one named data global:
* float iggy_aawidth; // 0x3faccccd == 1.35f (durango .data, all 4 objs)
* Those symbols come from the module itself once it exists and are NOT
* defined here.
*
* The only symbols that have to be supplied from outside are the genuine
* nm ' U ' imports (the cross-platform core -- see the cross test):
*
* - allocation: iggy_malloc_fileline, iggy_free_alloc
* - temp arena: iggy_temp_alloc, iggy_temp_free
* - 9-slice: iggy_9slice_set_stroke_xform, iggy_9slice_scale_forstroke
* - NaN guard: rr_isnan_f (kept REAL: x != x)
* - telemetry: iggy_tm_normal (a DATA global pointer, kept NULL so
* the `if (iggy_tm_normal != 0)` timing markers are skipped;
* the ps3 object shows it undot-prefixed -> data, not a fn)
* - libc/libm: memcpy / pow / sqrt resolve against the host and are
* deliberately NOT stubbed here, matching the sibling
* stb_truetype_wrapper harness convention.
*
* Conventions mirror tests/support/iggy_font_provider_test_support.c:
* - counters + an event log give the runtime tests an observable channel,
* - the temp/malloc stubs hand out real, usable, 16-byte-aligned buffers
* out of a bump arena so the module links AND runs,
* - fail_next_* setters let a test drive the allocation-failure paths.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
enum { GR_STROKEPATH_TEST_MAX = 128 };
/* ---- Telemetry global (data pointer, kept NULL). ---- */
void *iggy_tm_normal = 0;
/* ---- Allocation / temp bookkeeping. ---- */
static size_t malloc_count;
static size_t free_alloc_count;
static size_t temp_alloc_count;
static size_t temp_free_count;
static int fail_next_malloc;
static int fail_next_temp_alloc;
static size_t last_malloc_size;
static int last_malloc_tag;
static size_t last_temp_alloc_size;
static void *last_temp_free_ctx;
static size_t last_temp_free_size;
/* free_alloc event log: (context/tag, pointer) in call order. */
static void *freed_ctx[GR_STROKEPATH_TEST_MAX];
static void *freed_ptr[GR_STROKEPATH_TEST_MAX];
/* ---- 9-slice bookkeeping. ---- */
static size_t slice_set_xform_count;
static void *last_slice_set_ctx;
static void *last_slice_set_xform;
static void *last_slice_set_translate;
static size_t slice_scale_count;
static void *last_slice_scale_ctx;
/* ---- rr_isnan_f bookkeeping. ---- */
static size_t isnan_count;
/* Bump arena backing the temp / malloc stubs. */
static uint8_t arena_pool[1 << 20];
static size_t arena_used;
static void *arena_blocks[GR_STROKEPATH_TEST_MAX];
static size_t arena_block_count;
static void *pool_alloc(size_t size) {
size_t aligned = (size + 15u) & ~(size_t)15u;
if (aligned == 0) {
aligned = 16u;
}
if (arena_block_count == GR_STROKEPATH_TEST_MAX ||
arena_used + aligned > sizeof(arena_pool)) {
return 0;
}
void *result = arena_pool + arena_used;
arena_used += aligned;
arena_blocks[arena_block_count++] = result;
return result;
}
void gr_strokepath_test_reset(void) {
malloc_count = 0;
free_alloc_count = 0;
temp_alloc_count = 0;
temp_free_count = 0;
fail_next_malloc = 0;
fail_next_temp_alloc = 0;
last_malloc_size = 0;
last_malloc_tag = 0;
last_temp_alloc_size = 0;
last_temp_free_ctx = 0;
last_temp_free_size = 0;
memset(freed_ctx, 0, sizeof(freed_ctx));
memset(freed_ptr, 0, sizeof(freed_ptr));
slice_set_xform_count = 0;
last_slice_set_ctx = 0;
last_slice_set_xform = 0;
last_slice_set_translate = 0;
slice_scale_count = 0;
last_slice_scale_ctx = 0;
isnan_count = 0;
arena_used = 0;
arena_block_count = 0;
memset(arena_blocks, 0, sizeof(arena_blocks));
}
void gr_strokepath_test_fail_next_malloc(void) { fail_next_malloc = 1; }
void gr_strokepath_test_fail_next_temp_alloc(void) { fail_next_temp_alloc = 1; }
size_t gr_strokepath_test_malloc_count(void) { return malloc_count; }
size_t gr_strokepath_test_free_alloc_count(void) { return free_alloc_count; }
size_t gr_strokepath_test_temp_alloc_count(void) { return temp_alloc_count; }
size_t gr_strokepath_test_temp_free_count(void) { return temp_free_count; }
size_t gr_strokepath_test_last_malloc_size(void) { return last_malloc_size; }
int gr_strokepath_test_last_malloc_tag(void) { return last_malloc_tag; }
size_t gr_strokepath_test_last_temp_alloc_size(void) {
return last_temp_alloc_size;
}
void *gr_strokepath_test_last_temp_free_ctx(void) { return last_temp_free_ctx; }
size_t gr_strokepath_test_last_temp_free_size(void) {
return last_temp_free_size;
}
void *gr_strokepath_test_freed_ctx(size_t index) {
if (index >= GR_STROKEPATH_TEST_MAX) {
return 0;
}
return freed_ctx[index];
}
void *gr_strokepath_test_freed_ptr(size_t index) {
if (index >= GR_STROKEPATH_TEST_MAX) {
return 0;
}
return freed_ptr[index];
}
size_t gr_strokepath_test_slice_set_xform_count(void) {
return slice_set_xform_count;
}
void *gr_strokepath_test_last_slice_set_ctx(void) { return last_slice_set_ctx; }
void *gr_strokepath_test_last_slice_set_xform(void) {
return last_slice_set_xform;
}
void *gr_strokepath_test_last_slice_set_translate(void) {
return last_slice_set_translate;
}
size_t gr_strokepath_test_slice_scale_count(void) { return slice_scale_count; }
void *gr_strokepath_test_last_slice_scale_ctx(void) {
return last_slice_scale_ctx;
}
size_t gr_strokepath_test_isnan_count(void) { return isnan_count; }
/* ---- Allocation imports. ---- */
/* finish_current_chunk: iggy_malloc_fileline(*builder, builder[1], size,
* 0x29, file, line) -- 6 args. */
void *iggy_malloc_fileline(void *player, void *heap, uint32_t size,
int32_t tag, const char *file, int32_t line) {
(void)player;
(void)heap;
(void)file;
(void)line;
++malloc_count;
last_malloc_tag = tag;
last_malloc_size = size;
if (fail_next_malloc) {
fail_next_malloc = 0;
return 0;
}
return pool_alloc(size);
}
/* iggy_stroke_path_free / finish_current_chunk: iggy_free_alloc(ctx, ptr). */
void iggy_free_alloc(void *ctx, void *ptr) {
if (free_alloc_count < GR_STROKEPATH_TEST_MAX) {
freed_ctx[free_alloc_count] = ctx;
freed_ptr[free_alloc_count] = ptr;
}
++free_alloc_count;
}
/* init_buffers: iggy_temp_alloc(ctx, size) -> buffer. */
void *iggy_temp_alloc(void *ctx, intptr_t size) {
(void)ctx;
++temp_alloc_count;
last_temp_alloc_size = (size_t)size;
if (fail_next_temp_alloc) {
fail_next_temp_alloc = 0;
return 0;
}
return pool_alloc((size_t)size);
}
/* iggy_temp_free(ctx, size). */
void iggy_temp_free(void *ctx, intptr_t size) {
++temp_free_count;
last_temp_free_ctx = ctx;
last_temp_free_size = (size_t)size;
}
/* ---- 9-slice imports (recording stubs). ---- */
void iggy_9slice_set_stroke_xform(void *ctx, void *xform, void *translate) {
++slice_set_xform_count;
last_slice_set_ctx = ctx;
last_slice_set_xform = xform;
last_slice_set_translate = translate;
}
/* iggy_9slice_scale_forstroke(ctx, *x, *y, *count) -- 4 pointer args. */
void iggy_9slice_scale_forstroke(void *ctx, float *x, float *y, int *count) {
(void)x;
(void)y;
(void)count;
++slice_scale_count;
last_slice_scale_ctx = ctx;
}
/* ---- NaN guard (kept real). ---- */
int rr_isnan_f(float value) {
++isnan_count;
return value != value;
}