xo-alloc2 : work on X1Collector unit test [WIP]

This commit is contained in:
Roland Conybeare 2025-12-15 22:43:21 -05:00
commit 1fd5d544f2
14 changed files with 542 additions and 41 deletions

View file

@ -7,11 +7,14 @@ set(UTEST_SRCS
arena.test.cpp
objectmodel.test.cpp
Collector.test.cpp
random_allocs.cpp
)
if (ENABLE_TESTING)
xo_add_utest_executable(${UTEST_EXE} ${UTEST_SRCS})
xo_headeronly_dependency(${UTEST_EXE} randomgen)
xo_self_dependency(${UTEST_EXE} xo_alloc2)
xo_headeronly_dependency(${UTEST_EXE} indentlog)
xo_headeronly_dependency(${UTEST_EXE} xo_facet)
xo_external_target_dependency(${UTEST_EXE} Catch2 Catch2::Catch2)
endif()

View file

@ -7,24 +7,36 @@
* see xo-object2/utest
**/
#include "Allocator.hpp"
#include "Collector.hpp"
#include "random_allocs.hpp"
#include "gc/ICollector_DX1Collector.hpp"
#include "gc/IAllocator_DX1Collector.hpp"
//#include "gc/DX1Collector.hpp"
#include <xo/randomgen/xoshiro256.hpp>
#include <xo/randomgen/random_seed.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <xo/indentlog/print/array.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::mm::AAllocator;
using xo::mm::ACollector;
using xo::mm::CollectorConfig;
using xo::mm::DX1Collector;
using xo::mm::ArenaConfig;
using xo::mm::generation;
using xo::mm::c_max_generation;
using xo::facet::with_facet;
namespace ut {
// checklist
// - obj<ACollector> constructible [ ]
// - obj<ACollector> truthy [ ]
// - obj<ACollector> constructible [ ]
// - obj<ACollector> truthy [ ]
// - obj<ACollector,DX1Collector> constructible [ ]
//
// - obj<AAllocator,DX1Collector> constructible [ ]
// - obj<AAllocator,DX1Collector> allocation
TEST_CASE("collector-any-null", "[alloc2][gc][ACollector]")
{
@ -95,12 +107,68 @@ namespace xo {
DX1Collector gc = DX1Collector{cfg};
/* typed collector */
/* typed collector -- repr known at compile time */
obj<ACollector, DX1Collector> x1(&gc);
REQUIRE(x1.iface());
REQUIRE(x1.data());
}
TEST_CASE("collector-x1-facet-mkobj", "[alloc2][gc]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_size_mask_ = 0x0000ffff, };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
/* typed collector -- repr inferred at compile time */
auto x1 = with_facet<ACollector>::mkobj(&gc);
REQUIRE(x1.iface());
REQUIRE(x1.data());
}
TEST_CASE("collector-x1-alloc", "[alloc2][gc]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_size_mask_ = 0x0000ffff, };
/* collector with one generation collapses to a non-generational copying collector */
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 1,
.gc_trigger_v_ = {{64*1024, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector x1state = DX1Collector{cfg};
/* typed collector */
auto x1gc = with_facet<ACollector>::mkobj(&x1state);
auto x1alloc = with_facet<AAllocator>::mkobj(&x1state);
REQUIRE(x1gc.iface());
REQUIRE(x1gc.data());
REQUIRE(x1alloc.iface());
REQUIRE(x1alloc.data());
rng::Seed<rng::xoshiro256ss> seed;
std::cerr << "ratio: seed=" << seed << std::endl;
auto rng = rng::xoshiro256ss(seed);
utest::AllocUtil::random_allocs(25, true, &rng, x1alloc);
}
}
}

103
utest/random_allocs.cpp Normal file
View file

@ -0,0 +1,103 @@
/** @file random_allocs.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "random_allocs.hpp"
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <catch2/catch.hpp>
#include <map>
namespace utest {
using xo::rng::xoshiro256ss;
using xo::facet::obj;
using xo::scope;
using xo::xtag;
using std::uint32_t;
using std::byte;
/* remember an allocation result.
* application owns memory in [lo, lo+z)
*/
struct AllocInfo {
AllocInfo() = default;
AllocInfo(byte * lo, size_t z) : lo_{lo}, z_{z} {}
byte * lo() const { return lo_; }
byte * hi() const { return lo_ + z_; }
byte * lo_ = nullptr;
size_t z_ = 0;
};
bool
AllocUtil::random_allocs(uint32_t n_alloc,
bool catch_flag,
xoshiro256ss * p_rgen,
obj<AAllocator> mm)
{
scope log(XO_DEBUG(catch_flag), xtag("n-alloc", n_alloc));
/* track allocs. verify:
* - allocs are non-overlapping
* - allocs have valid alloc header
* - allocs surrounded by guard bytes
*
* allocs sorted on AllocInfo::lo
*/
std::map<byte *, AllocInfo> allocs_by_lo_map;
/* allocs sorted on AllocInfo::hi */
std::map<byte *, AllocInfo*> allocs_by_hi_map;
for (uint32_t i_alloc = 0; i_alloc < n_alloc; ++i_alloc) {
std::normal_distribution<double> ngen{5.0, 1.5};
double si = ngen(*p_rgen);
double zi = ::pow(2.0, si);
std::size_t z = ::ceil(zi);
bool ok_flag = true;
std::byte * mem = mm.alloc(z);
log && log(xtag("i_alloc", i_alloc),
xtag("si", si),
xtag("zi", zi),
xtag("mem", mem));
log && log(xtag("used", mm.allocated()),
xtag("avail", mm.available()),
xtag("commit", mm.committed()),
xtag("resv", mm.reserved()));
REQUIRE_ORFAIL(ok_flag, catch_flag, mem != nullptr);
REQUIRE_ORFAIL(ok_flag, catch_flag, mm.contains(mem));
REQUIRE_ORFAIL(ok_flag, catch_flag, mm.last_error().error_seq_ == 0);
REQUIRE_ORFAIL(ok_flag, catch_flag, mm.last_error().error_ == xo::mm::error::none);
{
auto ix = allocs_by_lo_map.lower_bound(mem);
if (ix != allocs_by_lo_map.end()) {
REQUIRE_ORFAIL(ok_flag, catch_flag, (ix->first > mem + z));
}
}
{
auto ix = allocs_by_hi_map.upper_bound(mem);
if (ix != allocs_by_hi_map.end()) {
--ix;
REQUIRE_ORFAIL(ok_flag, catch_flag, (ix->first < mem));
}
}
allocs_by_lo_map[mem] = AllocInfo(mem, z);
allocs_by_hi_map[mem + z] = &(allocs_by_lo_map[mem]);
}
return true;
}
}
/* end random_allocs.cpp */

45
utest/random_allocs.hpp Normal file
View file

@ -0,0 +1,45 @@
/** @file random_allocs.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "Allocator.hpp"
#include <xo/facet/obj.hpp>
#include <xo/randomgen/xoshiro256.hpp>
namespace utest {
/* note: trivial REQUIRE() call in else branch bc we still want
* catch2 to count assertions when verification succeeds
*/
# define REQUIRE_ORCAPTURE(ok_flag, catch_flag, expr) \
if (catch_flag) { \
REQUIRE((expr)); \
} else { \
REQUIRE(true); \
ok_flag &= (expr); \
}
# define REQUIRE_ORFAIL(ok_flag, catch_flag, expr) \
REQUIRE_ORCAPTURE(ok_flag, catch_flag, expr); \
if (!ok_flag) \
return ok_flag
struct AllocUtil {
using AAllocator = xo::mm::AAllocator;
/** generate a random sequence of allocations.
* verify allocator behavior
**/
static bool random_allocs(std::uint32_t n_alloc,
bool catch_flag,
xo::rng::xoshiro256ss * p_rgen,
xo::facet::obj<AAllocator> alloc);
};
}
/* end random_allocs.hpp */