From 84e9a5330a98a02aab4c498ed643603589970d8c Mon Sep 17 00:00:00 2001 From: HÃ¥vard Pettersen Date: Thu, 26 Nov 2020 10:52:30 +0000 Subject: improve and clean up unit test --- .../shared_string_repo/shared_string_repo_test.cpp | 60 ++++++++++++---------- .../src/vespa/vespalib/util/shared_string_repo.h | 1 + 2 files changed, 33 insertions(+), 28 deletions(-) (limited to 'vespalib') diff --git a/vespalib/src/tests/shared_string_repo/shared_string_repo_test.cpp b/vespalib/src/tests/shared_string_repo/shared_string_repo_test.cpp index 57faa76061a..dcf29cc1445 100644 --- a/vespalib/src/tests/shared_string_repo/shared_string_repo_test.cpp +++ b/vespalib/src/tests/shared_string_repo/shared_string_repo_test.cpp @@ -10,6 +10,7 @@ using namespace vespalib; using make_string_short::fmt; +using Handle = SharedStringRepo::Handle; bool verbose = false; double budget = 0.10; @@ -26,17 +27,8 @@ std::vector make_strings(size_t cnt) { return strings; } -std::vector copy_strings(const std::vector &strings) { - std::vector copy; - copy.reserve(strings.size()); - for (size_t i = 0; i < strings.size(); ++i) { - copy.push_back(strings[i]); - } - return copy; -} - -std::vector resolve_strings(const std::vector &strings) { - std::vector handles; +std::vector resolve_strings(const std::vector &strings) { + std::vector handles; handles.reserve(strings.size()); for (size_t i = 0; i < strings.size(); ++i) { handles.emplace_back(strings[i]); @@ -44,16 +36,7 @@ std::vector resolve_strings(const std::vector copy_handles(const std::vector &handles) { - std::vector copy; - copy.reserve(handles.size()); - for (size_t i = 0; i < handles.size(); ++i) { - copy.push_back(handles[i]); - } - return copy; -} - -std::vector get_strings(const std::vector &handles) { +std::vector get_strings(const std::vector &handles) { std::vector strings; strings.reserve(handles.size()); for (size_t i = 0; i < handles.size(); ++i) { @@ -62,8 +45,6 @@ std::vector get_strings(const std::vector) {} - //----------------------------------------------------------------------------- struct Avg : Rendezvous { @@ -150,13 +131,13 @@ struct Fixture { void benchmark(bool is_master) { for (bool once_more = true; vote(once_more); once_more = has_budget()) { std::vector copy_strings_result; - std::vector resolve_result; - std::vector copy_handles_result; - std::vector resolve_again_result; + std::vector resolve_result; + std::vector copy_handles_result; + std::vector resolve_again_result; std::vector get_result; - auto copy_strings_task = [&](){ copy_strings_result = copy_strings(work); }; + auto copy_strings_task = [&](){ copy_strings_result = work; }; auto resolve_task = [&](){ resolve_result = resolve_strings(work); }; - auto copy_handles_task = [&](){ copy_handles_result = copy_handles(resolve_result); }; + auto copy_handles_task = [&](){ copy_handles_result = resolve_result; }; auto resolve_again_task = [&](){ resolve_again_result = resolve_strings(work); }; auto get_task = [&](){ get_result = get_strings(resolve_result); }; auto reclaim_task = [&]() { resolve_again_result.clear(); }; @@ -177,6 +158,29 @@ struct Fixture { //----------------------------------------------------------------------------- +TEST("require that basic usage works") { + Handle empty; + Handle foo("foo"); + Handle bar("bar"); + Handle empty2; + Handle foo2("foo"); + Handle bar2(bar); + EXPECT_EQUAL(empty.id(), 0u); + EXPECT_TRUE(empty.id() != foo.id()); + EXPECT_TRUE(empty.id() != bar.id()); + EXPECT_TRUE(foo.id() != bar.id()); + EXPECT_EQUAL(empty.id(), empty2.id()); + EXPECT_EQUAL(foo.id(), foo2.id()); + EXPECT_EQUAL(bar.id(), bar2.id()); + EXPECT_EQUAL(empty.as_string(), vespalib::string("")); + EXPECT_EQUAL(foo.as_string(), vespalib::string("foo")); + EXPECT_EQUAL(bar.as_string(), vespalib::string("bar")); + EXPECT_EQUAL(foo2.as_string(), vespalib::string("foo")); + EXPECT_EQUAL(bar2.as_string(), vespalib::string("bar")); +} + +//----------------------------------------------------------------------------- + TEST_MT_F("test shared string repo operations with 1 threads", 1, Fixture(num_threads)) { f1.benchmark(thread_id == 0); } diff --git a/vespalib/src/vespa/vespalib/util/shared_string_repo.h b/vespalib/src/vespa/vespalib/util/shared_string_repo.h index 8a0e91dc030..cf0b5ce7459 100644 --- a/vespalib/src/vespa/vespalib/util/shared_string_repo.h +++ b/vespalib/src/vespa/vespalib/util/shared_string_repo.h @@ -185,6 +185,7 @@ public: return *this; } bool operator==(const Handle &rhs) const { return (_id == rhs._id); } + uint32_t id() const { return _id; } vespalib::string as_string() const { return SharedStringRepo::get(_id); } ~Handle() { reclaim(_id); } }; -- cgit v1.2.3