types.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Show what types conform to what type predicates.
  3. // (c) Daniel Llorens - 2016-2017
  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 <chrono>
  9. #include <numeric>
  10. #include <typeinfo>
  11. #include <iostream>
  12. #include "ra/test.hh"
  13. #include "mpdebug.hh"
  14. #ifdef __STDCPP_FLOAT128_T__
  15. #include <stdfloat>
  16. #endif
  17. using std::cout, std::endl, std::flush, ra::TestRecorder;
  18. using ra::mp::int_list, ra::mp::nil;
  19. template <class A>
  20. void
  21. test_predicates(char const * type, TestRecorder & tr,
  22. bool ra, bool slice, bool iterator, bool scalar, bool fov)
  23. {
  24. cout << std::string(90-size(std::string(type)), ' ') << type << " : "
  25. << (ra::is_ra<A> ? "ra " : "")
  26. << (ra::SliceConcept<A> ? "slice " : "")
  27. << (ra::IteratorConcept<A> ? "iterator " : "")
  28. << (ra::is_scalar<A> ? "scalar " : "")
  29. << (ra::is_fov<A> ? "fov " : "")
  30. << (ra::is_builtin_array<A> ? "builtinarray " : "")
  31. << (std::ranges::range<A> ? "range " : "")
  32. << (std::is_const_v<A> ? "const " : "")
  33. << (std::is_lvalue_reference_v<A> ? "ref " : "") << endl;
  34. tr.quiet().info(type).info("ra").test_eq(ra, ra::is_ra<A>);
  35. tr.quiet().info(type).info("slice").test_eq(slice, ra::SliceConcept<A>);
  36. tr.quiet().info(type).info("Iterator").test_eq(iterator, ra::IteratorConcept<A>);
  37. tr.quiet().info(type).info("scalar").test_eq(scalar, ra::is_scalar<A>);
  38. tr.quiet().info(type).info("fov").test_eq(fov, ra::is_fov<A>);
  39. tr.quiet().info(type).info("std::ranges::range").test_eq(std::ranges::range<A>, std::ranges::range<A>);
  40. }
  41. // Not registered with is_scalar_def.
  42. struct Unreg { int x; };
  43. // prefer decltype(declval(...)) to https://stackoverflow.com/a/13842784 the latter drops const
  44. #define TESTPRED(A, ...) test_predicates <A> (STRINGIZE(A), tr, ##__VA_ARGS__)
  45. int main()
  46. {
  47. TestRecorder tr(std::cout, TestRecorder::NOISY);
  48. {
  49. using T = std::chrono::duration<long int, std::ratio<1, 1000000000>>;
  50. ra::Big<T, 1> a;
  51. static_assert(std::is_same_v<ra::value_t<decltype(a)>, T>);
  52. }
  53. {
  54. TESTPRED(decltype(std::declval<ra::SmallView<ra::Dim, int_list<3>, int_list<1>>>()),
  55. true, true, false, false, false);
  56. TESTPRED(int,
  57. false, false, false, true, false);
  58. TESTPRED(ra::int_c<3>,
  59. false, false, false, true, false);
  60. TESTPRED(ra::dim_c<0>,
  61. false, false, false, true, false);
  62. TESTPRED(std::complex<double>,
  63. false, false, false, true, false);
  64. #ifdef __STDCPP_FLOAT128_T__
  65. TESTPRED(std::float128_t,
  66. false, false, false, true, false);
  67. TESTPRED(std::complex<std::float128_t>,
  68. false, false, false, true, false);
  69. #endif
  70. TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()),
  71. true, true, false, false, false);
  72. TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()) const,
  73. true, true, false, false, false);
  74. TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()) &,
  75. true, true, false, false, false);
  76. TESTPRED(decltype(std::declval<ra::View<int, 2>>()),
  77. true, true, false, false, false);
  78. TESTPRED(decltype(ra::Unique<int, 2>().iter()),
  79. true, false, true, false, false);
  80. static_assert(ra::IteratorConcept<decltype(ra::Unique<int, 2>().iter())>);
  81. TESTPRED(decltype(ra::Unique<int, 1>().iter()) &,
  82. true, false, true, false, false);
  83. TESTPRED(decltype(ra::iota(5)),
  84. true, false, true, false, false);
  85. TESTPRED(decltype(ra::iota<0>()),
  86. true, false, true, false, false);
  87. // is_iterator (by RA_IS_DEF) but not IteratorConcept, since it cannot be traversed.
  88. TESTPRED(decltype(ra::iota<0>()) const,
  89. true, false, false, false, false);
  90. TESTPRED(decltype(ra::iota<0>()) &,
  91. true, false, true, false, false);
  92. TESTPRED(decltype(std::declval<ra::Small<int, 2>>()),
  93. true, true, false, false, false);
  94. TESTPRED(decltype(ra::Small<int, 2>().iter()),
  95. true, false, true, false, false);
  96. TESTPRED(decltype(ra::Small<int, 2, 2>()()),
  97. true, true, false, false, false);
  98. TESTPRED(decltype(ra::Small<int, 2, 2>().iter()),
  99. true, false, true, false, false);
  100. TESTPRED(decltype(ra::Small<int, 2>()+3),
  101. true, false, true, false, false);
  102. TESTPRED(decltype(3+ra::Big<int>()),
  103. true, false, true, false, false);
  104. TESTPRED(std::vector<int>,
  105. false, false, false, false, true);
  106. TESTPRED(decltype(ra::start(std::vector<int> {})),
  107. true, false, true, false, false);
  108. TESTPRED(int *,
  109. false, false, false, false, false);
  110. TESTPRED(decltype(std::ranges::iota_view(-5, 10)),
  111. false, false, false, false, true);
  112. // std::string can be registered as is_scalar or not [ra13]. Use ra::ptr(std::string) or ra::scalar(std::string) to get the other behavior.
  113. if constexpr(ra::is_scalar<std::string>) {
  114. TESTPRED(std::string,
  115. false, false, false, true, false);
  116. } else {
  117. TESTPRED(std::string,
  118. false, false, false, false, true);
  119. }
  120. TESTPRED(Unreg,
  121. false, false, false, false, false);
  122. TESTPRED(int [4],
  123. false, false, false, false, false);
  124. TESTPRED(int (&) [3],
  125. false, false, false, false, false);
  126. TESTPRED(decltype("cstring"),
  127. false, false, false, false, false);
  128. }
  129. tr.section("establish meaning of selectors (TODO / convert to TestRecorder)");
  130. {
  131. static_assert(ra::SliceConcept<ra::View<int, 0>>);
  132. static_assert(ra::SliceConcept<ra::View<int, 2>>);
  133. static_assert(ra::SliceConcept<ra::SmallView<int, int_list<>, int_list<>>>);
  134. static_assert(ra::is_ra<ra::Small<int>>, "bad is_ra Small");
  135. static_assert(ra::is_ra<ra::SmallView<int, nil, nil>>, "bad is_ra SmallView");
  136. static_assert(ra::is_ra<ra::Unique<int, 0>>, "bad is_ra Unique");
  137. static_assert(ra::is_ra<ra::View<int, 0>>, "bad is_ra View");
  138. static_assert(ra::is_ra<ra::Small<int, 1>>, "bad is_ra Small");
  139. static_assert(ra::is_ra<ra::SmallView<int, int_list<1>, int_list<1>>>, "bad is_ra SmallView");
  140. static_assert(ra::is_ra<ra::Unique<int, 1>>, "bad is_ra Unique");
  141. static_assert(ra::is_ra<ra::View<int, 1>>, "bad is_ra View");
  142. static_assert(ra::is_ra<ra::View<int>>, "bad is_ra View");
  143. using Scalar = decltype(ra::scalar(3));
  144. using Vector = decltype(ra::start({1, 2, 3}));
  145. static_assert(ra::is_ra_scalar<Scalar>, "bad is_ra_scalar Scalar");
  146. static_assert(ra::is_ra<decltype(ra::scalar(3))>, "bad is_ra Scalar");
  147. static_assert(ra::is_ra<Vector>, "bad is_ra Vector");
  148. static_assert(!ra::is_ra<int *>, "bad is_ra int *");
  149. static_assert(ra::is_scalar<double>, "bad is_scalar real");
  150. static_assert(ra::is_scalar<std::complex<double>>, "bad is_scalar complex");
  151. static_assert(ra::is_scalar<int>, "bad is_scalar int");
  152. static_assert(!ra::is_scalar<decltype(ra::scalar(3))>, "bad is_scalar Scalar");
  153. static_assert(!ra::is_scalar<Vector>, "bad is_scalar Scalar");
  154. static_assert(!ra::is_scalar<decltype(ra::start(3))>, "bad is_scalar Scalar");
  155. int a = 3;
  156. static_assert(!ra::is_scalar<decltype(ra::start(a))>, "bad is_scalar Scalar");
  157. // a regression.
  158. static_assert(ra::is_zero_or_scalar<ra::Scalar<int>>, "bad");
  159. static_assert(!ra::is_ra_pos<ra::Scalar<int>>, "bad");
  160. static_assert(!ra::is_zero_or_scalar<decltype(ra::iota<0>())>, "bad");
  161. static_assert(ra::is_ra_pos<decltype(ra::iota<0>())>, "bad");
  162. static_assert(ra::is_ra_pos<ra::Expr<std::multiplies<>, std::tuple<decltype(ra::iota<0>()), ra::Scalar<int>>>>, "bad");
  163. static_assert(ra::is_ra_pos<ra::Pick<std::tuple<Vector, Vector, Vector>>>, "bad");
  164. }
  165. tr.section("builtin arrays");
  166. {
  167. int const a[] = {1, 2, 3};
  168. int b[] = {1, 2, 3};
  169. int c[][2] = {{1, 2}, {4, 5}};
  170. static_assert(ra::is_builtin_array<decltype(a) &>);
  171. static_assert(ra::is_builtin_array<decltype(a)>);
  172. static_assert(ra::is_builtin_array<decltype(b) &>);
  173. static_assert(ra::is_builtin_array<decltype(b)>);
  174. static_assert(ra::is_builtin_array<decltype(c) &>);
  175. static_assert(ra::is_builtin_array<decltype(c)>);
  176. tr.test_eq(1, ra::rank_s(a));
  177. tr.test_eq(1, ra::rank_s(b));
  178. tr.test_eq(2, ra::rank_s(c));
  179. tr.test_eq(3, ra::size(a));
  180. tr.test_eq(3, ra::size(b));
  181. tr.test_eq(4, ra::size(c));
  182. tr.test_eq(3, ra::size_s(a));
  183. tr.test_eq(3, ra::size_s(b));
  184. tr.test_eq(4, ra::size_s(c));
  185. }
  186. tr.section("adaptors I");
  187. {
  188. tr.test_eq(2, size_s(ra::start(std::array<int, 2> { 1, 2 })));
  189. tr.test_eq(2, ra::size_s(std::array<int, 2> { 1, 2 }));
  190. tr.test_eq(ra::ANY, ra::size_s<decltype(ra::start(std::vector<int> { 1, 2, 3}))>());
  191. tr.test_eq(ra::ANY, ra::size_s<decltype(std::vector<int> { 1, 2, 3})>());
  192. tr.test_eq(2, ra::start(std::array<int, 2> { 1, 2 }).len_s(0));
  193. tr.test_eq(ra::ANY, ra::start(std::vector<int> { 1, 2, 3 }).len_s(0));
  194. tr.test_eq(1, ra::rank_s<decltype(ra::start(std::array<int, 2> { 1, 2 }))>());
  195. tr.test_eq(1, ra::rank_s<decltype(ra::start(std::vector<int> { 1, 2, 3 }))>());
  196. tr.test_eq(1, ra::start(std::array<int, 2> { 1, 2 }).rank());
  197. tr.test_eq(1, ra::start(std::vector<int> { 1, 2, 3 }).rank());
  198. }
  199. tr.section("adaptors II");
  200. {
  201. static_assert(ra::is_iterator<decltype(ra::ptr(std::array { 1, 2 }))>);
  202. static_assert(ra::is_iterator<decltype(ra::start(std::array { 1, 2 }))>);
  203. }
  204. return tr.summary();
  205. }