diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-19 21:57:59 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-20 12:38:48 +0000 |
commit | 64500ab17deb86b394edc81f4ad42b5a2c43fe30 (patch) | |
tree | 64334ba1513b697dacd5068981a8ee5b7ad92f3b /vespalib/src/tests/objects/objectselection | |
parent | cfa6ec5cdbd1cf39558d3f85101de05230d6c225 (diff) |
Fold staging_vespalib into vespalib
Diffstat (limited to 'vespalib/src/tests/objects/objectselection')
3 files changed, 106 insertions, 0 deletions
diff --git a/vespalib/src/tests/objects/objectselection/.gitignore b/vespalib/src/tests/objects/objectselection/.gitignore new file mode 100644 index 00000000000..f6aefd07270 --- /dev/null +++ b/vespalib/src/tests/objects/objectselection/.gitignore @@ -0,0 +1,4 @@ +.depend +Makefile +objectselection_test +vespalib_objectselection_test_app diff --git a/vespalib/src/tests/objects/objectselection/CMakeLists.txt b/vespalib/src/tests/objects/objectselection/CMakeLists.txt new file mode 100644 index 00000000000..94f43078820 --- /dev/null +++ b/vespalib/src/tests/objects/objectselection/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_objectselection_test_app TEST + SOURCES + objectselection.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_objectselection_test_app COMMAND vespalib_objectselection_test_app) diff --git a/vespalib/src/tests/objects/objectselection/objectselection.cpp b/vespalib/src/tests/objects/objectselection/objectselection.cpp new file mode 100644 index 00000000000..aa9c841f2dc --- /dev/null +++ b/vespalib/src/tests/objects/objectselection/objectselection.cpp @@ -0,0 +1,94 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/vespalib/objects/identifiable.hpp> +#include <vespa/vespalib/objects/objectpredicate.h> +#include <vespa/vespalib/objects/objectoperation.h> + +using namespace vespalib; + +#define CID_Foo 60000005 +#define CID_Bar 60000010 + +struct Foo : public Identifiable +{ + typedef IdentifiablePtr<Foo> CP; + std::vector<CP> nodes; + + DECLARE_IDENTIFIABLE(Foo); + virtual Foo *clone() const { return new Foo(*this); } + void selectMembers(const ObjectPredicate &p, ObjectOperation &o) override { + for (uint32_t i = 0; i < nodes.size(); ++i) { + nodes[i]->select(p, o); + } + } +}; +IMPLEMENT_IDENTIFIABLE(Foo, Identifiable); + +struct Bar : public Foo +{ + int value; + + DECLARE_IDENTIFIABLE(Bar); + Bar() : value(0) {} + Bar(int v) { value = v; } + Bar *clone() const override { return new Bar(*this); } +}; +IMPLEMENT_IDENTIFIABLE(Bar, Identifiable); + +struct ObjectType : public ObjectPredicate +{ + uint32_t cid; + ObjectType(uint32_t id) : cid(id) {} + bool check(const Identifiable &obj) const override { + return (obj.getClass().id() == cid); + } +}; + +struct ObjectCollect : public ObjectOperation +{ + std::vector<Identifiable*> nodes; + ~ObjectCollect() override; + void execute(Identifiable &obj) override { + nodes.push_back(&obj); + } +}; + +ObjectCollect::~ObjectCollect() = default; + +TEST_SETUP(Test); + +int +Test::Main() +{ + TEST_INIT("objectselection_test"); + { + Foo f1; + Foo f2; + Foo f3; + Bar b1(1); + Bar b2(2); + Bar b3(3); + Bar b4(4); + f2.nodes.push_back(b1); + f2.nodes.push_back(b2); + f3.nodes.push_back(b3); + f3.nodes.push_back(b4); + f1.nodes.push_back(f2); + f1.nodes.push_back(f3); + + ObjectType predicate(Bar::classId); + ObjectCollect operation; + f1.select(predicate, operation); + ASSERT_TRUE(operation.nodes.size() == 4); + ASSERT_TRUE(operation.nodes[0]->getClass().id() == Bar::classId); + ASSERT_TRUE(operation.nodes[1]->getClass().id() == Bar::classId); + ASSERT_TRUE(operation.nodes[2]->getClass().id() == Bar::classId); + ASSERT_TRUE(operation.nodes[3]->getClass().id() == Bar::classId); + ASSERT_TRUE(((Bar*)operation.nodes[0])->value == 1); + ASSERT_TRUE(((Bar*)operation.nodes[1])->value == 2); + ASSERT_TRUE(((Bar*)operation.nodes[2])->value == 3); + ASSERT_TRUE(((Bar*)operation.nodes[3])->value == 4); + } + TEST_DONE(); +} |