builtin_array.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/any_of.hpp>
  5. #include <boost/hana/assert.hpp>
  6. #include <boost/hana/bool.hpp>
  7. #include <boost/hana/concept/foldable.hpp>
  8. #include <boost/hana/concept/searchable.hpp>
  9. #include <boost/hana/equal.hpp>
  10. #include <boost/hana/find_if.hpp>
  11. #include <boost/hana/functional/always.hpp>
  12. #include <boost/hana/functional/placeholder.hpp>
  13. #include <boost/hana/not.hpp>
  14. #include <boost/hana/optional.hpp>
  15. #include <boost/hana/unpack.hpp>
  16. #include <laws/base.hpp>
  17. #include <laws/foldable.hpp>
  18. #include <laws/searchable.hpp>
  19. #include <cstddef>
  20. namespace hana = boost::hana;
  21. template <typename T, std::size_t n>
  22. using array = T[n];
  23. int main() {
  24. // We can't check the laws because builtin arrays can't be passed
  25. // to functions.
  26. //////////////////////////////////////////////////////////////////////////
  27. // Foldable
  28. //////////////////////////////////////////////////////////////////////////
  29. {
  30. int a[] = {1};
  31. int b[] = {1, 2};
  32. int c[] = {1, 2, 3};
  33. int d[] = {1, 2, 3, 4};
  34. // unpack
  35. {
  36. hana::test::_injection<0> f{};
  37. BOOST_HANA_RUNTIME_CHECK(hana::equal(
  38. hana::unpack(a, f),
  39. f(1)
  40. ));
  41. BOOST_HANA_RUNTIME_CHECK(hana::equal(
  42. hana::unpack(b, f),
  43. f(1, 2)
  44. ));
  45. BOOST_HANA_RUNTIME_CHECK(hana::equal(
  46. hana::unpack(c, f),
  47. f(1, 2, 3)
  48. ));
  49. BOOST_HANA_RUNTIME_CHECK(hana::equal(
  50. hana::unpack(d, f),
  51. f(1, 2, 3, 4)
  52. ));
  53. }
  54. static_assert(hana::Foldable<int[3]>::value, "");
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. // Searchable
  58. //////////////////////////////////////////////////////////////////////////
  59. {
  60. // any_of
  61. {
  62. static_assert(
  63. hana::not_(hana::any_of(array<int, 1>{0}, hana::equal.to(1)))
  64. , "");
  65. static_assert(
  66. hana::any_of(array<int, 2>{0, 1}, hana::equal.to(0))
  67. , "");
  68. static_assert(
  69. hana::any_of(array<int, 2>{0, 1}, hana::equal.to(1))
  70. , "");
  71. static_assert(
  72. hana::not_(hana::any_of(array<int, 2>{0, 1}, hana::equal.to(2)))
  73. , "");
  74. static_assert(
  75. hana::any_of(array<int, 3>{0, 1, 2}, hana::equal.to(0))
  76. , "");
  77. static_assert(
  78. hana::any_of(array<int, 3>{0, 1, 2}, hana::equal.to(1))
  79. , "");
  80. static_assert(
  81. hana::any_of(array<int, 3>{0, 1, 2}, hana::equal.to(2))
  82. , "");
  83. static_assert(
  84. hana::not_(hana::any_of(array<int, 3>{0, 1, 2}, hana::equal.to(3)))
  85. , "");
  86. }
  87. // find_if
  88. // Note: Because we need the predicate to return a Constant, this
  89. // is incredibly not powerful.
  90. {
  91. static_assert(hana::equal(
  92. hana::find_if(array<int, 1>{0}, hana::always(hana::true_c)),
  93. hana::just(0)
  94. ), "");
  95. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  96. hana::find_if(array<int, 1>{0}, hana::always(hana::false_c)),
  97. hana::nothing
  98. ));
  99. }
  100. static_assert(hana::Searchable<int[3]>::value, "");
  101. }
  102. }