mutable.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. mutable.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/hof/mutable.hpp>
  8. #include <boost/hof/lazy.hpp>
  9. #include <boost/hof/detail/move.hpp>
  10. #include <memory>
  11. #include "test.hpp"
  12. struct mutable_fun
  13. {
  14. int x;
  15. mutable_fun() noexcept
  16. : x(1)
  17. {}
  18. int operator()(int i) noexcept
  19. {
  20. x+=i;
  21. return x;
  22. }
  23. };
  24. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  25. BOOST_HOF_TEST_CASE()
  26. {
  27. static_assert(noexcept(boost::hof::mutable_(mutable_fun())(3)), "noexcept mutable_");
  28. }
  29. #endif
  30. BOOST_HOF_TEST_CASE()
  31. {
  32. BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_fun())(3) == 4);
  33. }
  34. BOOST_HOF_TEST_CASE()
  35. {
  36. auto mut_fun = mutable_fun();
  37. auto by_5 = boost::hof::lazy(boost::hof::mutable_(std::ref(mut_fun)))(5);
  38. BOOST_HOF_TEST_CHECK(by_5() == 6);
  39. BOOST_HOF_TEST_CHECK(by_5() == 11);
  40. }
  41. struct mutable_move_fun
  42. {
  43. std::unique_ptr<int> x;
  44. mutable_move_fun() : x(new int(1))
  45. {}
  46. int operator()(int i)
  47. {
  48. *x+=i;
  49. return *x;
  50. }
  51. };
  52. BOOST_HOF_TEST_CASE()
  53. {
  54. BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_move_fun())(3) == 4);
  55. }
  56. BOOST_HOF_TEST_CASE()
  57. {
  58. auto mut_fun = mutable_move_fun();
  59. auto by_5 = boost::hof::lazy(boost::hof::mutable_(boost::hof::move(mut_fun)))(5);
  60. BOOST_HOF_TEST_CHECK(by_5() == 6);
  61. BOOST_HOF_TEST_CHECK(by_5() == 11);
  62. }