test-reshape.C 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // (c) Daniel Llorens - 2017
  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-reshape.C
  7. /// @brief Tests for reshape().
  8. #include "ra/operators.H"
  9. #include "ra/io.H"
  10. #include "ra/test.H"
  11. #include "ra/mpdebug.H"
  12. #include <memory>
  13. using std::cout; using std::endl;
  14. namespace ra {
  15. std::ostream & operator<<(std::ostream & o, ra::Dim const d)
  16. {
  17. o << "{" << d.size << ", " << d.stride << "}";
  18. return o;
  19. }
  20. } // namespace ra
  21. int main()
  22. {
  23. TestRecorder tr(std::cout);
  24. tr.section("reshape");
  25. {
  26. ra::Big<int, 3> aa({2, 3, 3}, ra::_0*3+ra::_1);
  27. auto a = aa(ra::all, ra::all, 0);
  28. tr.info("ravel_free").test_eq(ra::iota(6), ravel_free(a));
  29. tr.test_eq(ra::scalar(a.p), ra::scalar(ravel_free(a).p));
  30. // select.
  31. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, reshape(a, ra::Small<int, 1> {3}));
  32. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 1> {3}).p));
  33. // tile.
  34. tr.info("reshape select").test_eq(ra::Big<int, 3>({2, 2, 3}, {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 3> {2, 2, 3}));
  35. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 3> {2, 2, 3}).p));
  36. // reshape with free ravel
  37. tr.info("reshape w/free ravel I").test_eq(ra::Big<int, 2>({3, 2}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {3, 2}));
  38. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {3, 2}).p));
  39. tr.info("reshape w/free ravel II").test_eq(ra::Big<int, 3>({2, 1, 2}, {0, 1, 2, 3}), reshape(a, ra::Small<int, 3> {2, 1, 2}));
  40. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 3> {2, 1, 2}).p));
  41. tr.info("reshape w/free ravel III").test_eq(ra::Big<int, 2>({3, 2}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {-1, 2}));
  42. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {-1, 2}).p));
  43. tr.info("reshape w/free ravel IV").test_eq(ra::Big<int, 2>({2, 3}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 2> {2, -1}));
  44. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {2, -1}).p));
  45. tr.info("reshape w/free ravel V").test_eq(ra::Big<int, 3>({2, 1, 3}, {0, 1, 2, 3, 4, 5}), reshape(a, ra::Small<int, 3> {2, -1, 3}));
  46. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 3> {2, -1, 3}).p));
  47. }
  48. tr.section("reshape from var rank to fixed rank");
  49. {
  50. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  51. auto b = reshape(a, ra::Small<int, 1> {3});
  52. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  53. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  54. tr.info("reshape can fix rank").test_eq(1, ra::ra_traits<decltype(b)>::rank_s());
  55. }
  56. tr.section("reshape from var rank to var rank");
  57. {
  58. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  59. auto b = reshape(a, ra::Big<int, 1> {3});
  60. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  61. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  62. tr.info("reshape can return var rank").test_eq(ra::RANK_ANY, ra::ra_traits<decltype(b)>::rank_s());
  63. }
  64. tr.section("reshape to fixed rank to var rank");
  65. {
  66. // FIXME warning w/ gcc 6.3 in bootstrap.H inside() [ra32]. Apparent root is in decl of b in reshape().
  67. ra::Big<int, 2> a({2, 3}, ra::_0*3+ra::_1);
  68. auto b = reshape(a, ra::Big<int, 1> {3});
  69. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  70. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  71. tr.info("reshape can return var rank").test_eq(ra::RANK_ANY, ra::ra_traits<decltype(b)>::rank_s());
  72. }
  73. tr.section("conversion from var rank to fixed rank");
  74. {
  75. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  76. ra::View<int, 2> b = a;
  77. tr.info("fixing rank").test_eq(ra::_0*3+ra::_1, b);
  78. tr.info("fixing rank is view").test(a.data()==b.data());
  79. }
  80. return tr.summary();
  81. }