types.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // SmallBase is not excluded in SliceConcept.
  55. TESTPRED(decltype(std::declval<ra::SmallBase<ra::SmallArray, ra::Dim, int_list<3>, int_list<1>>>()),
  56. true, true, false, false, false);
  57. TESTPRED(int,
  58. false, false, false, true, false);
  59. TESTPRED(ra::int_c<3>,
  60. false, false, false, true, false);
  61. TESTPRED(ra::dim_c<0>,
  62. false, false, false, true, false);
  63. TESTPRED(std::complex<double>,
  64. false, false, false, true, false);
  65. #ifdef __STDCPP_FLOAT128_T__
  66. TESTPRED(std::float128_t,
  67. false, false, false, true, false);
  68. TESTPRED(std::complex<std::float128_t>,
  69. false, false, false, true, false);
  70. #endif
  71. TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()),
  72. true, true, false, false, false);
  73. TESTPRED(decltype(std::declval<ra::View<int, 2>>()),
  74. true, true, false, false, false);
  75. TESTPRED(decltype(ra::Unique<int, 2>().iter()),
  76. true, false, true, false, false);
  77. static_assert(ra::IteratorConcept<decltype(ra::Unique<int, 2>().iter())>);
  78. TESTPRED(decltype(ra::Unique<int, 1>().iter()) &,
  79. true, false, true, false, false);
  80. TESTPRED(decltype(ra::iota(5)),
  81. true, false, true, false, false);
  82. TESTPRED(decltype(ra::iota<0>()),
  83. true, false, true, false, false);
  84. TESTPRED(decltype(ra::iota<0>()) const, // is_iterator but not IteratorConcept (see RA_IS_DEF)
  85. true, false, false, false, false);
  86. TESTPRED(decltype(ra::iota<0>()) &,
  87. true, false, true, false, false);
  88. TESTPRED(decltype(std::declval<ra::Small<int, 2>>()),
  89. true, true, false, false, false);
  90. TESTPRED(decltype(ra::Small<int, 2>().iter()),
  91. true, false, true, false, false);
  92. TESTPRED(decltype(ra::Small<int, 2, 2>()()),
  93. true, true, false, false, false);
  94. TESTPRED(decltype(ra::Small<int, 2, 2>().iter()),
  95. true, false, true, false, false);
  96. TESTPRED(decltype(ra::Small<int, 2>()+3),
  97. true, false, true, false, false);
  98. TESTPRED(decltype(3+ra::Big<int>()),
  99. true, false, true, false, false);
  100. TESTPRED(std::vector<int>,
  101. false, false, false, false, true);
  102. TESTPRED(decltype(ra::start(std::vector<int> {})),
  103. true, false, true, false, false);
  104. TESTPRED(int *,
  105. false, false, false, false, false);
  106. TESTPRED(decltype(std::ranges::iota_view(-5, 10)),
  107. false, false, false, false, true);
  108. // std::string can be registered as is_scalar or not [ra13]. One may do ra::ptr(std::string) or ra::scalar(std::string) to get the other behavior.
  109. if constexpr(ra::is_scalar<std::string>) {
  110. TESTPRED(std::string,
  111. false, false, false, true, false);
  112. } else {
  113. TESTPRED(std::string,
  114. false, false, false, false, true);
  115. }
  116. TESTPRED(Unreg,
  117. false, false, false, false, false);
  118. TESTPRED(int [4],
  119. false, false, false, false, false);
  120. TESTPRED(int (&) [3],
  121. false, false, false, false, false);
  122. TESTPRED(decltype("cstring"),
  123. false, false, false, false, false);
  124. }
  125. tr.section("establish meaning of selectors (TODO / convert to TestRecorder)");
  126. {
  127. static_assert(ra::SliceConcept<ra::View<int, 0>>);
  128. static_assert(ra::SliceConcept<ra::View<int, 2>>);
  129. static_assert(ra::SliceConcept<ra::SmallView<int, int_list<>, int_list<>>>);
  130. static_assert(ra::is_ra<ra::Small<int>>, "bad is_ra Small");
  131. static_assert(ra::is_ra<ra::SmallView<int, nil, nil>>, "bad is_ra SmallView");
  132. static_assert(ra::is_ra<ra::Unique<int, 0>>, "bad is_ra Unique");
  133. static_assert(ra::is_ra<ra::View<int, 0>>, "bad is_ra View");
  134. static_assert(ra::is_ra<ra::Small<int, 1>>, "bad is_ra Small");
  135. static_assert(ra::is_ra<ra::SmallView<int, int_list<1>, int_list<1>>>, "bad is_ra SmallView");
  136. static_assert(ra::is_ra<ra::Unique<int, 1>>, "bad is_ra Unique");
  137. static_assert(ra::is_ra<ra::View<int, 1>>, "bad is_ra View");
  138. static_assert(ra::is_ra<ra::View<int>>, "bad is_ra View");
  139. using Scalar = decltype(ra::scalar(3));
  140. using Vector = decltype(ra::start({1, 2, 3}));
  141. static_assert(ra::is_ra_scalar<Scalar>, "bad is_ra_scalar Scalar");
  142. static_assert(ra::is_ra<decltype(ra::scalar(3))>, "bad is_ra Scalar");
  143. static_assert(ra::is_ra<Vector>, "bad is_ra Vector");
  144. static_assert(!ra::is_ra<int *>, "bad is_ra int *");
  145. static_assert(ra::is_scalar<double>, "bad is_scalar real");
  146. static_assert(ra::is_scalar<std::complex<double>>, "bad is_scalar complex");
  147. static_assert(ra::is_scalar<int>, "bad is_scalar int");
  148. static_assert(!ra::is_scalar<decltype(ra::scalar(3))>, "bad is_scalar Scalar");
  149. static_assert(!ra::is_scalar<Vector>, "bad is_scalar Scalar");
  150. static_assert(!ra::is_scalar<decltype(ra::start(3))>, "bad is_scalar Scalar");
  151. int a = 3;
  152. static_assert(!ra::is_scalar<decltype(ra::start(a))>, "bad is_scalar Scalar");
  153. // a regression.
  154. static_assert(ra::is_zero_or_scalar<ra::Scalar<int>>, "bad");
  155. static_assert(!ra::is_ra_pos<ra::Scalar<int>>, "bad");
  156. static_assert(!ra::is_zero_or_scalar<decltype(ra::iota<0>())>, "bad");
  157. static_assert(ra::is_ra_pos<decltype(ra::iota<0>())>, "bad");
  158. static_assert(ra::is_ra_pos<ra::Expr<std::multiplies<>, std::tuple<decltype(ra::iota<0>()), ra::Scalar<int>>>>, "bad");
  159. static_assert(ra::is_ra_pos<ra::Pick<std::tuple<Vector, Vector, Vector>>>, "bad");
  160. }
  161. tr.section("builtin arrays");
  162. {
  163. int const a[] = {1, 2, 3};
  164. int b[] = {1, 2, 3};
  165. int c[][2] = {{1, 2}, {4, 5}};
  166. static_assert(ra::is_builtin_array<decltype(a) &>);
  167. static_assert(ra::is_builtin_array<decltype(a)>);
  168. static_assert(ra::is_builtin_array<decltype(b) &>);
  169. static_assert(ra::is_builtin_array<decltype(b)>);
  170. static_assert(ra::is_builtin_array<decltype(c) &>);
  171. static_assert(ra::is_builtin_array<decltype(c)>);
  172. tr.test_eq(1, ra::rank_s(a));
  173. tr.test_eq(1, ra::rank_s(b));
  174. tr.test_eq(2, ra::rank_s(c));
  175. tr.test_eq(3, ra::size(a));
  176. tr.test_eq(3, ra::size(b));
  177. tr.test_eq(4, ra::size(c));
  178. tr.test_eq(3, ra::size_s(a));
  179. tr.test_eq(3, ra::size_s(b));
  180. tr.test_eq(4, ra::size_s(c));
  181. }
  182. tr.section("adaptors I");
  183. {
  184. tr.test_eq(2, size_s(ra::start(std::array<int, 2> { 1, 2 })));
  185. tr.test_eq(2, ra::size_s(std::array<int, 2> { 1, 2 }));
  186. tr.test_eq(ra::ANY, ra::size_s<decltype(ra::start(std::vector<int> { 1, 2, 3}))>());
  187. tr.test_eq(ra::ANY, ra::size_s<decltype(std::vector<int> { 1, 2, 3})>());
  188. tr.test_eq(2, ra::start(std::array<int, 2> { 1, 2 }).len_s(0));
  189. tr.test_eq(ra::ANY, ra::start(std::vector<int> { 1, 2, 3 }).len_s(0));
  190. tr.test_eq(1, ra::rank_s<decltype(ra::start(std::array<int, 2> { 1, 2 }))>());
  191. tr.test_eq(1, ra::rank_s<decltype(ra::start(std::vector<int> { 1, 2, 3 }))>());
  192. tr.test_eq(1, ra::start(std::array<int, 2> { 1, 2 }).rank());
  193. tr.test_eq(1, ra::start(std::vector<int> { 1, 2, 3 }).rank());
  194. }
  195. tr.section("adaptors II");
  196. {
  197. static_assert(ra::is_iterator<decltype(ra::ptr(std::array { 1, 2 }))>);
  198. static_assert(ra::is_iterator<decltype(ra::start(std::array { 1, 2 }))>);
  199. }
  200. return tr.summary();
  201. }