From e9e69735ddbfe112da4a4ff1b14c14ba6e1088fb Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Sat, 5 Oct 2024 07:59:29 +0200 Subject: [PATCH] add non-working test with reentrant deletion --- test/app/counter.cpp | 4 ++++ test/app/counter.h | 2 ++ test/unit/reentrant.cpp | 45 ++++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/test/app/counter.cpp b/test/app/counter.cpp index e763296b..ad0d36be 100644 --- a/test/app/counter.cpp +++ b/test/app/counter.cpp @@ -155,6 +155,10 @@ auto counter::obj::get() const -> size_t const& { return m_data; } +auto counter::obj::counts() -> counter& { + return *m_counts; +} + auto counter::obj::get() -> size_t& { if (nullptr != m_counts) { ++m_counts->m_data.m_get; diff --git a/test/app/counter.h b/test/app/counter.h index 15149c94..be0199b3 100644 --- a/test/app/counter.h +++ b/test/app/counter.h @@ -61,6 +61,8 @@ class counter { [[nodiscard]] auto get() const -> size_t const&; auto get() -> size_t&; + [[nodiscard]] auto counts() -> counter&; + void swap(obj& other); [[nodiscard]] auto get_for_hash() const -> size_t; diff --git a/test/unit/reentrant.cpp b/test/unit/reentrant.cpp index 70bf038a..085ffc71 100644 --- a/test/unit/reentrant.cpp +++ b/test/unit/reentrant.cpp @@ -3,33 +3,42 @@ #include #include -TEST_CASE_MAP("reentrant_destruct", counter::obj, counter::obj) { - auto counts = counter{}; - INFO(counts); +namespace { - auto container = typename map_t::value_container_type{}; +class EraseOnDestruct; - // TODO +using map_t = ankerl::unordered_dense::map>; - for (size_t i = 0; i < 100; ++i) { - container.emplace_back(counter::obj{i, counts}, counter::obj{i, counts}); - container.emplace_back(counter::obj{i, counts}, counter::obj{i, counts}); - } +class EraseOnDestruct { + map_t* m_map; + counter::obj m_counter; + +public: + EraseOnDestruct(map_t& map, size_t i, counter& counts) + : m_map(&map) + , m_counter(i, counts) {} - for (size_t i = 0; i < 10; ++i) { - container.emplace_back(counter::obj{i, counts}, counter::obj{i, counts}); + ~EraseOnDestruct() { + (void)m_map; + m_map->erase(counter::obj{m_counter.get() + 1, m_counter.counts()}); } +}; + +} // namespace + +TEST_CASE("reentrant_destruct") { + auto counts = counter{}; + INFO(counts); - // add some elements auto map = map_t(); - for (size_t i = 0; i < 10; ++i) { - map.try_emplace(counter::obj{i, counts}, counter::obj{i, counts}); - } - map.replace(std::move(container)); + /* + This doesn't work, it is not something that can be supported with using std::vector. + I'll leave the code here so it's not lost though. - REQUIRE(map.size() == 100U); for (size_t i = 0; i < 100; ++i) { - REQUIRE(map.contains(counter::obj{i, counts})); + map.try_emplace(counter::obj{i, counts}, std::make_unique(map, i, counts)); } + map.erase(counter::obj{0, counts}); + */ }