test_apply.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/apply.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. int fn5( int i, callable && c) {
  68. return i + c.k;
  69. }
  70. int fn6( int i, movable && m) {
  71. return i + m.k;
  72. }
  73. void test1() {
  74. int result = ctx::detail::apply( fn1, std::make_tuple( 1, 2) );
  75. BOOST_CHECK_EQUAL( result, 3);
  76. }
  77. void test2() {
  78. {
  79. int i = 3;
  80. int * ip = & i;
  81. int * result = ctx::detail::apply( fn2, std::make_tuple( ip) );
  82. BOOST_CHECK_EQUAL( result, ip);
  83. BOOST_CHECK_EQUAL( * result, i);
  84. }
  85. {
  86. int i = 3;
  87. int * result = ctx::detail::apply( fn2, std::make_tuple( & i) );
  88. BOOST_CHECK_EQUAL( result, & i);
  89. BOOST_CHECK_EQUAL( * result, i);
  90. }
  91. }
  92. void test3() {
  93. {
  94. int i = 'c';
  95. int & ir = i;
  96. int * result = ctx::detail::apply( fn3, std::make_tuple( std::ref( ir) ) );
  97. BOOST_CHECK_EQUAL( result, & ir);
  98. BOOST_CHECK_EQUAL( * result, i);
  99. }
  100. {
  101. int i = 'c';
  102. int * result = ctx::detail::apply( fn3, std::make_tuple( std::ref( i) ) );
  103. BOOST_CHECK_EQUAL( result, & i);
  104. BOOST_CHECK_EQUAL( * result, i);
  105. }
  106. }
  107. void test4() {
  108. {
  109. int i = 3;
  110. int & ir = i;
  111. int & result = ctx::detail::apply( fn4, std::make_tuple( std::ref( ir) ) );
  112. BOOST_CHECK_EQUAL( result, ir);
  113. BOOST_CHECK_EQUAL( & result, & ir);
  114. BOOST_CHECK_EQUAL( result, i);
  115. }
  116. {
  117. int i = 3;
  118. int & result = ctx::detail::apply( fn4, std::make_tuple( std::ref( i) ) );
  119. BOOST_CHECK_EQUAL( & result, & i);
  120. BOOST_CHECK_EQUAL( result, i);
  121. }
  122. }
  123. void test5() {
  124. {
  125. callable c( 5);
  126. int result = ctx::detail::apply( fn5, std::make_tuple( 1, std::move( c) ) );
  127. BOOST_CHECK_EQUAL( result, 6);
  128. BOOST_CHECK_EQUAL( c.k, 5);
  129. }
  130. {
  131. movable m( 5);
  132. int result = ctx::detail::apply( fn6, std::make_tuple( 1, std::move( m) ) );
  133. BOOST_CHECK_EQUAL( result, 6);
  134. BOOST_CHECK_EQUAL( m.k, -1);
  135. }
  136. }
  137. void test6() {
  138. {
  139. callable c;
  140. int result = ctx::detail::apply( c, std::make_tuple( 1, 2) );
  141. BOOST_CHECK_EQUAL( result, 3);
  142. BOOST_CHECK_EQUAL( c.k, 0);
  143. }
  144. {
  145. callable c;
  146. int result = ctx::detail::apply( & callable::foo, std::make_tuple( c, 1, 2) );
  147. BOOST_CHECK_EQUAL( result, 3);
  148. BOOST_CHECK_EQUAL( c.k, 0);
  149. }
  150. }
  151. void test7() {
  152. {
  153. movable m;
  154. int result = ctx::detail::apply( std::move( m), std::make_tuple( 1, 2) );
  155. BOOST_CHECK_EQUAL( result, 3);
  156. }
  157. {
  158. movable m;
  159. int result = ctx::detail::apply( & movable::foo, std::make_tuple( std::move( m), 1, 2) );
  160. BOOST_CHECK_EQUAL( result, 3);
  161. }
  162. }
  163. boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
  164. {
  165. boost::unit_test::test_suite * test =
  166. BOOST_TEST_SUITE("Boost.Context: apply test suite");
  167. test->add( BOOST_TEST_CASE( & test1) );
  168. test->add( BOOST_TEST_CASE( & test2) );
  169. test->add( BOOST_TEST_CASE( & test3) );
  170. test->add( BOOST_TEST_CASE( & test4) );
  171. test->add( BOOST_TEST_CASE( & test5) );
  172. test->add( BOOST_TEST_CASE( & test6) );
  173. test->add( BOOST_TEST_CASE( & test7) );
  174. return test;
  175. }