Files
regolith-ui/tests/testing.h
T
2026-11-02 19:42:48 -06:00

59 lines
1.5 KiB
C

#ifndef RG_TESTING_H
#define RG_TESTING_H
#include "regolith.h"
#include <stdio.h>
#include <stdlib.h>
static int g_failures;
#define CHECK(cond) \
do { \
if (!(cond)) { \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
g_failures++; \
} \
} while (0)
typedef struct testing_alloc {
size_t live;
size_t peak;
} testing_alloc;
static void ta_track(testing_alloc *t, size_t add, size_t sub) {
t->live = t->live + add - sub;
if (t->live > t->peak)
t->peak = t->live;
}
static void *ta_alloc(size_t size, void *user) {
void *p = malloc(size);
if (p)
ta_track(user, size, 0);
return p;
}
static void *ta_realloc(void *ptr, size_t old_size, size_t new_size,
void *user) {
void *p = realloc(ptr, new_size);
if (p)
ta_track(user, new_size, old_size);
return p;
}
static void ta_free(void *ptr, size_t size, void *user) {
ta_track(user, 0, size);
free(ptr);
}
static void testing_use_counting_allocator(rg_config *cfg, testing_alloc *t) {
cfg->allocator.alloc = ta_alloc;
cfg->allocator.realloc = ta_realloc;
cfg->allocator.free = ta_free;
cfg->allocator.user = t;
}
#endif