ra-2.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Positive cell rank.
  3. // (c) Daniel Llorens - 2013, 2014
  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. using std::cout, std::endl, std::flush, ra::TestRecorder;
  13. using real = double;
  14. int main()
  15. {
  16. TestRecorder tr;
  17. tr.section("iterators on cell rank > 0");
  18. {
  19. ra::Unique<real, 2> a({4, 3}, ra::none);
  20. std::iota(a.begin(), a.end(), 1);
  21. {
  22. auto i = a.iter<1>();
  23. tr.test_eq(1, i.rank());
  24. ply_ravel(expr([](ra::ViewBig<real, 1> const & x) { cout << x << endl; }, i));
  25. }
  26. }
  27. #define ARGa iter<1>(a)
  28. #define ARGi ra::Small<int, 4> {1, 2, 3, 4}.iter()
  29. #define ARGd dump.iter()
  30. tr.section("ply on cell rank > 0");
  31. {
  32. ra::Unique<real, 2> a({4, 3}, ra::none);
  33. std::iota(a.begin(), a.end(), 1);
  34. real check[4] = {6, 15, 24, 33};
  35. tr.section("not driving");
  36. {
  37. auto f = [](int i, ra::ViewBig<real, 1> const & a, real & d)
  38. {
  39. cout << i << ": " << a << endl;
  40. d = a[0] + a[1] + a[2];
  41. };
  42. ra::Small<real, 4> dump;
  43. // plier(ra::expr(f, a.iter<0>(), ARGa, ARGd)); // how the hell does this work,
  44. #define TEST(plier) \
  45. dump = 0; \
  46. cout << "-> explicit iterator" << endl; \
  47. plier(ra::expr(f, ARGi, ARGa, ARGd)); \
  48. tr.test(std::equal(check, check+4, dump.begin())); \
  49. dump = 0; \
  50. cout << "-> iter<cell rank>()" << endl; \
  51. plier(ra::expr(f, ARGi, a.iter<1>(), ARGd)); \
  52. tr.test(std::equal(check, check+4, dump.begin())); \
  53. dump = 0; \
  54. cout << "-> iter<frame rank>()" << endl; \
  55. plier(ra::expr(f, ARGi, a.iter<-1>(), ARGd)); \
  56. tr.test(std::equal(check, check+4, dump.begin()));
  57. TEST(ply_ravel);
  58. TEST(ply_fixed);
  59. #undef TEST
  60. }
  61. // TODO Use explicit DRIVER arg to ra::expr; the fixed size ARGi should always drive.
  62. tr.section("driving");
  63. {
  64. auto f = [](ra::ViewBig<real, 1> const & a, int i, real & d)
  65. {
  66. cout << i << ": " << a << endl;
  67. d = a[0] + a[1] + a[2];
  68. };
  69. ra::Small<real, 4> dump;
  70. #define TEST(plier) \
  71. dump = 0; \
  72. plier(ra::expr(f, ARGa, ARGi, ARGd)); \
  73. tr.test(std::equal(check, check+4, dump.begin())); \
  74. dump = 0; \
  75. plier(ra::expr(f, a.iter<1>(), ARGi, ARGd)); \
  76. tr.test(std::equal(check, check+4, dump.begin())); \
  77. dump = 0; \
  78. plier(ra::expr(f, a.iter<-1>(), ARGi, ARGd)); \
  79. tr.test(std::equal(check, check+4, dump.begin()));
  80. TEST(ply_ravel);
  81. TEST(ply_fixed);
  82. #undef TEST
  83. }
  84. }
  85. // Higher level tests are in test/iterator-small.cc (FIXME how about we square them).
  86. tr.section("ply on cell rank > 0, ref argument");
  87. {
  88. auto test_cell_rank_positive =
  89. [&](auto && a)
  90. {
  91. ra::Small<real, 4> dump { 1, 2, 3, 4 };
  92. real check[12] = {1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6};
  93. tr.section("not driving");
  94. {
  95. auto f = [](int i, auto && a, int d) { for (auto & ai: a) { ai = d; ++d; } };
  96. std::fill(a.begin(), a.end(), 0);
  97. ply(ra::expr(f, ARGi, ARGa, ARGd));
  98. tr.test(std::equal(check, check+12, a.begin()));
  99. std::fill(a.begin(), a.end(), 0);
  100. ply_ravel(ra::expr(f, ARGi, ARGa, ARGd));
  101. tr.test(std::equal(check, check+12, a.begin()));
  102. }
  103. tr.section("driving");
  104. {
  105. auto f = [](auto && a, int i, int d) { for (auto & ai: a) { ai = d; ++d; } };
  106. std::fill(a.begin(), a.end(), 0);
  107. ply(ra::expr(f, ARGa, ARGi, ARGd));
  108. tr.test(std::equal(check, check+12, a.begin()));
  109. std::fill(a.begin(), a.end(), 0);
  110. ply_ravel(ra::expr(f, ARGa, ARGi, ARGd));
  111. tr.test(std::equal(check, check+12, a.begin()));
  112. }
  113. };
  114. test_cell_rank_positive(ra::Unique<real, 2>({4, 3}, ra::none));
  115. test_cell_rank_positive(ra::Small<real, 4, 3> {}); // FIXME maybe ra::none should also work for Small
  116. }
  117. tr.section("ply on cell rank = 0 using iter<-1>, ref argument");
  118. {
  119. ra::Small<real, 3> dump { 1, 2, 3 };
  120. ra::Unique<real, 1> a({3}, ra::none);
  121. real check[3] = {1, 2, 3};
  122. tr.section("driving");
  123. {
  124. auto f = [](real & a, real d) { a = d; };
  125. std::fill(a.begin(), a.end(), 0);
  126. ply(map(f, a.iter<-1>(), dump.iter<0>()));
  127. tr.test(std::equal(check, check+3, a.begin()));
  128. std::fill(a.begin(), a.end(), 0);
  129. ply_ravel(map(f, a.iter<-1>(), dump.iter<0>()));
  130. tr.test(std::equal(check, check+3, a.begin()));
  131. }
  132. }
  133. tr.section("FYI");
  134. {
  135. cout << "..." << sizeof(ra::ViewBig<real, 0>) << endl;
  136. cout << "..." << sizeof(ra::ViewBig<real, 1>) << endl;
  137. }
  138. tr.section("ply on cell rank > 0, dynamic rank");
  139. {
  140. ra::Big<int> ad({5, 2}, ra::_0 - ra::_1);
  141. ra::Big<int, 2> as({5, 2}, ra::_0 - ra::_1);
  142. ra::Big<int, 2> b({5, 2}, (ra::_0 - ra::_1)*2);
  143. tr.test_eq(1, as.iter<-1>().rank());
  144. auto cellr = as.iter<-1>().cellr; tr.test_eq(1, cellr);
  145. tr.test_eq(1, ad.iter<-1>().rank());
  146. tr.test_eq(ra::ANY, ra::rank_s<decltype(ad.iter<-1>())>());
  147. auto e = ra::expr([](auto const & a, auto const & b) { cout << (b-2*a) << endl; },
  148. ad.iter<-1>(), b.iter<-1>());
  149. tr.test_eq(1, e.rank());
  150. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  151. as.iter<-1>(), b.iter<-1>()));
  152. tr.test_eq(0., map([](auto const & a, auto const & b) { return sum(abs(b-2*a)); },
  153. ad.iter<-1>(), b.iter<-1>()));
  154. }
  155. tr.section("FIXME ply_ravel with CellBig on VAR_RANK array [ra40]");
  156. {
  157. ra::Big<int> ad({5, 2}, ra::_0 - ra::_1);
  158. auto ii = iter<1>(ad);
  159. auto ee = map([&](auto && a) { tr.test_eq(1, a.rank()); }, ii);
  160. ply(ee);
  161. }
  162. return tr.summary();
  163. }