test_invoke.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright Oliver Kowalke 2009.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <iostream>
  6. #include <memory>
  7. #include <sstream>
  8. #include <stdexcept>
  9. #include <string>
  10. #include <tuple>
  11. #include <utility>
  12. #include <boost/assert.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/context/detail/invoke.hpp>
  15. #include <boost/context/detail/config.hpp>
  16. namespace ctx = boost::context;
  17. struct callable {
  18. int k{ 0 };
  19. callable() = default;
  20. callable( int k_) :
  21. k{ k_ } {
  22. }
  23. int foo( int i, int j) const {
  24. return i + j + k;
  25. }
  26. int operator()( int i, int j) const {
  27. return foo( i, j);
  28. }
  29. };
  30. struct movable {
  31. int k{ 0 };
  32. movable() = default;
  33. movable( int k_) :
  34. k{ k_ } {
  35. }
  36. movable( movable const&) = delete;
  37. movable & operator=( movable const&) = delete;
  38. movable( movable && other) :
  39. k{ other.k } {
  40. other.k = -1;
  41. }
  42. movable & operator=( movable && other) {
  43. if ( this == & other) return * this;
  44. k = other.k;
  45. other.k = -1;
  46. return * this;
  47. }
  48. int foo( int i, int j) const {
  49. return i + j + k;
  50. }
  51. int operator()( int i, int j) const {
  52. return foo( i, j);
  53. }
  54. };
  55. int fn1( int i, int j) {
  56. return i + j;
  57. }
  58. int * fn2( int * ip) {
  59. return ip;
  60. }
  61. int * fn3( int & ir) {
  62. return & ir;
  63. }
  64. int & fn4( int & ir) {
  65. return ir;
  66. }
  67. template< typename T >
  68. int fn5( int i, T && t_) {
  69. T t = std::forward< T >( t_);
  70. return i + t.k;
  71. }
  72. void test1() {
  73. int result = ctx::detail::invoke( fn1, 1, 2);
  74. BOOST_CHECK_EQUAL( result, 3);
  75. }
  76. void test2() {
  77. {
  78. int i = 3;
  79. int * ip = & i;
  80. int * result = ctx::detail::invoke( fn2, ip);
  81. BOOST_CHECK_EQUAL( result, ip);
  82. BOOST_CHECK_EQUAL( * result, i);
  83. }
  84. {
  85. int i = 3;
  86. int * result = ctx::detail::invoke( fn2, & i);
  87. BOOST_CHECK_EQUAL( result, & i);
  88. BOOST_CHECK_EQUAL( * result, i);
  89. }
  90. }
  91. void test3() {
  92. {
  93. int i = 3;
  94. int & ir = i;
  95. int * result = ctx::detail::invoke( fn3, ir);
  96. BOOST_CHECK_EQUAL( result, & ir);
  97. BOOST_CHECK_EQUAL( * result, i);
  98. }
  99. {
  100. int i = 3;
  101. int * result = ctx::detail::invoke( fn3, i);
  102. BOOST_CHECK_EQUAL( result, & i);
  103. BOOST_CHECK_EQUAL( * result, i);
  104. }
  105. }
  106. void test4() {
  107. {
  108. int i = 3;
  109. int & ir = i;
  110. int & result = ctx::detail::invoke( fn4, ir);
  111. BOOST_CHECK_EQUAL( result, ir);
  112. BOOST_CHECK_EQUAL( & result, & ir);
  113. BOOST_CHECK_EQUAL( result, i);
  114. }
  115. {
  116. int i = 3;
  117. int & result = ctx::detail::invoke( fn4, i);
  118. BOOST_CHECK_EQUAL( & result, & i);
  119. BOOST_CHECK_EQUAL( result, i);
  120. }
  121. }
  122. void test5() {
  123. {
  124. callable c( 5);
  125. int result = ctx::detail::invoke( fn5< callable >, 1, std::move( c) );
  126. BOOST_CHECK_EQUAL( result, 6);
  127. BOOST_CHECK_EQUAL( c.k, 5);
  128. }
  129. {
  130. movable m( 5);
  131. int result = ctx::detail::invoke( fn5< movable >, 1, std::move( m) );
  132. BOOST_CHECK_EQUAL( result, 6);
  133. BOOST_CHECK_EQUAL( m.k, -1);
  134. }
  135. }
  136. void test6() {
  137. {
  138. callable c;
  139. int result = ctx::detail::invoke( c, 1, 2);
  140. BOOST_CHECK_EQUAL( result, 3);
  141. BOOST_CHECK_EQUAL( c.k, 0);
  142. }
  143. {
  144. callable c;
  145. int result = ctx::detail::invoke( & callable::foo, c, 1, 2);
  146. BOOST_CHECK_EQUAL( result, 3);
  147. BOOST_CHECK_EQUAL( c.k, 0);
  148. }
  149. }
  150. void test7() {
  151. {
  152. int result = ctx::detail::invoke( movable{}, 1, 2);
  153. BOOST_CHECK_EQUAL( result, 3);
  154. }
  155. {
  156. int result = ctx::detail::invoke( & movable::foo, movable{}, 1, 2);
  157. BOOST_CHECK_EQUAL( result, 3);
  158. }
  159. }
  160. template< typename R, typename Fn, typename ... Args >
  161. R apply( Fn && fn, Args && ... args) {
  162. return ctx::detail::invoke(
  163. std::forward< Fn >( fn),
  164. std::forward< Args >( args) ... );
  165. }
  166. void test8() {
  167. {
  168. int result = apply< int >( fn1, 1, 2);
  169. BOOST_CHECK_EQUAL( result, 3);
  170. }
  171. {
  172. int i = 3;
  173. int & ir = i;
  174. int * result = apply< int * >( fn3, ir);
  175. BOOST_CHECK_EQUAL( result, & ir);
  176. BOOST_CHECK_EQUAL( * result, i);
  177. }
  178. {
  179. movable m( 5);
  180. int result = apply< int >( fn5< movable >, 1, std::move( m) );
  181. BOOST_CHECK_EQUAL( result, 6);
  182. BOOST_CHECK_EQUAL( m.k, -1);
  183. }
  184. }
  185. boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
  186. {
  187. boost::unit_test::test_suite * test =
  188. BOOST_TEST_SUITE("Boost.Context: invoke test suite");
  189. test->add( BOOST_TEST_CASE( & test1) );
  190. test->add( BOOST_TEST_CASE( & test2) );
  191. test->add( BOOST_TEST_CASE( & test3) );
  192. test->add( BOOST_TEST_CASE( & test4) );
  193. test->add( BOOST_TEST_CASE( & test5) );
  194. test->add( BOOST_TEST_CASE( & test6) );
  195. test->add( BOOST_TEST_CASE( & test7) );
  196. test->add( BOOST_TEST_CASE( & test8) );
  197. return test;
  198. }