diff options
author | Arne Juul <arnej@verizonmedia.com> | 2020-10-12 11:06:23 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2020-10-13 08:51:58 +0000 |
commit | ef36dfee0259436338751a2d801699b2cdf7fb8c (patch) | |
tree | e8a5eb14661cc82275289a74086ec811ff105ae8 /eval/src/tests/instruction/generic_map | |
parent | fce44db3418daa3ba3b4db03689eed4dead55636 (diff) |
add GenericMap
Diffstat (limited to 'eval/src/tests/instruction/generic_map')
-rw-r--r-- | eval/src/tests/instruction/generic_map/CMakeLists.txt | 9 | ||||
-rw-r--r-- | eval/src/tests/instruction/generic_map/generic_map_test.cpp | 68 |
2 files changed, 77 insertions, 0 deletions
diff --git a/eval/src/tests/instruction/generic_map/CMakeLists.txt b/eval/src/tests/instruction/generic_map/CMakeLists.txt new file mode 100644 index 00000000000..84e69e56541 --- /dev/null +++ b/eval/src/tests/instruction/generic_map/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(eval_generic_map_test_app TEST + SOURCES + generic_map_test.cpp + DEPENDS + vespaeval + GTest::GTest +) +vespa_add_test(NAME eval_generic_map_test_app COMMAND eval_generic_map_test_app) diff --git a/eval/src/tests/instruction/generic_map/generic_map_test.cpp b/eval/src/tests/instruction/generic_map/generic_map_test.cpp new file mode 100644 index 00000000000..1aa5a689f10 --- /dev/null +++ b/eval/src/tests/instruction/generic_map/generic_map_test.cpp @@ -0,0 +1,68 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/eval/eval/simple_value.h> +#include <vespa/eval/eval/value_codec.h> +#include <vespa/eval/instruction/generic_map.h> +#include <vespa/eval/eval/interpreted_function.h> +#include <vespa/eval/eval/test/tensor_model.hpp> +#include <vespa/vespalib/util/stringfmt.h> +#include <vespa/vespalib/gtest/gtest.h> + +using namespace vespalib; +using namespace vespalib::eval; +using namespace vespalib::eval::instruction; +using namespace vespalib::eval::test; + +using vespalib::make_string_short::fmt; + +std::vector<Layout> map_layouts = { + {x(3)}, + {x(3),y(5)}, + {x(3),y(5),z(7)}, + float_cells({x(3),y(5),z(7)}), + {x({"a","b","c"})}, + {x({"a","b","c"}),y({"foo","bar"})}, + {x({"a","b","c"}),y({"foo","bar"}),z({"i","j","k","l"})}, + float_cells({x({"a","b","c"}),y({"foo","bar"}),z({"i","j","k","l"})}), + {x(3),y({"foo", "bar"}),z(7)}, + {x({"a","b","c"}),y(5),z({"i","j","k","l"})}, + float_cells({x({"a","b","c"}),y(5),z({"i","j","k","l"})}) +}; + +TensorSpec reference_map(const TensorSpec &a, map_fun_t func) { + ValueType res_type = ValueType::from_spec(a.type()); + EXPECT_FALSE(res_type.is_error()); + TensorSpec result(res_type.to_spec()); + for (const auto &cell: a.cells()) { + result.add(cell.first, func(cell.second)); + } + return result; +} + +TensorSpec perform_generic_map(const TensorSpec &a, map_fun_t func, const ValueBuilderFactory &factory) +{ + Stash stash; + auto lhs = value_from_spec(a, factory); + auto my_op = GenericMap::make_instruction(lhs->type(), func, stash); + InterpretedFunction::EvalSingle single(my_op); + return spec_from_value(single.eval(std::vector<Value::CREF>({*lhs}))); +} + +void test_generic_map(const ValueBuilderFactory &factory) { + for (const auto & layout : map_layouts) { + TensorSpec lhs = spec(layout, Div16(N())); + ValueType lhs_type = ValueType::from_spec(lhs.type()); + for (auto func : {operation::Floor::f, operation::Fabs::f, operation::Square::f, operation::Inv::f}) { + SCOPED_TRACE(fmt("\n===\nLHS: %s\n===\n", lhs.to_string().c_str())); + auto expect = reference_map(lhs, func); + auto actual = perform_generic_map(lhs, func, factory); + EXPECT_EQ(actual, expect); + } + } +} + +TEST(GenericMapTest, generic_map_works_for_simple_values) { + test_generic_map(SimpleValueBuilderFactory::get()); +} + +GTEST_MAIN_RUN_ALL_TESTS() |