small-1.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Making ra::Small and its iterator work with expressions/traversal.
  3. // (c) Daniel Llorens - 2014-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. // See also small-0.cc.
  9. #include <iostream>
  10. #include <iterator>
  11. #include "ra/test.hh"
  12. #include "mpdebug.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. using ra::mp::int_list, ra::int_c, ra::mp::print_int_list, ra::mp::ref;
  15. int main()
  16. {
  17. TestRecorder tr;
  18. tr.section("pieces of transpose(ra::Small)");
  19. {
  20. using lens = int_list<1, 2, 3, 4, 5>;
  21. using steps = int_list<1, 10, 100, 1000, 10000>;
  22. using c0 = ra::axis_indices<int_list<0, 1, 3, 2, 0>, int_c<0>>::type;
  23. using e0 = int_list<0, 4>;
  24. tr.info(print_int_list<e0> {}, " vs ", print_int_list<c0> {}).test(std::is_same_v<e0, c0>);
  25. using c1 = ra::axis_indices<int_list<0, 1, 3, 2, 0>, int_c<1>>::type;
  26. using e1 = int_list<1>;
  27. tr.info(print_int_list<e1> {}, " vs ", print_int_list<c1> {}).test(std::is_same_v<e1, c1>);
  28. using call = ra::axes_list_indices<int_list<0, 1, 3, 2, 0>, lens, steps>::type;
  29. using eall = std::tuple<int_list<0, 4>, int_list<1>, int_list<3>, int_list<2>>;
  30. tr.info(print_int_list<eall> {}, " vs ", print_int_list<call> {}).test(std::is_same_v<eall, call>);
  31. }
  32. tr.section("transpose(ra::Small)");
  33. {
  34. ra::Small<double, 2, 3> const a(ra::_0 + 10*ra::_1);
  35. tr.info("<0 1>").test_eq(a, ra::transpose<0, 1>(a));
  36. tr.info("<1 0>").test_eq(ra::Small<double, 3, 2>(10*ra::_0 + ra::_1), ra::transpose<1, 0>(a));
  37. tr.info("<0 0>").test_eq(ra::Small<double, 2> {0, 11}, ra::transpose<0, 0>(a));
  38. ra::Small<double, 2, 3> b(ra::_0 + 10*ra::_1);
  39. tr.info("<0 1>").test_eq(a, ra::transpose<0, 1>(a));
  40. tr.info("<1 0>").test_eq(ra::Small<double, 3, 2>(10*ra::_0 + ra::_1), ra::transpose<1, 0>(a));
  41. ra::transpose<0, 0>(b) = {7, 9};
  42. tr.info("<0 0>").test_eq(ra::Small<double, 2, 3>{7, 10, 20, 1, 9, 21}, b);
  43. ra::Small<double> x {99};
  44. auto xt = transpose<>(x);
  45. tr.info("<> rank").test_eq(0, xt.rank());
  46. tr.info("<>").test_eq(99, xt);
  47. ra::Small<double, 3, 3> x3 = ra::_0 - ra::_1;
  48. ra::Small<double, 3, 3> y3 = transpose<1, 0>(x3);
  49. tr.info("transpose copy").test_eq(y3, ra::_1 - ra::_0);
  50. x3() = transpose<1, 0>(y3());
  51. tr.info("transpose copy").test_eq(x3, ra::_0 - ra::_1);
  52. }
  53. tr.section("sizeof");
  54. {
  55. // These all static, but show the numbers if there's an error.
  56. tr.info("sizeof(ra::Small<double>)")
  57. .test_eq(sizeof(double), sizeof(ra::Small<double>));
  58. tr.info("sizeof(ra::Small<double, 0>)")
  59. .test(sizeof(double)==sizeof(ra::Small<double, 0>) || 0==sizeof(ra::Small<double, 0>)); // don't rely on either.
  60. tr.info("sizeof(ra::Small<double, 1>)")
  61. .test_eq(sizeof(double), sizeof(ra::Small<double, 1>));
  62. tr.info("sizeof(ra::Small<double, 2>)")
  63. .test_eq(2*sizeof(double), sizeof(ra::Small<double, 2>));
  64. }
  65. tr.section("internal fields");
  66. {
  67. {
  68. using A = ra::Small<double, 10, 10>;
  69. alignas(A) double storage[sizeof(A)/sizeof(double)];
  70. A * a = new (&storage) A();
  71. std::fill(a->data(), a->data()+100, 0.);
  72. storage[99] = 1.3;
  73. std::cout << (*a) << std::endl;
  74. tr.test_eq(1.3, a->data()[99]);
  75. tr.test_eq(1.3, (*a)(9, 9));
  76. }
  77. {
  78. ra::Small<double, 2, 3> a {1, 2, 3, 4, 5, 6};
  79. tr.test_eq(2*3*sizeof(double), sizeof(a));
  80. tr.test_eq(1, a.data()[0]);
  81. }
  82. }
  83. tr.section("top level generics");
  84. {
  85. ra::Small<double, 2, 3> a {1, 2, 3, 4, 5, 6};
  86. tr.test_eq(ra::Small<ra::dim_t, 2> {2, 3}, shape(a));
  87. tr.test_eq(3u, ra::size(std::array<double, 3>()));
  88. }
  89. tr.section("static step computation");
  90. {
  91. using d = int_list<3, 4, 5>;
  92. using s = ra::default_steps<d>;
  93. tr.info("step 0").test_eq(20, ref<s, 0>::value);
  94. tr.info("step 1").test_eq(5, ref<s, 1>::value);
  95. tr.info("step 2").test_eq(1, ref<s, 2>::value);
  96. }
  97. tr.section("subscripts");
  98. {
  99. tr.section("with scalar indices");
  100. {
  101. ra::Small<double, 3, 2> s { 1, 4, 2, 5, 3, 6 };
  102. auto s0 = s();
  103. double check0[6] = { 1, 4, 2, 5, 3, 6 };
  104. tr.test(std::equal(s0.begin(), s0.end(), check0));
  105. auto s1 = s(1);
  106. double check1[3] = { 2, 5 };
  107. cout << "s1: " << s1(0) << ", " << s1(1) << endl;
  108. tr.test(s1(0)==2 && s1(1)==5);
  109. tr.test(std::equal(s1.begin(), s1.end(), check1));
  110. tr.test_eq(5, s(1, 1));
  111. }
  112. tr.section("using SmallView as rvalue");
  113. {
  114. ra::Small<double, 3, 2> s { 1, 4, 2, 5, 3, 6 };
  115. // use as rvalue.
  116. s(0) = { 3, 2 };
  117. s(1) = { 5, 4 };
  118. s(2) = { 7, 6 };
  119. cout << s << endl;
  120. tr.test_eq(ra::Small<double, 3, 2> { 3, 2, 5, 4, 7, 6 }, s);
  121. ra::Small<double, 3, 2> z = s;
  122. z *= -1;
  123. // check that SmallView = SmallView copies contents, just as View = View.
  124. s(0) = z(2);
  125. s(1) = z(1);
  126. s(2) = z(0);
  127. tr.test_eq(ra::Small<double, 3, 2> { -3, -2, -5, -4, -7, -6 }, z);
  128. tr.test_eq(ra::Small<double, 3, 2> { -7, -6, -5, -4, -3, -2 }, s);
  129. }
  130. tr.section("with tuples");
  131. {
  132. ra::Small<double, 3, 2> s { 1, 4, 2, 5, 3, 6 };
  133. ra::Small<int, 2> i2 { 1, 1 };
  134. ra::Small<int, 1> i1 { 1 };
  135. ra::Small<int, 0> i0 { };
  136. double check2[1] = { 5 };
  137. double check1[2] = { 2, 5 };
  138. double check0[6] = { 1, 4, 2, 5, 3, 6 };
  139. auto k2 = s.at(i2);
  140. tr.test_eq(0, ra::rank(k2));
  141. tr.test_eq(check2[0], k2);
  142. auto k1 = s.at(i1).begin(); tr.test(std::equal(check1, check1+2, k1));
  143. auto k0 = s.at(i0).begin(); tr.test(std::equal(check0, check0+6, k0));
  144. }
  145. tr.section("with rank 1 subscripts");
  146. {
  147. ra::Small<double, 3, 2> s { 1, 4, 2, 5, 3, 6 };
  148. tr.test_eq(ra::Small<int, 2> { 1, 4 }, s(0));
  149. tr.test_eq(ra::Small<int, 2> { 2, 5 }, s(1));
  150. tr.test_eq(ra::Small<int, 2> { 3, 6 }, s(2));
  151. tr.test_eq(ra::Small<int, 3> { 1, 2, 3 }, s(ra::all, 0));
  152. tr.test_eq(ra::Small<int, 3> { 4, 5, 6 }, s(ra::all, 1));
  153. tr.test_eq(1, s(ra::all, 1).rank());
  154. // check STL iterator.
  155. {
  156. int check0[] = { 1, 2, 3 };
  157. int check1[] = { 4, 5, 6 };
  158. tr.test(std::ranges::equal(check0, check0+3, s(ra::all, 0).begin(), s(ra::all, 0).end()));
  159. tr.test(std::ranges::equal(check1, check1+3, s(ra::all, 1).begin(), s(ra::all, 1).end()));
  160. tr.test(std::ranges::equal(s(ra::all, 0).begin(), s(ra::all, 0).end(), check0, check0+3));
  161. tr.test(std::ranges::equal(s(ra::all, 1).begin(), s(ra::all, 1).end(), check1, check1+3));
  162. }
  163. tr.test_eq(1, s(ra::all, 0)[0]);
  164. tr.test_eq(2, s(ra::all, 0)[1]);
  165. tr.test_eq(3, s(ra::all, 0)[2]);
  166. tr.test_eq(4, s(ra::all, 1)(0));
  167. tr.test_eq(5, s(ra::all, 1)(1));
  168. tr.test_eq(6, s(ra::all, 1)(2));
  169. using I0 = ra::Small<ra::dim_t, 1>;
  170. tr.test_eq(1, s(ra::all, 0).at(I0 {0}));
  171. tr.test_eq(2, s(ra::all, 0).at(I0 {1}));
  172. tr.test_eq(3, s(ra::all, 0).at(I0 {2}));
  173. tr.test_eq(4, s(ra::all, 1).at(I0 {0}));
  174. tr.test_eq(5, s(ra::all, 1).at(I0 {1}));
  175. tr.test_eq(6, s(ra::all, 1).at(I0 {2}));
  176. }
  177. tr.section("with rank 1 subscripts, result rank > 1");
  178. {
  179. ra::Small<double, 3, 2, 2> s = 100*ra::_0 + 10*ra::_1 + 1*ra::_2;
  180. cout << s << endl;
  181. auto t = s(ra::all, 1, ra::all);
  182. tr.test_eq(2, t.rank());
  183. tr.test_eq(3, t.len(0));
  184. tr.test_eq(2, t.len(1));
  185. tr.test_eq(10, t(0, 0));
  186. tr.test_eq(11, t(0, 1));
  187. tr.test_eq(110, t(1, 0));
  188. tr.test_eq(111, t(1, 1));
  189. tr.test_eq(210, t(2, 0));
  190. tr.test_eq(211, t(2, 1));
  191. tr.test_eq(ra::Small<int, 3, 2> { 10, 11, 110, 111, 210, 211 }, t);
  192. tr.test_eq(4, t.step(0));
  193. tr.test_eq(1, t.step(1));
  194. // check STL iterator.
  195. {
  196. int check[] = { 10, 11, 110, 111, 210, 211 };
  197. tr.test(std::ranges::equal(t.begin(), t.end(), check, check+6));
  198. tr.test(std::ranges::equal(check, check+6, t.begin(), t.end()));
  199. }
  200. }
  201. }
  202. tr.section("Small<> can be constexpr");
  203. {
  204. constexpr ra::Small<int, 2, 2> a = {1, 2, 3, 4};
  205. using Va = int_c<int(a(1, 0))>;
  206. tr.test_eq(3, Va::value);
  207. using Vc = int_c<sum(a)>; // constexpr reduction!
  208. tr.test_eq(10, Vc::value);
  209. constexpr ra::Small<int> b = { 9 }; // needs std::fill
  210. using Vb = int_c<int(b)>;
  211. tr.test_eq(9, Vb::value);
  212. }
  213. tr.section("custom steps. List init is row-major regardless.");
  214. {
  215. auto test = [&tr](auto && a)
  216. {
  217. tr.test_eq(1, a(0, 0));
  218. tr.test_eq(2, a(0, 1));
  219. tr.test_eq(3, a(0, 2));
  220. tr.test_eq(4, a(1, 0));
  221. tr.test_eq(5, a(1, 1));
  222. tr.test_eq(6, a(1, 2));
  223. tr.test_eq(1, a(0)(0));
  224. tr.test_eq(2, a(0)(1));
  225. tr.test_eq(3, a(0)(2));
  226. tr.test_eq(4, a(1)(0));
  227. tr.test_eq(5, a(1)(1));
  228. tr.test_eq(6, a(1)(2));
  229. using A = std::decay_t<decltype(a(0))>;
  230. using dim1 = std::array<ra::dim_t, 1>;
  231. auto lens = ra::mp::tuple_values<ra::dim_t, typename A::lens>();
  232. auto steps = ra::mp::tuple_values<ra::dim_t, typename A::steps>();
  233. tr.test_eq(dim1 {3}, ra::start(lens));
  234. tr.test_eq(dim1 {2}, ra::start(steps));
  235. };
  236. ra::SmallArray<double, int_list<2, 3>, int_list<1, 2>> a { 1, 2, 3, 4, 5, 6 };
  237. ra::SmallArray<double, int_list<2, 3>, int_list<1, 2>> b { {1, 2, 3}, {4, 5, 6} };
  238. test(a);
  239. test(b);
  240. }
  241. tr.section("SmallArray converted to SmallView");
  242. {
  243. ra::Small<double, 2, 3> a { 1, 2, 3, 4, 5, 6 };
  244. ra::SmallView<double, int_list<2, 3>, int_list<3, 1>> b = a();
  245. tr.test_eq(a, b);
  246. // non-default steps (fortran / column major order).
  247. ra::SmallArray<double, int_list<2, 3>, int_list<1, 2>> ax { 1, 2, 3, 4, 5, 6 };
  248. ra::SmallView<double, int_list<2, 3>, int_list<1, 2>> bx = ax();
  249. tr.test_eq(a, ax);
  250. tr.test_eq(a, bx);
  251. // check iterators.
  252. tr.test(std::ranges::equal(a.begin(), a.end(), ax.begin(), ax.end()));
  253. tr.test(std::ranges::equal(ax.begin(), ax.end(), a.begin(), a.end()));
  254. tr.test(std::ranges::equal(b.begin(), b.end(), bx.begin(), bx.end()));
  255. tr.test(std::ranges::equal(bx.begin(), bx.end(), b.begin(), b.end()));
  256. // check memory order.
  257. double fcheck[6] = { 1, 4, 2, 5, 3, 6 };
  258. tr.test(std::equal(fcheck, fcheck+6, ax.data()));
  259. tr.test(std::equal(fcheck, fcheck+6, bx.data()));
  260. // views work as views.
  261. bx = 77.;
  262. tr.test_eq(77., ax);
  263. b = 99.;
  264. tr.test_eq(99., a);
  265. }
  266. tr.section("expr with Small, rank 1");
  267. {
  268. ra::Small<double, 3> a { 1, 4, 2 };
  269. tr.test_eq(3, a.iter().len(0));
  270. #define TEST(plier) \
  271. { \
  272. double s = 0; \
  273. plier(ra::expr([&s](double & a) { s += a; }, a.iter())); \
  274. tr.test_eq(7, s); \
  275. }
  276. TEST(ply_ravel);
  277. TEST(ply);
  278. #undef TEST
  279. }
  280. tr.section("expr with Small, rank 2");
  281. {
  282. ra::Small<double, 3, 2> a { 1, 4, 2, 5, 3, 6 };
  283. tr.test_eq(3, a.iter().len(0));
  284. tr.test_eq(2, a.iter().len(1));
  285. #define TEST(plier) \
  286. { \
  287. double s = 0; \
  288. plier(ra::expr([&s](double & a) { s += a; }, a.iter())); \
  289. tr.test_eq(21, s); \
  290. }
  291. TEST(ply_ravel);
  292. TEST(ply);
  293. #undef TEST
  294. #define TEST(plier) \
  295. { \
  296. ra::Small<double, 3, 2> b; \
  297. plier(ra::expr([](double & a, double & b) { b = -a; }, a.iter(), b.iter())); \
  298. tr.test_eq(-1, b(0, 0)); \
  299. tr.test_eq(-4, b(0, 1)); \
  300. tr.test_eq(-2, b(1, 0)); \
  301. tr.test_eq(-5, b(1, 1)); \
  302. tr.test_eq(-3, b(2, 0)); \
  303. tr.test_eq(-6, b(2, 1)); \
  304. }
  305. TEST(ply_ravel);
  306. TEST(ply);
  307. #undef TEST
  308. }
  309. tr.section("Small as value type in var-size array");
  310. {
  311. {
  312. // This pain with rank 0 arrays and ra::scalar can be avoided with ply; see e.g. grid_interp_n() in src/grid.cc.
  313. ra::Unique<ra::Small<double, 2>, 1> b({4}, ra::scalar(ra::Small<double, 2> { 3., 1. }));
  314. tr.test_eq(3., b(0)(0));
  315. tr.test_eq(1., b(0)(1));
  316. // if () returns rank 0 instead of scalar, otherwise ct error.
  317. // b(1) = ra::scalar(ra::Small<double, 2> { 7., 9. });
  318. // cout << b << endl;
  319. // if () returns scalar instead of rank 0, otherwise bug. (This is what happens).
  320. b(1) = ra::Small<double, 2> { 7., 9. };
  321. tr.test_eq(3., b(0)(0));
  322. tr.test_eq(1., b(0)(1));
  323. tr.test_eq(7., b(1)(0));
  324. tr.test_eq(9., b(1)(1));
  325. }
  326. {
  327. ra::Unique<double, 1> b({2}, { 3., 1. });
  328. tr.test_eq(3., b(0));
  329. tr.test_eq(1., b(1));
  330. b = ra::Small<double, 2> { 7., 9. };
  331. cout << b << endl;
  332. tr.test_eq(7., b(0));
  333. tr.test_eq(9., b(1));
  334. }
  335. {
  336. ra::Unique<double, 2> b({2, 2}, { 3., 1., 3., 1. });
  337. b(1) = ra::Small<double, 2> { 7., 9. };
  338. tr.test_eq(3., b(0, 0));
  339. tr.test_eq(1., b(0, 1));
  340. tr.test_eq(7., b(1, 0));
  341. tr.test_eq(9., b(1, 1));
  342. }
  343. {
  344. ra::Unique<ra::Small<double, 2>, 0> b(ra::scalar(ra::Small<double, 2>{3., 1.}));
  345. b = ra::scalar(ra::Small<double, 2> { 7., 9. });
  346. tr.test_eq(7., b()(0));
  347. tr.test_eq(9., b()(1));
  348. }
  349. {
  350. ra::Unique<ra::Small<double, 2>, 1> b({4}, ra::scalar(ra::Small<double, 2> { 3., 1. }));
  351. ra::Small<double, 2> u = b(1);
  352. tr.test_eq(3, u[0]);
  353. tr.test_eq(1, u[1]);
  354. ra::Small<double, 2> v(b(1));
  355. tr.test_eq(3, v[0]);
  356. tr.test_eq(1, v[1]);
  357. }
  358. }
  359. tr.section("transpose");
  360. {
  361. ra::Small<double, 2, 3> a { 1, 2, 3, 4, 5, 6 };
  362. tr.test_eq(ra::Small<double, 3, 2> { 1, 4, 2, 5, 3, 6 }, transpose<1, 0>(a));
  363. ra::transpose<1, 0>(a) = { 1, 2, 3, 4, 5, 6 };
  364. tr.test_eq(ra::Small<double, 2, 3> { 1, 3, 5, 2, 4, 6 }, a);
  365. }
  366. tr.section("diag");
  367. {
  368. ra::Small<double, 3, 3> a = ra::_0*3 + ra::_1;
  369. tr.test_eq(ra::Small<double, 3> { 0, 4, 8 }, diag(a));
  370. diag(a) = { 11, 22, 33 };
  371. tr.test_eq(ra::Small<double, 3, 3> { 11, 1, 2, 3, 22, 5, 6, 7, 33 }, a);
  372. }
  373. tr.section(".back()");
  374. {
  375. ra::Small<double, 3> a = ra::_0*3;
  376. tr.test_eq(0, a[0]);
  377. tr.test_eq(3, a[1]);
  378. tr.test_eq(6, a[2]);
  379. tr.test_eq(6, a.back());
  380. }
  381. // TODO Replace with uniform subscripting (ra::iota).
  382. tr.section("compile time subscripting of ra::Small (as)");
  383. {
  384. auto test_as = [&tr](auto && a, auto && b)
  385. {
  386. tr.test_eq(2, b.size());
  387. tr.test_eq(1, b[0]);
  388. tr.test_eq(2, b[1]);
  389. b = { 7, 8 };
  390. tr.test_eq(7, a[0]);
  391. tr.test_eq(8, a[1]);
  392. tr.test_eq(3, a[2]);
  393. };
  394. {
  395. ra::Small<double, 3> a = { 1, 2, 3 };
  396. test_as(a, a.as<2>());
  397. ra::Small<double, 6> b = { 1, 99, 2, 99, 3, 99 };
  398. ra::SmallView<double, int_list<3>, int_list<2>> c(b.data()); // TODO no syntax yet.
  399. test_as(c, c.as<2>());
  400. }
  401. auto test_fra = [&tr](auto && a, auto && b)
  402. {
  403. tr.test_eq(2, b.size());
  404. tr.test_eq(2, b[0]);
  405. tr.test_eq(3, b[1]);
  406. b = { 7, 8 };
  407. tr.test_eq(1, a[0]);
  408. tr.test_eq(7, a[1]);
  409. tr.test_eq(8, a[2]);
  410. };
  411. {
  412. ra::Small<double, 3> a = { 1, 2, 3 };
  413. test_fra(a, a.as<2, 1>());
  414. ra::Small<double, 6> b = { 1, 99, 2, 99, 3, 99 };
  415. ra::SmallView<double, int_list<3>, int_list<2>> c(b.data()); // TODO no syntax yet.
  416. test_fra(c, c.as<2, 1>());
  417. }
  418. auto test_fra_rank_2 = [&tr](auto && a, auto && b)
  419. {
  420. tr.test_eq(2, b.len(0));
  421. tr.test_eq(2, b.len(1));
  422. tr.test_eq(ra::Small<double, 2, 2> { 3, 4, 5, 6 }, b);
  423. b = ra::Small<double, 2, 2> { 13, 14, 15, 16 };
  424. tr.test_eq(ra::Small<double, 3, 2> { 1, 2, 13, 14, 15, 16 }, a);
  425. };
  426. {
  427. ra::Small<double, 3, 2> a = { 1, 2, 3, 4, 5, 6 };
  428. test_fra_rank_2(a, a.as<2, 1>());
  429. ra::Small<double, 6, 2> b = { 1, 2, 99, 99, 3, 4, 99, 99, 5, 6, 99, 99 };
  430. ra::SmallView<double, int_list<3, 2>, int_list<4, 1>> c(b.data()); // TODO no syntax yet.
  431. test_fra_rank_2(c, c.as<2, 1>());
  432. }
  433. }
  434. tr.section("cat");
  435. {
  436. tr.test_eq(ra::Small<int, 4> {1, 2, 3, 4}, cat(ra::Small<int, 3> {1, 2, 3}, 4));
  437. tr.test_eq(ra::Small<int, 4> {4, 1, 2, 3}, cat(4, ra::Small<int, 3> {1, 2, 3}));
  438. tr.test_eq(ra::Small<int, 5> {1, 2, 3, 4, 5}, cat(ra::Small<int, 2> {1, 2}, ra::Small<int, 3> {3, 4, 5}));
  439. }
  440. tr.section("a demo on rank1of1 vs rank2");
  441. {
  442. // by prefix matching, first dim is 2 for both so they get matched. Then {1 2}
  443. // (a 'scalar') gets matched to 10 & 20 in succesion. This used to be forbidden in Small::Small(X && x), but now I value consistency more.
  444. ra::Small<ra::Small<double, 2>, 2> a = { {1, 2}, {3, 4} };
  445. ra::Small<double, 2, 2> b = { 10, 20, 30, 40 };
  446. cout << "a: " << a << endl;
  447. cout << "b: " << b << endl;
  448. // a = b; // TODO Check that this static fails
  449. cout << "a = b, a: " << a << endl;
  450. }
  451. // ASSIGNOPS for SmallBase.iter()
  452. {
  453. ra::Small<int, 3> s {1, 2, 3};
  454. s.iter() += 9;
  455. tr.test_eq(ra::start({10, 11, 12}), s);
  456. }
  457. #if __cpp_multidimensional_subscript >= 202110L
  458. tr.section("multidimensional []");
  459. {
  460. ra::Small<int, 3, 2, 4> a = ra::_0 + ra::_1 - ra::_2;
  461. tr.test_eq(a(ra::all, 0), a[ra::all, 0]);
  462. }
  463. #endif
  464. tr.section("deduction guides");
  465. {
  466. ra::SmallArray a {1, 2, 3}; // FIXME the deduction guide can't work for ra::Small
  467. tr.test_eq(ra::start({1, 2, 3}), a);
  468. }
  469. return tr.summary();
  470. }