From 1c236f4c9ae78cc58156eebe3b2bb43588897af4 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Wed, 1 Oct 2014 20:21:42 -0700 Subject: Added tests for tensors of const values and tensors of stringswwq:: --- unsupported/test/cxx11_tensor_of_const_values.cpp | 105 ++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 unsupported/test/cxx11_tensor_of_const_values.cpp (limited to 'unsupported/test/cxx11_tensor_of_const_values.cpp') diff --git a/unsupported/test/cxx11_tensor_of_const_values.cpp b/unsupported/test/cxx11_tensor_of_const_values.cpp new file mode 100644 index 000000000..f179a0c21 --- /dev/null +++ b/unsupported/test/cxx11_tensor_of_const_values.cpp @@ -0,0 +1,105 @@ +// 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 + +using Eigen::Tensor; +using Eigen::RowMajor; + +static void test_assign() +{ + float data1[6]; + TensorMap> mat1(data1, 2, 3); + float data2[6]; + const TensorMap> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + + Tensor rslt1; + rslt1 = mat1; + Tensor rslt2; + rslt2 = mat2; + + Tensor rslt3 = mat1; + Tensor rslt4 = mat2; + + Tensor rslt5(mat1); + Tensor rslt6(mat2); + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(rslt1(i,j), static_cast(i + 2*j)); + VERIFY_IS_APPROX(rslt2(i,j), static_cast(-i - 2*j)); + VERIFY_IS_APPROX(rslt3(i,j), static_cast(i + 2*j)); + VERIFY_IS_APPROX(rslt4(i,j), static_cast(-i - 2*j)); + VERIFY_IS_APPROX(rslt5(i,j), static_cast(i + 2*j)); + VERIFY_IS_APPROX(rslt6(i,j), static_cast(-i - 2*j)); + } + } +} + + +static void test_plus() +{ + float data1[6]; + TensorMap> mat1(data1, 2, 3); + float data2[6]; + TensorMap> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + + Tensor sum1; + sum1 = mat1 + mat2; + Tensor sum2; + sum2 = mat2 + mat1; + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(sum1(i,j), 0.0f); + VERIFY_IS_APPROX(sum2(i,j), 0.0f); + } + } +} + + +static void test_plus_equal() +{ + float data1[6]; + TensorMap> mat1(data1, 2, 3); + float data2[6]; + TensorMap> mat2(data2, 2, 3); + + for (int i = 0; i < 6; ++i) { + data1[i] = i; + data2[i] = -i; + } + mat2 += mat1; + + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 3; ++j) { + VERIFY_IS_APPROX(mat2(i,j), 0.0f); + } + } +} + + +void test_cxx11_tensor_of_const_values() +{ + CALL_SUBTEST(test_assign()); + CALL_SUBTEST(test_plus()); + CALL_SUBTEST(test_plus_equal()); +} -- cgit v1.2.3