xo-arena: DArenaHashMap: generative test + check load factor

This commit is contained in:
Roland Conybeare 2026-01-07 16:52:50 -05:00
commit cd8d6a2f67
8 changed files with 824 additions and 33 deletions

View file

@ -4,12 +4,19 @@
**/
#include "DArenaHashMap.hpp"
#include "random_hash_ops.hpp"
#include <xo/randomgen/random_seed.hpp>
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::mm::DArenaHashMapUtil;
using xo::mm::DArenaHashMap;
//using xo::mM::ArenaConfig;
using xo::rng::random_seed;
using xo::rng::xoshiro256ss;
using utest::UtestTools;
using utest::HashMapUtil;
namespace ut {
TEST_CASE("DArenaHashMap-ctor", "[arena][DArenaHashMap]")
@ -36,6 +43,98 @@ namespace xo {
REQUIRE(map.capacity() == std::max(512ul,
DArenaHashMapUtil::c_group_size));
}
TEST_CASE("DArenaHashMap-try-insert", "[arena][DArenaHashMap]")
{
using HashMap = DArenaHashMap<int, int>;
HashMap map;
REQUIRE(map.empty());
REQUIRE(map.size() == 0);
REQUIRE(map.groups() == 1);
REQUIRE(map.capacity() == DArenaHashMapUtil::c_group_size);
{
auto x = map.try_insert(std::make_pair(1, 11));
REQUIRE(x.first);
REQUIRE(x.second);
REQUIRE(!map.empty());
REQUIRE(map.size() == 1);
REQUIRE(map.groups() == 1);
REQUIRE(map.capacity() == DArenaHashMapUtil::c_group_size);
REQUIRE(map.load_factor() == 1/16.0);
}
{
auto x = map.try_insert(std::make_pair(2, 9));
REQUIRE(x.first);
REQUIRE(x.second);
REQUIRE(!map.empty());
REQUIRE(map.size() == 2);
REQUIRE(map.groups() == 1);
REQUIRE(map.capacity() == DArenaHashMapUtil::c_group_size);
REQUIRE(map.load_factor() == 2/16.0);
}
{
auto x = map.try_insert(std::make_pair(259, 12));
REQUIRE(x.first);
REQUIRE(x.second);
REQUIRE(!map.empty());
REQUIRE(map.size() == 3);
REQUIRE(map.groups() == 1);
REQUIRE(map.capacity() == DArenaHashMapUtil::c_group_size);
REQUIRE(map.load_factor() == 3/16.0);
}
}
TEST_CASE("DArenaHashMap-try-insert2", "[arena][DArenaHashMap]")
{
using HashMap = DArenaHashMap<int, int>;
std::uint64_t seed = 17747889312058974961ul;
//random_seed(&seed); // to get new random seed
//log && log(xtag("seed", seed));
auto rgen = xoshiro256ss(seed);
/* 1. Perform series of tests with increasing scale
* 2. Each test may run in two modes:
* a. silent fast fail. just report success.
* In this mode avoid catch2 REQUIRE
* b. noisy. run with logging enabled
* This mode automatically invoked when silent mode
* observes test failure
*/
for (std::uint32_t n = 0; n <= 2; ) {
HashMap hash_map;
auto test_fn = [&rgen, &hash_map](bool dbg_flag,
std::uint32_t n)
{
bool ok_flag = true;
ok_flag &= HashMapUtil<HashMap>::random_inserts(n, dbg_flag, &rgen, &hash_map);
return ok_flag;
};
bool ok_flag = UtestTools::bimodal_test("DArenaHashMap-try-insert2", test_fn, n);
if (n == 0)
n = 1;
else
n = 2*n;
}
}
// TODO:
// - let's try getting lcov to work in xo-umbrella2
}
}