summaryrefslogtreecommitdiffstats
path: root/eval/src/tests/instruction/dense_replace_type_function
diff options
context:
space:
mode:
Diffstat (limited to 'eval/src/tests/instruction/dense_replace_type_function')
-rw-r--r--eval/src/tests/instruction/dense_replace_type_function/CMakeLists.txt8
-rw-r--r--eval/src/tests/instruction/dense_replace_type_function/dense_replace_type_function_test.cpp76
2 files changed, 84 insertions, 0 deletions
diff --git a/eval/src/tests/instruction/dense_replace_type_function/CMakeLists.txt b/eval/src/tests/instruction/dense_replace_type_function/CMakeLists.txt
new file mode 100644
index 00000000000..dd4a8a58082
--- /dev/null
+++ b/eval/src/tests/instruction/dense_replace_type_function/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(eval_dense_replace_type_function_test_app TEST
+ SOURCES
+ dense_replace_type_function_test.cpp
+ DEPENDS
+ vespaeval
+)
+vespa_add_test(NAME eval_dense_replace_type_function_test_app COMMAND eval_dense_replace_type_function_test_app)
diff --git a/eval/src/tests/instruction/dense_replace_type_function/dense_replace_type_function_test.cpp b/eval/src/tests/instruction/dense_replace_type_function/dense_replace_type_function_test.cpp
new file mode 100644
index 00000000000..6b8e6faecf4
--- /dev/null
+++ b/eval/src/tests/instruction/dense_replace_type_function/dense_replace_type_function_test.cpp
@@ -0,0 +1,76 @@
+// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/testkit/test_kit.h>
+#include <vespa/eval/eval/fast_value.h>
+#include <vespa/eval/eval/value_codec.h>
+#include <vespa/eval/eval/interpreted_function.h>
+#include <vespa/eval/instruction/dense_replace_type_function.h>
+#include <vespa/eval/eval/test/tensor_model.hpp>
+
+using namespace vespalib::eval::tensor_function;
+using namespace vespalib::eval::test;
+using namespace vespalib::eval;
+using namespace vespalib;
+
+const ValueBuilderFactory &prod_factory = FastValueBuilderFactory::get();
+
+TypedCells getCellsRef(const Value &value) {
+ return value.cells();
+}
+
+struct ChildMock : Leaf {
+ bool is_mutable;
+ ChildMock(const ValueType &type) : Leaf(type), is_mutable(true) {}
+ bool result_is_mutable() const override { return is_mutable; }
+ InterpretedFunction::Instruction compile_self(const ValueBuilderFactory &, Stash &) const override { abort(); }
+};
+
+struct Fixture {
+ Value::UP my_value;
+ ValueType new_type;
+ ChildMock mock_child;
+ DenseReplaceTypeFunction my_fun;
+ std::vector<TensorFunction::Child::CREF> children;
+ InterpretedFunction::State state;
+ Fixture()
+ : my_value(value_from_spec(spec({x(10)}, N()), prod_factory)),
+ new_type(ValueType::from_spec("tensor(x[5],y[2])")),
+ mock_child(my_value->type()),
+ my_fun(new_type, mock_child),
+ children(),
+ state(prod_factory)
+ {
+ my_fun.push_children(children);
+ state.stack.push_back(*my_value);
+ my_fun.compile_self(prod_factory, state.stash).perform(state);
+ ASSERT_EQUAL(children.size(), 1u);
+ ASSERT_EQUAL(state.stack.size(), 1u);
+ ASSERT_TRUE(!new_type.is_error());
+ }
+};
+
+TEST_F("require that DenseReplaceTypeFunction works as expected", Fixture()) {
+ EXPECT_EQUAL(f1.my_fun.result_type(), f1.new_type);
+ EXPECT_EQUAL(f1.my_fun.result_is_mutable(), true);
+ f1.mock_child.is_mutable = false;
+ EXPECT_EQUAL(f1.my_fun.result_is_mutable(), false);
+ EXPECT_EQUAL(&f1.children[0].get().get(), &f1.mock_child);
+ EXPECT_EQUAL(getCellsRef(f1.state.stack[0]).data, getCellsRef(*f1.my_value).data);
+ EXPECT_EQUAL(getCellsRef(f1.state.stack[0]).size, getCellsRef(*f1.my_value).size);
+ EXPECT_EQUAL(f1.state.stack[0].get().type(), f1.new_type);
+ fprintf(stderr, "%s\n", f1.my_fun.as_string().c_str());
+}
+
+TEST("require that create_compact will collapse duplicate replace operations") {
+ Stash stash;
+ ValueType type = ValueType::double_type();
+ ChildMock leaf(type);
+ const DenseReplaceTypeFunction &a = DenseReplaceTypeFunction::create_compact(type, leaf, stash);
+ const DenseReplaceTypeFunction &b = DenseReplaceTypeFunction::create_compact(type, a, stash);
+ EXPECT_EQUAL(a.result_type(), type);
+ EXPECT_EQUAL(&a.child(), &leaf);
+ EXPECT_EQUAL(b.result_type(), type);
+ EXPECT_EQUAL(&b.child(), &leaf);
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }