Skip to content

Commit

Permalink
Merge pull request #31510 from vespa-engine/toregge/rewrite-vsm-docsu…
Browse files Browse the repository at this point in the history
…m-unit-test-to-gtest

Rewrite vsm docsum unit test to gtest.
  • Loading branch information
baldersheim authored Jun 10, 2024
2 parents 1787c7f + 11edb11 commit 367b751
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
1 change: 1 addition & 0 deletions streamingvisitors/src/tests/docsum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ vespa_add_executable(vsm_docsum_test_app TEST
docsum_test.cpp
DEPENDS
streamingvisitors
GTest::gtest
)
vespa_add_test(NAME vsm_docsum_test_app COMMAND vsm_docsum_test_app)
106 changes: 52 additions & 54 deletions streamingvisitors/src/tests/docsum/docsum_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>

#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/document/datatype/structdatatype.h>
#include <vespa/document/datatype/weightedsetdatatype.h>
Expand All @@ -9,6 +9,7 @@
#include <vespa/vsm/vsm/flattendocsumwriter.h>
#include <vespa/vespalib/data/smart_buffer.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace document;

Expand Down Expand Up @@ -48,24 +49,29 @@ class TestDocument : public vsm::Document
};


class DocsumTest : public vespalib::TestApp
class DocsumTest : public ::testing::Test
{
private:
protected:
ArrayFieldValue createFieldValue(const StringList & fv);
WeightedSetFieldValue createFieldValue(const WeightedStringList & fv);

void assertFlattenDocsumWriter(const FieldValue & fv, const std::string & exp) {
void assertFlattenDocsumWriter(const FieldValue & fv, const std::string & exp, const std::string& label) {
FlattenDocsumWriter fdw;
assertFlattenDocsumWriter(fdw, fv, exp);
assertFlattenDocsumWriter(fdw, fv, exp, label);
}
void assertFlattenDocsumWriter(FlattenDocsumWriter & fdw, const FieldValue & fv, const std::string & exp);
void testFlattenDocsumWriter();
void testDocSumCache();
void assertFlattenDocsumWriter(FlattenDocsumWriter & fdw, const FieldValue & fv, const std::string & exp, const std::string& label);

public:
int Main() override;
DocsumTest();
~DocsumTest() override;
};

DocsumTest::DocsumTest()
: ::testing::Test()
{
}

DocsumTest::~DocsumTest() = default;

ArrayFieldValue
DocsumTest::createFieldValue(const StringList & fv)
{
Expand All @@ -90,62 +96,54 @@ DocsumTest::createFieldValue(const WeightedStringList & fv)
}

void
DocsumTest::assertFlattenDocsumWriter(FlattenDocsumWriter & fdw, const FieldValue & fv, const std::string & exp)
DocsumTest::assertFlattenDocsumWriter(FlattenDocsumWriter & fdw, const FieldValue & fv, const std::string & exp, const std::string& label)
{
SCOPED_TRACE(label);
FieldPath empty;
fv.iterateNested(empty.getFullRange(), fdw);
std::string actual(fdw.getResult().getBuffer(), fdw.getResult().getPos());
EXPECT_EQUAL(actual, exp);
EXPECT_EQ(exp, actual);
}

void
DocsumTest::testFlattenDocsumWriter()
TEST_F(DocsumTest, flatten_docsum_writer_basic)
{
{ // basic tests
TEST_DO(assertFlattenDocsumWriter(StringFieldValue("foo bar"), "foo bar"));
TEST_DO(assertFlattenDocsumWriter(RawFieldValue("foo bar"), "foo bar"));
TEST_DO(assertFlattenDocsumWriter(BoolFieldValue(true), "true"));
TEST_DO(assertFlattenDocsumWriter(BoolFieldValue(false), "false"));
TEST_DO(assertFlattenDocsumWriter(LongFieldValue(123456789), "123456789"));
TEST_DO(assertFlattenDocsumWriter(createFieldValue(StringList().add("foo bar").add("baz").add(" qux ")),
"foo bar baz qux "));
}
{ // test mulitple invocations
FlattenDocsumWriter fdw("#");
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("foo"), "foo"));
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("bar"), "foo#bar"));
fdw.clear();
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("baz"), "baz"));
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("qux"), "baz qux"));
}
{ // test resizing
FlattenDocsumWriter fdw("#");
EXPECT_EQUAL(fdw.getResult().getPos(), 0u);
EXPECT_EQUAL(fdw.getResult().getLength(), 32u);
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("aaaabbbbccccddddeeeeffffgggghhhh"),
"aaaabbbbccccddddeeeeffffgggghhhh"));
EXPECT_EQUAL(fdw.getResult().getPos(), 32u);
EXPECT_EQUAL(fdw.getResult().getLength(), 32u);
TEST_DO(assertFlattenDocsumWriter(fdw, StringFieldValue("aaaa"), "aaaabbbbccccddddeeeeffffgggghhhh#aaaa"));
EXPECT_EQUAL(fdw.getResult().getPos(), 37u);
EXPECT_TRUE(fdw.getResult().getLength() >= 37u);
fdw.clear();
EXPECT_EQUAL(fdw.getResult().getPos(), 0u);
EXPECT_TRUE(fdw.getResult().getLength() >= 37u);
}
assertFlattenDocsumWriter(StringFieldValue("foo bar"), "foo bar", "string foo bar");
assertFlattenDocsumWriter(RawFieldValue("foo bar"), "foo bar", "raw foo bar");
assertFlattenDocsumWriter(BoolFieldValue(true), "true", "bool true");
assertFlattenDocsumWriter(BoolFieldValue(false), "false", "bool false");
assertFlattenDocsumWriter(LongFieldValue(123456789), "123456789", "long");
assertFlattenDocsumWriter(createFieldValue(StringList().add("foo bar").add("baz").add(" qux ")),
"foo bar baz qux ", "wset");
}

int
DocsumTest::Main()
TEST_F(DocsumTest, flatten_docsum_writer_multiple_invocations)
{
TEST_INIT("docsum_test");

TEST_DO(testFlattenDocsumWriter());

TEST_DONE();
FlattenDocsumWriter fdw("#");
assertFlattenDocsumWriter(fdw, StringFieldValue("foo"), "foo", "string foo");
assertFlattenDocsumWriter(fdw, StringFieldValue("bar"), "foo#bar", "string bar");
fdw.clear();
assertFlattenDocsumWriter(fdw, StringFieldValue("baz"), "baz", "string baz");
assertFlattenDocsumWriter(fdw, StringFieldValue("qux"), "baz qux", "string qux");
}

TEST_F(DocsumTest, flatten_docsum_writer_resizing)
{
FlattenDocsumWriter fdw("#");
EXPECT_EQ(fdw.getResult().getPos(), 0u);
EXPECT_EQ(fdw.getResult().getLength(), 32u);
assertFlattenDocsumWriter(fdw, StringFieldValue("aaaabbbbccccddddeeeeffffgggghhhh"),
"aaaabbbbccccddddeeeeffffgggghhhh",
"string long");
EXPECT_EQ(fdw.getResult().getPos(), 32u);
EXPECT_EQ(fdw.getResult().getLength(), 32u);
assertFlattenDocsumWriter(fdw, StringFieldValue("aaaa"), "aaaabbbbccccddddeeeeffffgggghhhh#aaaa", "string second long");
EXPECT_EQ(fdw.getResult().getPos(), 37u);
EXPECT_TRUE(fdw.getResult().getLength() >= 37u);
fdw.clear();
EXPECT_EQ(fdw.getResult().getPos(), 0u);
EXPECT_TRUE(fdw.getResult().getLength() >= 37u);
}

TEST_APPHOOK(vsm::DocsumTest);
}

GTEST_MAIN_RUN_ALL_TESTS()

0 comments on commit 367b751

Please sign in to comment.