small-1.C 20 KB

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