constexpr_core.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Range v3 library
  2. //
  3. // Copyright Gonzalo Brito Gadeschi 2015
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <range/v3/detail/config.hpp>
  12. #if RANGES_CXX_CONSTEXPR >= RANGES_CXX_CONSTEXPR_14
  13. #include <range/v3/range/access.hpp>
  14. #include <range/v3/range/operations.hpp>
  15. #include <range/v3/range/primitives.hpp>
  16. #include <range/v3/utility/addressof.hpp>
  17. #include "array.hpp"
  18. #include "test_iterators.hpp"
  19. // Test sequence 1,2,3,4
  20. template<typename It>
  21. constexpr /*c++14*/ auto test_it_back(It, It last,
  22. std::bidirectional_iterator_tag) -> bool
  23. {
  24. auto end_m1_2 = It{ranges::prev(last, 1)};
  25. if (*end_m1_2 != 4) { return false; }
  26. return true;
  27. }
  28. template<typename It, typename Concept>
  29. constexpr /*c++14*/ auto test_it_back(It, It, Concept) -> bool
  30. {
  31. return true;
  32. }
  33. template<typename It>
  34. constexpr /*c++14*/ auto test_it_(It beg, It last) -> bool
  35. {
  36. if (*beg != 1) { return false; }
  37. if (ranges::distance(beg, last) != 4) { return false; }
  38. if (ranges::next(beg, 4) != last) { return false; }
  39. auto end_m1 = It{ranges::next(beg, 3)};
  40. if (*end_m1 != 4) { return false; }
  41. if (!test_it_back(beg, last, ranges::iterator_tag_of<It>{})) { return false; }
  42. auto end2 = beg;
  43. ranges::advance(end2, last);
  44. if (end2 != last) { return false; }
  45. auto end3 = beg;
  46. ranges::advance(end3, 4);
  47. if (end3 != last) { return false; }
  48. if (ranges::iter_enumerate(beg, last) != std::pair<std::ptrdiff_t, It>{4, last})
  49. {
  50. return false;
  51. }
  52. if (ranges::iter_distance(beg, last) != 4) { return false; }
  53. if (ranges::iter_distance_compare(beg, last, 4) != 0) { return false; }
  54. if (ranges::iter_distance_compare(beg, last, 3) != 1) { return false; }
  55. if (ranges::iter_distance_compare(beg, last, 5) != -1) { return false; }
  56. return true;
  57. }
  58. // Test sequence 4,3,2,1 (for reverse iterators)
  59. template<typename It>
  60. constexpr /*c++14*/ auto test_rit_(It beg, It last) -> bool
  61. {
  62. if (ranges::distance(beg, last) != 4) { return false; }
  63. if (ranges::next(beg, 4) != last) { return false; }
  64. auto end_m1 = It{ranges::next(beg, 3)};
  65. if (*end_m1 != 1) { return false; }
  66. if (ranges::detail::is_convertible<ranges::iterator_tag_of<It>,
  67. std::bidirectional_iterator_tag>{})
  68. {
  69. auto end_m1_2 = It{ranges::prev(last, 1)};
  70. if (*end_m1_2 != 1) { return false; }
  71. }
  72. auto end2 = beg;
  73. ranges::advance(end2, last);
  74. if (end2 != last) { return false; }
  75. auto end3 = beg;
  76. ranges::advance(end3, 4);
  77. if (end3 != last) { return false; }
  78. using D = ranges::iter_difference_t<It>;
  79. if (ranges::iter_enumerate(beg, last) != std::pair<D, It>{4, last})
  80. {
  81. return false;
  82. }
  83. if (ranges::iter_distance(beg, last) != 4) { return false; }
  84. if (ranges::iter_distance_compare(beg, last, 4) != 0) { return false; }
  85. if (ranges::iter_distance_compare(beg, last, 3) != 1) { return false; }
  86. if (ranges::iter_distance_compare(beg, last, 5) != -1) { return false; }
  87. return true;
  88. }
  89. template<typename It, typename Sequence1234>
  90. constexpr /*c++14*/ auto test_it(Sequence1234&& a) -> bool
  91. {
  92. auto beg = It{ranges::begin(a)};
  93. auto last = It{ranges::end(a)};
  94. return test_it_(beg, last);
  95. }
  96. template<typename Sequence1234>
  97. constexpr /*c++14*/ auto test_its_c(Sequence1234&& a) -> bool
  98. {
  99. return test_it<InputIterator<int const *>>(a)
  100. && test_it<ForwardIterator<int const *>>(a)
  101. && test_it<BidirectionalIterator<int const *>>(a)
  102. && test_it<RandomAccessIterator<int const *>>(a);
  103. }
  104. template<typename Sequence1234>
  105. constexpr /*c++14*/ auto test_its(Sequence1234&& a) -> bool
  106. {
  107. return test_it<InputIterator<int *>>(a)
  108. && test_it<ForwardIterator<int *>>(a)
  109. && test_it<BidirectionalIterator<int *>>(a)
  110. && test_it<RandomAccessIterator<int *>>(a)
  111. && test_its_c(a);
  112. }
  113. template<typename It, typename Sequence1234>
  114. constexpr /*c++14*/ auto test_rit(Sequence1234&& a) -> bool
  115. {
  116. auto beg = It{ranges::rbegin(a)};
  117. auto last = It{ranges::rend(a)};
  118. return test_rit_(beg, last);
  119. }
  120. template<typename Sequence1234>
  121. constexpr /*c++14*/ auto test_rits(Sequence1234&& a) -> bool
  122. {
  123. using rit = decltype(ranges::rbegin(a));
  124. return test_rit<BidirectionalIterator<rit>>(a)
  125. && test_rit<BidirectionalIterator<rit>>(a);
  126. }
  127. template<typename It, typename Sequence1234>
  128. constexpr /*c++14*/ auto test_cit(Sequence1234&& a) -> bool
  129. {
  130. auto beg = It{ranges::cbegin(a)};
  131. auto last = It{ranges::cend(a)};
  132. return test_it_(beg, last);
  133. }
  134. template<typename Sequence1234>
  135. constexpr /*c++14*/ auto test_cits(Sequence1234&& a) -> bool
  136. {
  137. return test_cit<InputIterator<int const *>>(a)
  138. && test_cit<ForwardIterator<int const *>>(a)
  139. && test_cit<BidirectionalIterator<int const *>>(a)
  140. && test_cit<RandomAccessIterator<int const *>>(a);
  141. }
  142. template<typename It, typename Sequence1234>
  143. constexpr /*c++14*/ auto test_crit(Sequence1234&& a) -> bool
  144. {
  145. auto beg = It{ranges::crbegin(a)};
  146. auto last = It{ranges::crend(a)};
  147. return test_rit_(beg, last);
  148. }
  149. template<typename Sequence1234>
  150. constexpr /*c++14*/ auto test_crits(Sequence1234&& a) -> bool
  151. {
  152. using rit = decltype(ranges::crbegin(a));
  153. return test_crit<BidirectionalIterator<rit>>(a)
  154. && test_crit<RandomAccessIterator<rit>>(a);
  155. }
  156. template<typename Sequence1234>
  157. constexpr /*c++14*/ auto test_non_member_f(Sequence1234&& a) -> bool
  158. {
  159. if (ranges::empty(a)) { return false; }
  160. if (ranges::front(a) != 1) { return false; }
  161. if (ranges::back(a) != 4) { return false; }
  162. if (ranges::index(a, 2) != 3) { return false; }
  163. if (ranges::at(a, 2) != 3) { return false; }
  164. if (ranges::size(a) != 4) { return false; }
  165. return true;
  166. }
  167. constexpr /*c++14*/ auto test_array() -> bool
  168. {
  169. test::array<int, 4> a{{1, 2, 3, 4}};
  170. auto beg = ranges::begin(a);
  171. auto three = ranges::next(beg, 2);
  172. if ((false)) {
  173. ranges::iter_swap(beg, three);
  174. if (*beg != 3) { return false; }
  175. if (*three != 1) { return false; }
  176. ranges::iter_swap(beg, three);
  177. }
  178. if (!test_its(a)) { return false; }
  179. if (!test_cits(a)) { return false; }
  180. if (!test_rits(a)) { return false; }
  181. if (!test_crits(a)) { return false; }
  182. if (!test_non_member_f(a)) { return false; }
  183. // This can be worked around but is just bad:
  184. test::array<int, 4> b{{5, 6, 7, 8}};
  185. ranges::swap(a, b);
  186. if (a[0] != 5 || b[0] != 1 || a[3] != 8 || b[3] != 4) { return false; }
  187. return true;
  188. }
  189. constexpr /*c++14*/ auto test_c_array() -> bool
  190. {
  191. int a[4]{1, 2, 3, 4};
  192. if (!test_its(a)) { return false; }
  193. if (!test_cits(a)) { return false; }
  194. if (!test_rits(a)) { return false; }
  195. if (!test_crits(a)) { return false; }
  196. if (!test_non_member_f(a)) { return false; }
  197. // C-arrays have no associated namespace, so this can't work:
  198. // int b[4]{5, 6, 7, 8};
  199. // ranges::swap(a, b);
  200. // if (a[0] != 5 || b[0] != 1 || a[3] != 8 || b[3] != 4) { return false; }
  201. return true;
  202. }
  203. constexpr /*c++14*/ auto test_init_list() -> bool
  204. {
  205. std::initializer_list<int> a{1, 2, 3, 4};
  206. if (!test_its_c(a)) { return false; }
  207. if (!test_cits(a)) { return false; }
  208. if (!test_rits(a)) { return false; }
  209. if (!test_crits(a)) { return false; }
  210. if (!test_non_member_f(a)) { return false; }
  211. std::initializer_list<int> b{5, 6, 7, 8};
  212. ranges::swap(a, b);
  213. if (ranges::at(a, 0) != 5 || ranges::at(b, 0) != 1
  214. || ranges::at(a, 3) != 8 || ranges::at(b, 3) != 4)
  215. {
  216. return false;
  217. }
  218. return true;
  219. }
  220. #ifdef __cpp_lib_addressof_constexpr
  221. #define ADDR_CONSTEXPR constexpr
  222. #else
  223. #define ADDR_CONSTEXPR
  224. #endif
  225. namespace addr {
  226. struct Good { };
  227. struct Bad { void operator&() const; };
  228. struct Bad2 { friend void operator&(Bad2); };
  229. }
  230. void test_constexpr_addressof() {
  231. static constexpr int i = 0;
  232. static constexpr int const* pi = ranges::detail::addressof(i);
  233. static_assert(&i == pi, "");
  234. static constexpr addr::Good g = {};
  235. static constexpr addr::Good const* pg = ranges::detail::addressof(g);
  236. static_assert(&g == pg, "");
  237. static constexpr addr::Bad b = {};
  238. static ADDR_CONSTEXPR addr::Bad const* pb = ranges::detail::addressof(b);
  239. static constexpr addr::Bad2 b2 = {};
  240. static ADDR_CONSTEXPR addr::Bad2 const* pb2 = ranges::detail::addressof(b2);
  241. #ifdef __cpp_lib_addressof_constexpr
  242. static_assert(std::addressof(b) == pb, "");
  243. static_assert(std::addressof(b2) == pb2, "");
  244. #else
  245. (void)pb;
  246. (void)pb2;
  247. #endif
  248. }
  249. int main()
  250. {
  251. static_assert(test_array(), "");
  252. static_assert(test_c_array(), "");
  253. static_assert(test_init_list(), "");
  254. }
  255. #else
  256. int main() {}
  257. #endif