From 17753316f82622b6c93055f6c5e907df0dd6731d Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 6 Jan 2026 15:34:51 -0500 Subject: [PATCH] xo-arena: DArena unit test --- CMakeLists.txt | 2 +- include/xo/arena/DArena.hpp | 3 ++ src/arena/DArena.cpp | 16 ++++++++++ utest/CMakeLists.txt | 23 ++++++++++++++ utest/DArena.test.cpp | 61 +++++++++++++++++++++++++++++++++++++ utest/arena_utest_main.cpp | 6 ++++ 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 utest/CMakeLists.txt create mode 100644 utest/DArena.test.cpp create mode 100644 utest/arena_utest_main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 24d544e..36fbfcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ add_definitions(${PROJECT_CXX_FLAGS}) # output targets add_subdirectory(src/arena) -#add_subdirectory(utest) +add_subdirectory(utest) # ---------------------------------------------------------------- # cmake export diff --git a/include/xo/arena/DArena.hpp b/include/xo/arena/DArena.hpp index f9c2e3c..3ad10db 100644 --- a/include/xo/arena/DArena.hpp +++ b/include/xo/arena/DArena.hpp @@ -219,6 +219,9 @@ namespace xo { **/ void clear() noexcept; + /** swap contents (including configuration) with another arena **/ + void swap(DArena & other) noexcept; + ///@} /** @defgroup mm-arena-instance-vars **/ diff --git a/src/arena/DArena.cpp b/src/arena/DArena.cpp index 5c0e364..ecd698a 100644 --- a/src/arena/DArena.cpp +++ b/src/arena/DArena.cpp @@ -616,6 +616,22 @@ namespace xo { this->free_ = lo_; this->establish_initial_guard(); } + + void + DArena::swap(DArena & other) noexcept + { + std::swap(config_, other.config_); + std::swap(page_z_, other.page_z_); + std::swap(arena_align_z_, other.arena_align_z_); + std::swap(lo_, other.lo_); + std::swap(committed_z_, other.committed_z_); + std::swap(last_header_, other.last_header_); + std::swap(free_, other.free_); + std::swap(limit_, other.limit_); + std::swap(hi_, other.hi_); + std::swap(error_count_, other.error_count_); + std::swap(last_error_, other.last_error_); + } } } /*namespace xo*/ diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt new file mode 100644 index 0000000..34bf3ca --- /dev/null +++ b/utest/CMakeLists.txt @@ -0,0 +1,23 @@ +# xo-alloc2/utest/CMakeLists.txt +# + +set(UTEST_EXE utest.arena) +set(UTEST_SRCS + arena_utest_main.cpp +# objectmodel.test.cpp + DArena.test.cpp +# DArenaIterator.test.cpp +# Collector.test.cpp +# DX1CollectorIterator.test.cpp +# random_allocs.cpp +) + +if (ENABLE_TESTING) + xo_add_utest_executable(${UTEST_EXE} ${UTEST_SRCS}) + xo_self_dependency(${UTEST_EXE} xo_arena) +# xo_headeronly_dependency(${UTEST_EXE} randomgen) +# xo_headeronly_dependency(${UTEST_EXE} indentlog) + xo_external_target_dependency(${UTEST_EXE} Catch2 Catch2::Catch2) +endif() + +# end CMakeLists.txt diff --git a/utest/DArena.test.cpp b/utest/DArena.test.cpp new file mode 100644 index 0000000..2787047 --- /dev/null +++ b/utest/DArena.test.cpp @@ -0,0 +1,61 @@ +/** @file DArena.test.cpp + * + * @author Roland Conybeare, Jan 2026 + **/ + +#include "xo/arena/DArena.hpp" +#include + +namespace xo { + using xo::mm::DArena; + using xo::mm::ArenaConfig; + using std::byte; + + namespace ut { + TEST_CASE("DArena-tiny", "[arena][DArena]") + { + ArenaConfig cfg { .name_ = "testarena", + .size_ = 1 }; + DArena arena = DArena::map(cfg); + + REQUIRE(arena.config_.name_ == cfg.name_); + REQUIRE(arena.lo_ != nullptr); + REQUIRE(arena.free_ == arena.lo_); + REQUIRE(arena.limit_ == arena.lo_); + REQUIRE(arena.hi_ != nullptr); + REQUIRE(arena.hi_ > arena.lo_); + REQUIRE(((size_t)arena.hi_ - (size_t)arena.lo_) % arena.page_z_ == 0); + REQUIRE(arena.lo_ + cfg.size_ <= arena.hi_); + + /* verify arena.lo_ is aligned on a page boundary */ + REQUIRE(((size_t)(arena.lo_) & (arena.page_z_ - 1)) == 0); + + /* verify arena.hi_ is aligned on a hugepage boundary */ + REQUIRE(((size_t)(arena.hi_) & (arena.page_z_ - 1)) == 0); + + byte * lo = arena.lo_; + byte * free = arena.free_; + byte * limit = arena.limit_; + byte * hi = arena.hi_; + size_t committed_z = arena.committed_z_; + + DArena arena2 = std::move(arena); + + REQUIRE(arena.lo_ == nullptr); + REQUIRE(arena.free_ == nullptr); + REQUIRE(arena.limit_ == nullptr); + REQUIRE(arena.hi_ == nullptr); + REQUIRE(arena.committed_z_ == 0); + + REQUIRE(arena.lo_ == nullptr); + REQUIRE(arena2.lo_ == lo); + REQUIRE(arena2.free_ == free); + REQUIRE(arena2.limit_ == limit); + REQUIRE(arena2.hi_ == hi); + REQUIRE(arena2.committed_z_ == committed_z); + } + + } +} + +/* end DArena.test.cpp */ diff --git a/utest/arena_utest_main.cpp b/utest/arena_utest_main.cpp new file mode 100644 index 0000000..e0fa95f --- /dev/null +++ b/utest/arena_utest_main.cpp @@ -0,0 +1,6 @@ +/* file arena_utest_main.cpp */ + +#define CATCH_CONFIG_MAIN +#include "catch2/catch.hpp" + +/* end arena_utest_main.cpp */