checks.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #ifdef RA_DO_CHECK
  9. #undef RA_DO_CHECK
  10. #endif
  11. #define RA_DO_CHECK 1 // 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. // FIXME checking at Match isn't enough, and ply() doesn't check nicely
  101. // ------------------------------
  102. /*
  103. ra::Big<double> z(10, 0.);
  104. auto w = z(ra::insert<1>, ra::all);
  105. // causes abort() in ply(). Checks are skipped bc there's only one positive rank arg
  106. w *= -1;
  107. // causes abort() in ply(). Checks are skipped bc there's only one argument
  108. for_each([](auto & w) { w *= -1; }, -w) ;
  109. // causes abort() in ply(). Checks are skipped bc no expr is built.
  110. for_each([](auto & w) { w *= -1; }, w) ;
  111. std::cout << w << std::endl;
  112. */
  113. // ------------------------------
  114. // see test/frame-new.cc
  115. // ------------------------------
  116. tr.section("static match in dynamic case");
  117. {
  118. ra::Big<int> a({2, 3, 4}, 0);
  119. tr.test_eq(2, agree_s(a, 99));
  120. }
  121. tr.section("dynamic (implicit) match");
  122. {
  123. ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
  124. ra::Big<int, 4> b({2, 4, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
  125. tr.test_eq(1, agree_s(a, b));
  126. tr.test_eq(0, agree(a, b));
  127. #define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
  128. int x = 0;
  129. try {
  130. tr.test_eq(ra::ANY, EXPR.len_s(1));
  131. x = 1;
  132. } catch (ra_error & e) {
  133. }
  134. tr.test_eq(0, x);
  135. #undef EXPR
  136. }
  137. // 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().
  138. tr.section("static mismatch");
  139. {
  140. ra::Small<int, 2, 2> a;
  141. ra::Small<int, 3, 2> b;
  142. static_assert(0==agree_s(a, b));
  143. }
  144. tr.section("static match");
  145. {
  146. ra::Small<int, 2, 2> a;
  147. ra::Small<int, 2> b;
  148. static_assert(2==agree_s(a, b));
  149. }
  150. tr.section("static match with dynamic term");
  151. {
  152. ra::Small<int> a;
  153. ra::Big<int, 1> b({1}, 77);
  154. tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(2, agree_s(a, b));
  155. }
  156. tr.section("dynamic terms in match, static rank");
  157. {
  158. int error = 0;
  159. std::string s;
  160. try {
  161. ra::Small<int, 2> a {2, 3};
  162. ra::Big<int, 1> b({1}, 77);
  163. std::cout << "LEN " << decltype(a+b)::len_s(0) << std::endl;
  164. tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(1, decltype(a+b)::check_s());
  165. tr.test(!agree(a, b));
  166. a = b;
  167. } catch (ra_error & e) {
  168. error = 1;
  169. s = e.s;
  170. }
  171. tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
  172. }
  173. tr.section("dynamic terms in match, dynamic rank. See frame-new.cc");
  174. {
  175. auto f2 = [](ra::Big<int, 2> const & a) { cout << ra::start(shape(a)) << endl; };
  176. int error = 0;
  177. std::string s;
  178. try {
  179. ra::Big<int> a {};
  180. // flag the error when casting rank-0 to rank-2 array. FIXME check that copying is still possible.
  181. f2(a);
  182. error = 0;
  183. } catch (ra_error & e) {
  184. error = 1;
  185. s = e.s;
  186. }
  187. tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
  188. }
  189. // ------------------------------
  190. // see test/big-0.cc
  191. // ------------------------------
  192. tr.section("check rank errors with dynamic rank (I)");
  193. {
  194. int x = 0;
  195. try {
  196. ra::Big<int> a = 0;
  197. cout << shape(a, 0) << endl;
  198. x = 1;
  199. } catch (ra_error & e) {
  200. x = 2;
  201. }
  202. tr.info("caught error").test_eq(2, x);
  203. }
  204. tr.section("check rank errors with dynamic rank (II)");
  205. {
  206. int x = 0;
  207. // initialization is to rank 1, size 0.
  208. try {
  209. ra::Big<int> a;
  210. a = ra::Big<int, 1> { 1 };
  211. x = 1;
  212. } catch (ra_error & e) {
  213. x = 2;
  214. }
  215. tr.info("uninitialized dynamic rank").test_eq(2, x);
  216. }
  217. tr.section("iffy conversions");
  218. // won't fail until runtime bc val[0] might have size 1, but FIXME this is very likely a coding error.
  219. // FIXME shape doesn't show in the error message as it should.
  220. {
  221. ra::Big<double, 2> val = { { 7, 0, 0, 0.5, 1.5, 1, 1, 1 } };
  222. ra::Big<double, 2> bal = { { 7 }, { 0 } };
  223. double x = bal[0];
  224. tr.test_eq(7, x);
  225. int error = 0;
  226. string s;
  227. try {
  228. double x = val[0];
  229. cout << "x " << x << " (!)" << endl;
  230. } catch (ra_error & e) {
  231. error = 1;
  232. s = e.s;
  233. }
  234. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  235. }
  236. // ------------------------------
  237. // see test/frame-old.cc
  238. // ------------------------------
  239. #define EXPR ra::expr(plus2double_print, a.iter(), b.iter())
  240. tr.section("frame matching should-be-error cases. See frame-old.cc");
  241. {
  242. ra::Unique<double, 1> a({3}, 10);
  243. ra::Unique<double, 1> b({4}, 1);
  244. auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
  245. int error = 0;
  246. string s;
  247. try {
  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. tr.section("frame matching should-be-error cases - dynamic rank. See frame-old.cc");
  258. {
  259. ra::Unique<double> a({3}, 10);
  260. ra::Unique<double> b({4}, 1);
  261. auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
  262. int error = 0;
  263. string s;
  264. try {
  265. std::cout << "A: " << a.iter().len(0) << endl;
  266. std::cout << "B: " << b.iter().len(0) << endl;
  267. tr.info("dynamic test is needed").test_eq(1, decltype(EXPR)::check_s());
  268. tr.test(!agree(a, b));
  269. ply_ravel(EXPR);
  270. } catch (ra_error & e) {
  271. error = 1;
  272. s = e.s;
  273. }
  274. tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
  275. }
  276. #undef EXPR
  277. return tr.summary();
  278. }