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 using Eigen::array;
     16 
     17 static void test_simple_cast()
     18 {
     19   Tensor<float, 2> ftensor(20,30);
     20   ftensor = ftensor.random() * 100.f;
     21   Tensor<char, 2> chartensor(20,30);
     22   chartensor.setRandom();
     23   Tensor<std::complex<float>, 2> cplextensor(20,30);
     24   cplextensor.setRandom();
     25 
     26   chartensor = ftensor.cast<char>();
     27   cplextensor = ftensor.cast<std::complex<float> >();
     28 
     29   for (int i = 0; i < 20; ++i) {
     30     for (int j = 0; j < 30; ++j) {
     31       VERIFY_IS_EQUAL(chartensor(i,j), static_cast<char>(ftensor(i,j)));
     32       VERIFY_IS_EQUAL(cplextensor(i,j), static_cast<std::complex<float> >(ftensor(i,j)));
     33     }
     34   }
     35 }
     36 
     37 
     38 static void test_vectorized_cast()
     39 {
     40   Tensor<int, 2> itensor(20,30);
     41   itensor = itensor.random() / 1000;
     42   Tensor<float, 2> ftensor(20,30);
     43   ftensor.setRandom();
     44   Tensor<double, 2> dtensor(20,30);
     45   dtensor.setRandom();
     46 
     47   ftensor = itensor.cast<float>();
     48   dtensor = itensor.cast<double>();
     49 
     50   for (int i = 0; i < 20; ++i) {
     51     for (int j = 0; j < 30; ++j) {
     52       VERIFY_IS_EQUAL(itensor(i,j), static_cast<int>(ftensor(i,j)));
     53       VERIFY_IS_EQUAL(dtensor(i,j), static_cast<double>(ftensor(i,j)));
     54     }
     55   }
     56 }
     57 
     58 
     59 static void test_float_to_int_cast()
     60 {
     61   Tensor<float, 2> ftensor(20,30);
     62   ftensor = ftensor.random() * 1000.0f;
     63   Tensor<double, 2> dtensor(20,30);
     64   dtensor = dtensor.random() * 1000.0;
     65 
     66   Tensor<int, 2> i1tensor = ftensor.cast<int>();
     67   Tensor<int, 2> i2tensor = dtensor.cast<int>();
     68 
     69   for (int i = 0; i < 20; ++i) {
     70     for (int j = 0; j < 30; ++j) {
     71       VERIFY_IS_EQUAL(i1tensor(i,j), static_cast<int>(ftensor(i,j)));
     72       VERIFY_IS_EQUAL(i2tensor(i,j), static_cast<int>(dtensor(i,j)));
     73     }
     74   }
     75 }
     76 
     77 
     78 static void test_big_to_small_type_cast()
     79 {
     80   Tensor<double, 2> dtensor(20, 30);
     81   dtensor.setRandom();
     82   Tensor<float, 2> ftensor(20, 30);
     83   ftensor = dtensor.cast<float>();
     84 
     85   for (int i = 0; i < 20; ++i) {
     86     for (int j = 0; j < 30; ++j) {
     87       VERIFY_IS_APPROX(dtensor(i,j), static_cast<double>(ftensor(i,j)));
     88     }
     89   }
     90 }
     91 
     92 
     93 static void test_small_to_big_type_cast()
     94 {
     95   Tensor<float, 2> ftensor(20, 30);
     96   ftensor.setRandom();
     97   Tensor<double, 2> dtensor(20, 30);
     98   dtensor = ftensor.cast<double>();
     99 
    100   for (int i = 0; i < 20; ++i) {
    101     for (int j = 0; j < 30; ++j) {
    102       VERIFY_IS_APPROX(dtensor(i,j), static_cast<double>(ftensor(i,j)));
    103     }
    104   }
    105 }
    106 
    107 
    108 void test_cxx11_tensor_casts()
    109 {
    110    CALL_SUBTEST(test_simple_cast());
    111    CALL_SUBTEST(test_vectorized_cast());
    112    CALL_SUBTEST(test_float_to_int_cast());
    113    CALL_SUBTEST(test_big_to_small_type_cast());
    114    CALL_SUBTEST(test_small_to_big_type_cast());
    115 }
    116