From 573b377110d488886bfc6b319c140a3375d5d91a Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Fri, 27 Feb 2015 08:46:04 -0800 Subject: Added support for vectorized type casting of tensors --- unsupported/test/cxx11_tensor_casts.cpp | 56 ++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'unsupported/test/cxx11_tensor_casts.cpp') diff --git a/unsupported/test/cxx11_tensor_casts.cpp b/unsupported/test/cxx11_tensor_casts.cpp index 4f7ff7067..f53679d7b 100644 --- a/unsupported/test/cxx11_tensor_casts.cpp +++ b/unsupported/test/cxx11_tensor_casts.cpp @@ -17,7 +17,7 @@ using Eigen::array; static void test_simple_cast() { Tensor ftensor(20,30); - ftensor.setRandom(); + ftensor = ftensor.random() * 100.f; Tensor chartensor(20,30); chartensor.setRandom(); Tensor, 2> cplextensor(20,30); @@ -35,7 +35,61 @@ static void test_simple_cast() } +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_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))); + } + } +} + + void test_cxx11_tensor_casts() { CALL_SUBTEST(test_simple_cast()); + CALL_SUBTEST(test_vectorized_cast()); + CALL_SUBTEST(test_big_to_small_type_cast()); + CALL_SUBTEST(test_small_to_big_type_cast()); } -- cgit v1.2.3