Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #include "main.h"
     11 
     12 #include <Eigen/CXX11/Tensor>
     13 
     14 using Eigen::Tensor;
     15 
     16 template<int DataLayout>
     17 static void test_simple_padding()
     18 {
     19   Tensor<float, 4, DataLayout> tensor(2,3,5,7);
     20   tensor.setRandom();
     21 
     22   array<std::pair<ptrdiff_t, ptrdiff_t>, 4> paddings;
     23   paddings[0] = std::make_pair(0, 0);
     24   paddings[1] = std::make_pair(2, 1);
     25   paddings[2] = std::make_pair(3, 4);
     26   paddings[3] = std::make_pair(0, 0);
     27 
     28   Tensor<float, 4, DataLayout> padded;
     29   padded = tensor.pad(paddings);
     30 
     31   VERIFY_IS_EQUAL(padded.dimension(0), 2+0);
     32   VERIFY_IS_EQUAL(padded.dimension(1), 3+3);
     33   VERIFY_IS_EQUAL(padded.dimension(2), 5+7);
     34   VERIFY_IS_EQUAL(padded.dimension(3), 7+0);
     35 
     36   for (int i = 0; i < 2; ++i) {
     37     for (int j = 0; j < 6; ++j) {
     38       for (int k = 0; k < 12; ++k) {
     39         for (int l = 0; l < 7; ++l) {
     40           if (j >= 2 && j < 5 && k >= 3 && k < 8) {
     41             VERIFY_IS_EQUAL(padded(i,j,k,l), tensor(i,j-2,k-3,l));
     42           } else {
     43             VERIFY_IS_EQUAL(padded(i,j,k,l), 0.0f);
     44           }
     45         }
     46       }
     47     }
     48   }
     49 }
     50 
     51 template<int DataLayout>
     52 static void test_padded_expr()
     53 {
     54   Tensor<float, 4, DataLayout> tensor(2,3,5,7);
     55   tensor.setRandom();
     56 
     57   array<std::pair<ptrdiff_t, ptrdiff_t>, 4> paddings;
     58   paddings[0] = std::make_pair(0, 0);
     59   paddings[1] = std::make_pair(2, 1);
     60   paddings[2] = std::make_pair(3, 4);
     61   paddings[3] = std::make_pair(0, 0);
     62 
     63   Eigen::DSizes<ptrdiff_t, 2> reshape_dims;
     64   reshape_dims[0] = 12;
     65   reshape_dims[1] = 84;
     66 
     67   Tensor<float, 2, DataLayout> result;
     68   result = tensor.pad(paddings).reshape(reshape_dims);
     69 
     70   for (int i = 0; i < 2; ++i) {
     71     for (int j = 0; j < 6; ++j) {
     72       for (int k = 0; k < 12; ++k) {
     73         for (int l = 0; l < 7; ++l) {
     74           const float result_value = DataLayout == ColMajor ?
     75               result(i+2*j,k+12*l) : result(j+6*i,l+7*k);
     76           if (j >= 2 && j < 5 && k >= 3 && k < 8) {
     77             VERIFY_IS_EQUAL(result_value, tensor(i,j-2,k-3,l));
     78           } else {
     79             VERIFY_IS_EQUAL(result_value, 0.0f);
     80           }
     81         }
     82       }
     83     }
     84   }
     85 }
     86 
     87 void test_cxx11_tensor_padding()
     88 {
     89   CALL_SUBTEST(test_simple_padding<ColMajor>());
     90   CALL_SUBTEST(test_simple_padding<RowMajor>());
     91   CALL_SUBTEST(test_padded_expr<ColMajor>());
     92   CALL_SUBTEST(test_padded_expr<RowMajor>());
     93 }
     94