small-1.cc 20 KB

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