small-0.cc 17 KB

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