summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util/generation_hold_list
diff options
context:
space:
mode:
authorGeir Storli <geirst@yahooinc.com>2022-10-10 12:52:56 +0000
committerGeir Storli <geirst@yahooinc.com>2022-10-10 13:08:59 +0000
commit846907b511ccf4f2ecdc4d2b12273e6287c08575 (patch)
treefc7f9b92e1b114b3e20d7badc10bee8aecc4bf0f /vespalib/src/tests/util/generation_hold_list
parent240a62de8a9b3c93fb9f7031f5e204264d414817 (diff)
Implement a generic hold list for data elements associated with a generation.
Diffstat (limited to 'vespalib/src/tests/util/generation_hold_list')
-rw-r--r--vespalib/src/tests/util/generation_hold_list/CMakeLists.txt9
-rw-r--r--vespalib/src/tests/util/generation_hold_list/generation_hold_list_test.cpp45
2 files changed, 54 insertions, 0 deletions
diff --git a/vespalib/src/tests/util/generation_hold_list/CMakeLists.txt b/vespalib/src/tests/util/generation_hold_list/CMakeLists.txt
new file mode 100644
index 00000000000..c85b2537745
--- /dev/null
+++ b/vespalib/src/tests/util/generation_hold_list/CMakeLists.txt
@@ -0,0 +1,9 @@
+# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_generation_hold_list_test_app TEST
+ SOURCES
+ generation_hold_list_test.cpp
+ DEPENDS
+ vespalib
+ GTest::GTest
+)
+vespa_add_test(NAME vespalib_generation_hold_list_test_app COMMAND vespalib_generation_hold_list_test_app)
diff --git a/vespalib/src/tests/util/generation_hold_list/generation_hold_list_test.cpp b/vespalib/src/tests/util/generation_hold_list/generation_hold_list_test.cpp
new file mode 100644
index 00000000000..0490a99f1e0
--- /dev/null
+++ b/vespalib/src/tests/util/generation_hold_list/generation_hold_list_test.cpp
@@ -0,0 +1,45 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/gtest/gtest.h>
+#include <vespa/vespalib/util/generation_hold_list.hpp>
+#include <vespa/vespalib/util/generationholder.h>
+
+using vespalib::GenerationHeldBase;
+using vespalib::GenerationHoldList;
+
+using MyElem = GenerationHeldBase;
+using MyHoldList = GenerationHoldList<MyElem::UP, true>;
+
+TEST(GenerationHoldListTest, holding_of_unique_ptr_elements_with_tracking_of_held_bytes)
+{
+ MyHoldList h;
+ h.insert(std::make_unique<MyElem>(3));
+ h.assign_generation(0);
+ h.insert(std::make_unique<MyElem>(5));
+ h.assign_generation(1);
+ h.insert(std::make_unique<MyElem>(7));
+ h.assign_generation(2);
+ h.insert(std::make_unique<MyElem>(11));
+ h.assign_generation(4);
+ EXPECT_EQ(3 + 5 + 7 + 11, h.get_held_bytes());
+
+ h.reclaim(0);
+ EXPECT_EQ(3 + 5 + 7 + 11, h.get_held_bytes());
+ h.reclaim(1);
+ EXPECT_EQ(5 + 7 + 11, h.get_held_bytes());
+ h.reclaim(2);
+ EXPECT_EQ(7 + 11, h.get_held_bytes());
+
+ h.insert(std::make_unique<MyElem>(13));
+ h.assign_generation(6);
+ EXPECT_EQ(7 + 11 + 13, h.get_held_bytes());
+
+ h.reclaim(6);
+ EXPECT_EQ(13, h.get_held_bytes());
+ h.reclaim(7);
+ EXPECT_EQ(0, h.get_held_bytes());
+ h.reclaim(7);
+ EXPECT_EQ(0, h.get_held_bytes());
+}
+
+GTEST_MAIN_RUN_ALL_TESTS()