protect.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. protect.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/protect.hpp>
  8. #include <boost/hof/lazy.hpp>
  9. #include <boost/hof/placeholders.hpp>
  10. #include <memory>
  11. #include "test.hpp"
  12. #include <boost/hof/function.hpp>
  13. int f(int x)
  14. {
  15. return x;
  16. }
  17. int& g(int& x)
  18. {
  19. return x;
  20. }
  21. template<class T>
  22. const T& constify(const T& arg)
  23. {
  24. return arg;
  25. }
  26. BOOST_HOF_TEST_CASE()
  27. {
  28. int i[9] = {0,1,2,3,4,5,6,7,8};
  29. // non-const
  30. // test nullary
  31. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(1))() == 1);
  32. // test lvalues
  33. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0]) == &i[0]);
  34. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1]) == &i[0]);
  35. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1]) == &i[1]);
  36. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2]) == &i[0]);
  37. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2]) == &i[1]);
  38. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2]) == &i[2]);
  39. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3]) == &i[0]);
  40. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3]) == &i[1]);
  41. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3]) == &i[2]);
  42. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3]) == &i[3]);
  43. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3], i[4]) == &i[0]);
  44. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3], i[4]) == &i[1]);
  45. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3], i[4]) == &i[2]);
  46. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3], i[4]) == &i[3]);
  47. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5))(i[0], i[1], i[2], i[3], i[4]) == &i[4]);
  48. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[0]);
  49. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[1]);
  50. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[2]);
  51. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[3]);
  52. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[4]);
  53. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[5]);
  54. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[0]);
  55. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[1]);
  56. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[2]);
  57. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[3]);
  58. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[4]);
  59. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[5]);
  60. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[6]);
  61. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[0]);
  62. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[1]);
  63. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[2]);
  64. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[3]);
  65. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[4]);
  66. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[5]);
  67. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[6]);
  68. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_8))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[7]);
  69. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[0]);
  70. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[1]);
  71. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[2]);
  72. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[3]);
  73. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[4]);
  74. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[5]);
  75. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[6]);
  76. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_8))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[7]);
  77. BOOST_HOF_TEST_CHECK(&boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_9))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[8]);
  78. // test rvalues
  79. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0) == 0);
  80. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1) == 0);
  81. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1) == 1);
  82. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2) == 0);
  83. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2) == 1);
  84. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2) == 2);
  85. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3) == 0);
  86. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3) == 1);
  87. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3) == 2);
  88. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3) == 3);
  89. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3, 4) == 0);
  90. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3, 4) == 1);
  91. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3, 4) == 2);
  92. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3, 4) == 3);
  93. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5))(0, 1, 2, 3, 4) == 4);
  94. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3, 4, 5) == 0);
  95. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3, 4, 5) == 1);
  96. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3, 4, 5) == 2);
  97. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3, 4, 5) == 3);
  98. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5))(0, 1, 2, 3, 4, 5) == 4);
  99. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6))(0, 1, 2, 3, 4, 5) == 5);
  100. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3, 4, 5, 6) == 0);
  101. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3, 4, 5, 6) == 1);
  102. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3, 4, 5, 6) == 2);
  103. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3, 4, 5, 6) == 3);
  104. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5))(0, 1, 2, 3, 4, 5, 6) == 4);
  105. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6))(0, 1, 2, 3, 4, 5, 6) == 5);
  106. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7))(0, 1, 2, 3, 4, 5, 6) == 6);
  107. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3, 4, 5, 6, 7) == 0);
  108. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3, 4, 5, 6, 7) == 1);
  109. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3, 4, 5, 6, 7) == 2);
  110. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3, 4, 5, 6, 7) == 3);
  111. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5))(0, 1, 2, 3, 4, 5, 6, 7) == 4);
  112. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6))(0, 1, 2, 3, 4, 5, 6, 7) == 5);
  113. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7))(0, 1, 2, 3, 4, 5, 6, 7) == 6);
  114. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_8))(0, 1, 2, 3, 4, 5, 6, 7) == 7);
  115. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 0);
  116. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 1);
  117. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 2);
  118. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 3);
  119. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 4);
  120. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 5);
  121. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 6);
  122. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_8))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 7);
  123. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_9))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 8);
  124. // test mixed perfect forwarding
  125. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(i[0], 1) == 0);
  126. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(i[0], 1) == 1);
  127. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1))(0, i[1]) == 0);
  128. BOOST_HOF_TEST_CHECK(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2))(0, i[1]) == 1);
  129. // const
  130. // test nullary
  131. BOOST_HOF_TEST_CHECK(constify(constify(boost::hof::protect(boost::hof::lazy(f)(1))))() == 1);
  132. // test lvalues
  133. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))))(i[0]) == &i[0]);
  134. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))))(i[0], i[1]) == &i[0]);
  135. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))))(i[0], i[1]) == &i[1]);
  136. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1))))(i[0], i[1], i[2]) == &i[0]);
  137. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2))))(i[0], i[1], i[2]) == &i[1]);
  138. BOOST_HOF_TEST_CHECK(&constify(constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3))))(i[0], i[1], i[2]) == &i[2]);
  139. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3]) == &i[0]);
  140. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3]) == &i[1]);
  141. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3]) == &i[2]);
  142. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3]) == &i[3]);
  143. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3], i[4]) == &i[0]);
  144. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3], i[4]) == &i[1]);
  145. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3], i[4]) == &i[2]);
  146. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3], i[4]) == &i[3]);
  147. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5)))(i[0], i[1], i[2], i[3], i[4]) == &i[4]);
  148. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[0]);
  149. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[1]);
  150. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[2]);
  151. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[3]);
  152. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[4]);
  153. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6)))(i[0], i[1], i[2], i[3], i[4], i[5]) == &i[5]);
  154. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[0]);
  155. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[1]);
  156. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[2]);
  157. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[3]);
  158. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[4]);
  159. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[5]);
  160. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6]) == &i[6]);
  161. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[0]);
  162. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[1]);
  163. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[2]);
  164. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[3]);
  165. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[4]);
  166. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[5]);
  167. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[6]);
  168. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_8)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]) == &i[7]);
  169. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_1)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[0]);
  170. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_2)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[1]);
  171. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_3)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[2]);
  172. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_4)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[3]);
  173. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_5)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[4]);
  174. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_6)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[5]);
  175. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_7)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[6]);
  176. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_8)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[7]);
  177. BOOST_HOF_TEST_CHECK(&constify(boost::hof::protect(boost::hof::lazy(g)(std::placeholders::_9)))(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8]) == &i[8]);
  178. // test rvalues
  179. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0) == 0);
  180. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1) == 0);
  181. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1) == 1);
  182. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2) == 0);
  183. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2) == 1);
  184. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2) == 2);
  185. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3) == 0);
  186. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3) == 1);
  187. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3) == 2);
  188. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3) == 3);
  189. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3, 4) == 0);
  190. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3, 4) == 1);
  191. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3, 4) == 2);
  192. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3, 4) == 3);
  193. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5)))(0, 1, 2, 3, 4) == 4);
  194. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3, 4, 5) == 0);
  195. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3, 4, 5) == 1);
  196. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3, 4, 5) == 2);
  197. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3, 4, 5) == 3);
  198. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5)))(0, 1, 2, 3, 4, 5) == 4);
  199. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6)))(0, 1, 2, 3, 4, 5) == 5);
  200. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3, 4, 5, 6) == 0);
  201. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3, 4, 5, 6) == 1);
  202. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3, 4, 5, 6) == 2);
  203. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3, 4, 5, 6) == 3);
  204. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5)))(0, 1, 2, 3, 4, 5, 6) == 4);
  205. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6)))(0, 1, 2, 3, 4, 5, 6) == 5);
  206. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7)))(0, 1, 2, 3, 4, 5, 6) == 6);
  207. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3, 4, 5, 6, 7) == 0);
  208. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3, 4, 5, 6, 7) == 1);
  209. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3, 4, 5, 6, 7) == 2);
  210. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3, 4, 5, 6, 7) == 3);
  211. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5)))(0, 1, 2, 3, 4, 5, 6, 7) == 4);
  212. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6)))(0, 1, 2, 3, 4, 5, 6, 7) == 5);
  213. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7)))(0, 1, 2, 3, 4, 5, 6, 7) == 6);
  214. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_8)))(0, 1, 2, 3, 4, 5, 6, 7) == 7);
  215. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 0);
  216. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 1);
  217. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_3)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 2);
  218. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_4)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 3);
  219. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_5)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 4);
  220. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_6)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 5);
  221. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_7)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 6);
  222. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_8)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 7);
  223. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_9)))(0, 1, 2, 3, 4, 5, 6, 7, 8) == 8);
  224. // test mixed perfect forwarding
  225. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(i[0], 1) == 0);
  226. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(i[0], 1) == 1);
  227. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_1)))(0, i[1]) == 0);
  228. BOOST_HOF_TEST_CHECK(constify(boost::hof::protect(boost::hof::lazy(f)(std::placeholders::_2)))(0, i[1]) == 1);
  229. }
  230. BOOST_HOF_TEST_CASE()
  231. {
  232. BOOST_HOF_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(boost::hof::identity)(boost::hof::_1)), boost::hof::_1)(17) == 17);
  233. BOOST_HOF_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(boost::hof::identity)(boost::hof::_1)), 17)() == 17);
  234. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(boost::hof::identity)(boost::hof::_1)), boost::hof::_1)(17) == 17);
  235. BOOST_HOF_STATIC_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(boost::hof::identity)(boost::hof::_1)), 17)() == 17);
  236. }
  237. namespace test1 {
  238. int id(int x)
  239. {
  240. return x;
  241. }
  242. BOOST_HOF_TEST_CASE()
  243. {
  244. BOOST_HOF_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(id)(std::placeholders::_1)), std::placeholders::_1)(17) == 17);
  245. BOOST_HOF_TEST_CHECK(boost::hof::lazy(boost::hof::apply)(boost::hof::protect(boost::hof::lazy(id)(std::placeholders::_1)), 17)() == 17);
  246. }
  247. }