fold_left.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/assert.hpp>
  5. #include <boost/hana/concept/struct.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/fold_left.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include "minimal_struct.hpp"
  10. #include <laws/base.hpp>
  11. #include <support/minimal_product.hpp>
  12. namespace hana = boost::hana;
  13. using hana::test::ct_eq;
  14. template <int i = 0>
  15. struct undefined { };
  16. struct MoveOnly {
  17. MoveOnly() = default;
  18. MoveOnly(MoveOnly&&) = default;
  19. MoveOnly(MoveOnly const&) = delete;
  20. MoveOnly& operator=(MoveOnly&&) = default;
  21. MoveOnly& operator=(MoveOnly const&) = delete;
  22. };
  23. int main() {
  24. constexpr auto pair = ::minimal_product;
  25. ct_eq<999> s{};
  26. hana::test::_injection<0> f{};
  27. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  28. hana::fold_left(obj(), s, undefined<>{}),
  29. s
  30. ));
  31. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  32. hana::fold_left(obj(ct_eq<0>{}), s, f),
  33. f(s, pair(hana::int_c<0>, ct_eq<0>{}))
  34. ));
  35. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  36. hana::fold_left(obj(ct_eq<0>{}, ct_eq<1>{}), s, f),
  37. f(f(s, pair(hana::int_c<0>, ct_eq<0>{})), pair(hana::int_c<1>, ct_eq<1>{}))
  38. ));
  39. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  40. hana::fold_left(obj(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}), s, f),
  41. f(f(f(s, pair(hana::int_c<0>, ct_eq<0>{})),
  42. pair(hana::int_c<1>, ct_eq<1>{})),
  43. pair(hana::int_c<2>, ct_eq<2>{}))
  44. ));
  45. // fold_left with move-only members
  46. hana::fold_left(obj(MoveOnly{}), 0, [](int, auto) { return 0; });
  47. hana::fold_left(obj(MoveOnly{}, MoveOnly{}), 0, [](int, auto) { return 0; });
  48. }