diff options
author | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-10 16:17:26 -0700 |
---|---|---|
committer | Benoit Steiner <benoit.steiner.goog@gmail.com> | 2014-10-10 16:17:26 -0700 |
commit | 0219f8aed44279858330b1c07402c066f5b75459 (patch) | |
tree | 1447f4ac1f2b6f5aa77db66ea29f5d5ebb273ad9 /unsupported/test/cxx11_tensor_io.cpp | |
parent | 2ed1838aeb6d3c70c35dbd8d545fba1e7e1c68dc (diff) |
Added ability to print a tensor using an iostream.
Diffstat (limited to 'unsupported/test/cxx11_tensor_io.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_io.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/unsupported/test/cxx11_tensor_io.cpp b/unsupported/test/cxx11_tensor_io.cpp new file mode 100644 index 000000000..b73c024f5 --- /dev/null +++ b/unsupported/test/cxx11_tensor_io.cpp @@ -0,0 +1,70 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com> +// +// 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 <sstream> +#include <string> +#include <Eigen/CXX11/Tensor> + + +static void test_output_1d() +{ + Tensor<int, 1> tensor(5); + for (int i = 0; i < 5; ++i) { + tensor(i) = i; + } + + std::stringstream os; + os << tensor; + + std::string expected("0\n1\n2\n3\n4"); + VERIFY_IS_EQUAL(std::string(os.str()), expected); +} + + +static void test_output_2d() +{ + Tensor<int, 2> tensor(5, 3); + for (int i = 0; i < 5; ++i) { + for (int j = 0; j < 3; ++j) { + tensor(i, j) = i*j; + } + } + + std::stringstream os; + os << tensor; + + std::string expected("0 0 0\n0 1 2\n0 2 4\n0 3 6\n0 4 8"); + VERIFY_IS_EQUAL(std::string(os.str()), expected); +} + + +static void test_output_expr() +{ + Tensor<int, 1> tensor1(5); + Tensor<int, 1> tensor2(5); + for (int i = 0; i < 5; ++i) { + tensor1(i) = i; + tensor2(i) = 7; + } + + std::stringstream os; + os << tensor1 + tensor2; + + std::string expected(" 7\n 8\n 9\n10\n11"); + VERIFY_IS_EQUAL(std::string(os.str()), expected); +} + + +void test_cxx11_tensor_io() +{ + CALL_SUBTEST(test_output_1d()); + CALL_SUBTEST(test_output_2d()); + CALL_SUBTEST(test_output_expr()); +} |