navid/haze / 221433b37405f424c4ca0e8131b8d4d4c92a29bd547e3562beac70a7b8f81878 / tests/runtime_test.cpp
runtime_test.cpp
← tests · 2791 bytes · raw · download
#include "xyzrt.h"
#include <cstdio>
#include <cstdlib>
namespace {
int failures = 0;
void check(bool ok, const char* what) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what);
failures++;
}
}
} // namespace
int main() {
xyz_runtime_init();
/* A descriptor with two managed pointer fields. */
static const uint32_t offsets[] = {8, 16};
uint32_t parent_type = xyz_register_type(32, 2, offsets);
void* child = xyz_alloc_slow(16, 0);
void* parent = xyz_alloc_slow(16, parent_type);
check(child && parent, "allocation");
*reinterpret_cast<void**>(static_cast<unsigned char*>(parent) + 8) = child;
void* root = parent;
xyz_root_add(&root);
xyz_gc_collect();
xyz_gc_verify();
check(xyz_gc_object_alive(parent), "parent kept by global root");
check(xyz_gc_object_alive(child), "child traced through descriptor");
xyz_root_remove(&root);
parent = nullptr;
child = nullptr;
xyz_gc_collect();
xyz_gc_verify();
/* Medium object. */
void* medium = xyz_alloc_slow(4000, 0);
check(medium != nullptr, "medium allocation");
root = medium;
xyz_root_add(&root);
xyz_gc_collect();
check(xyz_gc_object_alive(medium), "medium object survives");
xyz_root_remove(&root);
/* Large object space. */
void* large = xyz_alloc_large(10000, 0);
check(large != nullptr, "large allocation");
root = large;
xyz_root_add(&root);
xyz_gc_collect();
check(xyz_gc_object_alive(large), "large object survives");
xyz_root_remove(&root);
xyz_gc_collect();
check(!xyz_gc_object_alive(large), "large object reclaimed");
/* Shadow-stack frame. */
void* slot[1] = {nullptr};
static const uint32_t root_offsets[] = {0};
XyzRootMap map = {1, 24, root_offsets};
XyzRootFrame frame = {nullptr, &map, slot};
slot[0] = xyz_alloc_slow(64, 0);
xyz_root_enter(&frame);
xyz_gc_collect();
xyz_gc_verify();
check(xyz_gc_object_alive(slot[0]), "object kept by root frame");
xyz_root_leave(&frame);
xyz_gc_collect();
check(!xyz_gc_object_alive(slot[0]), "object collected after root leave");
/* Epoch wraparound smoke test. */
void* stable = xyz_alloc_slow(32, 0);
root = stable;
xyz_root_add(&root);
for (int i = 0; i < 300; i++) {
xyz_gc_collect();
if (!xyz_gc_object_alive(stable)) {
check(false, "stable object survived epoch wrap");
break;
}
}
xyz_root_remove(&root);
xyz_gc_collect();
check(!xyz_gc_object_alive(stable), "stable object reclaimed");
if (failures) {
std::fprintf(stderr, "%d runtime test(s) failed\n", failures);
return 1;
}
std::puts("runtime tests passed");
return 0;
}