reshape.cc 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <memory>
  10. #include "ra/test.hh"
  11. #include "mpdebug.hh"
  12. using std::cout, std::endl, ra::TestRecorder;
  13. int main()
  14. {
  15. TestRecorder tr(std::cout);
  16. tr.section("reshape");
  17. {
  18. ra::Big<int, 3> aa({2, 3, 3}, ra::_0*3+ra::_1);
  19. auto a = aa(ra::all, ra::all, 0);
  20. tr.info("ravel_free").test_eq(ra::iota(6), ravel_free(a));
  21. tr.test_eq(ra::scalar(a.data()), ra::scalar(ravel_free(a).data()));
  22. // select.
  23. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, reshape(a, ra::Small<int, 1> {3}));
  24. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 1> {3}).data()));
  25. // tile.
  26. auto tilea = reshape(a, ra::Small<int, 3> {2, 2, 3});
  27. 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);
  28. tr.info("some tile-reshapes are free (I)").test_eq(0, tilea.step(0));
  29. tr.info("some tile-reshapes are free (II)").test_eq(ra::scalar(a.data()), ra::scalar(tilea.data()));
  30. // reshape with free ravel
  31. 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}));
  32. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {3, 2}).data()));
  33. 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}));
  34. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 3> {2, 1, 2}).data()));
  35. 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}));
  36. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {-1, 2}).data()));
  37. 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}));
  38. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 2> {2, -1}).data()));
  39. 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}));
  40. tr.test_eq(ra::scalar(a.data()), ra::scalar(reshape(a, ra::Small<int, 3> {2, -1, 3}).data()));
  41. }
  42. tr.section("reshape from var rank to fixed rank");
  43. {
  44. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  45. auto b = reshape(a, ra::Small<int, 1> {3});
  46. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  47. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  48. tr.info("reshape can fix rank").test_eq(1, rank_s(b));
  49. }
  50. tr.section("reshape from var rank to fixed rank using the initializer_list shim");
  51. {
  52. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  53. auto b = reshape(a, {3, 2});
  54. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, b);
  55. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  56. tr.info("reshape can return fixed rank (2)").test_eq(2, rank_s(b));
  57. auto c = reshape(a, {3l, 2l}); // check deduction works regardless
  58. tr.info("reshape").test_eq(ra::Big<int, 2> {{0, 1}, {2, 3}, {4, 5}}, c);
  59. tr.test_eq(ra::scalar(a.data()), ra::scalar(c.data()));
  60. tr.info("reshape can return fixed rank (3)").test_eq(2, rank_s(c));
  61. }
  62. tr.section("reshape from var rank to var rank");
  63. {
  64. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  65. auto b = reshape(a, ra::Big<int, 1> {3});
  66. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  67. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  68. tr.info("reshape can return var rank (1)").test_eq(ra::ANY, rank_s(b));
  69. }
  70. tr.section("reshape to fixed rank to var rank");
  71. {
  72. // FIXME warning w/ gcc 6.3 in bootstrap.hh inside() [ra32]. Apparent root is in decl of b in reshape().
  73. ra::Big<int, 2> a({2, 3}, ra::_0*3+ra::_1);
  74. auto b = reshape(a, ra::Big<int, 1> {3});
  75. tr.info("reshape select").test_eq(ra::Big<int, 1> {0, 1, 2}, b);
  76. tr.test_eq(ra::scalar(a.data()), ra::scalar(b.data()));
  77. tr.info("reshape can return var rank").test_eq(ra::ANY, rank_s(b));
  78. }
  79. tr.section("conversion from var rank to fixed rank");
  80. {
  81. ra::Big<int> a({2, 3}, ra::_0*3+ra::_1);
  82. ra::View<int, 2> b = a;
  83. tr.info("fixing rank").test_eq(ra::_0*3+ra::_1, b);
  84. tr.info("fixing rank is view").test(a.data()==b.data());
  85. }
  86. return tr.summary();
  87. }