From 62e47dcf3ddfcfe4fad06ed39423e7d9de70e9f3 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Sun, 25 Aug 2024 16:36:00 +0200 Subject: [PATCH] Rewrite searchlib PostingListMerger unit test to gtest. --- .../posting_list_merger/CMakeLists.txt | 1 + .../posting_list_merger_test.cpp | 79 ++++++++----------- 2 files changed, 36 insertions(+), 44 deletions(-) diff --git a/searchlib/src/tests/attribute/posting_list_merger/CMakeLists.txt b/searchlib/src/tests/attribute/posting_list_merger/CMakeLists.txt index 636fe01f1c2d..567d8b02d22b 100644 --- a/searchlib/src/tests/attribute/posting_list_merger/CMakeLists.txt +++ b/searchlib/src/tests/attribute/posting_list_merger/CMakeLists.txt @@ -4,5 +4,6 @@ vespa_add_executable(searchlib_posting_list_merger_test_app TEST posting_list_merger_test.cpp DEPENDS vespa_searchlib + GTest::gtest ) vespa_add_test(NAME searchlib_posting_list_merger_test_app COMMAND searchlib_posting_list_merger_test_app) diff --git a/searchlib/src/tests/attribute/posting_list_merger/posting_list_merger_test.cpp b/searchlib/src/tests/attribute/posting_list_merger/posting_list_merger_test.cpp index 22170461f251..a9fcf3e00046 100644 --- a/searchlib/src/tests/attribute/posting_list_merger/posting_list_merger_test.cpp +++ b/searchlib/src/tests/attribute/posting_list_merger/posting_list_merger_test.cpp @@ -1,11 +1,9 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include -#include #include #include -#include -#include +#include using vespalib::btree::BTreeNoLeafData; using search::attribute::PostingListMerger; @@ -60,16 +58,17 @@ class WeightedPostingList constexpr uint32_t docIdLimit = 16_Ki; -struct WeightedFixture +class PostingListMergerTest : public ::testing::Test { +protected: PostingListMerger _merger; - WeightedFixture() + PostingListMergerTest() : _merger(docIdLimit) { } - ~WeightedFixture() = default; + ~PostingListMergerTest() override; void reserveArray(uint32_t postingsCount, size_t postingsSize) { _merger.reserveArray(postingsCount, postingsSize); } @@ -93,67 +92,59 @@ struct WeightedFixture } return result; } - - void assertArray(std::vector exp) - { - EXPECT_EQUAL(exp, asArray()); - } - - void assertBitVector(std::vector exp) - { - EXPECT_EQUAL(exp, bvAsArray()); - } }; -TEST_F("Single weighted array", WeightedFixture) +PostingListMergerTest::~PostingListMergerTest() = default; + +TEST_F(PostingListMergerTest, Single_weighted_array) { - f._merger.reserveArray(1, 4); - f._merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); - f._merger.merge(); - TEST_DO(f.assertArray({{2, 102}, {3, 103}, {5, 105}, {9, 109}})); + _merger.reserveArray(1, 4); + _merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); + _merger.merge(); + EXPECT_EQ((std::vector{{2, 102}, {3, 103}, {5, 105}, {9, 109}}), asArray()); } -TEST_F("Merge array", WeightedFixture) +TEST_F(PostingListMergerTest, Merge_array) { - f._merger.reserveArray(2, 8); - f._merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); - f._merger.addToArray(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}})); - f._merger.merge(); - TEST_DO(f.assertArray({{2, 102}, {3, 103}, {5, 105}, {6, 106}, {8, 108}, {9, 109}, {14, 114}, {17, 117}})); + _merger.reserveArray(2, 8); + _merger.addToArray(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); + _merger.addToArray(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}})); + _merger.merge(); + EXPECT_EQ((std::vector{{2, 102}, {3, 103}, {5, 105}, {6, 106}, {8, 108}, {9, 109}, {14, 114}, {17, 117}}), asArray()); } -TEST_F("Merge many arrays", WeightedFixture) +TEST_F(PostingListMergerTest, Merge_many_arrays) { std::vector res; - f._merger.reserveArray(10, 100); + _merger.reserveArray(10, 100); for (uint32_t i = 0; i < 10; ++i) { std::vector fragment; for (uint32_t j = 0; j < 10; ++j) { fragment.emplace_back(j * 100 + i, j * 200 + i); } - f._merger.addToArray(WeightedPostingList(fragment)); + _merger.addToArray(WeightedPostingList(fragment)); res.insert(res.end(), fragment.begin(), fragment.end()); } - f._merger.merge(); + _merger.merge(); std::sort(res.begin(), res.end()); - TEST_DO(f.assertArray(res)); + EXPECT_EQ(res, asArray()); } -TEST_F("Merge single posting list into bitvector", WeightedFixture) +TEST_F(PostingListMergerTest, Merge_single_posting_list_into_bitvector) { - f._merger.allocBitVector(); - f._merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); - f._merger.merge(); - TEST_DO(f.assertBitVector({2, 3, 5, 9})); + _merger.allocBitVector(); + _merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); + _merger.merge(); + EXPECT_EQ((std::vector{2, 3, 5, 9}), bvAsArray()); } -TEST_F("Merge multiple posting lists into bitvector", WeightedFixture) +TEST_F(PostingListMergerTest, Merge_multiple_posting_lists_into_bitvector) { - f._merger.allocBitVector(); - f._merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); - f._merger.addToBitVector(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}})); - f._merger.merge(); - TEST_DO(f.assertBitVector({2, 3, 5, 6, 8, 9, 14, 17})); + _merger.allocBitVector(); + _merger.addToBitVector(WeightedPostingList({{ 2, 102}, {3, 103}, { 5, 105}, {9, 109}})); + _merger.addToBitVector(WeightedPostingList({{ 6, 106}, {8, 108}, { 14, 114}, {17, 117}})); + _merger.merge(); + EXPECT_EQ((std::vector{2, 3, 5, 6, 8, 9, 14, 17}), bvAsArray()); } -TEST_MAIN() { TEST_RUN_ALL(); } +GTEST_MAIN_RUN_ALL_TESTS()