small-0.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file small-0.cc
  3. /// @brief Small constructors and assignment.
  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-1.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. struct test_type { int a; };
  19. int main()
  20. {
  21. TestRecorder tr;
  22. tr.section("basic");
  23. {
  24. tr.test(std::is_standard_layout_v<ra::Small<float, 2>>);
  25. }
  26. tr.section("BUG ambiguous case I");
  27. {
  28. double U[] = { 1, 2, 3 };
  29. ra::Small<ra::Big<double, 1>, 1> u = { {U} };
  30. tr.test_eq(U, u[0]);
  31. // std::cout << "---------" << std::endl;
  32. // ra::Small<ra::Big<double, 1>, 1> v = { U }; // BUG [ra45] (Big inits as empty, then assignment fails)
  33. // std::cout << "---------" << std::endl;
  34. // cout << v << endl;
  35. }
  36. // tr.section("BUG ambiguous case II");
  37. // {
  38. // ra::Small<int, 1, 2> a({ 1, 2 }); // BUG ambiguous [ra46]
  39. // // ra::Small<int, 1, 2> a { 1, 2 }; // works as ravel
  40. // // ra::Small<int, 1, 2> a {{ 1, 2 }}; // works
  41. // // ra::Small<int, 1, 2> a({{ 1, 2 }}); // works
  42. // tr.test_eq(1, a(0, 0));
  43. // tr.test_eq(2, a(0, 1));
  44. // }
  45. /*
  46. Small nested constructors rely on initialized_list so that we can size-check
  47. the arguments. This check has to be done at run time but I consider that
  48. preferable to the builtin array syntax which defect errors non-detectable
  49. (even though excess errors are compile time).
  50. Still, if we ever want to do that, it's good to know that we can construct
  51. higher rank builtin array types like this.
  52. */
  53. tr.section("= unbraced");
  54. {
  55. ra::Small<int, 2> a = {9, 3};
  56. tr.test_eq(9, a[0]);
  57. tr.test_eq(3, a[1]);
  58. }
  59. tr.section("basic facts about builtin arrays");
  60. {
  61. using A0 = double[3];
  62. using A1 = A0[2];
  63. using A2 = double[2][3];
  64. A1 x1 = {{1, 2, 3}, {4, 5, 6}};
  65. A2 x2 = {{1, 2, 3}, {4, 5, 6}};
  66. tr.test_eq(2u, std::rank<A1>::value);
  67. tr.test_eq(2, ra::start(x1).rank());
  68. tr.test_eq(2u, std::rank<A2>::value);
  69. tr.test_eq(2, ra::start(x2).rank());
  70. tr.test_eq(ra::start(x2), x1);
  71. }
  72. tr.section("top level generics on builtin arrays");
  73. {
  74. int a[3] = {1, 2, 3};
  75. tr.test_eq(1, ra::rank(a));
  76. tr.test_eq(3, ra::size(a));
  77. tr.test_eq(3, ra::shape(a)[0]);
  78. }
  79. tr.section("top level generics on builtin arrays II");
  80. {
  81. int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
  82. tr.test_eq(2, ra::rank(a));
  83. tr.test_eq(6, ra::size(a));
  84. tr.test_eq(2, ra::shape(a)[0]);
  85. tr.test_eq(3, ra::shape(a)[1]);
  86. }
  87. tr.section("scalar constructors");
  88. {
  89. tr.section("scalar paren unbraced");
  90. {
  91. ra::Small<int, 2> a(9);
  92. tr.test_eq(9, a[0]);
  93. tr.test_eq(9, a[1]);
  94. }
  95. tr.section("scalar op unbraced");
  96. {
  97. ra::Small<complex, 2> a = 9.;
  98. tr.test_eq(9., a[0]);
  99. tr.test_eq(9., a[1]);
  100. }
  101. tr.section("variants I wish didn't work :-/ just checking for regressions");
  102. {
  103. {
  104. ra::Small<int, 2> a({9});
  105. tr.test_eq(9, a[0]);
  106. tr.test_eq(9, a[1]);
  107. }
  108. {
  109. ra::Small<int, 2> a {9};
  110. tr.test_eq(9, a[0]);
  111. tr.test_eq(9, a[1]);
  112. }
  113. {
  114. ra::Small<complex, 2> a = {9.};
  115. tr.test_eq(9., a[0]);
  116. tr.test_eq(9., a[1]);
  117. }
  118. }
  119. }
  120. tr.section("empty");
  121. {
  122. ra::Small<int, 0> x;
  123. tr.test_eq(0, x.size());
  124. }
  125. tr.section("rank 0");
  126. {
  127. tr.section("default");
  128. {
  129. ra::Small<int> x;
  130. x = 3;
  131. tr.test_eq(3, x());
  132. }
  133. tr.section("rank 0 paren unbraced");
  134. {
  135. ra::Small<int> a(9);
  136. tr.test_eq(9, a());
  137. }
  138. tr.section("rank 0 paren braced");
  139. {
  140. ra::Small<int> a({9});
  141. tr.test_eq(9, a());
  142. }
  143. tr.section("rank 0 op unbraced");
  144. {
  145. ra::Small<int> a = 9;
  146. tr.test_eq(9, a());
  147. }
  148. tr.section("rank 0 op braced");
  149. {
  150. ra::Small<int> a = {9};
  151. tr.test_eq(9, a());
  152. }
  153. tr.section("rank 0 raw braced");
  154. {
  155. ra::Small<int> a {9};
  156. tr.test_eq(9, a());
  157. }
  158. }
  159. tr.section("rank 1");
  160. {
  161. tr.section("= unbraced");
  162. {
  163. ra::Small<int, 2> a = {9, 3};
  164. tr.test_eq(9, a[0]);
  165. tr.test_eq(3, a[1]);
  166. }
  167. tr.section("raw unbraced");
  168. {
  169. ra::Small<int, 2> a {9, 3};
  170. tr.test_eq(9, a[0]);
  171. tr.test_eq(3, a[1]);
  172. }
  173. tr.section("paren unbraced");
  174. {
  175. ra::Small<int, 2> a({9, 3});
  176. tr.test_eq(9, a[0]);
  177. tr.test_eq(3, a[1]);
  178. }
  179. tr.section("paren raw");
  180. {
  181. ra::Small<int, 2> a(9, 3);
  182. tr.test_eq(9, a[0]);
  183. tr.test_eq(3, a[1]);
  184. }
  185. tr.section("= scalar init");
  186. {
  187. ra::Small<int, 2> a = 9;
  188. tr.test_eq(9, a[0]);
  189. tr.test_eq(9, a[1]);
  190. }
  191. tr.section("= scalar init with non-ra::scalar type"); // [ra44]
  192. {
  193. ra::Small<test_type, 2> a = test_type { 9 };
  194. tr.test_eq(9, a[0].a);
  195. tr.test_eq(9, a[1].a);
  196. a = test_type { 3 };
  197. tr.test_eq(3, a[0].a);
  198. tr.test_eq(3, a[1].a);
  199. }
  200. // Braced versions used to work but now they don't. This is on purpose.
  201. // tr.section("= braced");
  202. // {
  203. // ra::Small<int, 2> a = {{9, 3}};
  204. // tr.test_eq(9, a[0]);
  205. // tr.test_eq(3, a[1]);
  206. // }
  207. // tr.section("raw braced");
  208. // {
  209. // ra::Small<int, 2> a {{9, 3}};
  210. // tr.test_eq(9, a[0]);
  211. // tr.test_eq(3, a[1]);
  212. // }
  213. // tr.section("paren braced");
  214. // {
  215. // ra::Small<int, 2> a({{9, 3}});
  216. // tr.test_eq(9, a[0]);
  217. // tr.test_eq(3, a[1]);
  218. // }
  219. }
  220. tr.section("rank 2");
  221. {
  222. tr.section("rank 2 (paren unun)");
  223. {
  224. ra::Small<double, 2, 2> a({1, 2}, {3, 4});
  225. tr.test_eq(1., a(0, 0));
  226. tr.test_eq(2., a(0, 1));
  227. tr.test_eq(3., a(1, 0));
  228. tr.test_eq(4., a(1, 1));
  229. }
  230. tr.section("rank 2 (paren un)");
  231. {
  232. ra::Small<double, 2, 2> a({{1, 2}, {3, 4}});
  233. tr.test_eq(1., a(0, 0));
  234. tr.test_eq(2., a(0, 1));
  235. tr.test_eq(3., a(1, 0));
  236. tr.test_eq(4., a(1, 1));
  237. }
  238. tr.section("rank 2 (= unbraced)");
  239. {
  240. ra::Small<double, 2, 2> a = {{1., 2.}, {3., 4.}};
  241. tr.test_eq(1., a(0, 0));
  242. tr.test_eq(2., a(0, 1));
  243. tr.test_eq(3., a(1, 0));
  244. tr.test_eq(4., a(1, 1));
  245. }
  246. tr.section("rank 2 (raw unbraced) I");
  247. {
  248. ra::Small<double, 2, 2> a {{1, 2}, {3, 4}};
  249. tr.test_eq(1., a(0, 0));
  250. tr.test_eq(2., a(0, 1));
  251. tr.test_eq(3., a(1, 0));
  252. tr.test_eq(4., a(1, 1));
  253. }
  254. tr.section("rank 2 (raw unbraced) II");
  255. {
  256. ra::Small<double, 2, 3> a {{1, 2, 3}, {4, 5, 6}};
  257. tr.test_eq(1., a(0, 0));
  258. tr.test_eq(2., a(0, 1));
  259. tr.test_eq(3., a(0, 2));
  260. tr.test_eq(4., a(1, 0));
  261. tr.test_eq(5., a(1, 1));
  262. tr.test_eq(6., a(1, 2));
  263. }
  264. tr.section("rank 2 (paren raw)");
  265. {
  266. ra::Small<double, 2, 2> a({1, 2}, {3, 4});
  267. tr.test_eq(1., a(0, 0));
  268. tr.test_eq(2., a(0, 1));
  269. tr.test_eq(3., a(1, 0));
  270. tr.test_eq(4., a(1, 1));
  271. }
  272. tr.section("size 1, higher rank");
  273. {
  274. ra::Small<int, 1, 1> a = {{4}}; // nested
  275. tr.test_eq(4, a(0, 0));
  276. ra::Small<int, 1, 1> b = {4}; // ravel
  277. tr.test_eq(4, b(0, 0));
  278. }
  279. tr.section("singular sizes I");
  280. {
  281. ra::Small<int, 1, 2> a = {{4, 3}}; // nested. This requires the nested constructor with size(0)==1.
  282. tr.test_eq(4, a(0, 0));
  283. tr.test_eq(3, a(0, 1));
  284. ra::Small<int, 1, 2> b = {4, 3}; // ravel
  285. tr.test_eq(4, b(0, 0));
  286. tr.test_eq(3, b(0, 1));
  287. ra::Small<int, 1, 2> c = {4}; // scalar
  288. tr.test_eq(4, c(0, 0));
  289. tr.test_eq(4, c(0, 1));
  290. // ra::Small<int, 1, 3> d = {4, 2}; // ravel // [ra42] FIXME cannot check ct errors yet
  291. // tr.test_eq(4, b(0, 0));
  292. // tr.test_eq(2, b(0, 1));
  293. }
  294. // Braced versions used to work but now they don't. This is on purpose.
  295. // tr.section("rank 2 (paren braced)");
  296. // {
  297. // ra::Small<double, 2, 2> a({{{1, 2}, {3, 4}}});
  298. // tr.test_eq(1., a(0, 0));
  299. // tr.test_eq(2., a(0, 1));
  300. // tr.test_eq(3., a(1, 0));
  301. // tr.test_eq(4., a(1, 1));
  302. // }
  303. // tr.section("rank 2 (= braced)");
  304. // {
  305. // ra::Small<double, 2, 2> a = {{{1, 2}, {3, 4}}};
  306. // tr.test_eq(1., a(0, 0));
  307. // tr.test_eq(2., a(0, 1));
  308. // tr.test_eq(3., a(1, 0));
  309. // tr.test_eq(4., a(1, 1));
  310. // }
  311. // tr.section("rank 2 (raw braced)");
  312. // {
  313. // ra::Small<double, 2, 2> a {{{1, 2}, {3, 4}}};
  314. // tr.test_eq(1., a(0, 0));
  315. // tr.test_eq(2., a(0, 1));
  316. // tr.test_eq(3., a(1, 0));
  317. // tr.test_eq(4., a(1, 1));
  318. // }
  319. }
  320. tr.section("higher rank, unbraced");
  321. {
  322. ra::Small<int, 2, 3, 4, 2> a = {{{{ 1, 2}, { 3, 4}, { 5, 6}, { 7, 8}},
  323. {{ 9, 10}, {11, 12}, {13, 14}, {15, 16}},
  324. {{17, 18}, {19, 20}, {21, 22}, {23, 24}}},
  325. {{{25, 26}, {27, 28}, {29, 30}, {31, 32}},
  326. {{33, 34}, {35, 36}, {37, 38}, {39, 40}},
  327. {{41, 42}, {43, 44}, {45, 46}, {47, 48}}}};
  328. // FIXME reshape for Small or something! Big/View have these things.
  329. ra::Small<int, 2, 3, 4, 2> b = 0;
  330. ra::SmallView<int, mp::int_list<48>, mp::int_list<1>> c(b.data());
  331. c = ra::iota(48, 1);
  332. tr.test_eq(b, a);
  333. }
  334. tr.section("free constructor");
  335. {
  336. ra::Small<int, 2, 2> a = {{1, 2}, ra::iota(2, 33)};
  337. tr.test_eq(1, a(0, 0));
  338. tr.test_eq(2, a(0, 1));
  339. tr.test_eq(33, a(1, 0));
  340. tr.test_eq(34, a(1, 1));
  341. }
  342. tr.section("raveling constructor");
  343. {
  344. ra::Small<int, 2, 2> a = {1, 2, 3, 4};
  345. tr.test_eq(1, a(0, 0));
  346. tr.test_eq(2, a(0, 1));
  347. tr.test_eq(3, a(1, 0));
  348. tr.test_eq(4, a(1, 1));
  349. }
  350. tr.section("raveling constructor from iterators");
  351. {
  352. int AA[4] = { 1, 2, 3, 4 };
  353. auto a = ra::ravel_from_iterators<ra::Small<int, 2, 2>>(AA, AA+4);
  354. tr.test_eq(1, a(0, 0));
  355. tr.test_eq(2, a(0, 1));
  356. tr.test_eq(3, a(1, 0));
  357. tr.test_eq(4, a(1, 1));
  358. }
  359. tr.section("nested Small I");
  360. {
  361. ra::Small<ra::Small<int, 2>, 2> a = {{1, 2}, {3, 4}};
  362. tr.test_eq(1, a(0)(0));
  363. tr.test_eq(2, a(0)(1));
  364. tr.test_eq(3, a(1)(0));
  365. tr.test_eq(4, a(1)(1));
  366. }
  367. tr.section("nested Small II");
  368. {
  369. auto test = [&](auto && a)
  370. {
  371. tr.test_eq(1, a(0)(0, 0));
  372. tr.test_eq(2, a(0)(0, 1));
  373. tr.test_eq(3, a(0)(0, 2));
  374. tr.test_eq(4, a(0)(1, 0));
  375. tr.test_eq(5, a(0)(1, 1));
  376. tr.test_eq(6, a(0)(1, 2));
  377. tr.test_eq(10, a(1)(0, 0));
  378. tr.test_eq(20, a(1)(0, 1));
  379. tr.test_eq(30, a(1)(0, 2));
  380. tr.test_eq(40, a(1)(1, 0));
  381. tr.test_eq(50, a(1)(1, 1));
  382. tr.test_eq(60, a(1)(1, 2));
  383. };
  384. using In = ra::Small<int, 2, 3>;
  385. ra::Small<In, 2> a = {{{1, 2, 3}, {4, 5, 6}}, {{10, 20, 30}, {40, 50, 60}}};
  386. test(a);
  387. ra::Small<In, 2> b = {In {{1, 2, 3}, {4, 5, 6}}, In {{10, 20, 30}, {40, 50, 60}}};
  388. test(b);
  389. int x[2][3] = {{1, 2, 3}, {4, 5, 6}};
  390. int y[2][3] = {{10, 20, 30}, {40, 50, 60}};
  391. ra::Small<In, 2> c = {x, y};
  392. test(c);
  393. }
  394. tr.section("operator=");
  395. {
  396. tr.section("operator= rank 1");
  397. {
  398. ra::Small<complex, 2> a { 3, 4 };
  399. a = complex(99.);
  400. tr.test_eq(99., a[0]);
  401. tr.test_eq(99., a[1]);
  402. a = 88.;
  403. tr.test_eq(88., a[0]);
  404. tr.test_eq(88., a[1]);
  405. a += 1.;
  406. tr.test_eq(89., a[0]);
  407. tr.test_eq(89., a[1]);
  408. }
  409. tr.section("operator= rank 2 unbraced");
  410. {
  411. ra::Small<int, 2, 2> a = 0;
  412. a = {{1, 2}, {3, 4}};
  413. tr.test_eq(1, a(0, 0));
  414. tr.test_eq(2, a(0, 1));
  415. tr.test_eq(3, a(1, 0));
  416. tr.test_eq(4, a(1, 1));
  417. }
  418. // tr.section("operator= rank 2 braced"); // Doesn't work but cannot check ct errors [ra42]
  419. // {
  420. // ra::Small<int, 2, 2> a = 0;
  421. // a = {{{1, 2}, {3, 4}}};
  422. // tr.test_eq(1, a(0, 0));
  423. // tr.test_eq(2, a(0, 1));
  424. // tr.test_eq(3, a(1, 0));
  425. // tr.test_eq(4, a(1, 1));
  426. // }
  427. tr.section("operator= of view into view");
  428. {
  429. {
  430. ra::Small<int, 2, 2> a = {{1, 2}, {3, 4}};
  431. ra::SmallView<int, mp::int_list<2>, mp::int_list<1>> a0 = a(0);
  432. ra::SmallView<int, mp::int_list<2>, mp::int_list<1>> a1 = a(1);
  433. a0 = a1;
  434. tr.test_eq(ra::Small<int, 2, 2> {{3, 4}, {3, 4}}, a);
  435. }
  436. {
  437. ra::Small<int, 2, 2> a = {{1, 2}, {3, 4}};
  438. ra::SmallView<int, mp::int_list<2>, mp::int_list<1>> a0 = a(0);
  439. a0 = a(1);
  440. tr.test_eq(ra::Small<int, 2, 2> {{3, 4}, {3, 4}}, a);
  441. }
  442. }
  443. tr.section("repeat a test from test/big-0 [ra38]");
  444. {
  445. ra::Small<double, 2> A = {1, 2};
  446. ra::Small<double, 2> X = {0, 0};
  447. X(ra::all) = A();
  448. tr.test_eq(ra::start({1, 2}), X);
  449. }
  450. }
  451. // These tests should fail at compile time. No way to check them yet [ra42].
  452. // tr.section("size checks");
  453. // {
  454. // ra::Small<int, 3> a = { 1, 2, 3 };
  455. // ra::Small<int, 2> b = { 4, 5 };
  456. // ra::Small<int, 3> c = a+b;
  457. // cout << c << endl;
  458. // }
  459. return tr.summary();
  460. }