small-0.cc 17 KB

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