aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/tests/instruction
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2021-06-21 13:46:26 +0000
committerHåvard Pettersen <havardpe@oath.com>2021-06-28 17:57:57 +0000
commit74f8c30232f86a5ff9ef70748258e6f8a39238b8 (patch)
treec024704e927b3bce036d328b46d5b74f3e0ee27a /eval/src/tests/instruction
parent9b7582100d7752185dd94a1dceea5b625c26044c (diff)
unpack bits function
Diffstat (limited to 'eval/src/tests/instruction')
-rw-r--r--eval/src/tests/instruction/unpack_bits_function/CMakeLists.txt9
-rw-r--r--eval/src/tests/instruction/unpack_bits_function/unpack_bits_function_test.cpp87
2 files changed, 96 insertions, 0 deletions
diff --git a/eval/src/tests/instruction/unpack_bits_function/CMakeLists.txt b/eval/src/tests/instruction/unpack_bits_function/CMakeLists.txt
new file mode 100644
index 00000000000..9416ce1fa4c
--- /dev/null
+++ b/eval/src/tests/instruction/unpack_bits_function/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_unpack_bits_function_test_app TEST
+ SOURCES
+ unpack_bits_function_test.cpp
+ DEPENDS
+ vespaeval
+ GTest::GTest
+)
+vespa_add_test(NAME eval_unpack_bits_function_test_app COMMAND eval_unpack_bits_function_test_app)
diff --git a/eval/src/tests/instruction/unpack_bits_function/unpack_bits_function_test.cpp b/eval/src/tests/instruction/unpack_bits_function/unpack_bits_function_test.cpp
new file mode 100644
index 00000000000..8250893225a
--- /dev/null
+++ b/eval/src/tests/instruction/unpack_bits_function/unpack_bits_function_test.cpp
@@ -0,0 +1,87 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/eval/eval/fast_value.h>
+#include <vespa/eval/eval/simple_value.h>
+#include <vespa/eval/instruction/unpack_bits_function.h>
+#include <vespa/eval/eval/test/eval_fixture.h>
+#include <vespa/vespalib/gtest/gtest.h>
+
+using namespace vespalib::eval;
+using namespace vespalib::eval::test;
+
+const ValueBuilderFactory &prod_factory = FastValueBuilderFactory::get();
+const ValueBuilderFactory &test_factory = SimpleValueBuilderFactory::get();
+
+//-----------------------------------------------------------------------------
+
+auto my_seq = Seq({-128, -43, 85, 127});
+
+EvalFixture::ParamRepo make_params() {
+ return EvalFixture::ParamRepo()
+ .add("full", GenSpec(-128).idx("x", 256).cells(CellType::INT8))
+ .add("vx8", GenSpec().seq(my_seq).idx("x", 8).cells(CellType::INT8))
+ .add("vy8", GenSpec().seq(my_seq).idx("y", 8).cells(CellType::INT8))
+ .add("vxf", GenSpec().seq(my_seq).idx("x", 8).cells(CellType::FLOAT));
+}
+EvalFixture::ParamRepo param_repo = make_params();
+
+void assert_optimized(const vespalib::string &expr) {
+ EvalFixture fast_fixture(prod_factory, expr, param_repo, true);
+ EvalFixture test_fixture(test_factory, expr, param_repo, true);
+ EvalFixture slow_fixture(prod_factory, expr, param_repo, false);
+ auto expect = EvalFixture::ref(expr, param_repo);
+ EXPECT_EQ(fast_fixture.result(), expect);
+ EXPECT_EQ(test_fixture.result(), expect);
+ EXPECT_EQ(slow_fixture.result(), expect);
+ EXPECT_EQ(fast_fixture.find_all<UnpackBitsFunction>().size(), 1u);
+ EXPECT_EQ(test_fixture.find_all<UnpackBitsFunction>().size(), 1u);
+ EXPECT_EQ(slow_fixture.find_all<UnpackBitsFunction>().size(), 0u);
+}
+
+void assert_not_optimized(const vespalib::string &expr) {
+ EvalFixture fast_fixture(prod_factory, expr, param_repo, true);
+ EXPECT_EQ(fast_fixture.result(), EvalFixture::ref(expr, param_repo));
+ EXPECT_EQ(fast_fixture.find_all<UnpackBitsFunction>().size(), 0u);
+}
+
+//-----------------------------------------------------------------------------
+
+TEST(UnpackBitsTest, expression_can_be_optimized) {
+ assert_optimized("tensor<int8>(x[2048])(bit(full{x:(x/8)},7-x%8))");
+ assert_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},7-x%8))");
+}
+
+TEST(UnpackBitsTest, unpack_bits_can_rename_dimension) {
+ assert_optimized("tensor<int8>(x[64])(bit(vy8{y:(x/8)},7-x%8))");
+}
+
+//-----------------------------------------------------------------------------
+
+TEST(UnpackBitsTest, dimension_sizes_must_be_appropriate) {
+ assert_not_optimized("tensor<int8>(x[60])(bit(vx8{x:(x/8)},7-x%8))");
+ assert_not_optimized("tensor<int8>(x[68])(bit(vx8{x:(x/8)},7-x%8))");
+}
+
+TEST(UnpackBitsTest, source_must_be_int8) {
+ assert_not_optimized("tensor<int8>(x[64])(bit(vxf{x:(x/8)},7-x%8))");
+}
+
+TEST(UnpackBitsTest, result_must_be_int8) {
+ assert_not_optimized("tensor<float>(x[64])(bit(vx8{x:(x/8)},7-x%8))");
+}
+
+TEST(UnpackBitsTest, similar_expressions_are_not_optimized) {
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},7-x%7))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},7-x%9))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/7)},7-x%8))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/9)},7-x%8))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},x%8-7))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(8/x)},7-x%8))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},7+x%8))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x*8)},7-x%8))");
+ assert_not_optimized("tensor<int8>(x[64])(bit(vx8{x:(x/8)},(7-x)%8))");
+}
+
+//-----------------------------------------------------------------------------
+
+GTEST_MAIN_RUN_ALL_TESTS()