fold_left.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/contains.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/fold_left.hpp>
  8. #include <boost/hana/map.hpp>
  9. #include <boost/hana/permutations.hpp>
  10. #include <boost/hana/transform.hpp>
  11. #include <laws/base.hpp>
  12. #include <support/seq.hpp>
  13. #include <support/minimal_product.hpp>
  14. namespace hana = boost::hana;
  15. template <int i>
  16. auto key() { return hana::test::ct_eq<i>{}; }
  17. template <int i>
  18. auto val() { return hana::test::ct_eq<-i>{}; }
  19. template <int i, int j>
  20. auto p() { return ::minimal_product(key<i>(), val<j>()); }
  21. struct undefined { };
  22. int main() {
  23. auto sequence = ::seq;
  24. // Use pointers to work around a Clang ICE
  25. hana::test::_injection<0> f{};
  26. auto* fp = &f;
  27. hana::test::ct_eq<999> state{};
  28. auto* statep = &state;
  29. auto check = [=](auto ...pairs) {
  30. auto possible_results = hana::transform(hana::permutations(sequence(pairs...)),
  31. [=](auto xs) {
  32. return hana::fold_left(xs, *statep, *fp);
  33. }
  34. );
  35. BOOST_HANA_CONSTANT_CHECK(hana::contains(
  36. possible_results,
  37. hana::fold_left(hana::make_map(pairs...), state, f)
  38. ));
  39. };
  40. check();
  41. check(p<1, 1>());
  42. check(p<1, 1>(), p<2, 2>());
  43. check(p<1, 1>(), p<2, 2>(), p<3, 3>());
  44. check(p<1, 1>(), p<2, 2>(), p<3, 3>(), p<4, 4>());
  45. }