diff options
author | Tor Egge <Tor.Egge@yahoo-inc.com> | 2016-08-30 09:28:26 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@yahoo-inc.com> | 2016-08-30 09:28:26 +0000 |
commit | b019298bef4bf5cb6a8800a3e151f151f9d1b577 (patch) | |
tree | 6b8ff1b7807b80574afff37710a65bfa6458b3d0 /searchlib/src/tests/features | |
parent | a85fdd721f00efbb48b0496796e6c12fdead5a6d (diff) |
Add constant feature blueprint and constant feature executor.
Diffstat (limited to 'searchlib/src/tests/features')
4 files changed, 154 insertions, 0 deletions
diff --git a/searchlib/src/tests/features/constant/.gitignore b/searchlib/src/tests/features/constant/.gitignore new file mode 100644 index 00000000000..ec1ff674d9e --- /dev/null +++ b/searchlib/src/tests/features/constant/.gitignore @@ -0,0 +1 @@ +searchlib_constant_test_app diff --git a/searchlib/src/tests/features/constant/CMakeLists.txt b/searchlib/src/tests/features/constant/CMakeLists.txt new file mode 100644 index 00000000000..9a653234a0a --- /dev/null +++ b/searchlib/src/tests/features/constant/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(searchlib_constant_test_app TEST + SOURCES + constant_test.cpp + DEPENDS + searchlib +) +vespa_add_test(NAME searchlib_constant_test_app COMMAND searchlib_constant_test_app) diff --git a/searchlib/src/tests/features/constant/FILES b/searchlib/src/tests/features/constant/FILES new file mode 100644 index 00000000000..5ff67b0b742 --- /dev/null +++ b/searchlib/src/tests/features/constant/FILES @@ -0,0 +1 @@ +constant_test.cpp diff --git a/searchlib/src/tests/features/constant/constant_test.cpp b/searchlib/src/tests/features/constant/constant_test.cpp new file mode 100644 index 00000000000..aae22213861 --- /dev/null +++ b/searchlib/src/tests/features/constant/constant_test.cpp @@ -0,0 +1,144 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/fastos/fastos.h> +#include <vespa/vespalib/testkit/test_kit.h> +#include <vespa/vespalib/eval/function.h> + +#include <vespa/searchlib/features/setup.h> +#include <vespa/searchlib/fef/fef.h> +#include <vespa/searchlib/fef/test/ftlib.h> +#include <vespa/searchlib/fef/test/indexenvironment.h> +#include <vespa/vespalib/eval/interpreted_function.h> +#include <vespa/vespalib/tensor/default_tensor.h> +#include <vespa/vespalib/tensor/default_tensor_engine.h> +#include <vespa/vespalib/tensor/tensor_factory.h> + +using search::feature_t; +using namespace search::fef; +using namespace search::fef::indexproperties; +using namespace search::fef::test; +using namespace search::features; +using vespalib::eval::Function; +using vespalib::eval::InterpretedFunction; +using vespalib::eval::Value; +using vespalib::eval::DoubleValue; +using vespalib::eval::TensorValue; +using vespalib::eval::ValueType; +using vespalib::tensor::DefaultTensorEngine; +using vespalib::tensor::DenseTensorCells; +using vespalib::tensor::Tensor; +using vespalib::tensor::TensorCells; +using vespalib::tensor::TensorDimensions; +using vespalib::tensor::TensorFactory; + +namespace +{ + +Tensor::UP createTensor(const TensorCells &cells, + const TensorDimensions &dimensions) { + vespalib::tensor::DefaultTensor::builder builder; + return TensorFactory::create(cells, dimensions, builder); +} + +} + +struct ExecFixture +{ + BlueprintFactory factory; + FtFeatureTest test; + ExecFixture(const vespalib::string &feature) + : factory(), + test(factory, feature) + { + setup_search_features(factory); + } + bool setup() { return test.setup(); } + const Tensor &extractTensor() { + const Value::CREF *value = test.resolveObjectFeature(); + ASSERT_TRUE(value != nullptr); + ASSERT_TRUE(value->get().is_tensor()); + return static_cast<const Tensor &>(*value->get().as_tensor()); + } + const Tensor &executeTensor(uint32_t docId = 1) { + test.executeOnly(docId); + return extractTensor(); + } + double extractDouble() { + const Value::CREF *value = test.resolveObjectFeature(); + ASSERT_TRUE(value != nullptr); + ASSERT_TRUE(value->get().is_double()); + return value->get().as_double(); + } + double executeDouble(uint32_t docId = 1) { + test.executeOnly(docId); + return extractDouble(); + } + void addTensor(const vespalib::string &name, + const TensorCells &cells, + const TensorDimensions &dimensions) + { + Tensor::UP tensor = createTensor(cells, dimensions); + ValueType type(tensor->getType().as_value_type()); + test.getIndexEnv().addConstantValue(name, + std::move(type), + std::make_unique<TensorValue>(std::move(tensor))); + } + + void addDouble(const vespalib::string &name, const double value) { + test.getIndexEnv().addConstantValue(name, + ValueType::double_type(), + std::make_unique<DoubleValue>(value)); + } +}; + +struct AsTensor { + InterpretedFunction ifun; + InterpretedFunction::Context ctx; + const Value *result; + const Tensor *tensor; + explicit AsTensor(const vespalib::string &expr) + : ifun(DefaultTensorEngine::ref(), Function::parse(expr)), ctx(), result(&ifun.eval(ctx)) + { + ASSERT_TRUE(result->is_tensor()); + tensor = static_cast<const Tensor *>(result->as_tensor()); + } + bool operator==(const Tensor &rhs) const { + return tensor->equals(rhs); + } +}; + +std::ostream &operator<<(std::ostream &os, const AsTensor &my_tensor) { + os << my_tensor.result->as_tensor(); + return os; +} + + +TEST_F("require that missing constant is detected", + ExecFixture("constant(foo)")) +{ + EXPECT_TRUE(!f.setup()); +} + + +TEST_F("require that existing tensor constant is detected", + ExecFixture("constant(foo)")) +{ + f.addTensor("foo", + { {{{"x", "a"}}, 3}, + {{{"x", "b"}}, 5}, + {{{"x", "c"}}, 7} }, + { "x" }); + EXPECT_TRUE(f.setup()); + EXPECT_EQUAL(AsTensor("{ {x:b}:5, {x:c}:7, {x:a}:3 }"), f.executeTensor()); +} + + +TEST_F("require that existing double constant is detected", + ExecFixture("constant(foo)")) +{ + f.addDouble("foo", 42.0); + EXPECT_TRUE(f.setup()); + EXPECT_EQUAL(42.0, f.executeDouble()); +} + + +TEST_MAIN() { TEST_RUN_ALL(); } |