frame-old.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file frame-old.cc
  3. /// @brief Frame-matching tests for pre v10 Expr, previously in test/ra-0.cc.
  4. // (c) Daniel Llorens - 2013-2014, 2019
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iterator>
  11. #include <numeric>
  12. #include "ra/format.hh"
  13. // -------------------------------------
  14. // bit from example/throw.cc which FIXME should be easier. Maybe an option in ra/macros.hh.
  15. struct ra_error: public std::exception
  16. {
  17. std::string s;
  18. template <class ... A> ra_error(A && ... a): s(ra::format(std::forward<A>(a) ...)) {}
  19. virtual char const * what() const throw ()
  20. {
  21. return s.c_str();
  22. }
  23. };
  24. #ifdef RA_ASSERT
  25. #error RA_ASSERT is already defined!
  26. #endif
  27. #define RA_ASSERT( cond, ... ) \
  28. { if (!( cond )) throw ra_error("ra:: assert [" STRINGIZE(cond) "]", ##__VA_ARGS__); }
  29. // -------------------------------------
  30. #include "ra/complex.hh"
  31. #include "ra/test.hh"
  32. #include "ra/big.hh"
  33. using std::cout, std::endl, std::flush, std::string, ra::TestRecorder;
  34. using real = double;
  35. int main()
  36. {
  37. TestRecorder tr;
  38. tr.section("frame matching - TensorIndex/Scalar");
  39. {
  40. // driver is highest rank, which is ra::_0 (1).
  41. constexpr auto e = ra::_0+1;
  42. static_assert(e.rank_s()==1, "bad rank_s");
  43. static_assert(e.rank()==1, "bad rank");
  44. static_assert(e.size_s(0)==ra::DIM_BAD, "bad size");
  45. }
  46. tr.section("frame matching - Unique/TensorIndex");
  47. {
  48. ra::Unique<real, 2> c({3, 2}, ra::none);
  49. ra::Unique<real, 2> a({3, 2}, ra::none);
  50. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  51. std::iota(a.begin(), a.end(), 1);
  52. std::fill(c.begin(), c.end(), 0);
  53. ply(expr([](real & c, real a, int b) { c = a-(b+1); },
  54. c.iter(), a.iter(), ra::start(ra::_0)));
  55. tr.test_eq(check, c);
  56. std::fill(c.begin(), c.end(), 0);
  57. ply(expr([](real & c, int a, real b) { c = b-(a+1); },
  58. c.iter(), ra::start(ra::_0), a.iter()));
  59. tr.test_eq(check, c);
  60. }
  61. tr.section("frame matching - Unique/TensorIndex - TensorIndex can't be driving arg");
  62. {
  63. ra::Unique<real, 2> c({3, 2}, ra::none);
  64. ra::Unique<real, 2> a({3, 2}, ra::none);
  65. real check[3][2] = { {0, 0}, {2, 2}, {4, 4} };
  66. std::iota(a.begin(), a.end(), 1);
  67. std::fill(c.begin(), c.end(), 0);
  68. ply(expr([](real a, int b, real & c) { c = a-(b+1); },
  69. a.iter(), ra::start(ra::_1), c.iter()));
  70. tr.test_eq(check, c);
  71. std::fill(c.begin(), c.end(), 0);
  72. ply(expr([](int a, real b, real & c) { c = b-(a+1); },
  73. ra::start(ra::_1), a.iter(), c.iter()));
  74. tr.test_eq(check, c);
  75. }
  76. #define TEST(plier) \
  77. std::fill(c.begin(), c.end(), 0); \
  78. plier(expr([](real & c, real a, real b) { c = a-b; }, \
  79. c.iter(), a.iter(), b.iter())); \
  80. tr.test_eq(check, c); \
  81. \
  82. std::fill(c.begin(), c.end(), 0); \
  83. plier(expr([](real & c, real a, real b) { c = b-a; }, \
  84. c.iter(), b.iter(), a.iter())); \
  85. tr.test_eq(check, c);
  86. tr.section("frame matching - Unique/Unique");
  87. {
  88. ra::Unique<real, 2> c({3, 2}, ra::none);
  89. ra::Unique<real, 2> a({3, 2}, ra::none);
  90. ra::Unique<real, 1> b({3}, ra::none);
  91. std::iota(a.begin(), a.end(), 1);
  92. std::iota(b.begin(), b.end(), 1);
  93. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  94. TEST(ply_ravel);
  95. }
  96. tr.section("frame matching - Unique/Small");
  97. {
  98. ra::Unique<real, 2> c({3, 2}, ra::none);
  99. ra::Unique<real, 2> a({3, 2}, ra::none);
  100. ra::Small<real, 3> b;
  101. std::iota(a.begin(), a.end(), 1);
  102. std::iota(b.begin(), b.end(), 1);
  103. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  104. TEST(ply_ravel);
  105. }
  106. tr.section("frame matching - Small/Small");
  107. {
  108. ra::Small<real, 3, 2> c;
  109. ra::Small<real, 3, 2> a;
  110. ra::Small<real, 3> b;
  111. std::iota(a.begin(), a.end(), 1);
  112. std::iota(b.begin(), b.end(), 1);
  113. real check[3][2] = { {0, 1}, {1, 2}, {2, 3} };
  114. TEST(ply_ravel);
  115. }
  116. #undef TEST
  117. tr.section("frame match is good only for full expr, so test on ply, not construction");
  118. {
  119. ra::Unique<real, 2> a({2, 2}, 0.);
  120. ra::Unique<real, 1> b {1., 2.};
  121. // note that b-c has no driver, but all that matters is that the full expression does.
  122. auto e = expr([](real & a, real bc) { a = bc; },
  123. a.iter(), expr([](real b, real c) { return b-c; }, b.iter(), ra::start(ra::_1)));
  124. ply(e);
  125. tr.test_eq(1, a(0, 0));
  126. tr.test_eq(0, a(0, 1));
  127. tr.test_eq(2, a(1, 0));
  128. tr.test_eq(1, a(1, 1));
  129. }
  130. #define EXPR ra::expr(plus2real_print, a.iter(), b.iter())
  131. tr.section("frame matching should-be-error cases");
  132. {
  133. ra::Unique<real, 1> a({3}, 10);
  134. ra::Unique<real, 1> b({4}, 1);
  135. auto plus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  136. int error = 0;
  137. string s;
  138. try {
  139. tr.info("dynamic test is needed").test_eq(1, ra::check_expr_s<decltype(EXPR)>());
  140. ply_ravel(EXPR);
  141. } catch (ra_error & e) {
  142. error = 1;
  143. s = e.s;
  144. }
  145. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  146. }
  147. tr.section("frame matching should-be-error cases - dynamic rank");
  148. {
  149. ra::Unique<real> a({3}, 10);
  150. ra::Unique<real> b({4}, 1);
  151. auto plus2real_print = [](real a, real b) { cout << (a - b) << " "; };
  152. int error = 0;
  153. string s;
  154. try {
  155. std::cout << "A: " << a.iter().size(0) << endl;
  156. std::cout << "B: " << b.iter().size(0) << endl;
  157. tr.info("dynamic test is needed").test_eq(1, ra::check_expr_s<decltype(EXPR)>());
  158. ply_ravel(EXPR);
  159. } catch (ra_error & e) {
  160. error = 1;
  161. s = e.s;
  162. }
  163. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  164. }
  165. #undef EXPR
  166. tr.section("unintiuitive behavior [ra33]");
  167. {
  168. ra::Big<int, 1> i = {0, 1, 2};
  169. ra::Big<double, 2> A({3, 2}, ra::_0 - ra::_1);
  170. ra::Big<double, 2> F({3, 2}, 0.);
  171. iter<-1>(F) = A(i); // A(i) returns a nested expression. FIXME Should it?
  172. tr.test_eq(A, F);
  173. }
  174. return tr.summary();
  175. }