small-0.cc 17 KB

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