// 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 #include template struct test_tensor_ostream_impl {}; template struct test_tensor_ostream_impl { static void run() { Eigen::Tensor t; t.setValues(1); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == "1"); } }; template struct test_tensor_ostream_impl { static void run() { Eigen::Tensor t = {3}; t.setValues({1, 2, 3}); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == "1 2 3"); } }; template struct test_tensor_ostream_impl { static void run() { Eigen::Tensor t = {3, 2}; t.setValues({{1, 2}, {3, 4}, {5, 6}}); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == "1 2\n3 4\n5 6"); } }; template struct test_tensor_ostream_impl { static void run() { Eigen::Tensor t = {4, 3, 2}; t.setValues({{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == " 1 2\n 3 4\n 5 6\n\n 7 8\n 9 10\n11 12\n\n13 14\n15 16\n17 18\n\n19 20\n21 22\n23 24"); } }; template struct test_tensor_ostream_impl { static void run() { Eigen::Tensor t = {3, 2}; t.setValues({{false, true}, {true, false}, {false, false}}); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == "0 1\n1 0\n0 0"); } }; template struct test_tensor_ostream_impl, 2, Layout> { static void run() { Eigen::Tensor, 2> t = {3, 2}; t.setValues({{std::complex(1, 2), std::complex(12, 3)}, {std::complex(-4, 2), std::complex(0, 5)}, {std::complex(-1, 4), std::complex(5, 27)}}); std::ostringstream os; os << t.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == " (1,2) (12,3)\n(-4,2) (0,5)\n(-1,4) (5,27)"); } }; template void test_tensor_ostream() { test_tensor_ostream_impl::run(); } void test_const_tensor_ostream() { Eigen::Tensor t; t.setValues(1); const Eigen::TensorMap, Eigen::Unaligned> t_const( t.data(), Eigen::DSizes{}); std::ostringstream os; os << t_const.format(Eigen::TensorIOFormat::Plain()); VERIFY(os.str() == "1"); } EIGEN_DECLARE_TEST(cxx11_tensor_io) { CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream())); CALL_SUBTEST((test_tensor_ostream, 2, Eigen::ColMajor>())); CALL_SUBTEST((test_tensor_ostream, 2, Eigen::ColMajor>())); // Test printing TensorMap with const elements. CALL_SUBTEST((test_const_tensor_ostream())); }