1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10 #include "main.h"
11 #include <sstream>
12 #include <string>
13 #include <Eigen/CXX11/Tensor>
14
15
16 template<int DataLayout>
test_output_0d()17 static void test_output_0d()
18 {
19 Tensor<int, 0, DataLayout> tensor;
20 tensor() = 123;
21
22 std::stringstream os;
23 os << tensor;
24
25 std::string expected("123");
26 VERIFY_IS_EQUAL(std::string(os.str()), expected);
27 }
28
29
30 template<int DataLayout>
test_output_1d()31 static void test_output_1d()
32 {
33 Tensor<int, 1, DataLayout> tensor(5);
34 for (int i = 0; i < 5; ++i) {
35 tensor(i) = i;
36 }
37
38 std::stringstream os;
39 os << tensor;
40
41 std::string expected("0\n1\n2\n3\n4");
42 VERIFY_IS_EQUAL(std::string(os.str()), expected);
43
44 Eigen::Tensor<double,1,DataLayout> empty_tensor(0);
45 std::stringstream empty_os;
46 empty_os << empty_tensor;
47 std::string empty_string;
48 VERIFY_IS_EQUAL(std::string(empty_os.str()), empty_string);
49 }
50
51
52 template<int DataLayout>
test_output_2d()53 static void test_output_2d()
54 {
55 Tensor<int, 2, DataLayout> tensor(5, 3);
56 for (int i = 0; i < 5; ++i) {
57 for (int j = 0; j < 3; ++j) {
58 tensor(i, j) = i*j;
59 }
60 }
61
62 std::stringstream os;
63 os << tensor;
64
65 std::string expected("0 0 0\n0 1 2\n0 2 4\n0 3 6\n0 4 8");
66 VERIFY_IS_EQUAL(std::string(os.str()), expected);
67 }
68
69
70 template<int DataLayout>
test_output_expr()71 static void test_output_expr()
72 {
73 Tensor<int, 1, DataLayout> tensor1(5);
74 Tensor<int, 1, DataLayout> tensor2(5);
75 for (int i = 0; i < 5; ++i) {
76 tensor1(i) = i;
77 tensor2(i) = 7;
78 }
79
80 std::stringstream os;
81 os << tensor1 + tensor2;
82
83 std::string expected(" 7\n 8\n 9\n10\n11");
84 VERIFY_IS_EQUAL(std::string(os.str()), expected);
85 }
86
87
88 template<int DataLayout>
test_output_string()89 static void test_output_string()
90 {
91 Tensor<std::string, 2, DataLayout> tensor(5, 3);
92 tensor.setConstant(std::string("foo"));
93
94 std::cout << tensor << std::endl;
95
96 std::stringstream os;
97 os << tensor;
98
99 std::string expected("foo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo");
100 VERIFY_IS_EQUAL(std::string(os.str()), expected);
101 }
102
103
104 template<int DataLayout>
test_output_const()105 static void test_output_const()
106 {
107 Tensor<int, 1, DataLayout> tensor(5);
108 for (int i = 0; i < 5; ++i) {
109 tensor(i) = i;
110 }
111
112 TensorMap<Tensor<const int, 1, DataLayout> > tensor_map(tensor.data(), 5);
113
114 std::stringstream os;
115 os << tensor_map;
116
117 std::string expected("0\n1\n2\n3\n4");
118 VERIFY_IS_EQUAL(std::string(os.str()), expected);
119 }
120
121
test_cxx11_tensor_io()122 void test_cxx11_tensor_io()
123 {
124 CALL_SUBTEST(test_output_0d<ColMajor>());
125 CALL_SUBTEST(test_output_0d<RowMajor>());
126 CALL_SUBTEST(test_output_1d<ColMajor>());
127 CALL_SUBTEST(test_output_1d<RowMajor>());
128 CALL_SUBTEST(test_output_2d<ColMajor>());
129 CALL_SUBTEST(test_output_2d<RowMajor>());
130 CALL_SUBTEST(test_output_expr<ColMajor>());
131 CALL_SUBTEST(test_output_expr<RowMajor>());
132 CALL_SUBTEST(test_output_string<ColMajor>());
133 CALL_SUBTEST(test_output_string<RowMajor>());
134 CALL_SUBTEST(test_output_const<ColMajor>());
135 CALL_SUBTEST(test_output_const<RowMajor>());
136 }
137