checks.cc 7.9 KB

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