reshape.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file reshape.cc
  3. /// @brief Tests for reshape().
  4. // (c) Daniel Llorens - 2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include "ra/ra.hh"
  10. #include "ra/test.hh"
  11. #include "ra/mpdebug.hh"
  12. #include <memory>
  13. using std::cout, std::endl, ra::TestRecorder;
  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. auto tilea = reshape(a, ra::Small<int, 3> {2, 2, 3});
  35. 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}), tilea);
  36. tr.info("some tile-reshapes are free (I)").test_eq(0, tilea.stride(0));
  37. tr.info("some tile-reshapes are free (II)").test_eq(ra::scalar(a.data()), ra::scalar(tilea.data()));
  38. // reshape with free ravel
  39. 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}));
  40. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {3, 2}).p));
  41. 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}));
  42. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 3> {2, 1, 2}).p));
  43. 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}));
  44. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {-1, 2}).p));
  45. 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}));
  46. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 2> {2, -1}).p));
  47. 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}));
  48. tr.test_eq(ra::scalar(a.p), ra::scalar(reshape(a, ra::Small<int, 3> {2, -1, 3}).p));
  49. }
  50. tr.section("reshape from var rank to fixed rank");
  51. {
  52. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  53. auto b = reshape(a, ra::Small<int, 1> {3});
  54. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  55. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  56. tr.info("reshape can fix rank").test_eq(1, rank_s(b));
  57. }
  58. tr.section("reshape from var rank to fixed rank using the initializer_list shim");
  59. {
  60. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  61. auto b = reshape(a, {3, 2});
  62. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, b);
  63. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  64. tr.info("reshape can return fixed rank (2)").test_eq(2, rank_s(b));
  65. auto c = reshape(a, {3l, 2l}); // check deduction works regardless
  66. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, c);
  67. tr.test_eq(ra::scalar(a.p), ra::scalar(c.p));
  68. tr.info("reshape can return fixed rank (3)").test_eq(2, rank_s(c));
  69. }
  70. tr.section("reshape from var rank to var rank");
  71. {
  72. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  73. auto b = reshape(a, ra::Big<int, 1> {3});
  74. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  75. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  76. tr.info("reshape can return var rank (1)").test_eq(ra::RANK_ANY, rank_s(b));
  77. }
  78. tr.section("reshape to fixed rank to var rank");
  79. {
  80. // FIXME warning w/ gcc 6.3 in bootstrap.hh inside() [ra32]. Apparent root is in decl of b in reshape().
  81. ra::Big<int, 2> a({2, 3}, ra::_0*3+ra::_1);
  82. auto b = reshape(a, ra::Big<int, 1> {3});
  83. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  84. tr.test_eq(ra::scalar(a.p), ra::scalar(b.p));
  85. tr.info("reshape can return var rank").test_eq(ra::RANK_ANY, rank_s(b));
  86. }
  87. tr.section("conversion from var rank to fixed rank");
  88. {
  89. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  90. ra::View<int *, 2> b = a;
  91. tr.info("fixing rank").test_eq(ra::_0*3+ra::_1, b);
  92. tr.info("fixing rank is view").test(a.data()==b.data());
  93. }
  94. return tr.summary();
  95. }