diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-10-15 14:58:49 -0700 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2015-10-15 14:58:49 -0700 |
commit | de1e9f29f4db2c837ffb354c90f9e9fb7df05e85 (patch) | |
tree | 99832d8d52f1b46063a82c1c9133e9b598df2d1b /unsupported/test/cxx11_tensor_custom_index.cpp | |
parent | 6585efc55354b38c65de8c23599e99f3caaca843 (diff) |
Updated the custom indexing code: we can now use any container that provides the [] operator to index a tensor. Added unit tests to validate the use of std::map and a few more types as valid custom index containers
Diffstat (limited to 'unsupported/test/cxx11_tensor_custom_index.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_custom_index.cpp | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/unsupported/test/cxx11_tensor_custom_index.cpp b/unsupported/test/cxx11_tensor_custom_index.cpp index ff9545a7a..4528cc176 100644 --- a/unsupported/test/cxx11_tensor_custom_index.cpp +++ b/unsupported/test/cxx11_tensor_custom_index.cpp @@ -9,6 +9,7 @@ #include "main.h" #include <limits> +#include <map> #include <Eigen/Dense> #include <Eigen/CXX11/Tensor> @@ -17,22 +18,83 @@ using Eigen::Tensor; template <int DataLayout> -static void test_custom_index() { +static void test_map_as_index() +{ +#ifdef EIGEN_HAS_SFINAE + Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); + tensor.setRandom(); + + using NormalIndex = DSizes<ptrdiff_t, 4>; + using CustomIndex = std::map<ptrdiff_t, ptrdiff_t>; + CustomIndex coeffC; + coeffC[0] = 1; + coeffC[1] = 2; + coeffC[2] = 4; + coeffC[3] = 1; + NormalIndex coeff(1,2,4,1); + VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); + VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); +#endif +} + + +template <int DataLayout> +static void test_matrix_as_index() +{ +#ifdef EIGEN_HAS_SFINAE Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); tensor.setRandom(); using NormalIndex = DSizes<ptrdiff_t, 4>; - using CustomIndex = Matrix<unsigned int , 4, 1>; + using CustomIndex = Matrix<unsigned int, 4, 1>; CustomIndex coeffC(1,2,4,1); NormalIndex coeff(1,2,4,1); VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); +#endif +} + + +template <int DataLayout> +static void test_varlist_as_index() +{ +#ifdef EIGEN_HAS_SFINAE + Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); + tensor.setRandom(); + + DSizes<ptrdiff_t, 4> coeff(1,2,4,1); + + VERIFY_IS_EQUAL(tensor.coeff({1,2,4,1}), tensor.coeff(coeff)); + VERIFY_IS_EQUAL(tensor.coeffRef({1,2,4,1}), tensor.coeffRef(coeff)); +#endif +} + + +template <int DataLayout> +static void test_sizes_as_index() +{ +#ifdef EIGEN_HAS_SFINAE + Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); + tensor.setRandom(); + + DSizes<ptrdiff_t, 4> coeff(1,2,4,1); + Sizes<1,2,4,1> coeffC; + + VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); + VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); +#endif } void test_cxx11_tensor_custom_index() { - test_custom_index<ColMajor>(); - test_custom_index<RowMajor>(); + test_map_as_index<ColMajor>(); + test_map_as_index<RowMajor>(); + test_matrix_as_index<ColMajor>(); + test_matrix_as_index<RowMajor>(); + test_varlist_as_index<ColMajor>(); + test_varlist_as_index<RowMajor>(); + test_sizes_as_index<ColMajor>(); + test_sizes_as_index<RowMajor>(); } |