// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2014 Benoit Steiner // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" #include "random_without_cast_overflow.h" #include using Eigen::Tensor; using Eigen::array; static void test_simple_cast() { Tensor ftensor(20,30); ftensor = ftensor.random() * 100.f; Tensor chartensor(20,30); chartensor.setRandom(); Tensor, 2> cplextensor(20,30); cplextensor.setRandom(); chartensor = ftensor.cast(); cplextensor = ftensor.cast >(); for (int i = 0; i < 20; ++i) { for (int j = 0; j < 30; ++j) { VERIFY_IS_EQUAL(chartensor(i,j), static_cast(ftensor(i,j))); VERIFY_IS_EQUAL(cplextensor(i,j), static_cast >(ftensor(i,j))); } } } static void test_vectorized_cast() { Tensor itensor(20,30); itensor = itensor.random() / 1000; Tensor ftensor(20,30); ftensor.setRandom(); Tensor dtensor(20,30); dtensor.setRandom(); ftensor = itensor.cast(); dtensor = itensor.cast(); for (int i = 0; i < 20; ++i) { for (int j = 0; j < 30; ++j) { VERIFY_IS_EQUAL(itensor(i,j), static_cast(ftensor(i,j))); VERIFY_IS_EQUAL(dtensor(i,j), static_cast(ftensor(i,j))); } } } static void test_float_to_int_cast() { Tensor ftensor(20,30); ftensor = ftensor.random() * 1000.0f; Tensor dtensor(20,30); dtensor = dtensor.random() * 1000.0; Tensor i1tensor = ftensor.cast(); Tensor i2tensor = dtensor.cast(); for (int i = 0; i < 20; ++i) { for (int j = 0; j < 30; ++j) { VERIFY_IS_EQUAL(i1tensor(i,j), static_cast(ftensor(i,j))); VERIFY_IS_EQUAL(i2tensor(i,j), static_cast(dtensor(i,j))); } } } static void test_big_to_small_type_cast() { Tensor dtensor(20, 30); dtensor.setRandom(); Tensor ftensor(20, 30); ftensor = dtensor.cast(); for (int i = 0; i < 20; ++i) { for (int j = 0; j < 30; ++j) { VERIFY_IS_APPROX(dtensor(i,j), static_cast(ftensor(i,j))); } } } static void test_small_to_big_type_cast() { Tensor ftensor(20, 30); ftensor.setRandom(); Tensor dtensor(20, 30); dtensor = ftensor.cast(); for (int i = 0; i < 20; ++i) { for (int j = 0; j < 30; ++j) { VERIFY_IS_APPROX(dtensor(i,j), static_cast(ftensor(i,j))); } } } template static void test_type_cast() { Tensor ftensor(100, 200); // Generate random values for a valid cast. for (int i = 0; i < 100; ++i) { for (int j = 0; j < 200; ++j) { ftensor(i, j) = internal::random_without_cast_overflow::value(); } } Tensor ttensor(100, 200); ttensor = ftensor.template cast(); for (int i = 0; i < 100; ++i) { for (int j = 0; j < 200; ++j) { const ToType ref = internal::cast(ftensor(i, j)); VERIFY_IS_APPROX(ttensor(i, j), ref); } } } template struct test_cast_runner { static void run() { test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast(); test_type_cast>(); test_type_cast>(); } }; // Only certain types allow cast from std::complex<>. template struct test_cast_runner::IsComplex>::type> { static void run() { test_type_cast(); test_type_cast(); test_type_cast>(); test_type_cast>(); } }; EIGEN_DECLARE_TEST(cxx11_tensor_casts) { CALL_SUBTEST(test_simple_cast()); CALL_SUBTEST(test_vectorized_cast()); CALL_SUBTEST(test_float_to_int_cast()); CALL_SUBTEST(test_big_to_small_type_cast()); CALL_SUBTEST(test_small_to_big_type_cast()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner::run()); CALL_SUBTEST(test_cast_runner>::run()); CALL_SUBTEST(test_cast_runner>::run()); }