checks.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Runtime checks.
  3. // (c) Daniel Llorens - 2019-2024
  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. #if RA_DO_CHECK==0
  9. #undef RA_DO_CHECK
  10. #endif
  11. #define RA_DO_CHECK 1 // kind of the point here
  12. #include "ra/base.hh"
  13. #include <exception>
  14. // -------------------------------------
  15. // bit from example/throw.cc which FIXME maybe a prepared option.
  16. struct ra_error: public std::exception
  17. {
  18. std::string s;
  19. template <class ... A> ra_error(A && ... a): s(ra::format(std::forward<A>(a) ...)) {}
  20. virtual char const * what() const throw () { return s.c_str(); }
  21. };
  22. #define RA_ASSERT( cond, ... ) \
  23. { if (!( cond )) [[unlikely]] throw ra_error("ra::", std::source_location::current(), " (" STRINGIZE(cond) ") " __VA_OPT__(,) __VA_ARGS__); }
  24. // -------------------------------------
  25. #include "ra/test.hh"
  26. #include "mpdebug.hh"
  27. using std::cout, std::endl, std::flush, std::string, ra::TestRecorder;
  28. using ra::int_c, ra::mp::int_list;
  29. int main()
  30. {
  31. cout << "******* " << RA_DO_CHECK << "******" << endl;
  32. TestRecorder tr(std::cout);
  33. tr.section("bad cell rank");
  34. {
  35. bool yes = false;
  36. ra::Big<int> a0 = 0;
  37. std::string msg;
  38. try {
  39. std::cout << ra::iter<1>(a0) << std::endl;
  40. } catch (ra_error & e) {
  41. msg = e.what();
  42. yes = true;
  43. }
  44. tr.info(msg).test(yes);
  45. }
  46. // ------------------------------
  47. // see test/iota.cc
  48. // ------------------------------
  49. tr.section("ot of range with iota");
  50. {
  51. std::string msg;
  52. try {
  53. cout << ra::iota(10).at(std::array {11}) << endl;
  54. } catch (ra_error & e) {
  55. msg = e.what();
  56. }
  57. tr.info(msg).test(0<msg.size());
  58. }
  59. // ------------------------------
  60. // see test/fromb.cc
  61. // ------------------------------
  62. tr.section("out of range with iota subscripts");
  63. {
  64. std::string msg;
  65. try {
  66. ra::Small<int, 10> a = ra::_0;
  67. cout << a(ra::iota(ra::int_c<1>(), 10)) << endl;
  68. } catch (ra_error & e) {
  69. msg = e.what();
  70. }
  71. tr.info(msg).test(0<msg.size());
  72. }
  73. // ------------------------------
  74. // see test/compatibility.cc
  75. // ------------------------------
  76. tr.section("bad indices to ptr().at()");
  77. {
  78. int x = 0;
  79. try {
  80. int p[] = {10, 20, 30};
  81. tr.test_eq(p[2], ra::ptr(p).at(std::array {2}));
  82. x = 1;
  83. tr.test_eq(p[2], ra::ptr((int *)p, ra::int_c<2>{}).at(std::array {2}));
  84. x = 2;
  85. } catch (ra_error & e) {
  86. x = x+10;
  87. }
  88. tr.info("ptr.at()").test_eq(11, x);
  89. x = 0;
  90. try {
  91. int p[] = {10, 20, 30};
  92. tr.test_eq(p[2], ra::ptr((int *)p, 2).at(std::array {2}));
  93. x = 1;
  94. } catch (ra_error & e) {
  95. x = x+10;
  96. }
  97. tr.info("ptr.at()").test_eq(10, x);
  98. }
  99. // ------------------------------
  100. // see test/frame-new.cc
  101. // ------------------------------
  102. tr.section("dynamic (implicit) match");
  103. {
  104. ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  105. ra::Big<int, 4> b({2, 4, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  106. tr.test_eq(1, agree_s(a, b));
  107. tr.test_eq(0, agree(a, b));
  108. #define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  109. int x = 0;
  110. try {
  111. tr.test_eq(ra::ANY, EXPR.len_s(1));
  112. x = 1;
  113. } catch (ra_error & e) {
  114. }
  115. tr.test_eq(0, x);
  116. #undef EXPR
  117. }
  118. // If the size of an expr is static, dynamic checks may still need to be run if any of the terms of the expr has dynamic size. This is checked in Match::check_s().
  119. tr.section("static mismatch");
  120. {
  121. ra::Small<int, 2, 2> a;
  122. ra::Small<int, 3, 2> b;
  123. static_assert(0==agree_s(a, b));
  124. }
  125. tr.section("static match");
  126. {
  127. ra::Small<int, 2, 2> a;
  128. ra::Small<int, 2> b;
  129. static_assert(2==agree_s(a, b));
  130. }
  131. tr.section("static match with dynamic term");
  132. {
  133. ra::Small<int> a;
  134. ra::Big<int, 1> b({1}, 77);
  135. tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(2, agree_s(a, b));
  136. }
  137. tr.section("dynamic terms in match, static rank");
  138. {
  139. int error = 0;
  140. std::string s;
  141. try {
  142. ra::Small<int, 2> a {2, 3};
  143. ra::Big<int, 1> b({1}, 77);
  144. std::cout << "LEN " << decltype(a+b)::len_s(0) << std::endl;
  145. tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(1, decltype(a+b)::check_s());
  146. tr.test(!agree(a, b));
  147. a = b;
  148. } catch (ra_error & e) {
  149. error = 1;
  150. s = e.s;
  151. }
  152. tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
  153. }
  154. tr.section("dynamic terms in match, dynamic rank. See frame-new.cc");
  155. {
  156. auto f2 = [](ra::Big<int, 2> const & a) { cout << ra::start(shape(a)) << endl; };
  157. int error = 0;
  158. std::string s;
  159. try {
  160. ra::Big<int> a {};
  161. // flag the error when casting rank-0 to rank-2 array. FIXME check that copying is still possible.
  162. f2(a);
  163. error = 0;
  164. } catch (ra_error & e) {
  165. error = 1;
  166. s = e.s;
  167. }
  168. tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
  169. }
  170. // ------------------------------
  171. // see test/big-0.cc
  172. // ------------------------------
  173. tr.section("check rank errors with dynamic rank (I)");
  174. {
  175. int x = 0;
  176. try {
  177. ra::Big<int> a = 0;
  178. cout << shape(a, 0) << endl;
  179. x = 1;
  180. } catch (ra_error & e) {
  181. x = 2;
  182. }
  183. tr.info("caught error").test_eq(2, x);
  184. }
  185. tr.section("check rank errors with dynamic rank (II)");
  186. {
  187. int x = 0;
  188. // initialization is to rank 1, size 0.
  189. try {
  190. ra::Big<int> a;
  191. a = ra::Big<int, 1> { 1 };
  192. x = 1;
  193. } catch (ra_error & e) {
  194. x = 2;
  195. }
  196. tr.info("uninitialized dynamic rank").test_eq(2, x);
  197. }
  198. tr.section("iffy conversions");
  199. // won't fail until runtime bc val[0] might have size 1, but FIXME this is very likely a coding error.
  200. // FIXME shape doesn't show in the error message as it should.
  201. {
  202. ra::Big<double, 2> val = { { 7, 0, 0, 0.5, 1.5, 1, 1, 1 } };
  203. ra::Big<double, 2> bal = { { 7 }, { 0 } };
  204. double x = bal[0];
  205. tr.test_eq(7, x);
  206. int error = 0;
  207. string s;
  208. try {
  209. double x = val[0];
  210. cout << "x " << x << " (!)" << endl;
  211. } catch (ra_error & e) {
  212. error = 1;
  213. s = e.s;
  214. }
  215. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  216. }
  217. // ------------------------------
  218. // see test/frame-old.cc
  219. // ------------------------------
  220. #define EXPR ra::expr(plus2double_print, a.iter(), b.iter())
  221. tr.section("frame matching should-be-error cases. See frame-old.cc");
  222. {
  223. ra::Unique<double, 1> a({3}, 10);
  224. ra::Unique<double, 1> b({4}, 1);
  225. auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
  226. int error = 0;
  227. string s;
  228. try {
  229. tr.info("dynamic test is needed").test_eq(1, decltype(EXPR)::check_s());
  230. tr.test(!agree(a, b));
  231. ply_ravel(EXPR);
  232. } catch (ra_error & e) {
  233. error = 1;
  234. s = e.s;
  235. }
  236. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  237. }
  238. tr.section("frame matching should-be-error cases - dynamic rank. See frame-old.cc");
  239. {
  240. ra::Unique<double> a({3}, 10);
  241. ra::Unique<double> b({4}, 1);
  242. auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
  243. int error = 0;
  244. string s;
  245. try {
  246. std::cout << "A: " << a.iter().len(0) << endl;
  247. std::cout << "B: " << b.iter().len(0) << endl;
  248. tr.info("dynamic test is needed").test_eq(1, decltype(EXPR)::check_s());
  249. tr.test(!agree(a, b));
  250. ply_ravel(EXPR);
  251. } catch (ra_error & e) {
  252. error = 1;
  253. s = e.s;
  254. }
  255. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  256. }
  257. #undef EXPR
  258. return tr.summary();
  259. }