frame-new.cc 10 KB

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