small-1.cc 20 KB

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