io.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file io.cc
  3. /// @brief IO checks for ra::.
  4. // (c) Daniel Llorens - 2013-2014
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iterator>
  11. #include "ra/ra.hh"
  12. #include "ra/complex.hh"
  13. #include "ra/test.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. template <int i> using TI = ra::TensorIndex<i>;
  16. using int3 = ra::Small<int, 3>;
  17. using int2 = ra::Small<int, 2>;
  18. template <class AA, class CC>
  19. void iocheck(TestRecorder & tr, AA && a, CC && check)
  20. {
  21. std::ostringstream o;
  22. o << a;
  23. cout << "\nwritten: " << o.str() << endl;
  24. std::istringstream i(o.str());
  25. std::decay_t<CC> c;
  26. i >> c;
  27. cout << "\nread: " << c << endl;
  28. // TODO start() outside b/c where(bool, vector, vector) not handled. ra::where(bool, scalar, scalar) should work at least.
  29. // TODO specific check in TestRecorder
  30. tr.test_eq(ra::start(shape(check)), ra::start(shape(c)));
  31. tr.test_eq(check, c);
  32. }
  33. int main()
  34. {
  35. TestRecorder tr;
  36. tr.section("common arrays or slices");
  37. {
  38. ra::Unique<int, 2> a({5, 3}, ra::_0 - ra::_1);
  39. ra::Unique<int, 2> ref({5, 3}, a); // TODO how about an explicit copy() function?
  40. iocheck(tr.info("output of Unique (1)"), a, ref);
  41. iocheck(tr.info("output of Unique (1)"), a, ref);
  42. }
  43. tr.section("regression");
  44. {
  45. ra::Big<int, 1> a = {1};
  46. ra::Big<int, 1> ref = {1};
  47. iocheck(tr.info("vector(Big)"), ra::vector(a), ref);
  48. }
  49. tr.section("IO format parameters against default (I)");
  50. {
  51. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  52. std::ostringstream o;
  53. o << ra::withshape << format_array(A, "|", "-");
  54. tr.test_eq(std::string("2 2\n1|2-3|4"), o.str());
  55. }
  56. tr.section("IO format parameters against default (II)");
  57. {
  58. ra::Big<int, 2> A({2, 2}, {1, 2, 3, 4});
  59. std::ostringstream o;
  60. o << ra::noshape << format_array(A, "|", "-");
  61. tr.test_eq(std::string("1|2-3|4"), o.str());
  62. }
  63. tr.section("IO manip without FormatArray");
  64. {
  65. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  66. std::ostringstream o;
  67. o << ra::withshape << A;
  68. tr.test_eq(std::string("2 2\n1 2\n3 4"), o.str());
  69. }
  70. tr.section("IO manip without FormatArray");
  71. {
  72. ra::Big<int, 2> A({2, 2}, {1, 2, 3, 4});
  73. std::ostringstream o;
  74. o << ra::noshape << A;
  75. tr.test_eq(std::string("1 2\n3 4"), o.str());
  76. }
  77. tr.section("[ra02a] printing Expr");
  78. {
  79. iocheck(tr.info("output of expr (1)"),
  80. ra::expr([](double i) { return -i; }, start(ra::Small<double, 3>{0, 1, 2})),
  81. ra::Small<double, 3>{0, -1, -2});
  82. iocheck(tr.info("output of expr (2)"),
  83. ra::expr([](double i) { return -i; }, start(ra::Small<double, 3, 2, 3> (ra::_0 - ra::_1 + ra::_2))),
  84. (ra::Small<double, 3, 2, 3> (-(ra::_0 - ra::_1 + ra::_2))));
  85. }
  86. {
  87. ra::Unique<int, 2> a({2, 3}, { 1, 2, 3, 4, 5, 6 });
  88. iocheck(tr.info("output of expr (3)"),
  89. ra::expr([](int i) { return -i; }, a.iter()),
  90. ra::Unique<int, 2>({2, 3}, { -1, -2, -3, -4, -5, -6 }));
  91. }
  92. tr.section("[ra02b] printing array iterators");
  93. {
  94. ra::Unique<int, 2> a({3, 2}, { 1, 2, 3, 4, 5, 6 });
  95. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  96. // note that transpose({1, 0}, ...) will have dynamic rank, so the type expected from read must also.
  97. iocheck(tr.info("output of transposed array through its iterator"),
  98. transpose({1, 0}, a).iter(),
  99. ra::Unique<int>({2, 3}, { 1, 3, 5, 2, 4, 6 }));
  100. }
  101. tr.section("[ra02c] printing array iterators");
  102. {
  103. ra::Small<int, 3, 2> a { 1, 2, 3, 4, 5, 6 };
  104. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  105. iocheck(tr.info("output of transposed array through its iterator"),
  106. transpose<1, 0>(a).iter(),
  107. ra::Small<int, 2, 3> { 1, 3, 5, 2, 4, 6 });
  108. }
  109. tr.section("IO can handle tensorindex, too");
  110. {
  111. iocheck(tr.info("output of expr (1)"),
  112. ra::expr([](double i, auto j) { return -i*double(j); }, ra::Small<double, 3>{0, 1, 2}.iter(), TI<0>()),
  113. ra::Small<double, 3>{0, -1, -4});
  114. }
  115. tr.section("IO of var rank expression");
  116. {
  117. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  118. ra::Unique<int> B({2, 2}, {1, 2, 3, 4});
  119. iocheck(tr.info("var rank expr"), A+B, ra::Unique<int>({2, 2}, { 2, 4, 6, 8 }));
  120. }
  121. tr.section("IO of nested type");
  122. {
  123. ra::Small<ra::Big<double, 1>, 3> g = { { 1 }, { 1, 2 }, { 1, 2, 3 } };
  124. iocheck(tr.info("nested type"), g, g);
  125. }
  126. return tr.summary();
  127. }