big-0.C 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file big-0.C
  3. /// @brief Tests specific to Container. Constructors.
  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 <iostream>
  10. #include <iterator>
  11. #include "ra/ra.H"
  12. #include "ra/test.H"
  13. #include "ra/mpdebug.H"
  14. using std::cout, std::endl, std::flush;
  15. int main(int argc, char * * argv)
  16. {
  17. TestRecorder tr;
  18. tr.section("constructors");
  19. {
  20. tr.section("regression with some shape arguments (fixed rank) [ra43]");
  21. {
  22. ra::Big<int, 1> sizes = {5};
  23. ra::Big<double, 1> a(sizes, ra::none);
  24. a = 33.;
  25. cout << a << endl;
  26. }
  27. tr.section("regression with implicitly declared View constructors [ra38]. Reduced from examples/maxwell.C");
  28. {
  29. ra::Big<int, 1> A = {1, 2};
  30. ra::Big<int, 1> X = {0, 0};
  31. X(ra::all) = A();
  32. tr.test_eq(ra::start({1, 2}), X);
  33. }
  34. tr.section("need for explicit View::operator=(View const & x) [ra34]");
  35. {
  36. ra::Big<int, 1> a {0, 1, 2, 3, 4, 5};
  37. ra::View<int, 1> const va = a();
  38. ra::Big<int, 1> x(va); // replacing default operator= by copy-to-view.
  39. tr.test_eq(ra::iota(6), x);
  40. }
  41. tr.section("init list constructors handle implicit conversions");
  42. {
  43. ra::Big<int, 2> a({int(3), ssize_t(2)}, {0, 1, 2, 3, 4, 5});
  44. tr.test_eq(ra::_0 * 2 + ra::_1, a);
  45. tr.test_eq(3, a.size(0));
  46. tr.test_eq(2, a.size(1));
  47. }
  48. }
  49. tr.section("should-fail constructors");
  50. {
  51. tr.section("shape errors detected at ct FIXME cannot test ct errors yet [ra42]");
  52. {
  53. // ra::Big<int, 2> a({2, 3, 1}, 99); // does not compile
  54. // ra::Big<int, 2> b({2, 3, 1}, {1, 2, 3, 4, 5, 6}); // does not compile
  55. // ra::Big<int, 2> c({2, 3, 1}, ra::none); // does not compile
  56. // ra::Big<int, 2> d(2, ra::none); // does not compile (maybe it should? by rank extension)
  57. }
  58. tr.section("shape errors detected at ct FIXME cannot test ct errors yet [ra42]");
  59. {
  60. // ra::Big<double, 0> a({3, 4}, 3.); cout << a << endl; // does not compile
  61. }
  62. tr.section("init from init list fails at ct if rank != 1 FIXME cannot test ct errors yet [ra42]");
  63. {
  64. // ra::Big<double, 2> a {1, 2, 3, 4, 5, 6}; cout << a << endl; // does not compile
  65. }
  66. tr.section("bad rank with ct size X && type shape argument FIXME cannot test ct errors yet [ra42]");
  67. {
  68. // ra::Big<double, 2> a(ra::Small<int, 3>{2, 3, 4}, 99.); cout << a << endl; // does not compile
  69. }
  70. }
  71. tr.section("any rank 1 expression for the shape argument");
  72. {
  73. ra::Big<int, 2> a (2+ra::iota(2), {0, 1, 2, 3, 4, 5});
  74. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  75. }
  76. tr.section("even non-drivable expressions if the rank is fixed");
  77. {
  78. ra::Big<int, 2> a(ra::_0 + 2, {0, 1, 2, 3, 4, 5});
  79. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  80. }
  81. tr.section("also on raw views");
  82. {
  83. int ap[6] = {0, 1, 2, 3, 4, 5};
  84. ra::View<int, 2> a(2+ra::iota(2), ap);
  85. tr.test_eq(2, a.size(0));
  86. tr.test_eq(3, a.size(1));
  87. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  88. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  89. }
  90. tr.section("also on raw views with var rank");
  91. {
  92. int ap[6] = {0, 1, 2, 3, 4, 5};
  93. ra::View<int> a(2+ra::iota(2), ap);
  94. tr.test_eq(2, a.size(0));
  95. tr.test_eq(3, a.size(1));
  96. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  97. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  98. }
  99. tr.section("nested braces operator=");
  100. {
  101. ra::Big<int, 2> a({2, 3}, {0, 1, 2, 3, 4, 5});
  102. auto ap = a.data();
  103. a = {{4, 5, 6}, {7, 8, 9}}; // this uses operator=(nested_braces_r)
  104. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  105. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  106. a = {{{4, 5, 6}, {7, 8, 9}}}; // this uses the nested_braces_r constructor (!!)
  107. tr.skip().test_eq(ra::scalar(ap), ra::scalar(a.data())); // FIXME fairly dangerous!
  108. tr.test_eq(2, a.size(0));
  109. tr.test_eq(3, a.size(1));
  110. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  111. }
  112. tr.section("nested braces constructor");
  113. {
  114. ra::Big<int, 2> a = {{4, 5, 6}, {7, 8, 9}};
  115. tr.test_eq(2, a.size(0));
  116. tr.test_eq(3, a.size(1));
  117. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  118. }
  119. tr.section("nested braces for nested type I");
  120. {
  121. ra::Big<ra::Small<int, 2>, 2> a({2, 2}, { {1, 2}, {2, 3}, {4, 5}, {6, 7} });
  122. ra::Big<ra::Small<int, 2>, 2> b({{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}});
  123. ra::Big<ra::Small<int, 2>, 2> c {{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}};
  124. ra::Big<ra::Small<int, 2>, 2> d = {{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}};
  125. tr.test_eq(a, b);
  126. }
  127. tr.section("nested braces for nested type II");
  128. {
  129. int x[2][3] = {{1, 2, 3}, {4, 5, 6}};
  130. int y[2][3] = {{10, 20, 30}, {40, 50, 60}};
  131. ra::Big<ra::Small<int, 2, 3>, 1> a = {x, y};
  132. tr.test_eq(ra::_0*3+ra::_1 + 1, a(0));
  133. tr.test_eq(10*(ra::_0*3+ra::_1 + 1), a(1));
  134. }
  135. tr.section("nested braces for nested type II");
  136. {
  137. int x[2][3] = {{1, 2, 3}, {4, 5, 6}};
  138. int y[2][3] = {{10, 20, 30}, {40, 50, 60}};
  139. ra::Big<ra::Small<int, 2, 3>, 1> a = {x, y};
  140. tr.test_eq(ra::_0*3+ra::_1 + 1, a(0));
  141. tr.test_eq(10*(ra::_0*3+ra::_1 + 1), a(1));
  142. }
  143. tr.section("nested braces for nested type III");
  144. {
  145. int x[4] = {1, 2, 3, 4};
  146. int y[6] = {10, 20, 30, 40, 50, 60};
  147. ra::Big<ra::Big<int, 1>, 1> a = {x, y};
  148. tr.test_eq(ra::iota(4, 1), a(0));
  149. tr.test_eq(ra::iota(6, 10, 10), a(1));
  150. }
  151. tr.section("nested braces for nested type IV");
  152. {
  153. ra::Big<ra::Big<int, 1>, 1> a = {{1, 2, 3, 4}, {10, 20, 30, 40, 50, 60}};
  154. tr.test_eq(ra::iota(4, 1), a(0));
  155. tr.test_eq(ra::iota(6, 10, 10), a(1));
  156. }
  157. tr.section("nested braces for nested type V [ra45]");
  158. {
  159. int u[3] = { 1, 2, 3 };
  160. ra::Big<int, 1> v = u;
  161. ra::Small<ra::Big<int, 1>, 1> b = { {u} }; // ok; broken with { u }
  162. tr.test_eq(ra::iota(3, 1), b(0));
  163. ra::Small<ra::Big<int, 1>, 1> c = { v }; // ok
  164. tr.test_eq(ra::iota(3, 1), c(0));
  165. ra::Small<ra::Big<int, 1>, 1> d = { {1, 2, 3} }; // ok
  166. tr.test_eq(ra::iota(3, 1), d(0));
  167. auto x = ra::iota(3, 1);
  168. ra::Small<ra::Big<int, 1>, 1> f = { {x} }; // ok; broken with { x }
  169. tr.test_eq(ra::iota(3, 1), f(0));
  170. // ra::Small<int, 3> w = { 1, 2, 3 };
  171. // ra::Small<ra::Big<int, 1>, 1> e = { w }; // broken with { w }, ct error with { {w} }
  172. // tr.test_eq(ra::iota(3, 1), e(0));
  173. }
  174. tr.section("nested braces for nested type VI");
  175. {
  176. ra::Small<ra::Big<double, 1>, 3> g = { { 1 }, { 1, 2 }, { 1, 2, 3 } };
  177. tr.test_eq(ra::start({1}), g[0]);
  178. tr.test_eq(ra::start({1, 2}), g[1]);
  179. tr.test_eq(ra::start({1, 2, 3}), g[2]);
  180. }
  181. // FIXME This works for Small, would be nice here as well. Right now this calls the 2-elem constructor shape, content instead of the braces constructor, since intializer_list<T> doesn't match. I'd need multiple args, as in Small.
  182. // tr.section("free constructor FIXME");
  183. // {
  184. // ra::Big<int, 2> a = {{1, 2}, ra::iota(2, 33)};
  185. // tr.test_eq(1, a(0, 0));
  186. // tr.test_eq(2, a(0, 1));
  187. // tr.test_eq(33, a(1, 0));
  188. // tr.test_eq(34, a(1, 1));
  189. // }
  190. tr.section("at() takes foreign vector");
  191. {
  192. ra::Big<double, 2> a({3, 3}, ra::_0 + 10*ra::_1);
  193. std::array<int, 2> b = {2, 2};
  194. tr.test_eq(22, a.at(b));
  195. }
  196. return tr.summary();
  197. }