big-0.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Tests specific to Container, constructors.
  3. // (c) Daniel Llorens - 2017-2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. #include "mpdebug.hh"
  12. using std::cout, std::endl, std::flush, ra::TestRecorder;
  13. template <class T> constexpr bool ctest1 = requires { ra::Big<T, 2> ({2, 3, 1}, 99); }; // bad shape for rank
  14. template <class T> constexpr bool ctest2 = requires { ra::Big<T, 2> ({2, 3, 1}, {1, 2, 3, 4, 5, 6}); }; // bad shape for rank
  15. template <class T> constexpr bool ctest3 = requires { ra::Big<T, 2> ({2, 3, 1}, ra::none); }; // bad shape for rank
  16. template <class T> constexpr bool ctest4 = requires { ra::Big<T, 0> ({3, 4}, 3.); }; // bad shape for rank
  17. int main(int argc, char * * argv)
  18. {
  19. TestRecorder tr;
  20. tr.section("predicates");
  21. {
  22. ra::View<int, 2> a;
  23. static_assert(ra::rank_s<decltype(a().iter<0>())>()==ra::rank_s<decltype(a().iter())>());
  24. static_assert(std::input_iterator<decltype(a.begin())>);
  25. static_assert(std::output_iterator<decltype(a.begin()), int>);
  26. }
  27. tr.section("constructors");
  28. {
  29. tr.section("null View constructor");
  30. {
  31. ra::View<int, 1> a;
  32. tr.test(nullptr==a.data());
  33. }
  34. tr.section("regression with some shape arguments (fixed rank)");
  35. {
  36. ra::Big<int, 1> sizes = {5};
  37. ra::Big<double, 1> a(sizes, ra::none);
  38. a = 33.;
  39. tr.test_eq(5, a.size());
  40. tr.test_eq(33, a);
  41. }
  42. {
  43. ra::Big<int> sizes = {5};
  44. ra::Big<double, 1> a(sizes, ra::none);
  45. a = 33.;
  46. tr.test_eq(5, a.size());
  47. tr.test_eq(33, a);
  48. }
  49. tr.section("regression with implicitly declared View constructors [ra38]. Reduced from examples/maxwell.cc");
  50. {
  51. ra::Big<int, 1> A = {1, 2};
  52. ra::Big<int, 1> X = {0, 0};
  53. X(ra::all) = A();
  54. tr.test_eq(ra::start({1, 2}), X);
  55. }
  56. tr.section("need for non-default View::operator=(View const & x) [ra34]");
  57. {
  58. ra::Big<int, 1> a {0, 1, 2, 3, 4, 5};
  59. ra::View<int, 1> const va = a();
  60. ra::Big<int, 1> x(va); // replacing default operator= by copy-to-view.
  61. tr.test_eq(ra::iota(6), x);
  62. }
  63. tr.section("init list constructors handle implicit conversions");
  64. {
  65. ra::Big<int, 2> a({int(3), ra::dim_t(2)}, {0, 1, 2, 3, 4, 5});
  66. tr.test_eq(ra::_0 * 2 + ra::_1, a);
  67. tr.test_eq(3, a.len(0));
  68. tr.test_eq(2, a.len(1));
  69. }
  70. }
  71. tr.section("should-fail constructors");
  72. {
  73. static_assert(!ctest1<int>);
  74. static_assert(!ctest2<int>);
  75. static_assert(!ctest3<int>);
  76. static_assert(!ctest4<int>);
  77. // FIXME these errors depend on static_assert so cannot be checked with requires.
  78. // ra::Big<T, 2> {3, 4}; // Invalid shape for rank
  79. // ra::Big<int, 2> (2, ra::none); // shape arg must have rank 1 for array rank>1
  80. // ra::Big<T, 2> {1, 2, 3, 4, 5, 6}; // bad deduced shape from content arg
  81. // ra::Big<T, 2> (ra::Small<int, 3>{2, 3, 4}, 99.); // bad shape for rank
  82. }
  83. tr.section("any rank 1 expression for the shape argument");
  84. {
  85. ra::Big<int, 2> a (2+ra::iota(2), {0, 1, 2, 3, 4, 5});
  86. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  87. }
  88. tr.section("even non-drivable expressions if the rank is fixed");
  89. {
  90. ra::Big<int, 2> a(ra::_0 + 2, {0, 1, 2, 3, 4, 5});
  91. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  92. }
  93. tr.section("also on raw views");
  94. {
  95. int ap[6] = {0, 1, 2, 3, 4, 5};
  96. ra::View<int, 2> a(2+ra::iota(2), ap);
  97. tr.test_eq(2, a.len(0));
  98. tr.test_eq(3, a.len(1));
  99. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  100. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  101. }
  102. tr.section("also on raw views with var rank");
  103. {
  104. int ap[6] = {0, 1, 2, 3, 4, 5};
  105. ra::View<int> a(2+ra::iota(2), ap);
  106. tr.test_eq(2, a.len(0));
  107. tr.test_eq(3, a.len(1));
  108. tr.test_eq(ra::Small<int, 2, 3> {{0, 1, 2}, {3, 4, 5}}, a);
  109. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  110. }
  111. tr.section("nested braces operator=");
  112. {
  113. ra::Big<int, 2> a({2, 3}, {0, 1, 2, 3, 4, 5});
  114. auto ap = a.data();
  115. {
  116. // this uses operator=(nested_braces_r)
  117. a() = {{4, 5, 6}, {7, 8, 9}};
  118. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  119. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  120. }
  121. {
  122. // uses operator=(nested_braces_r)
  123. a = {{5, 6, 7}, {8, 9, 10}};
  124. tr.test_eq(ra::scalar(ap), ra::scalar(a.data()));
  125. tr.test_eq(ra::iota(6, 5), ra::ptr(a.data()));
  126. // uses nested_braces_r constructor, so a's storage is NOT preserved. Don't rely on this either way
  127. a = {{{4, 5, 6}, {7, 8, 9}}};
  128. tr.skip().test_eq(ra::scalar(ap), ra::scalar(a.data()));
  129. tr.test_eq(2, a.len(0));
  130. tr.test_eq(3, a.len(1));
  131. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  132. }
  133. }
  134. tr.section("nested braces constructor");
  135. {
  136. ra::Big<int, 2> a = {{4, 5, 6}, {7, 8, 9}};
  137. tr.test_eq(2, a.len(0));
  138. tr.test_eq(3, a.len(1));
  139. tr.test_eq(ra::iota(6, 4), ra::ptr(a.data()));
  140. }
  141. tr.section("nested braces for nested type I");
  142. {
  143. using int2 = ra::Small<int, 2>;
  144. // FIXME removed (shape, nested) constructors so this wouldn't be ambiguous (bc 1 converts to int2). But maybe int shouldn't convert to int2 [ra16]
  145. ra::Big<int2, 2> a({2, 2}, { {1, 2}, {2, 3}, {4, 5}, {6, 7} });
  146. ra::Big<int2, 2> b({{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}});
  147. ra::Big<int2, 2> c {{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}};
  148. ra::Big<int2, 2> d = {{{1, 2}, {2, 3}}, {{4, 5}, {6, 7}}};
  149. tr.test_eq(a, b);
  150. tr.test_eq(a, c);
  151. tr.test_eq(a, d);
  152. }
  153. tr.section("nested braces for nested type II");
  154. {
  155. int x[2][3] = {{1, 2, 3}, {4, 5, 6}};
  156. int y[2][3] = {{10, 20, 30}, {40, 50, 60}};
  157. ra::Big<ra::Small<int, 2, 3>, 1> a = {x, y};
  158. tr.test_eq(ra::_0*3+ra::_1 + 1, a(0));
  159. tr.test_eq(10*(ra::_0*3+ra::_1 + 1), a(1));
  160. }
  161. tr.section("nested braces for nested type II");
  162. {
  163. int x[2][3] = {{1, 2, 3}, {4, 5, 6}};
  164. int y[2][3] = {{10, 20, 30}, {40, 50, 60}};
  165. ra::Big<ra::Small<int, 2, 3>, 1> a = {x, y};
  166. tr.test_eq(ra::_0*3+ra::_1 + 1, a(0));
  167. tr.test_eq(10*(ra::_0*3+ra::_1 + 1), a(1));
  168. }
  169. tr.section("nested braces for nested type III");
  170. {
  171. int x[4] = {1, 2, 3, 4};
  172. int y[6] = {10, 20, 30, 40, 50, 60};
  173. ra::Big<ra::Big<int, 1>, 1> a = {x, y};
  174. tr.test_eq(ra::iota(4, 1), a(0));
  175. tr.test_eq(ra::iota(6, 10, 10), a(1));
  176. }
  177. tr.section("nested braces for nested type IV");
  178. {
  179. ra::Big<ra::Big<int, 1>, 1> a = {{1, 2, 3, 4}, {10, 20, 30, 40, 50, 60}};
  180. tr.test_eq(ra::iota(4, 1), a(0));
  181. tr.test_eq(ra::iota(6, 10, 10), a(1));
  182. }
  183. tr.section("nested braces for nested type V [ra45]");
  184. {
  185. int u[3] = { 1, 2, 3 };
  186. ra::Big<int, 1> v = u;
  187. ra::Small<ra::Big<int, 1>, 1> b = { {u} }; // ok; broken with { u }
  188. tr.test_eq(ra::iota(3, 1), b(0));
  189. ra::Small<ra::Big<int, 1>, 1> c = { v }; // ok
  190. tr.test_eq(ra::iota(3, 1), c(0));
  191. ra::Small<ra::Big<int, 1>, 1> d = { {1, 2, 3} }; // ok
  192. tr.test_eq(ra::iota(3, 1), d(0));
  193. auto x = ra::iota(3, 1);
  194. ra::Small<ra::Big<int, 1>, 1> f = { {x} }; // ok; broken with { x }
  195. tr.test_eq(ra::iota(3, 1), f(0));
  196. // ra::Small<int, 3> w = { 1, 2, 3 };
  197. // ra::Small<ra::Big<int, 1>, 1> e = { w }; // broken with { w }, ct error with { {w} }
  198. // tr.test_eq(ra::iota(3, 1), e(0));
  199. }
  200. tr.section("nested braces for nested type VI");
  201. {
  202. ra::Small<ra::Big<double, 1>, 3> g = { { 1 }, { 1, 2 }, { 1, 2, 3 } };
  203. tr.test_eq(ra::start({1}), g[0]);
  204. tr.test_eq(ra::start({1, 2}), g[1]);
  205. tr.test_eq(ra::start({1, 2, 3}), g[2]);
  206. }
  207. tr.section("more nested braces");
  208. {
  209. tr.section("with dynamic rank");
  210. ra::Small<float, 2, 4> ref = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  211. ra::Big<float> A = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  212. ra::Big<float> B({2, 4}, {1, 2, 3, 4, 5, 6, 7, 8});
  213. // ra::Big<float> C({2, 4}, { {1, 2, 3, 4}, {5, 6, 7, 8} }); // not allowed bc ambiguity with empty braces
  214. ra::Big<float, 2> A2 = { {1, 2, 3, 4}, {5, 6, 7, 8} };
  215. ra::Big<float, 2> B2({2, 4}, {1, 2, 3, 4, 5, 6, 7, 8});
  216. // ra::Big<float, 2> C2({2, 4}, { {1, 2, 3, 4}, {5, 6, 7, 8} }); // not allowed to avoid ambiguity in [ra16] :-/
  217. tr.test_eq(ref, A);
  218. tr.test_eq(ref, B);
  219. tr.test_eq(ref, A2);
  220. tr.test_eq(ref, B2);
  221. }
  222. // FIXME This works for Small, that has multi-arg constructors. Right now this calls the 2-elem constructor shape, content instead of the braces constructor, since initializer_list<T> doesn't match.
  223. // tr.section("item constructor");
  224. // {
  225. // ra::Big<int, 2> a {{1, 2}, ra::iota(2, 33)};
  226. // tr.test_eq(1, a(0, 0));
  227. // tr.test_eq(2, a(0, 1));
  228. // tr.test_eq(33, a(1, 0));
  229. // tr.test_eq(34, a(1, 1));
  230. // }
  231. tr.section("at() takes foreign vector");
  232. {
  233. ra::Big<double, 2> a({3, 3}, ra::_0 + 10*ra::_1);
  234. std::array<int, 2> b = {2, 2};
  235. tr.test_eq(22, a.at(b));
  236. }
  237. tr.section("default constructor of var rank");
  238. {
  239. ra::Big<int> a {};
  240. ra::Big<int, 1> b {};
  241. tr.test_eq(b.rank(), a.rank());
  242. tr.test_eq(b.len(0), a.len(0));
  243. tr.test_eq(1, a.rank());
  244. tr.test_eq(0, a.len(0));
  245. }
  246. tr.section("allow scalar for rank 1 shapes");
  247. {
  248. ra::Big<int> a(3, ra::_0);
  249. tr.test_eq(1, rank(a));
  250. tr.test_eq(ra::iota(3), a);
  251. ra::Big<int, 1> b(4, ra::_0);
  252. tr.test_eq(ra::iota(4), b);
  253. }
  254. tr.section("default constructor for ct rank 0 Container");
  255. {
  256. ra::Big<int, 0> a = {}; // uninitialized
  257. a = 3;
  258. tr.test_eq(3, a);
  259. }
  260. tr.section("scalar constructor for rt rank 0 Container");
  261. {
  262. ra::Big<int> a(4);
  263. tr.test_eq(4, a);
  264. }
  265. tr.section("index with rank 0 exprs");
  266. {
  267. ra::Big<int, 0> a = 1;
  268. ra::Big<int, 1> b = { 1, 2, 3 };
  269. tr.test(std::is_same_v<int &, decltype(b(a))>);
  270. tr.test_eq(2, b(a));
  271. }
  272. return tr.summary();
  273. }