diff options
author | Tor Egge <Tor.Egge@yahoo-inc.com> | 2016-10-11 14:18:59 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@yahoo-inc.com> | 2016-10-11 14:18:59 +0000 |
commit | 813faf137c9e307ea7b4deb80d54df672368be24 (patch) | |
tree | aef5c87b3e41c467882853346569a4c879065e85 | |
parent | 37ec16b1c249cca50850856a0c954acad08a55f6 (diff) |
Remove old tensor operation unit tests. The relevant test cases have
been moved to the tensor conformance unit test.
10 files changed, 0 insertions, 1141 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index 61ec9f1ccf9..a310bcd32a9 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -75,12 +75,10 @@ vespa_define_module( src/tests/tensor/sparse_tensor_builder src/tests/tensor/dense_tensor_address_combiner src/tests/tensor/dense_tensor_builder - src/tests/tensor/dense_tensor_operations src/tests/tensor/tensor_address src/tests/tensor/tensor_conformance src/tests/tensor/tensor_function src/tests/tensor/tensor_mapper - src/tests/tensor/tensor_operations src/tests/tensor/tensor_performance src/tests/tensor/tensor_serialization src/tests/tensor/tensor_slime_serialization diff --git a/vespalib/src/testlist.txt b/vespalib/src/testlist.txt index 67982805df7..638a863e16e 100644 --- a/vespalib/src/testlist.txt +++ b/vespalib/src/testlist.txt @@ -64,13 +64,11 @@ tests/stringfmt tests/sync tests/tensor/sparse_tensor_builder tests/tensor/dense_tensor_builder -tests/tensor/dense_tensor_operations tests/tensor/simple_tensor_builder tests/tensor/tensor tests/tensor/tensor_address tests/tensor/tensor_function tests/tensor/tensor_mapper -tests/tensor/tensor_operations tests/tensor/tensor_performance tests/tensor/tensor_serialization tests/tensor/tensor_slime_serialization diff --git a/vespalib/src/tests/tensor/dense_tensor_operations/.gitignore b/vespalib/src/tests/tensor/dense_tensor_operations/.gitignore deleted file mode 100644 index 1180a740209..00000000000 --- a/vespalib/src/tests/tensor/dense_tensor_operations/.gitignore +++ /dev/null @@ -1 +0,0 @@ -vespalib_dense_tensor_operations_test_app diff --git a/vespalib/src/tests/tensor/dense_tensor_operations/CMakeLists.txt b/vespalib/src/tests/tensor/dense_tensor_operations/CMakeLists.txt deleted file mode 100644 index 4805b8dcf22..00000000000 --- a/vespalib/src/tests/tensor/dense_tensor_operations/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(vespalib_dense_tensor_operations_test_app TEST - SOURCES - dense_tensor_operations_test.cpp - DEPENDS - vespalib - vespalib_vespalib_tensor -) -vespa_add_test(NAME vespalib_dense_tensor_operations_test_app COMMAND vespalib_dense_tensor_operations_test_app) diff --git a/vespalib/src/tests/tensor/dense_tensor_operations/FILES b/vespalib/src/tests/tensor/dense_tensor_operations/FILES deleted file mode 100644 index e9ba06f506a..00000000000 --- a/vespalib/src/tests/tensor/dense_tensor_operations/FILES +++ /dev/null @@ -1 +0,0 @@ -dense_tensor_operations_test.cpp diff --git a/vespalib/src/tests/tensor/dense_tensor_operations/dense_tensor_operations_test.cpp b/vespalib/src/tests/tensor/dense_tensor_operations/dense_tensor_operations_test.cpp deleted file mode 100644 index 69642fb1658..00000000000 --- a/vespalib/src/tests/tensor/dense_tensor_operations/dense_tensor_operations_test.cpp +++ /dev/null @@ -1,484 +0,0 @@ -// Copyright 2016 Yahoo Inc. 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/vespalib/tensor/dense/dense_tensor.h> -#include <vespa/vespalib/tensor/dense/dense_tensor_builder.h> -#include <vespa/vespalib/tensor/tensor_function.h> -#include <vespa/vespalib/tensor/tensor_visitor.h> - -using namespace vespalib::tensor; - -using DenseTensorCells = std::map<std::map<vespalib::string, size_t>, double>; - -namespace vespalib { -namespace tensor { - -static bool operator==(const Tensor &lhs, const Tensor &rhs) -{ - return lhs.equals(rhs); -} - -} -} - -//----------------------------------------------------------------------------- - -class MyInput : public TensorFunction::Input -{ -private: - std::vector<Tensor::CREF> tensors; - std::vector<CellFunction::CREF> cell_functions; - const Tensor &get_tensor(size_t id) const override { - ASSERT_GREATER(tensors.size(), id); - return tensors[id]; - } - virtual const CellFunction &get_cell_function(size_t id) const override { - ASSERT_GREATER(cell_functions.size(), id); - return cell_functions[id]; - } -public: - size_t add(const Tensor &tensor) { - size_t id = tensors.size(); - tensors.push_back(tensor); - return id; - } - size_t add(const CellFunction &cell_function) { - size_t id = cell_functions.size(); - cell_functions.push_back(cell_function); - return id; - } -}; - -const Tensor &eval_tensor_checked(function::Node &function_ir, const TensorFunction::Input &input) { - ASSERT_TRUE(function_ir.type().is_tensor()); - TensorFunction &function = function_ir; // compile step - const Tensor &result = function.eval(input).as_tensor; - EXPECT_EQUAL(result.getType(), function_ir.type()); - return result; -} - -const Tensor &eval_tensor_unchecked(function::Node &function_ir, const TensorFunction::Input &input) { - TensorFunction &function = function_ir; // compile step - return function.eval(input).as_tensor; -} - -const Tensor &eval_tensor(function::Node &function_ir, const TensorFunction::Input &input, bool check_types) { - if (check_types) { - return eval_tensor_checked(function_ir, input); - } else { - return eval_tensor_unchecked(function_ir, input); - } -} - -double eval_number(function::Node &function_ir, const TensorFunction::Input &input) { - ASSERT_TRUE(function_ir.type().is_double()); - TensorFunction &function = function_ir; // compile step - return function.eval(input).as_double; -} - -//----------------------------------------------------------------------------- - -template <typename BuilderType> -struct Fixture -{ - BuilderType _builder; - Fixture() : _builder() {} - - Tensor::UP createTensor(const DenseTensorCells &cells) { - std::map<std::string, size_t> dimensionSizes; - for (const auto &cell : cells) { - for (const auto &addressElem : cell.first) { - dimensionSizes[addressElem.first] = std::max(dimensionSizes[addressElem.first], - (addressElem.second + 1)); - } - } - std::map<std::string, typename BuilderType::Dimension> dimensionEnums; - for (const auto &dimensionElem : dimensionSizes) { - dimensionEnums[dimensionElem.first] = - _builder.defineDimension(dimensionElem.first, dimensionElem.second); - } - for (const auto &cell : cells) { - for (const auto &addressElem : cell.first) { - const auto &dimension = addressElem.first; - size_t label = addressElem.second; - _builder.addLabel(dimensionEnums[dimension], label); - } - _builder.addCell(cell.second); - } - return _builder.build(); - } - void assertAddImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::add(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertAdd(const DenseTensorCells &exp, - const DenseTensorCells &lhs, const DenseTensorCells &rhs, bool check_types = true) { - assertAddImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertSubtractImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::subtract(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertSubtract(const DenseTensorCells &exp, - const DenseTensorCells &lhs, - const DenseTensorCells &rhs, bool check_types = true) { - assertSubtractImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMinImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::min(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMin(const DenseTensorCells &exp, const DenseTensorCells &lhs, - const DenseTensorCells &rhs, bool check_types = true) { - assertMinImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMaxImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::max(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMax(const DenseTensorCells &exp, const DenseTensorCells &lhs, - const DenseTensorCells &rhs, bool check_types = true) { - assertMaxImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertSumImpl(double exp, const Tensor &tensor) { - MyInput input; - function::Node_UP ir = function::sum(function::input(tensor.getType(), input.add(tensor))); - EXPECT_EQUAL(exp, eval_number(*ir, input)); - } - void assertSum(double exp, const DenseTensorCells &cells) { - assertSumImpl(exp, *createTensor(cells)); - } - void assertMatchImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::match(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMatch(const DenseTensorCells &exp, const DenseTensorCells &lhs, - const DenseTensorCells &rhs, bool check_types = true) { - assertMatchImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertApplyImpl(const Tensor &exp, const Tensor &tensor, const CellFunction &func) { - MyInput input; - function::Node_UP ir = function::apply(function::input(tensor.getType(), input.add(tensor)), input.add(func)); - EXPECT_EQUAL(exp, eval_tensor_checked(*ir, input)); - } - void assertApply(const DenseTensorCells &exp, const DenseTensorCells &arg, - const CellFunction &func) { - assertApplyImpl(*createTensor(exp), *createTensor(arg), func); - } - void assertDimensionSumImpl(const Tensor &exp, const Tensor &tensor, const vespalib::string &dimension, bool check_types) { - MyInput input; - function::Node_UP ir = function::dimension_sum(function::input(tensor.getType(), input.add(tensor)), dimension); - if (ir->type().is_error()) { - // According to the ir, it is not allowed to sum over a - // non-existing dimension. The current implementation - // allows this, resulting in a tensor with no cells and - // with all dimensions not sliced. - EXPECT_EQUAL(exp, eval_tensor_unchecked(*ir, input)); - } else { - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - } - void assertDimensionSum(const DenseTensorCells &exp, - const DenseTensorCells &arg, - const vespalib::string &dimension, bool check_types = true) { - assertDimensionSumImpl(*createTensor(exp), *createTensor(arg), dimension, check_types); - } - void assertMultiplyImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::multiply(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMultiply(const DenseTensorCells &exp, - const DenseTensorCells &lhs, const DenseTensorCells &rhs, bool check_types = true) { - assertMultiplyImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } -}; - -using DenseFixture = Fixture<DenseTensorBuilder>; - - -template <typename FixtureType> -void -testTensorAdd(FixtureType &f) -{ - TEST_DO(f.assertAdd({},{},{}, false)); - TEST_DO(f.assertAdd({ {{{"x",0}}, 8} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} })); - TEST_DO(f.assertAdd({ {{{"x",0}}, -2} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, -5} })); - TEST_DO(f.assertAdd({ {{{"x",0}}, 10}, {{{"x",1}}, 16} }, - { {{{"x",0}}, 3}, {{{"x",1}}, 5} }, - { {{{"x",0}}, 7}, {{{"x",1}}, 11} })); - TEST_DO(f.assertAdd({ {{{"x",0},{"y",0}}, 8} }, - { {{{"x",0},{"y",0}}, 3} }, - { {{{"x",0},{"y",0}}, 5} })); - TEST_DO(f.assertAdd({ {{{"x",0}}, 3} }, - { {{{"x",0}}, 3} }, - { {{{"x",1}}, 5} })); -} - -template <typename FixtureType> -void -testTensorSubtract(FixtureType &f) -{ - TEST_DO(f.assertSubtract({},{},{}, false)); - TEST_DO(f.assertSubtract({ {{{"x",0}}, -2} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"x",0}}, 8} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, -5} })); - TEST_DO(f.assertSubtract({ {{{"x",0}}, -4}, {{{"x",1}}, -6} }, - { {{{"x",0}}, 3}, {{{"x",1}}, 5} }, - { {{{"x",0}}, 7}, {{{"x",1}}, 11} })); - TEST_DO(f.assertSubtract({ {{{"x",0},{"y",0}}, -2} }, - { {{{"x",0},{"y",0}}, 3} }, - { {{{"x",0},{"y",0}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"x",0}}, -5} }, - { {{{"x",1}}, 3} }, - { {{{"x",0}}, 5} })); -} - -template <typename FixtureType> -void -testTensorMin(FixtureType &f) -{ - TEST_DO(f.assertMin({},{},{}, false)); - TEST_DO(f.assertMin({ {{{"x",0}}, 3} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} })); - TEST_DO(f.assertMin({ {{{"x",0}}, -5} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, -5} })); - TEST_DO(f.assertMin({ {{{"x",0}}, 3}, {{{"x",1}}, 5} }, - { {{{"x",0}}, 3}, {{{"x",1}}, 5} }, - { {{{"x",0}}, 7}, {{{"x",1}}, 11} })); - TEST_DO(f.assertMin({ {{{"x",0},{"y",0}}, 3} }, - { {{{"x",0},{"y",0}}, 3} }, - { {{{"x",0},{"y",0}}, 5} })); - TEST_DO(f.assertMin({ {{{"x",0}}, 0} }, - { {{{"x",1}}, 3} }, - { {{{"x",0}}, 5} })); -} - -template <typename FixtureType> -void -testTensorMax(FixtureType &f) -{ - TEST_DO(f.assertMax({},{},{}, false)); - TEST_DO(f.assertMax({ {{{"x",0}}, 5} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} })); - TEST_DO(f.assertMax({ {{{"x",0}}, 3} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, -5} })); - TEST_DO(f.assertMax({ {{{"x",0}}, 7}, {{{"x",1}}, 11} }, - { {{{"x",0}}, 3}, {{{"x",1}}, 5} }, - { {{{"x",0}}, 7}, {{{"x",1}}, 11} })); - TEST_DO(f.assertMax({ {{{"x",0},{"y",0}}, 5} }, - { {{{"x",0},{"y",0}}, 3} }, - { {{{"x",0},{"y",0}}, 5} })); - TEST_DO(f.assertMax({ {{{"x",0}}, 5} }, - { {{{"x",1}}, 3} }, - { {{{"x",0}}, 5} })); -} - -template <typename FixtureType> -void -testTensorSum(FixtureType &f) -{ - TEST_DO(f.assertSum(0.0, {})); - TEST_DO(f.assertSum(0.0, { {{{"x",0}}, 0} })); - TEST_DO(f.assertSum(3.0, { {{{"x",0}}, 3} })); - TEST_DO(f.assertSum(8.0, { {{{"x",0}}, 3}, {{{"x",1}}, 5} })); - TEST_DO(f.assertSum(-2.0, { {{{"x",0}}, 3}, {{{"x",1}}, -5} })); -} - -template <typename FixtureType> -void -testTensorMatch(FixtureType &f) -{ - f.assertMatch({}, {}, {}, false); - f.assertMatch({ {{{"x",0}}, 15} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} }); - f.assertMatch({ {{{"x",0}}, 0} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 0} }); - f.assertMatch({ {{{"x",0}}, -15} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, -5} }); - f.assertMatch({ {{{"x",0}, {"y",0}}, 39}, - {{{"x",1}, {"y",0}}, 85}, - {{{"x",0}, {"y",1}}, 133}, - {{{"x",1}, {"y",1}}, 253} }, - { {{{"x",0}, {"y",0}}, 3}, - {{{"x",1}, {"y",0}}, 5}, - {{{"x",0}, {"y",1}}, 7}, - {{{"x",1}, {"y",1}}, 11} }, - { {{{"x",0}, {"y",0}}, 13}, - {{{"x",1}, {"y",0}}, 17}, - {{{"x",0}, {"y",1}}, 19}, - {{{"x",1}, {"y",1}}, 23} }); -} - -template <typename FixtureType> -void -testTensorMultiply(FixtureType &f) -{ - f.assertMultiply({}, {}, {}, false); - f.assertMultiply({ {{{"x",0}}, 15} }, - { {{{"x",0}}, 3} }, - { {{{"x",0}}, 5} }); - f.assertMultiply({ {{{"x",0}}, 21}, - {{{"x",1}}, 55} }, - { {{{"x",0}}, 3}, - {{{"x",1}}, 5} }, - { {{{"x",0}}, 7}, - {{{"x",1}}, 11} }); - f.assertMultiply({ {{{"x",0},{"y",0}}, 15} }, - { {{{"x",0}}, 3} }, - { {{{"y",0}}, 5} }); - f.assertMultiply({ {{{"x",0},{"y",0}}, 21}, - {{{"x",0},{"y",1}}, 33}, - {{{"x",1},{"y",0}}, 35}, - {{{"x",1},{"y",1}}, 55} }, - { {{{"x",0}}, 3}, - {{{"x",1}}, 5} }, - { {{{"y",0}}, 7}, - {{{"y",1}}, 11} }); - f.assertMultiply({ {{{"x",0},{"y",0},{"z",0}}, 7}, - {{{"x",0},{"y",0},{"z",1}}, 11}, - {{{"x",0},{"y",1},{"z",0}}, 26}, - {{{"x",0},{"y",1},{"z",1}}, 34}, - {{{"x",1},{"y",0},{"z",0}}, 21}, - {{{"x",1},{"y",0},{"z",1}}, 33}, - {{{"x",1},{"y",1},{"z",0}}, 65}, - {{{"x",1},{"y",1},{"z",1}}, 85} }, - { {{{"x",0},{"y",0}}, 1}, - {{{"x",0},{"y",1}}, 2}, - {{{"x",1},{"y",0}}, 3}, - {{{"x",1},{"y",1}}, 5} }, - { {{{"y",0},{"z",0}}, 7}, - {{{"y",0},{"z",1}}, 11}, - {{{"y",1},{"z",0}}, 13}, - {{{"y",1},{"z",1}}, 17} }); -} - -template <typename FixtureType> -void -testTensorMultiplePreservationOfDimensions(FixtureType &f) -{ - (void) f; -} - -struct MyFunction : public CellFunction -{ - virtual double apply(double value) const override { - return value + 5; - } -}; - -template <typename FixtureType> -void -testTensorApply(FixtureType &f) -{ - f.assertApply({ {{{"x",0}}, 6}, {{{"x",1}}, 2} }, - { {{{"x",0}}, 1}, {{{"x",1}}, -3} }, - MyFunction()); -} - -template <typename FixtureType> -void -testTensorSumDimension(FixtureType &f) -{ - f.assertDimensionSum({ {{{"y",0}}, 4}, {{{"y",1}}, 12} }, - { {{{"x",0},{"y",0}}, 1}, - {{{"x",1},{"y",0}}, 3}, - {{{"x",0},{"y",1}}, 5}, - {{{"x",1},{"y",1}}, 7} }, "x"); - - f.assertDimensionSum({ {{{"x",0}}, 6}, {{{"x",1}}, 10} }, - { {{{"x",0},{"y",0}}, 1}, - {{{"x",1},{"y",0}}, 3}, - {{{"x",0},{"y",1}}, 5}, - {{{"x",1},{"y",1}}, 7} }, "y"); - f.assertDimensionSum({ {{{"y",0}, {"z",0}}, 4}, - {{{"y",1}, {"z",0}}, 12}, - {{{"y",0}, {"z",1}}, 24}, - {{{"y",1}, {"z",1}}, 36} }, - { {{{"x",0},{"y",0}, {"z",0}}, 1}, - {{{"x",1},{"y",0}, {"z",0}}, 3}, - {{{"x",0},{"y",1}, {"z",0}}, 5}, - {{{"x",1},{"y",1}, {"z",0}}, 7}, - {{{"x",0},{"y",0}, {"z",1}}, 11}, - {{{"x",1},{"y",0}, {"z",1}}, 13}, - {{{"x",0},{"y",1}, {"z",1}}, 17}, - {{{"x",1},{"y",1}, {"z",1}}, 19} }, "x"); - f.assertDimensionSum({ {{{"x",0}, {"z",0}}, 6}, - {{{"x",1}, {"z",0}}, 10}, - {{{"x",0}, {"z",1}}, 28}, - {{{"x",1}, {"z",1}}, 32} }, - { {{{"x",0},{"y",0}, {"z",0}}, 1}, - {{{"x",1},{"y",0}, {"z",0}}, 3}, - {{{"x",0},{"y",1}, {"z",0}}, 5}, - {{{"x",1},{"y",1}, {"z",0}}, 7}, - {{{"x",0},{"y",0}, {"z",1}}, 11}, - {{{"x",1},{"y",0}, {"z",1}}, 13}, - {{{"x",0},{"y",1}, {"z",1}}, 17}, - {{{"x",1},{"y",1}, {"z",1}}, 19} }, "y"); - f.assertDimensionSum({ {{{"x",0}, {"y",0}}, 12}, - {{{"x",1}, {"y",0}}, 16}, - {{{"x",0}, {"y",1}}, 22}, - {{{"x",1}, {"y",1}}, 26} }, - { {{{"x",0},{"y",0}, {"z",0}}, 1}, - {{{"x",1},{"y",0}, {"z",0}}, 3}, - {{{"x",0},{"y",1}, {"z",0}}, 5}, - {{{"x",1},{"y",1}, {"z",0}}, 7}, - {{{"x",0},{"y",0}, {"z",1}}, 11}, - {{{"x",1},{"y",0}, {"z",1}}, 13}, - {{{"x",0},{"y",1}, {"z",1}}, 17}, - {{{"x",1},{"y",1}, {"z",1}}, 19} }, "z"); - f.assertDimensionSum({ {{{"x",0}}, 3} }, - { {{{"x",0}}, 3} }, - "y"); - f.assertDimensionSum({ {{}, 3} }, - { {{{"x",0}}, 3} }, - "x", false); -} - -template <typename FixtureType> -void -testAllTensorOperations(FixtureType &f) -{ - TEST_DO(testTensorAdd(f)); - TEST_DO(testTensorSubtract(f)); - TEST_DO(testTensorMin(f)); - TEST_DO(testTensorMax(f)); - TEST_DO(testTensorSum(f)); - TEST_DO(testTensorMatch(f)); - TEST_DO(testTensorMultiply(f)); - TEST_DO(testTensorMultiplePreservationOfDimensions(f)); - TEST_DO(testTensorApply(f)); - TEST_DO(testTensorSumDimension(f)); -} - -TEST_F("test tensor operations for DenseTensor", DenseFixture) -{ - testAllTensorOperations(f); -} - -TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/tests/tensor/tensor_operations/.gitignore b/vespalib/src/tests/tensor/tensor_operations/.gitignore deleted file mode 100644 index 2b54654dfb3..00000000000 --- a/vespalib/src/tests/tensor/tensor_operations/.gitignore +++ /dev/null @@ -1 +0,0 @@ -vespalib_tensor_operations_test_app diff --git a/vespalib/src/tests/tensor/tensor_operations/CMakeLists.txt b/vespalib/src/tests/tensor/tensor_operations/CMakeLists.txt deleted file mode 100644 index 9b18e5384e6..00000000000 --- a/vespalib/src/tests/tensor/tensor_operations/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(vespalib_tensor_operations_test_app TEST - SOURCES - tensor_operations_test.cpp - DEPENDS - vespalib - vespalib_vespalib_tensor -) -vespa_add_test(NAME vespalib_tensor_operations_test_app COMMAND vespalib_tensor_operations_test_app) diff --git a/vespalib/src/tests/tensor/tensor_operations/FILES b/vespalib/src/tests/tensor/tensor_operations/FILES deleted file mode 100644 index b3cc5fa2a92..00000000000 --- a/vespalib/src/tests/tensor/tensor_operations/FILES +++ /dev/null @@ -1 +0,0 @@ -tensor_operations_test.cpp diff --git a/vespalib/src/tests/tensor/tensor_operations/tensor_operations_test.cpp b/vespalib/src/tests/tensor/tensor_operations/tensor_operations_test.cpp deleted file mode 100644 index 5ad26e979c5..00000000000 --- a/vespalib/src/tests/tensor/tensor_operations/tensor_operations_test.cpp +++ /dev/null @@ -1,631 +0,0 @@ -// Copyright 2016 Yahoo Inc. 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/vespalib/tensor/sparse/sparse_tensor.h> -#include <vespa/vespalib/tensor/sparse/sparse_tensor_builder.h> -#include <vespa/vespalib/tensor/types.h> -#include <vespa/vespalib/tensor/tensor_factory.h> -#include <vespa/vespalib/tensor/tensor_function.h> -#include <vespa/vespalib/tensor/tensor_visitor.h> -#include <iostream> - -using namespace vespalib::tensor; - -namespace vespalib { -namespace tensor { - -static bool operator==(const Tensor &lhs, const Tensor &rhs) -{ - return lhs.equals(rhs); -} - -} -} - -//----------------------------------------------------------------------------- - -class MyInput : public TensorFunction::Input -{ -private: - std::vector<Tensor::CREF> tensors; - std::vector<CellFunction::CREF> cell_functions; - const Tensor &get_tensor(size_t id) const override { - ASSERT_GREATER(tensors.size(), id); - return tensors[id]; - } - virtual const CellFunction &get_cell_function(size_t id) const override { - ASSERT_GREATER(cell_functions.size(), id); - return cell_functions[id]; - } -public: - size_t add(const Tensor &tensor) { - size_t id = tensors.size(); - tensors.push_back(tensor); - return id; - } - size_t add(const CellFunction &cell_function) { - size_t id = cell_functions.size(); - cell_functions.push_back(cell_function); - return id; - } -}; - -const Tensor &eval_tensor_checked(function::Node &function_ir, const TensorFunction::Input &input) { - ASSERT_TRUE(function_ir.type().is_tensor()); - TensorFunction &function = function_ir; // compile step - const Tensor &result = function.eval(input).as_tensor; - EXPECT_EQUAL(result.getType(), function_ir.type()); - return result; -} - -const Tensor &eval_tensor_unchecked(function::Node &function_ir, const TensorFunction::Input &input) { - TensorFunction &function = function_ir; // compile step - return function.eval(input).as_tensor; -} - -const Tensor &eval_tensor(function::Node &function_ir, const TensorFunction::Input &input, bool check_types) { - if (check_types) { - return eval_tensor_checked(function_ir, input); - } else { - return eval_tensor_unchecked(function_ir, input); - } -} - -double eval_number(function::Node &function_ir, const TensorFunction::Input &input) { - ASSERT_TRUE(function_ir.type().is_double()); - TensorFunction &function = function_ir; // compile step - return function.eval(input).as_double; -} - -//----------------------------------------------------------------------------- - -template <typename BuilderType> -struct Fixture -{ - BuilderType _builder; - Fixture() : _builder() {} - - Tensor::UP createTensor(const TensorCells &cells) { - return TensorFactory::create(cells, _builder); - } - Tensor::UP createTensor(const TensorCells &cells, const TensorDimensions &dimensions) { - return TensorFactory::create(cells, dimensions, _builder); - } - void assertEquals(const TensorCells &lhs, - const TensorDimensions &lhsDimensions, - const TensorCells &rhs, - const TensorDimensions &rhsDimensions) { - EXPECT_EQUAL(*createTensor(lhs, lhsDimensions), - *createTensor(rhs, rhsDimensions)); - } - void assertEquals(const TensorCells &lhs, const TensorCells &rhs) { - EXPECT_EQUAL(*createTensor(lhs), *createTensor(rhs)); - } - void assertNotEquals(const TensorCells &lhs, const TensorCells &rhs) { - EXPECT_NOT_EQUAL(*createTensor(lhs), *createTensor(rhs)); - } - void assertNotEquals(const TensorCells &lhs, - const TensorDimensions &lhsDimensions, - const TensorCells &rhs, - const TensorDimensions &rhsDimensions) { - EXPECT_NOT_EQUAL(*createTensor(lhs, lhsDimensions), - *createTensor(rhs, rhsDimensions)); - } - void assertAddImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::add(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertAdd(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertAddImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertAdd(const TensorCells &exp, const TensorDimensions &expDimensions, - const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertAddImpl(*createTensor(exp, expDimensions), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertSubtractImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::subtract(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertSubtract(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertSubtractImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertSubtract(const TensorCells &exp, const TensorDimensions &expDimensions, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertSubtractImpl(*createTensor(exp, expDimensions), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMinImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::min(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMin(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertMinImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMin(const TensorCells &exp, const TensorDimensions &expDimensions, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertMinImpl(*createTensor(exp, expDimensions), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMaxImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::max(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMax(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertMaxImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMax(const TensorCells &exp, const TensorDimensions &expDimensions, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertMaxImpl(*createTensor(exp, expDimensions), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertSumImpl(double exp, const Tensor &tensor) { - MyInput input; - function::Node_UP ir = function::sum(function::input(tensor.getType(), input.add(tensor))); - EXPECT_EQUAL(exp, eval_number(*ir, input)); - } - void assertSum(double exp, const TensorCells &cells) { - assertSumImpl(exp, *createTensor(cells)); - } - void assertMatchImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs) { - MyInput input; - function::Node_UP ir = function::match(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - // The match operation currently ends up the union of input - // dimensions. It should be the intersection of input - // dimensions as claimed by the intermediate - // representation. The tensor result type checking is disabled - // until the corresponding bug is fixed. - EXPECT_EQUAL(exp, eval_tensor_unchecked(*ir, input)); // UNCHECKED (ref VESPA-1868) - } - void assertMatch(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs) { - assertMatchImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs)); - } - void assertMatch(const TensorCells &expTensor, const TensorDimensions &expDimensions, - const TensorCells &lhs, const TensorCells &rhs) { - assertMatchImpl(*createTensor(expTensor, expDimensions), *createTensor(lhs), *createTensor(rhs)); - } - void assertMultiplyImpl(const Tensor &exp, const Tensor &lhs, const Tensor &rhs, bool check_types) { - MyInput input; - function::Node_UP ir = function::multiply(function::input(lhs.getType(), input.add(lhs)), - function::input(rhs.getType(), input.add(rhs))); - EXPECT_EQUAL(exp, eval_tensor(*ir, input, check_types)); - } - void assertMultiply(const TensorCells &exp, const TensorCells &lhs, const TensorCells &rhs, bool check_types = true) { - assertMultiplyImpl(*createTensor(exp), *createTensor(lhs), *createTensor(rhs), check_types); - } - void assertMultiply(const TensorCells &expTensor, const TensorDimensions &expDimensions, - const TensorCells &lhs, const TensorCells &rhs) { - assertMultiplyImpl(*createTensor(expTensor, expDimensions), *createTensor(lhs), *createTensor(rhs), true); - } - void assertMultiplyImpl(const Tensor &exp, const Tensor &arg1, const Tensor &arg2, const Tensor &arg3) { - MyInput input; - function::Node_UP ir = function::multiply( - function::multiply(function::input(arg1.getType(), input.add(arg1)), - function::input(arg2.getType(), input.add(arg2))), - function::input(arg3.getType(), input.add(arg3))); - EXPECT_EQUAL(exp, eval_tensor_checked(*ir, input)); - } - void assertMultiply(const TensorCells &expTensor, const TensorDimensions &expDimensions, - const TensorCells &arg1, const TensorCells &arg2, const TensorCells &arg3) { - assertMultiplyImpl(*createTensor(expTensor, expDimensions), *createTensor(arg1), *createTensor(arg2), *createTensor(arg3)); - } - void assertApplyImpl(const Tensor &exp, const Tensor &tensor, const CellFunction &func) { - MyInput input; - function::Node_UP ir = function::apply(function::input(tensor.getType(), input.add(tensor)), input.add(func)); - EXPECT_EQUAL(exp, eval_tensor_checked(*ir, input)); - } - void assertApply(const TensorCells &exp, const TensorCells &arg, const CellFunction &func) { - assertApplyImpl(*createTensor(exp), *createTensor(arg), func); - } - void assertDimensionSumImpl(const Tensor &exp, const Tensor &tensor, const vespalib::string &dimension) { - MyInput input; - function::Node_UP ir = function::dimension_sum(function::input(tensor.getType(), input.add(tensor)), dimension); - EXPECT_EQUAL(exp, eval_tensor_checked(*ir, input)); - } - void assertDimensionSum(const TensorCells &exp, const TensorCells &arg, - const vespalib::string &dimension) { - assertDimensionSumImpl(*createTensor(exp), *createTensor(arg), dimension); - } -}; - -using SparseFixture = Fixture<SparseTensorBuilder>; - - -template <typename FixtureType> -void -testTensorEquals(FixtureType &f) -{ - TEST_DO(f.assertEquals({}, {})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {})); - TEST_DO(f.assertNotEquals({}, { {{{"x","1"}}, 3} })); - TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, { {{{"x","1"}}, 3} })); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"x","1"}}, 4} })); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"x","2"}}, 3} })); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, { {{{"y","1"}}, 3} })); - TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, {"x"}, - { {{{"x","1"}}, 3} }, {"x"})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"}, - { {{{"x","1"}}, 4} }, {"x"})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"}, - { {{{"x","2"}}, 3} }, {"x"})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x"}, - { {{{"x","2"}}, 3} }, {"x"})); - TEST_DO(f.assertEquals({ {{{"x","1"}}, 3} }, {"x", "y"}, - { {{{"x","1"}}, 3} }, {"x", "y"})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x", "y"}, - { {{{"x","1"}}, 3} }, {"x", "z"})); - TEST_DO(f.assertNotEquals({ {{{"x","1"}}, 3} }, {"x", "y"}, - { {{{"y","1"}}, 3} }, {"y", "z"})); -} - -template <typename FixtureType> -void -testTensorAdd(FixtureType &f) -{ - f.assertAdd({},{},{}, false); - TEST_DO(f.assertAdd({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} })); - TEST_DO(f.assertAdd({ {{{"x","1"}}, 8} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} })); - TEST_DO(f.assertAdd({ {{{"x","1"}}, -2} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -5} })); - TEST_DO(f.assertAdd({ {{{"x","1"}}, 0} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -3} })); - TEST_DO(f.assertAdd({ {{{"x","1"},{"z","3"}}, 14}, {{{"y","2"}}, 12} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertAdd({ {{{"x","1"},{"z","3"}}, 14}, {{{"y","2"}}, 12} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertAdd({ {{{"y","2"}}, 12} }, { "y", "z" }, - { {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertAdd({ {{{"y","2"}}, 12} }, { "y", "z" }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"y","2"}}, 5} })); - TEST_DO(f.assertAdd({ {{{"y","2"}}, 12} }, { "x", "y" }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7} })); - TEST_DO(f.assertAdd({ {{{"y","2"}}, 12} }, { "x", "y" }, - { {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertAdd({ {{{"x","1"},{"z","3"}}, 14} }, - { {{{"x","1"}}, 3} }, - { {{{"z","3"}}, 11} })); - TEST_DO(f.assertAdd({ {{{"x","1"},{"z","3"}}, 14} }, - { {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} })); -} - -template <typename FixtureType> -void -testTensorSubtract(FixtureType &f) -{ - f.assertSubtract({},{},{}, false); - TEST_DO(f.assertSubtract({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"x","1"}}, -2} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"x","1"}}, 8} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -5} })); - TEST_DO(f.assertSubtract({ {{{"x","1"}}, 0} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 3} })); - TEST_DO(f.assertSubtract({ {{{"x","1"},{"z","3"}}, -8}, {{{"y","2"}},-2} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertSubtract({ {{{"x","1"},{"z","3"}}, 8}, {{{"y","2"}}, 2} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"y","2"}},-2} }, { "y", "z" }, - { {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertSubtract({ {{{"y","2"}}, 2} }, { "y", "z" }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"y","2"}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"y","2"}},-2} }, { "x", "y" }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7} })); - TEST_DO(f.assertSubtract({ {{{"y","2"}}, 2} }, { "x", "y" }, - { {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertSubtract({ {{{"x","1"},{"z","3"}}, -8} }, - { {{{"x","1"}}, 3} }, - { {{{"z","3"}}, 11} })); - TEST_DO(f.assertSubtract({ {{{"x","1"},{"z","3"}}, 8} }, - { {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} })); -} - -template <typename FixtureType> -void -testTensorMin(FixtureType &f) -{ - f.assertMin({},{},{}, false); - TEST_DO(f.assertMin({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} })); - TEST_DO(f.assertMin({ {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} })); - TEST_DO(f.assertMin({ {{{"x","1"}}, -5} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -5} })); - TEST_DO(f.assertMin({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 0} })); - TEST_DO(f.assertMin({ {{{"x","1"},{"z","3"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMin({ {{{"x","1"},{"z","3"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMin({ {{{"y","2"}}, 5} }, { "y", "z" }, - { {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMin({ {{{"y","2"}}, 5} }, { "y", "z" }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"y","2"}}, 5} })); - TEST_DO(f.assertMin({ {{{"y","2"}}, 5} }, { "x", "y" }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7} })); - TEST_DO(f.assertMin({ {{{"y","2"}}, 5} }, { "x", "y" }, - { {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMin({ {{{"x","1"},{"z","3"}}, 3} }, - { {{{"x","1"}}, 3} }, - { {{{"z","3"}}, 11} })); - TEST_DO(f.assertMin({ {{{"x","1"},{"z","3"}}, 3} }, - { {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} })); -} - -template <typename FixtureType> -void -testTensorMax(FixtureType &f) -{ - f.assertMax({},{},{}, false); - TEST_DO(f.assertMax({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} })); - TEST_DO(f.assertMax({ {{{"x","1"}}, 5} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} })); - TEST_DO(f.assertMax({ {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -5} })); - TEST_DO(f.assertMax({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 0} })); - TEST_DO(f.assertMax({}, { "x" }, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, -5} })); - TEST_DO(f.assertMax({ {{{"x","1"},{"z","3"}}, 11}, {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMax({ {{{"x","1"},{"z","3"}}, 11}, {{{"y","2"}}, 7} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMax({ {{{"y","2"}}, 7} }, { "y", "z" }, - { {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMax({ {{{"y","2"}}, 7} }, { "y", "z" }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"y","2"}}, 5} })); - TEST_DO(f.assertMax({ {{{"y","2"}}, 7} }, { "x", "y" }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7} })); - TEST_DO(f.assertMax({ {{{"y","2"}}, 7} }, { "x", "y" }, - { {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMax({ {{{"x","1"},{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} }, - { {{{"z","3"}}, 11} })); - TEST_DO(f.assertMax({ {{{"x","1"},{"z","3"}}, 11} }, - { {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} })); -} - -template <typename FixtureType> -void -testTensorSum(FixtureType &f) -{ - f.assertSum(0.0, {}); - f.assertSum(0.0, { {{{"x","1"}}, 0} }); - f.assertSum(3.0, { {{{"x","1"}}, 3} }); - f.assertSum(8.0, { {{{"x","1"}}, 3}, {{{"x","2"}}, 5} }); - f.assertSum(-2.0, { {{{"x","1"}}, 3}, {{{"x","2"}}, -5} }); -} - -template <typename FixtureType> -void -testTensorMatch(FixtureType &f) -{ - TEST_DO(f.assertMatch({}, {}, {})); - TEST_DO(f.assertMatch({}, {"x"}, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} })); - TEST_DO(f.assertMatch({ {{{"x","1"}}, 15} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} })); - TEST_DO(f.assertMatch({ {{{"x","1"}}, 0} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 0} })); - TEST_DO(f.assertMatch({ {{{"x","1"}}, -15} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, -5} })); - TEST_DO(f.assertMatch({ {{{"x","1"}}, 15}, - {{{"x","1"}, {"y","1"}}, 7} }, {"x","y","z"}, - { {{{"x","1"}}, 3}, - {{{"x","2"}}, 3}, - {{{"x","1"},{"y","1"}}, 1}, - {{{"x","1"},{"y","2"}}, 6} }, - { {{{"x","1"}}, 5}, - {{{"x","1"},{"y","1"}}, 7}, - {{{"x","1"},{"y","1"},{"z","1"}}, 6} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y", "z"}, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y", "z"}, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"y", "z"}, - { {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"y", "z"}, - { {{{"y","2"}}, 7}, {{{"z","3"}}, 11} }, - { {{{"y","2"}}, 5} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y"}, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} }, - { {{{"y","2"}}, 7} })); - TEST_DO(f.assertMatch({ {{{"y","2"}}, 35} }, {"x", "y"}, - { {{{"y","2"}}, 7} }, - { {{{"x","1"}}, 3}, {{{"y","2"}}, 5} })); - TEST_DO(f.assertMatch({ }, {"x", "z"}, - { {{{"x","1"}}, 3} }, - { {{{"z","3"}}, 11} })); - TEST_DO(f.assertMatch({ }, {"x", "z"}, - { {{{"z","3"}}, 11} }, - { {{{"x","1"}}, 3} })); -} - -template <typename FixtureType> -void -testTensorMultiply(FixtureType &f) -{ - f.assertMultiply({}, {}, {}, false); - f.assertMultiply({}, {"x"}, - { {{{"x","1"}}, 3} }, - { {{{"x","2"}}, 5} }); - f.assertMultiply({ {{{"x","1"}}, 15} }, - { {{{"x","1"}}, 3} }, - { {{{"x","1"}}, 5} }); - f.assertMultiply({ {{{"x","1"},{"y","1"}}, 15} }, - { {{{"x","1"}}, 3} }, - { {{{"y","1"}}, 5} }); - f.assertMultiply({ {{{"x","1"},{"y","1"}}, 15}, {{{"x","2"},{"y","1"}}, 35} }, - { {{{"x","1"}}, 3}, {{{"x","2"}}, 7} }, - { {{{"y","1"}}, 5} }); - f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7}, - {{{"x","1"},{"y","1"},{"z","2"}}, 13}, - {{{"x","2"},{"y","1"},{"z","1"}}, 21}, - {{{"x","2"},{"y","1"},{"z","2"}}, 39}, - {{{"x","1"},{"y","2"},{"z","1"}}, 55} }, - { {{{"x","1"},{"y","1"}}, 1}, - {{{"x","2"},{"y","1"}}, 3}, - {{{"x","1"},{"y","2"}}, 5} }, - { {{{"y","1"},{"z","1"}}, 7}, - {{{"y","2"},{"z","1"}}, 11}, - {{{"y","1"},{"z","2"}}, 13} }); - f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7} }, - { {{{"x","1"}}, 5}, {{{"x","1"},{"y","1"}}, 1} }, - { {{{"y","1"},{"z","1"}}, 7} }); - f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7}, {{{"x","1"},{"z","1"}}, 55} }, - { {{{"x","1"}}, 5}, {{{"x","1"},{"y","1"}}, 1} }, - { {{{"z","1"}}, 11}, {{{"y","1"},{"z","1"}}, 7} }); - f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7} }, - { {{}, 5}, {{{"x","1"},{"y","1"}}, 1} }, - { {{{"y","1"},{"z","1"}}, 7} }); - f.assertMultiply({ {{{"x","1"},{"y","1"},{"z","1"}}, 7}, {{}, 55} }, - { {{}, 5}, {{{"x","1"},{"y","1"}}, 1} }, - { {{}, 11}, {{{"y","1"},{"z","1"}}, 7} }); -} - -template <typename FixtureType> -void -testTensorMultiplePreservationOfDimensions(FixtureType &f) -{ - f.assertMultiply({}, {"x"}, - { {{{"x","1"}}, 1} }, - { {{{"x","2"}}, 1} }); - f.assertMultiply({ {{{"x","1"}}, 1} }, {"x","y"}, - { {{{"x","1"}}, 1} }, - { {{{"x","2"},{"y","1"}}, 1}, {{{"x","1"}}, 1} }); - f.assertMultiply({}, {"x","y"}, - { {{{"x","1"}}, 1} }, - { {{{"x","2"},{"y","1"}}, 1}, {{{"x","1"}}, 1} }, - { {{{"x","1"},{"y","1"}}, 1} }); - f.assertMultiply({ {{{"x","1"},{"y","1"}}, 1} }, {"x","y"}, - { {{{"x","1"}}, 1} }, - { {{{"x","1"},{"y","1"}}, 1} }); -} - -struct MyFunction : public CellFunction -{ - virtual double apply(double value) const override { - return value + 5; - } -}; - -template <typename FixtureType> -void -testTensorApply(FixtureType &f) -{ - f.assertApply({ {{{"x","1"}}, 6}, {{{"y","1"}}, 2} }, - { {{{"x","1"}}, 1}, {{{"y","1"}}, -3} }, - MyFunction()); -} - -template <typename FixtureType> -void -testTensorSumDimension(FixtureType &f) -{ - f.assertDimensionSum({ {{{"y","1"}}, 4}, {{{"y","2"}}, 12} }, - { {{{"x","1"},{"y","1"}}, 1}, - {{{"x","2"},{"y","1"}}, 3}, - {{{"x","1"},{"y","2"}}, 5}, - {{{"x","2"},{"y","2"}}, 7} }, "x"); - f.assertDimensionSum({ {{{"x","1"}}, 6}, {{{"x","2"}}, 10} }, - { {{{"x","1"},{"y","1"}}, 1}, - {{{"x","2"},{"y","1"}}, 3}, - {{{"x","1"},{"y","2"}}, 5}, - {{{"x","2"},{"y","2"}}, 7} }, "y"); - f.assertDimensionSum({ {{}, 13}, {{{"x","1"}}, 17}, {{{"x","2"}}, 10} }, - { {{{"x","1"},{"y","1"}}, 1}, - {{{"x","2"},{"y","1"}}, 3}, - {{{"x","1"},{"y","2"}}, 5}, - {{{"x","2"},{"y","2"}}, 7}, - {{{"x","1"}}, 11}, - {{{"y","2"}}, 13} }, "y"); - f.assertDimensionSum({ {{}, 11}, {{{"y","1"}}, 4}, {{{"y","2"}}, 25}, {{{"z","1"}}, 19} }, - { {{{"x","1"},{"y","1"}}, 1}, - {{{"x","2"},{"y","1"}}, 3}, - {{{"x","1"},{"y","2"}}, 5}, - {{{"x","2"},{"y","2"}}, 7}, - {{{"x","1"}}, 11}, - {{{"y","2"}}, 13}, - {{{"z","1"}}, 19}, }, "x"); -} - -template <typename FixtureType> -void -testAllTensorOperations(FixtureType &f) -{ - TEST_DO(testTensorEquals(f)); - TEST_DO(testTensorAdd(f)); - TEST_DO(testTensorSubtract(f)); - TEST_DO(testTensorMin(f)); - TEST_DO(testTensorMax(f)); - TEST_DO(testTensorSum(f)); - TEST_DO(testTensorMatch(f)); - TEST_DO(testTensorMultiply(f)); - TEST_DO(testTensorMultiplePreservationOfDimensions(f)); - TEST_DO(testTensorApply(f)); - TEST_DO(testTensorSumDimension(f)); -} - -TEST_F("test tensor operations for SparseTensor", SparseFixture) -{ - testAllTensorOperations(f); -} - -TEST_MAIN() { TEST_RUN_ALL(); } |