// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include #include #include #include #include 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::Value; using vespalib::eval::DoubleValue; using vespalib::eval::TensorSpec; using vespalib::eval::ValueType; using vespalib::tensor::DefaultTensorEngine; using vespalib::tensor::Tensor; namespace { Tensor::UP make_tensor(const TensorSpec &spec) { auto tensor = DefaultTensorEngine::ref().from_spec(spec); return Tensor::UP(dynamic_cast(tensor.release())); } } 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(uint32_t docid) { Value::CREF value = test.resolveObjectFeature(docid); ASSERT_TRUE(value.get().is_tensor()); return static_cast(*value.get().as_tensor()); } const Tensor &executeTensor(uint32_t docId = 1) { return extractTensor(docId); } double extractDouble(uint32_t docid) { Value::CREF value = test.resolveObjectFeature(docid); ASSERT_TRUE(value.get().is_double()); return value.get().as_double(); } double executeDouble(uint32_t docId = 1) { return extractDouble(docId); } void addTensor(const vespalib::string &name, const TensorSpec &spec) { Tensor::UP tensor = make_tensor(spec); ValueType type(tensor->type()); test.getIndexEnv().addConstantValue(name, std::move(type), std::move(tensor)); } void addDouble(const vespalib::string &name, const double value) { test.getIndexEnv().addConstantValue(name, ValueType::double_type(), std::make_unique(value)); } }; 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", TensorSpec("tensor(x{})") .add({{"x","a"}}, 3) .add({{"x","b"}}, 5) .add({{"x","c"}}, 7)); EXPECT_TRUE(f.setup()); auto expect = make_tensor(TensorSpec("tensor(x{})") .add({{"x","b"}}, 5) .add({{"x","c"}}, 7) .add({{"x","a"}}, 3)); EXPECT_EQUAL(*expect, 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(); }