ra-1.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Fundamental tests.
  3. // (c) Daniel Llorens - 2013-2023
  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. #include <numeric>
  9. #include <iostream>
  10. #include <iterator>
  11. #include "ra/test.hh"
  12. #include "ra/complex.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. using A2 = ra::Unique<int, 2>;
  15. using A1 = ra::Unique<int, 1>;
  16. using int3 = ra::Small<int, 3>;
  17. using int2 = ra::Small<int, 2>;
  18. using std_int3 = std::array<int, 3>;
  19. using std_int2 = std::array<int, 2>;
  20. using ra::mp::int_list;
  21. template <class AA>
  22. void CheckPlyReverse1(TestRecorder & tr, AA && a)
  23. {
  24. std::iota(a.begin(), a.end(), 1);
  25. auto invert = [](int & a) { a = -a; return a; };
  26. ply_ravel(ra::expr(invert, a.iter()));
  27. for (int i=0; i<6; ++i) {
  28. tr.test_eq(-(i+1), a(i));
  29. }
  30. auto b = reverse(a, 0);
  31. ply_ravel(ra::expr(invert, b.iter()));
  32. for (int i=0; i<6; ++i) {
  33. tr.test_eq(6-i, b(i));
  34. tr.test_eq(i+1, a(i));
  35. }
  36. }
  37. template <class CC, class AA, class BB>
  38. void CheckPly(TestRecorder & tr, char const * tag, AA && A, BB && B)
  39. {
  40. // need to slice because B may be Unique (!) and I have left own-type constructors as default on purpose. Here, I need C's contents to be a fresh copy of B's.
  41. CC C(B());
  42. auto sub = [](int & b, int const a) -> int { return b -= a; };
  43. ra::ply_ravel(ra::expr(sub, B.iter(), A.iter()));
  44. for (int i=0; i!=A.len(0); ++i) {
  45. for (int j=0; j!=A.len(1); ++j) {
  46. tr.info(tag, " ravel").test_eq(C(i, j)-A(i, j), B(i, j));
  47. }
  48. }
  49. auto add = [](int & b, int const a) -> int { return b += a; };
  50. ra::ply_ravel(ra::expr(add, B.iter(), A.iter()));
  51. ra::ply(ra::expr(sub, B.iter(), A.iter()));
  52. for (int i=0; i!=A.len(0); ++i) {
  53. for (int j=0; j!=A.len(1); ++j) {
  54. tr.info(tag, " index").test_eq(C(i, j)-A(i, j), B(i, j));
  55. }
  56. }
  57. }
  58. using complex = std::complex<double>;
  59. int main()
  60. {
  61. TestRecorder tr(std::cout);
  62. tr.section("nested, with references, ply or ply_ravel");
  63. {
  64. int check[3] = {0, 2, 4};
  65. ra::Small<int, 3> A {1, 0, -1};
  66. ra::Small<int, 3> B {1, 2, 3};
  67. #define TEST(plier) \
  68. [&tr, &A, &B, check](auto && C) \
  69. { \
  70. std::fill(C.begin(), C.end(), -99); \
  71. plier(ra::expr([](int & k, int const i) { k = -i; }, \
  72. C.iter(), \
  73. ra::expr([](int const i, int const j) { return i-j; }, \
  74. A.iter(), B.iter()))); \
  75. tr.test(std::equal(check, check+3, C.begin())); \
  76. }
  77. #define TEST2(plier) \
  78. TEST(plier)(ra::Small<int, 3> {}); \
  79. TEST(plier)(ra::Unique<int, 1>({3}, ra::none));
  80. TEST2(ply_ravel)
  81. TEST2(ply_fixed)
  82. #undef TEST2
  83. #undef TEST
  84. }
  85. tr.section("with ref terms only");
  86. {
  87. #define TEST(plier, Biter, Citer) \
  88. [&tr](auto && B, auto && C) \
  89. { \
  90. plier(ra::expr([](int & k, int const i, int const j) { k = i+j; return k; }, \
  91. Citer, Biter, Biter)); \
  92. tr.test_eq(2, C[0]); \
  93. tr.test_eq(4, C[1]); \
  94. tr.test_eq(6, C[2]); \
  95. }
  96. #define TEST2(plier) \
  97. TEST(plier, B.iter(), C.iter())(int3 { 1, 2, 3 }, int3 { 77, 88, 99 }); \
  98. TEST(plier, ra::ptr(B), ra::ptr(C))(std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  99. TEST2(ply_ravel)
  100. TEST2(ply_fixed)
  101. #undef TEST2
  102. #undef TEST
  103. }
  104. tr.section("with ref & value terms");
  105. {
  106. #define TEST(plier, Biter, Citer, Btemp) \
  107. [&tr](auto && B, auto && C) \
  108. { \
  109. plier(ra::expr([](int & k, int const i, int const j) { k = i*j; return k; }, \
  110. Citer, Btemp, Biter)); \
  111. tr.test_eq(1, C[0]); \
  112. tr.test_eq(4, C[1]); \
  113. tr.test_eq(9, C[2]); \
  114. }
  115. TEST(ply_ravel, B.iter(), C.iter(), (int3 {1, 2, 3}.iter()))(int3 { 1, 2, 3 }, int3 { 77, 88, 99 });
  116. TEST(ply_ravel, ra::ptr(B), ra::ptr(C), ra::ptr(std_int3 {{1, 2, 3}}))
  117. (std_int3 {{ 1, 2, 3 }}, std_int3 {{ 77, 88, 99 }});
  118. #undef TEST
  119. }
  120. tr.section("complex or nested types");
  121. {
  122. using A2of2 = ra::Unique<int2, 2>;
  123. auto sum2 = [](int2 const i, int2 const j, int2 & x) { x = { i[0]+j[0], i[1]+j[1] }; };
  124. A2of2 A({2, 3}, { int2{1,1}, int2{2,2}, int2{3,3}, int2{4,4}, int2{5,5}, int2{6,6} });
  125. ply(ra::expr([](int2 & a, int i, int j) { int k = i*3+j; a = {k, k}; },
  126. A.iter(), ra::iota<0>(), ra::iota<1>()));
  127. A2of2 B({2, 3}, ra::scalar(int2 {0, 0}));
  128. cout << "A: " << A << endl;
  129. cout << "B: " << B << endl;
  130. cout << "\ntraverse_index..." << endl;
  131. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  132. ply(ra::expr(sum2, A.iter(), ra::scalar(int2{2, 2}), B.iter()));
  133. cout << B << endl;
  134. for (int i=2; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  135. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  136. ply(ra::expr(sum2, ra::scalar(int2{3, 3}), A.iter(), B.iter()));
  137. cout << B << endl;
  138. for (int i=3; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  139. cout << "\ntraverse..." << endl;
  140. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  141. ply_ravel(ra::expr(sum2, A.iter(), ra::scalar(int2{4, 5}), B.iter()));
  142. cout << B << endl;
  143. for (int i=4; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i+1, b[1]); ++i; }
  144. ply_ravel(ra::expr([](int2 & b) { b = {0, 0}; }, B.iter()));
  145. ply_ravel(ra::expr(sum2, ra::scalar(int2{5, 5}), A.iter(), B.iter()));
  146. cout << B << endl;
  147. for (int i=5; int2 & b: B) { tr.test_eq(i, b[0]); tr.test_eq(i, b[1]); ++i; }
  148. }
  149. tr.section("reversed arrays");
  150. {
  151. ra::Unique<int, 1> A({ 6 }, ra::none);
  152. std::iota(A.begin(), A.end(), 1);
  153. ra::Unique<int, 1> B { {6}, ra::scalar(99) };
  154. auto copy = [](int & b, int const a) { b = a; return b; };
  155. ply(ra::expr(copy, B.iter(), A.iter()));
  156. for (int i=0; i<6; ++i) {
  157. tr.test_eq(i+1, B(i));
  158. }
  159. ply(ra::expr(copy, B.iter(), reverse(A, 0).iter()));
  160. for (int i=0; i<6; ++i) {
  161. tr.test_eq(6-i, B(i));
  162. }
  163. }
  164. tr.section("reversed arrays, traverse, only one");
  165. {
  166. CheckPlyReverse1(tr, ra::Unique<int, 1>({ 6 }, ra::none));
  167. CheckPlyReverse1(tr, ra::Unique<int>({ 6 }, ra::none));
  168. }
  169. tr.section("mismatched steps");
  170. {
  171. auto sum2 = [](int a, int b, int & c) { return c = a-b; };
  172. A2 a = A2({2, 3}, ra::none); std::iota(a.begin(), a.end(), 1);
  173. A2 b = A2({3, 2}, ra::none); std::iota(b.begin(), b.end(), 1);
  174. A2 c = A2({2, 3}, ra::none);
  175. int check[6] = {0, -1, -2, 2, 1, 0};
  176. #define TEST(plier) \
  177. { \
  178. std::fill(c.begin(), c.end(), 0); \
  179. plier(ra::expr(sum2, a.iter(), transpose<1, 0>(b).iter(), c.iter())); \
  180. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  181. } \
  182. { \
  183. std::fill(c.begin(), c.end(), 0); \
  184. plier(ra::expr(sum2, transpose<1, 0>(a).iter(), b.iter(), transpose<1, 0>(c).iter())); \
  185. tr.info(STRINGIZE(plier)).test(std::equal(check, check+6, c.begin())); \
  186. }
  187. TEST(ply_ravel);
  188. TEST(ply_fixed);
  189. #undef TEST
  190. }
  191. tr.section("reverse 1/1 axis, traverse");
  192. #define TEST(plier) \
  193. { \
  194. A1 a({ 6 }, ra::none); \
  195. std::iota(a.begin(), a.end(), 1); \
  196. A1 b { {6}, ra::scalar(99) }; \
  197. auto copy = [](int & b, int const a) { b = a; }; \
  198. plier(ra::expr(copy, b.iter(), a.iter())); \
  199. cout << flush; \
  200. for (int i=0; i<6; ++i) { \
  201. tr.test_eq(i+1, b[i]); \
  202. } \
  203. plier(ra::expr(copy, b.iter(), reverse(a, 0).iter())); \
  204. for (int i=0; i<6; ++i) { \
  205. tr.test_eq(6-i, b(i)); \
  206. } \
  207. }
  208. TEST(ply_ravel)
  209. TEST(ply_fixed)
  210. #undef TEST
  211. tr.section("reverse (ref & non ref), traverse");
  212. {
  213. A2 A({2, 3}, { 1, 2, 3, 4, 5, 6 });
  214. A2 B({2, 3}, { 1, 2, 3, 4, 5, 6 });
  215. CheckPly<A2>(tr, "(a)", A, B);
  216. CheckPly<A2>(tr, "(b)", reverse(A, 0), B);
  217. CheckPly<A2>(tr, "(c)", A, reverse(B, 0));
  218. CheckPly<A2>(tr, "(d)", reverse(A, 0), reverse(B, 0));
  219. CheckPly<A2>(tr, "(e)", reverse(A, 1), B);
  220. CheckPly<A2>(tr, "(f)", A, reverse(B, 1));
  221. CheckPly<A2>(tr, "(g)", reverse(A, 1), reverse(B, 1));
  222. // When BOTH steps are negative, B is still compact and this can be reduced to a single loop.
  223. // TODO Enforce that the loop is linearized over both dimensions.
  224. CheckPly<A2>(tr, "(h)", A, reverse(reverse(B, 0), 1));
  225. CheckPly<A2>(tr, "(i)", reverse(reverse(A, 0), 1), B);
  226. CheckPly<A2>(tr, "(j)", reverse(reverse(A, 0), 1), reverse(reverse(B, 0), 1));
  227. }
  228. tr.section("reverse & transpose (ref & non ref), traverse");
  229. {
  230. using A2 = ra::Unique<int, 2>;
  231. A2 A({2, 2}, { 1, 2, 3, 4 });
  232. A2 B({2, 2}, { 1, 2, 3, 4 });
  233. CheckPly<A2>(tr, "(a)", transpose({1, 0}, A), B);
  234. CheckPly<A2>(tr, "(b)", A, transpose({1, 0}, B));
  235. CheckPly<A2>(tr, "(c)", reverse(reverse(transpose({1, 0}, A), 1), 0), B);
  236. CheckPly<A2>(tr, "(d)", A, reverse(reverse(transpose({1, 0}, B), 1), 0));
  237. CheckPly<A2>(tr, "(e)", transpose<1, 0>(A), B);
  238. CheckPly<A2>(tr, "(f)", A, transpose<1, 0>(B));
  239. CheckPly<A2>(tr, "(g)", reverse(reverse(transpose<1, 0>(A), 1), 0), B);
  240. CheckPly<A2>(tr, "(h)", A, reverse(reverse(transpose<1, 0>(B), 1), 0));
  241. CheckPly<A2>(tr, "(i)", transpose(int_list<1, 0>(), A), B);
  242. CheckPly<A2>(tr, "(j)", A, transpose(int_list<1, 0>(), B));
  243. CheckPly<A2>(tr, "(k)", reverse(reverse(transpose(int_list<1, 0>(), A), 1), 0), B);
  244. CheckPly<A2>(tr, "(l)", A, reverse(reverse(transpose(int_list<1, 0>(), B), 1), 0));
  245. }
  246. return tr.summary();
  247. }