io.cc 5.3 KB

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