Skip to content

Commit

Permalink
Merge pull request #32269 from vespa-engine/toregge/rewrite-searchlib…
Browse files Browse the repository at this point in the history
…-same-element-unit-test-to-gtest

Rewrite searchlib same elements unit test to gtest.
  • Loading branch information
baldersheim authored Aug 27, 2024
2 parents 9f10d7c + e5ac78a commit 1513575
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
1 change: 1 addition & 0 deletions searchlib/src/tests/queryeval/same_element/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ vespa_add_executable(searchlib_same_element_test_app TEST
same_element_test.cpp
DEPENDS
vespa_searchlib
GTest::gtest
)
vespa_add_test(NAME searchlib_same_element_test_app COMMAND searchlib_same_element_test_app)
55 changes: 27 additions & 28 deletions searchlib/src/tests/queryeval/same_element/same_element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
#include <vespa/searchlib/queryeval/same_element_search.h>
#include <vespa/searchcommon/attribute/i_search_context.h>
#include <vespa/searchlib/attribute/searchcontextelementiterator.h>
#include <vespa/vespalib/test/insertion_operators.h>
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/testkit/test_master.hpp>
#include <vespa/vespalib/gtest/gtest.h>

using namespace search::fef;
using namespace search::queryeval;
using search::attribute::SearchContextElementIterator;

void verify_elements(SameElementSearch &se, uint32_t docid, const std::initializer_list<uint32_t> list) {
SCOPED_TRACE("verify elements, docid=" + std::to_string(docid));
std::vector<uint32_t> expect(list);
std::vector<uint32_t> actual;
se.find_matching_elements(docid, actual);
EXPECT_EQUAL(actual, expect);
EXPECT_EQ(actual, expect);
}

FieldSpec make_field_spec() {
Expand Down Expand Up @@ -72,15 +71,15 @@ FakeResult make_result(const std::vector<std::pair<uint32_t,std::vector<uint32_t
return result;
}

TEST("require that simple match can be found") {
TEST(SameElementTest, require_that_simple_match_can_be_found) {
auto a = make_result({{5, {1,3,7}}});
auto b = make_result({{5, {3,5,10}}});
SimpleResult result = find_matches({a, b});
SimpleResult expect({5});
EXPECT_EQUAL(result, expect);
EXPECT_EQ(result, expect);
}

TEST("require that matching elements can be identified") {
TEST(SameElementTest, require_that_matching_elements_can_be_identified) {
auto a = make_result({{5, {1,3,7,12}}, {10, {1,2,3}}});
auto b = make_result({{5, {3,5,7,10}}, {10, {4,5,6}}});
auto bp = finalize(make_blueprint({a,b}), false);
Expand All @@ -89,70 +88,70 @@ TEST("require that matching elements can be identified") {
search->initRange(1, 1000);
auto *se = dynamic_cast<SameElementSearch*>(search.get());
ASSERT_TRUE(se != nullptr);
TEST_DO(verify_elements(*se, 5, {3, 7}));
TEST_DO(verify_elements(*se, 10, {}));
TEST_DO(verify_elements(*se, 20, {}));
verify_elements(*se, 5, {3, 7});
verify_elements(*se, 10, {});
verify_elements(*se, 20, {});
}

TEST("require that children must match within same element") {
TEST(SameElementTest, require_that_children_must_match_within_same_element) {
auto a = make_result({{5, {1,3,7}}});
auto b = make_result({{5, {2,5,10}}});
SimpleResult result = find_matches({a, b});
SimpleResult expect;
EXPECT_EQUAL(result, expect);
EXPECT_EQ(result, expect);
}

TEST("require that strict iterator seeks to next hit and can unpack matching docid") {
TEST(SameElementTest, require_that_strict_iterator_seeks_to_next_hit_and_can_unpack_matching_docid) {
auto md = make_match_data();
auto a = make_result({{5, {1,2}}, {7, {1,2}}, {8, {1,2}}, {9, {1,2}}});
auto b = make_result({{5, {3}}, {6, {1,2}}, {7, {2,4}}, {9, {1}}});
auto bp = finalize(make_blueprint({a,b}), true);
auto search = bp->createSearch(*md);
auto* tfmd = md->resolveTermField(0);
search->initRange(1, 1000);
EXPECT_LESS(search->getDocId(), 1u);
EXPECT_LT(search->getDocId(), 1u);
EXPECT_FALSE(search->seek(1));
EXPECT_EQUAL(search->getDocId(), 7u);
EXPECT_EQ(search->getDocId(), 7u);
search->unpack(7);
EXPECT_EQUAL(tfmd->getDocId(), 7u);
EXPECT_EQ(tfmd->getDocId(), 7u);
EXPECT_TRUE(search->seek(9));
EXPECT_EQUAL(search->getDocId(), 9u);
EXPECT_EQUAL(tfmd->getDocId(), 7u);
EXPECT_EQ(search->getDocId(), 9u);
EXPECT_EQ(tfmd->getDocId(), 7u);
search->unpack(9);
EXPECT_EQUAL(tfmd->getDocId(), 9u);
EXPECT_EQ(tfmd->getDocId(), 9u);
EXPECT_FALSE(search->seek(10));
EXPECT_TRUE(search->isAtEnd());
}

TEST("require that results are estimated appropriately") {
TEST(SameElementTest, require_that_results_are_estimated_appropriately) {
auto a = make_result({{5, {0}}, {5, {0}}, {5, {0}}});
auto b = make_result({{5, {0}}, {5, {0}}});
auto c = make_result({{5, {0}}, {5, {0}}, {5, {0}}, {5, {0}}});
auto bp = finalize(make_blueprint({a,b,c}), true);
EXPECT_EQUAL(bp->getState().estimate().estHits, 2u);
EXPECT_EQ(bp->getState().estimate().estHits, 2u);
}

TEST("require that children are sorted") {
TEST(SameElementTest, require_that_children_are_sorted) {
auto a = make_result({{5, {0}}, {5, {0}}, {5, {0}}});
auto b = make_result({{5, {0}}, {5, {0}}});
auto c = make_result({{5, {0}}, {5, {0}}, {5, {0}}, {5, {0}}});
auto bp = finalize(make_blueprint({a,b,c}), true);
EXPECT_EQUAL(dynamic_cast<SameElementBlueprint&>(*bp).terms()[0]->getState().estimate().estHits, 2u);
EXPECT_EQUAL(dynamic_cast<SameElementBlueprint&>(*bp).terms()[1]->getState().estimate().estHits, 3u);
EXPECT_EQUAL(dynamic_cast<SameElementBlueprint&>(*bp).terms()[2]->getState().estimate().estHits, 4u);
EXPECT_EQ(dynamic_cast<SameElementBlueprint&>(*bp).terms()[0]->getState().estimate().estHits, 2u);
EXPECT_EQ(dynamic_cast<SameElementBlueprint&>(*bp).terms()[1]->getState().estimate().estHits, 3u);
EXPECT_EQ(dynamic_cast<SameElementBlueprint&>(*bp).terms()[2]->getState().estimate().estHits, 4u);
}

TEST("require that attribute iterators are wrapped for element unpacking") {
TEST(SameElementTest, require_that_attribute_iterators_are_wrapped_for_element_unpacking) {
auto a = make_result({{5, {1,3,7}}});
auto b = make_result({{5, {3,5,10}}});
auto bp = finalize(make_blueprint({a,b}, true), false);
auto md = make_match_data();
auto search = bp->createSearch(*md);
auto *se = dynamic_cast<SameElementSearch*>(search.get());
ASSERT_TRUE(se != nullptr);
ASSERT_EQUAL(se->children().size(), 2u);
ASSERT_EQ(se->children().size(), 2u);
EXPECT_TRUE(dynamic_cast<SearchContextElementIterator*>(se->children()[0].get()) != nullptr);
EXPECT_TRUE(dynamic_cast<SearchContextElementIterator*>(se->children()[1].get()) != nullptr);
}

TEST_MAIN() { TEST_RUN_ALL(); }
GTEST_MAIN_RUN_ALL_TESTS()

0 comments on commit 1513575

Please sign in to comment.