frame-new.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Test abilities of post v10 driverless frame matching Expr.
  3. // (c) Daniel Llorens - 2019-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 <iostream>
  9. #include <iterator>
  10. #include "ra/test.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder;
  12. template <int i> using UU = decltype(std::declval<ra::Unique<double, i>>().iter());
  13. using ra::int_c;
  14. int
  15. main()
  16. {
  17. TestRecorder tr(std::cout);
  18. tr.section("view");
  19. {
  20. ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  21. ra::Big<int, 4> b({2, 2, 3, 4}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  22. cout << a << endl;
  23. }
  24. tr.section("II");
  25. {
  26. ra::mp::int_list<6, 3, -4> x;
  27. constexpr int ma = ra::mp::fold_tuple(-99, x, [](auto && k, auto && a) { return max(k, std::decay_t<decltype(a)>::value); });
  28. constexpr int mi = ra::mp::fold_tuple(+99, x, [](auto && k, auto && a) { return min(k, std::decay_t<decltype(a)>::value); });
  29. constexpr int su = ra::mp::fold_tuple(0, x, [](auto && k, auto && a) { return k + std::decay_t<decltype(a)>::value; });
  30. cout << ma << endl;
  31. cout << mi << endl;
  32. cout << su << endl;
  33. }
  34. tr.section("static size - like Expr");
  35. {
  36. ra::Small<int, 2, 3, 4> a = (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1);
  37. ra::Small<int, 2, 3, 4, 5> b = (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1);
  38. #define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  39. tr.test_eq(4, EXPR.rank());
  40. tr.test_eq(b.len(0), EXPR.len(0));
  41. tr.test_eq(b.len(1), EXPR.len(1));
  42. tr.test_eq(b.len(2), EXPR.len(2));
  43. tr.test_eq(b.len(3), EXPR.len(3));
  44. tr.test_eq(2*3*4*5, size(EXPR));
  45. static_assert(4==ra::rank_s<decltype(EXPR)>());
  46. static_assert(b.len_s(0)==EXPR.len_s(0));
  47. static_assert(b.len_s(1)==EXPR.len_s(1));
  48. static_assert(b.len_s(2)==EXPR.len_s(2));
  49. static_assert(b.len_s(3)==EXPR.len_s(3));
  50. static_assert(2*3*4*5 == ra::size_s<decltype(EXPR)>());
  51. #undef EXPR
  52. }
  53. tr.section("check mismatches - static");
  54. {
  55. ra::Small<int, 2, 3, 4> a = (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1);
  56. ra::Small<int, 2, 4, 4, 5> b = (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1);
  57. // properly fails to compile, which we cannot check at present [ra42]
  58. // #define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  59. // tr.test_eq(2*3*4*5, ra::size_s<decltype(EXPR)>());
  60. // tr.test_eq(3, EXPR.len_s(1));
  61. // #undef EXPR
  62. // we can use non-static Match::check() as constexpr however.
  63. static_assert(!agree(a, b));
  64. }
  65. tr.section("static rank, dynamic size - like Expr");
  66. {
  67. ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  68. ra::Big<int, 4> b({2, 3, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  69. #define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  70. tr.test_eq(4, EXPR.rank());
  71. tr.test_eq(b.len(0), EXPR.len(0));
  72. tr.test_eq(b.len(1), EXPR.len(1));
  73. tr.test_eq(b.len(2), EXPR.len(2));
  74. tr.test_eq(b.len(3), EXPR.len(3));
  75. tr.test_eq(2*3*4*5, size(EXPR));
  76. // could check all statically through decltype, although Big cannot be constexpr yet.
  77. static_assert(4==ra::rank_s<decltype(EXPR)>());
  78. tr.test_eq(ra::ANY, EXPR.len_s(0));
  79. tr.test_eq(ra::ANY, EXPR.len_s(1));
  80. tr.test_eq(ra::ANY, EXPR.len_s(2));
  81. tr.test_eq(ra::ANY, EXPR.len_s(3));
  82. tr.test_eq(ra::ANY, ra::size_s<decltype(EXPR)>());
  83. cout << EXPR << endl;
  84. #undef EXPR
  85. }
  86. tr.section("check mismatches - dynamic (explicit)");
  87. {
  88. {
  89. ra::Big<int, 3> a({2, 3, 4}, 0);
  90. ra::Big<int, 4> b({2, 4, 4, 5}, 0);
  91. tr.test(!ra::agree(a, b));
  92. // TestRecorder sees mismatches as another kind of error, it used to happen this would RA_ASSERT instead.
  93. // FIXME This isn't true for static mismatches, which will fail to compile.
  94. tr.expectfail().test_eq(a, b);
  95. }
  96. {
  97. ra::Big<int, 3> a({2, 3, 4}, 0);
  98. ra::Big<int, 4> b({2, 3, 4, 5}, 0);
  99. tr.test(ra::agree(a, b));
  100. tr.test_eq(a, b);
  101. }
  102. }
  103. tr.section("dynamic rank - Expr driver selection is broken in this case.");
  104. {
  105. ra::Big<int, 3> as({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  106. ra::Big<int> ad({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  107. ra::Big<int, 4> bs({2, 3, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  108. ra::Big<int> bd({2, 3, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  109. #define EXPR(a, b) expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  110. auto test = [&tr](auto tag, auto && a, auto && b)
  111. {
  112. tr.section(tag);
  113. tr.test_eq(4, EXPR(a, b).rank());
  114. tr.info("0d").test_eq(b.len(0), EXPR(a, b).len(0));
  115. tr.test_eq(b.len(1), EXPR(a, b).len(1));
  116. tr.test_eq(b.len(2), EXPR(a, b).len(2));
  117. tr.test_eq(b.len(3), EXPR(a, b).len(3));
  118. tr.info("0-size()").test_eq(2*3*4*5, size(EXPR(a, b)));
  119. tr.test_eq(ra::ANY, ra::rank_s<decltype(EXPR(a, b))>());
  120. tr.test_eq(ra::ANY, ra::size_s<decltype(EXPR(a, b))>());
  121. tr.test_eq(ra::ANY, EXPR(a, b).len_s(0));
  122. tr.test_eq(ra::ANY, EXPR(a, b).len_s(1));
  123. tr.test_eq(ra::ANY, EXPR(a, b).len_s(2));
  124. tr.test_eq(ra::ANY, EXPR(a, b).len_s(3));
  125. tr.info("0-size_s()").test_eq(ra::ANY, ra::size_s<decltype(EXPR(a, b))>());
  126. };
  127. test("sta-dyn", as, bd);
  128. test("dyn-sta", ad, bs);
  129. test("dyn-dyn", ad, bd);
  130. #undef EXPR
  131. }
  132. tr.section("cases with periodic axes - dynamic (broken with Expr)");
  133. {
  134. ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  135. auto b = a(ra::all, ra::insert<1>, ra::iota(4, 0, 0));
  136. #define EXPR(a, b) expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  137. tr.test_eq(4, EXPR(a, b).rank());
  138. tr.test_eq(b.len(0), EXPR(a, b).len(0));
  139. tr.test_eq(a.len(1), EXPR(a, b).len(1));
  140. tr.test_eq(b.len(2), EXPR(a, b).len(2));
  141. tr.test_eq(b.len(3), EXPR(a, b).len(3));
  142. tr.test_eq(2*3*4*4, size(EXPR(a, b)));
  143. // could check all statically through decltype, although Big cannot be constexpr yet.
  144. static_assert(4==ra::rank_s<decltype(EXPR(a, b))>());
  145. tr.test_eq(ra::ANY, EXPR(a, b).len_s(0));
  146. tr.test_eq(ra::ANY, EXPR(a, b).len_s(1));
  147. tr.test_eq(ra::ANY, EXPR(a, b).len_s(2));
  148. tr.test_eq(ra::ANY, EXPR(a, b).len_s(3));
  149. tr.test_eq(ra::ANY, ra::size_s<decltype(EXPR(a, b))>());
  150. cout << EXPR(a, b) << endl;
  151. // value test.
  152. ra::Big<int, 4> c({2, 3, 4, 4}, 0);
  153. c(ra::all, 0) = a(ra::all, ra::iota(4, 0, 0));
  154. c(ra::all, 1) = a(ra::all, ra::iota(4, 0, 0));
  155. c(ra::all, 2) = a(ra::all, ra::iota(4, 0, 0));
  156. tr.test_eq((a+c), EXPR(a, b));
  157. // order doesn't affect prefix matching with Expr
  158. tr.test_eq((a+c), EXPR(b, a));
  159. #undef EXPR
  160. }
  161. tr.section("broadcasting - like outer product");
  162. {
  163. ra::Big<int, 2> a({4, 3}, 10*ra::_1+100*ra::_0);
  164. ra::Big<int, 1> b({5}, ra::_0);
  165. cout << ra::start(ra::shape(from([](auto && a, auto && b) { return a-b; }, a, b))) << endl;
  166. #define EXPR(a, b) expr([](auto && a, auto && b) { return a-b; }, start(a(ra::dots<2>, ra::insert<1>)), start(b(ra::insert<2>, ra::dots<1>)))
  167. tr.test_eq(3, ra::rank_s<decltype(EXPR(a, b))>());
  168. tr.test_eq(ra::ANY, EXPR(a, b).len_s(0));
  169. tr.test_eq(ra::ANY, EXPR(a, b).len_s(1));
  170. tr.test_eq(ra::ANY, EXPR(a, b).len_s(2));
  171. tr.test_eq(3, EXPR(a, b).rank());
  172. tr.test_eq(4, EXPR(a, b).len(0));
  173. tr.test_eq(3, EXPR(a, b).len(1));
  174. tr.test_eq(5, EXPR(a, b).len(2));
  175. tr.test_eq(from([](auto && a, auto && b) { return a-b; }, a, b), EXPR(a, b));
  176. #undef EXPR
  177. }
  178. tr.section("Expr has operatorX=");
  179. {
  180. ra::Big<int, 2> a({4, 3}, 10*ra::_1+100*ra::_0);
  181. expr([](auto & a) -> decltype(auto) { return a; }, start(a)) += 1;
  182. tr.test_eq(10*ra::_1 + 100*ra::_0 + 1, a);
  183. }
  184. tr.section("Compat with old Expr, from ra-0.cc");
  185. {
  186. int p[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  187. int * pp = &p[0]; // force pointer decay in case we ever enforce p's shape
  188. ra::View<int> d(ra::pack<ra::Dim>(ra::Small<int, 3> {5, 1, 2}, ra::Small<int, 3> {1, 0, 5}), pp);
  189. #define EXPR expr([](auto && a, auto && b) { return a==b; }, ra::_0*1 + ra::_1*0 + ra::_2*5 + 1, start(d))
  190. tr.test(every(EXPR));
  191. auto x = EXPR;
  192. static_assert(ra::ANY==ra::size_s<decltype(x)>());
  193. static_assert(ra::ANY==ra::size_s<decltype(x)>());
  194. tr.test_eq(10, size(EXPR));
  195. }
  196. tr.section("BAD on any len_s(k) means size_s() is BAD");
  197. {
  198. using order = ra::mp::int_list<0, 1>;
  199. using T0 = ra::Expr<std::multiplies<void>, std::tuple<decltype(ra::iota<0>()), ra::Scalar<int>>, order>;
  200. ra::dim_t s0 = ra::size_s<T0>();
  201. using T1 = ra::Expr<std::multiplies<void>, std::tuple<decltype(ra::iota<1>()), ra::Scalar<int>>, order>;
  202. ra::dim_t s1 = ra::size_s<T1>();
  203. using T2 = ra::Expr<std::multiplies<void>, std::tuple<decltype(ra::iota<2>()), ra::Scalar<int>>, order>;
  204. ra::dim_t s2 = ra::size_s<T2>();
  205. tr.test_eq(ra::BAD, s0);
  206. tr.test_eq(ra::BAD, s1);
  207. tr.test_eq(ra::BAD, s2);
  208. }
  209. return tr.summary();
  210. }