cxx11_tensor_io.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "main.h"
  10. #include <sstream>
  11. #include <string>
  12. #include <Eigen/CXX11/Tensor>
  13. template<int DataLayout>
  14. static void test_output_0d()
  15. {
  16. Tensor<int, 0, DataLayout> tensor;
  17. tensor() = 123;
  18. std::stringstream os;
  19. os << tensor;
  20. std::string expected("123");
  21. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  22. }
  23. template<int DataLayout>
  24. static void test_output_1d()
  25. {
  26. Tensor<int, 1, DataLayout> tensor(5);
  27. for (int i = 0; i < 5; ++i) {
  28. tensor(i) = i;
  29. }
  30. std::stringstream os;
  31. os << tensor;
  32. std::string expected("0\n1\n2\n3\n4");
  33. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  34. Eigen::Tensor<double,1,DataLayout> empty_tensor(0);
  35. std::stringstream empty_os;
  36. empty_os << empty_tensor;
  37. std::string empty_string;
  38. VERIFY_IS_EQUAL(std::string(empty_os.str()), empty_string);
  39. }
  40. template<int DataLayout>
  41. static void test_output_2d()
  42. {
  43. Tensor<int, 2, DataLayout> tensor(5, 3);
  44. for (int i = 0; i < 5; ++i) {
  45. for (int j = 0; j < 3; ++j) {
  46. tensor(i, j) = i*j;
  47. }
  48. }
  49. std::stringstream os;
  50. os << tensor;
  51. std::string expected("0 0 0\n0 1 2\n0 2 4\n0 3 6\n0 4 8");
  52. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  53. }
  54. template<int DataLayout>
  55. static void test_output_expr()
  56. {
  57. Tensor<int, 1, DataLayout> tensor1(5);
  58. Tensor<int, 1, DataLayout> tensor2(5);
  59. for (int i = 0; i < 5; ++i) {
  60. tensor1(i) = i;
  61. tensor2(i) = 7;
  62. }
  63. std::stringstream os;
  64. os << tensor1 + tensor2;
  65. std::string expected(" 7\n 8\n 9\n10\n11");
  66. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  67. }
  68. template<int DataLayout>
  69. static void test_output_string()
  70. {
  71. Tensor<std::string, 2, DataLayout> tensor(5, 3);
  72. tensor.setConstant(std::string("foo"));
  73. std::cout << tensor << std::endl;
  74. std::stringstream os;
  75. os << tensor;
  76. std::string expected("foo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo\nfoo foo foo");
  77. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  78. }
  79. template<int DataLayout>
  80. static void test_output_const()
  81. {
  82. Tensor<int, 1, DataLayout> tensor(5);
  83. for (int i = 0; i < 5; ++i) {
  84. tensor(i) = i;
  85. }
  86. TensorMap<Tensor<const int, 1, DataLayout> > tensor_map(tensor.data(), 5);
  87. std::stringstream os;
  88. os << tensor_map;
  89. std::string expected("0\n1\n2\n3\n4");
  90. VERIFY_IS_EQUAL(std::string(os.str()), expected);
  91. }
  92. EIGEN_DECLARE_TEST(cxx11_tensor_io)
  93. {
  94. CALL_SUBTEST(test_output_0d<ColMajor>());
  95. CALL_SUBTEST(test_output_0d<RowMajor>());
  96. CALL_SUBTEST(test_output_1d<ColMajor>());
  97. CALL_SUBTEST(test_output_1d<RowMajor>());
  98. CALL_SUBTEST(test_output_2d<ColMajor>());
  99. CALL_SUBTEST(test_output_2d<RowMajor>());
  100. CALL_SUBTEST(test_output_expr<ColMajor>());
  101. CALL_SUBTEST(test_output_expr<RowMajor>());
  102. CALL_SUBTEST(test_output_string<ColMajor>());
  103. CALL_SUBTEST(test_output_string<RowMajor>());
  104. CALL_SUBTEST(test_output_const<ColMajor>());
  105. CALL_SUBTEST(test_output_const<RowMajor>());
  106. }