test-io.C 4.3 KB

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