special.fold_left.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/equal.hpp>
  6. #include <boost/hana/fold_left.hpp>
  7. #include <boost/hana/tuple.hpp>
  8. #include <boost/hana/type.hpp>
  9. namespace hana = boost::hana;
  10. template <typename ...>
  11. struct F { struct type; };
  12. struct x0;
  13. struct x1;
  14. struct x2;
  15. struct x3;
  16. int main() {
  17. // tuple_t and initial state
  18. {
  19. auto f = hana::metafunction<F>;
  20. auto s = hana::type_c<struct initial_state>;
  21. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  22. hana::fold_left(hana::tuple_t<>, s, f),
  23. s
  24. ));
  25. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  26. hana::fold_left(hana::tuple_t<x0>, s, f),
  27. f(s, hana::type_c<x0>)
  28. ));
  29. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  30. hana::fold_left(hana::tuple_t<x0, x1>, s, f),
  31. f(f(s, hana::type_c<x0>), hana::type_c<x1>)
  32. ));
  33. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  34. hana::fold_left(hana::tuple_t<x0, x1, x2>, s, f),
  35. f(f(f(s, hana::type_c<x0>), hana::type_c<x1>), hana::type_c<x2>)
  36. ));
  37. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  38. hana::fold_left(hana::tuple_t<x0, x1, x2, x3>, s, f),
  39. f(f(f(f(s, hana::type_c<x0>), hana::type_c<x1>), hana::type_c<x2>), hana::type_c<x3>)
  40. ));
  41. }
  42. // tuple_t and no initial state
  43. {
  44. auto f = hana::metafunction<F>;
  45. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  46. hana::fold_left(hana::tuple_t<x0>, f),
  47. hana::type_c<x0>
  48. ));
  49. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  50. hana::fold_left(hana::tuple_t<x0, x1>, f),
  51. f(hana::type_c<x0>, hana::type_c<x1>)
  52. ));
  53. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  54. hana::fold_left(hana::tuple_t<x0, x1, x2>, f),
  55. f(f(hana::type_c<x0>, hana::type_c<x1>), hana::type_c<x2>)
  56. ));
  57. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  58. hana::fold_left(hana::tuple_t<x0, x1, x2, x3>, f),
  59. f(f(f(hana::type_c<x0>, hana::type_c<x1>), hana::type_c<x2>), hana::type_c<x3>)
  60. ));
  61. }
  62. }