io.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - IO, formatting.
  3. // (c) Daniel Llorens - 2013-2024
  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 << fmt({ .shape=ra::withshape, .sep0="|", .sepn="-" }, 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 << fmt({ .shape=ra::noshape, .sep0="|", .sepn="-" }, A);
  62. tr.test_eq(std::string("1|2-3|4"), o.str());
  63. }
  64. tr.section("IO format parameters against default (III)");
  65. {
  66. ra::Big<int, 4> A({2, 2, 2, 2}, ra::_0 + ra::_1 + ra::_2 + ra::_3);
  67. {
  68. std::ostringstream o;
  69. o << "\n" << fmt(ra::cstyle, A) << endl;
  70. tr.test_seq(
  71. R"---(
  72. {{{{0, 1},
  73. {1, 2}},
  74. {{1, 2},
  75. {2, 3}}},
  76. {{{1, 2},
  77. {2, 3}},
  78. {{2, 3},
  79. {3, 4}}}}
  80. )---"
  81. , o.str());
  82. }
  83. {
  84. std::ostringstream o;
  85. auto style = ra::cstyle;
  86. style.shape = ra::withshape;
  87. o << "\n" << fmt(style, A) << endl;
  88. tr.test_seq(
  89. R"---(
  90. 2 2 2 2
  91. {{{{0, 1},
  92. {1, 2}},
  93. {{1, 2},
  94. {2, 3}}},
  95. {{{1, 2},
  96. {2, 3}},
  97. {{2, 3},
  98. {3, 4}}}}
  99. )---"
  100. , o.str());
  101. }
  102. {
  103. std::ostringstream o;
  104. o << "\n" << fmt(ra::jstyle, A) << endl;
  105. tr.test_seq(
  106. R"---(
  107. 2 2 2 2
  108. 0 1
  109. 1 2
  110. 1 2
  111. 2 3
  112. 1 2
  113. 2 3
  114. 2 3
  115. 3 4
  116. )---"
  117. , o.str());
  118. }
  119. {
  120. std::ostringstream o;
  121. o << "\n" << fmt(ra::lstyle, A) << endl;
  122. tr.test_seq(
  123. R"---(
  124. ((((0 1)
  125. (1 2))
  126. ((1 2)
  127. (2 3)))
  128. (((1 2)
  129. (2 3))
  130. ((2 3)
  131. (3 4))))
  132. )---"
  133. , o.str());
  134. }
  135. {
  136. std::ostringstream o;
  137. auto style = ra::lstyle;
  138. style.align = false;
  139. o << "\n" << fmt(style, A) << endl;
  140. tr.test_seq(
  141. R"---(
  142. ((((0 1)
  143. (1 2))
  144. ((1 2)
  145. (2 3)))
  146. (((1 2)
  147. (2 3))
  148. ((2 3)
  149. (3 4))))
  150. )---"
  151. , o.str());
  152. }
  153. {
  154. std::ostringstream o;
  155. o << "\n" << fmt(ra::pstyle, A) << endl;
  156. tr.test_seq(
  157. R"---(
  158. [[[[0, 1],
  159. [1, 2]],
  160. [[1, 2],
  161. [2, 3]]],
  162. [[[1, 2],
  163. [2, 3]],
  164. [[2, 3],
  165. [3, 4]]]]
  166. )---"
  167. , o.str());
  168. }
  169. }
  170. tr.section("IO manip I");
  171. {
  172. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  173. std::ostringstream o;
  174. auto style = ra::jstyle;
  175. style.shape = ra::withshape;
  176. o << fmt(style, A);
  177. tr.test_eq(std::string("2 2\n1 2\n3 4"), o.str());
  178. }
  179. tr.section("IO manip II");
  180. {
  181. ra::Big<int, 2> A({2, 2}, {1, 2, 3, 4});
  182. std::ostringstream o;
  183. o << fmt(ra::nstyle, A);
  184. tr.test_eq(std::string("1 2\n3 4"), o.str());
  185. }
  186. tr.section("[ra02a] printing Map");
  187. {
  188. iocheck(tr.info("output of map (1)"),
  189. ra::map_([](double i) { return -i; }, start(ra::Small<double, 3>{0, 1, 2})),
  190. ra::Small<double, 3>{0, -1, -2});
  191. iocheck(tr.info("output of map (2)"),
  192. ra::map_([](double i) { return -i; }, start(ra::Small<double, 3, 2, 3> (ra::_0 - ra::_1 + ra::_2))),
  193. (ra::Small<double, 3, 2, 3> (-(ra::_0 - ra::_1 + ra::_2))));
  194. }
  195. {
  196. ra::Unique<int, 2> a({2, 3}, { 1, 2, 3, 4, 5, 6 });
  197. iocheck(tr.info("output of map (3)"),
  198. ra::map_([](int i) { return -i; }, a.iter()),
  199. ra::Unique<int, 2>({2, 3}, { -1, -2, -3, -4, -5, -6 }));
  200. }
  201. tr.section("[ra02b] printing array iterators");
  202. {
  203. ra::Unique<int, 2> a({3, 2}, { 1, 2, 3, 4, 5, 6 });
  204. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  205. // note that transpose({1, 0}, ...) will have dynamic rank, so the type expected from read must also.
  206. iocheck(tr.info("output of transposed array through its iterator"),
  207. transpose({1, 0}, a).iter(),
  208. ra::Unique<int>({2, 3}, { 1, 3, 5, 2, 4, 6 }));
  209. }
  210. tr.section("[ra02c] printing array iterators");
  211. {
  212. ra::Small<int, 3, 2> a { 1, 2, 3, 4, 5, 6 };
  213. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  214. iocheck(tr.info("output of transposed array through its iterator"),
  215. transpose<1, 0>(a).iter(),
  216. ra::Small<int, 2, 3> { 1, 3, 5, 2, 4, 6 });
  217. }
  218. tr.section("IO can handle undef len iota, too");
  219. {
  220. iocheck(tr.info("output of map (1)"),
  221. ra::map_([](double i, auto j) { return -i*double(j); }, ra::Small<double, 3>{0, 1, 2}.iter(), ra::iota<0>()),
  222. ra::Small<double, 3>{0, -1, -4});
  223. }
  224. tr.section("IO of var rank map");
  225. {
  226. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  227. ra::Unique<int> B({2, 2}, {1, 2, 3, 4});
  228. iocheck(tr.info("var rank map"), A+B, ra::Unique<int>({2, 2}, { 2, 4, 6, 8 }));
  229. }
  230. tr.section("IO of nested type");
  231. {
  232. ra::Small<ra::Big<double, 1>, 3> g = { { 1 }, { 1, 2 }, { 1, 2, 3 } };
  233. iocheck(tr.info("nested type"), g, g);
  234. }
  235. // this behavior depends on [ra13] (std::string is scalar) and the specializations of ostream<< in ply.hh.
  236. tr.section("Non-ra types in format()");
  237. {
  238. std::ostringstream o;
  239. ra::print(o, std::string("once"), " ", std::array {1, 2, 3});
  240. tr.info(o.str()).test_eq(std::string("once 1 2 3"), o.str());
  241. }
  242. tr.section("regression against lack of forwarding in ra::format(...)");
  243. {
  244. std::ostringstream o;
  245. ra::print(o, Q { 33 });
  246. tr.test_eq(std::string("33"), o.str());
  247. }
  248. tr.section("empty array");
  249. {
  250. ra::Big<int, 1> pick;
  251. std::ostringstream o;
  252. o << fmt(ra::cstyle, pick);
  253. tr.test_eq(std::string("{}"), o.str());
  254. }
  255. tr.section("rank 0");
  256. {
  257. ra::Big<int, 0> pick = 7;
  258. std::ostringstream o;
  259. o << fmt(ra::format_t {}, pick);
  260. tr.test_eq(std::string("7"), o.str());
  261. }
  262. return tr.summary();
  263. }