io.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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, { .sep0="|", .sepn="-" } );
  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, { .sep0="|", .sepn="-" });
  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" << format_array(A, ra::cstyle) << 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" << format_array(A, style) << 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" << format_array(A, ra::jstyle) << 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" << format_array(A, ra::lstyle) << 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" << format_array(A, style) << 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" << format_array(A, ra::pstyle) << 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 without FormatArray");
  171. {
  172. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  173. std::ostringstream o;
  174. o << ra::withshape << A;
  175. tr.test_eq(std::string("2 2\n1 2\n3 4"), o.str());
  176. }
  177. tr.section("IO manip without FormatArray");
  178. {
  179. ra::Big<int, 2> A({2, 2}, {1, 2, 3, 4});
  180. std::ostringstream o;
  181. o << ra::noshape << A;
  182. tr.test_eq(std::string("1 2\n3 4"), o.str());
  183. }
  184. tr.section("[ra02a] printing Expr");
  185. {
  186. iocheck(tr.info("output of expr (1)"),
  187. ra::expr([](double i) { return -i; }, start(ra::Small<double, 3>{0, 1, 2})),
  188. ra::Small<double, 3>{0, -1, -2});
  189. iocheck(tr.info("output of expr (2)"),
  190. ra::expr([](double i) { return -i; }, start(ra::Small<double, 3, 2, 3> (ra::_0 - ra::_1 + ra::_2))),
  191. (ra::Small<double, 3, 2, 3> (-(ra::_0 - ra::_1 + ra::_2))));
  192. }
  193. {
  194. ra::Unique<int, 2> a({2, 3}, { 1, 2, 3, 4, 5, 6 });
  195. iocheck(tr.info("output of expr (3)"),
  196. ra::expr([](int i) { return -i; }, a.iter()),
  197. ra::Unique<int, 2>({2, 3}, { -1, -2, -3, -4, -5, -6 }));
  198. }
  199. tr.section("[ra02b] printing array iterators");
  200. {
  201. ra::Unique<int, 2> a({3, 2}, { 1, 2, 3, 4, 5, 6 });
  202. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  203. // note that transpose({1, 0}, ...) will have dynamic rank, so the type expected from read must also.
  204. iocheck(tr.info("output of transposed array through its iterator"),
  205. transpose({1, 0}, a).iter(),
  206. ra::Unique<int>({2, 3}, { 1, 3, 5, 2, 4, 6 }));
  207. }
  208. tr.section("[ra02c] printing array iterators");
  209. {
  210. ra::Small<int, 3, 2> a { 1, 2, 3, 4, 5, 6 };
  211. iocheck(tr.info("output of array through its iterator"), a.iter(), a);
  212. iocheck(tr.info("output of transposed array through its iterator"),
  213. transpose<1, 0>(a).iter(),
  214. ra::Small<int, 2, 3> { 1, 3, 5, 2, 4, 6 });
  215. }
  216. tr.section("IO can handle undef len iota, too");
  217. {
  218. iocheck(tr.info("output of expr (1)"),
  219. ra::expr([](double i, auto j) { return -i*double(j); }, ra::Small<double, 3>{0, 1, 2}.iter(), ra::iota<0>()),
  220. ra::Small<double, 3>{0, -1, -4});
  221. }
  222. tr.section("IO of var rank expression");
  223. {
  224. ra::Small<int, 2, 2> A {1, 2, 3, 4};
  225. ra::Unique<int> B({2, 2}, {1, 2, 3, 4});
  226. iocheck(tr.info("var rank expr"), A+B, ra::Unique<int>({2, 2}, { 2, 4, 6, 8 }));
  227. }
  228. tr.section("IO of nested type");
  229. {
  230. ra::Small<ra::Big<double, 1>, 3> g = { { 1 }, { 1, 2 }, { 1, 2, 3 } };
  231. iocheck(tr.info("nested type"), g, g);
  232. }
  233. // this behavior depends on [ra13] (std::string is scalar) and the specializations of ostream<< in ply.hh.
  234. tr.section("Non-ra types in format()");
  235. {
  236. std::ostringstream o;
  237. o << ra::format(std::string("once"), " ", std::array {1, 2, 3});
  238. tr.info(o.str()).test_eq(std::string("once 1 2 3"), o.str());
  239. }
  240. tr.section("regression against lack of forwarding in ra::format(...)");
  241. {
  242. std::ostringstream o;
  243. o << ra::format(Q { 33 });
  244. tr.test_eq(std::string("33"), o.str());
  245. }
  246. tr.section("empty array");
  247. {
  248. ra::Big<int, 1> pick;
  249. std::ostringstream o;
  250. o << format_array(pick, ra::cstyle);
  251. tr.test_eq(std::string("{}"), o.str());
  252. }
  253. tr.section("rank 0");
  254. {
  255. ra::Big<int, 0> pick = 7;
  256. std::ostringstream o;
  257. o << format_array(pick);
  258. tr.test_eq(std::string("7"), o.str());
  259. }
  260. return tr.summary();
  261. }