small-0.C 16 KB

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