xo-arena: DArena unit test

This commit is contained in:
Roland Conybeare 2026-01-06 15:34:51 -05:00
commit 17753316f8
6 changed files with 110 additions and 1 deletions

View file

@ -21,7 +21,7 @@ add_definitions(${PROJECT_CXX_FLAGS})
# output targets
add_subdirectory(src/arena)
#add_subdirectory(utest)
add_subdirectory(utest)
# ----------------------------------------------------------------
# cmake export

View file

@ -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 **/

View file

@ -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*/

23
utest/CMakeLists.txt Normal file
View file

@ -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

61
utest/DArena.test.cpp Normal file
View file

@ -0,0 +1,61 @@
/** @file DArena.test.cpp
*
* @author Roland Conybeare, Jan 2026
**/
#include "xo/arena/DArena.hpp"
#include <catch2/catch.hpp>
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 */

View file

@ -0,0 +1,6 @@
/* file arena_utest_main.cpp */
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
/* end arena_utest_main.cpp */