invoke_rvalue_pass.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2014 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/detail/invoke.hpp>
  14. #include <boost/thread/detail/invoke.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. int count = 0;
  17. // 1 arg, return void
  18. void f_void_1(int i)
  19. {
  20. count += i;
  21. }
  22. struct A_void_1
  23. {
  24. void operator()(int i)
  25. {
  26. count += i;
  27. }
  28. void mem1() {++count;}
  29. void mem2() const {count += 2;}
  30. };
  31. void
  32. test_void_1()
  33. {
  34. int save_count = count;
  35. // function
  36. {
  37. boost::detail::invoke(f_void_1, 2);
  38. BOOST_TEST(count == save_count + 2);
  39. save_count = count;
  40. }
  41. // function pointer
  42. {
  43. void (*fp)(int) = f_void_1;
  44. boost::detail::invoke(fp, 3);
  45. BOOST_TEST(count == save_count+3);
  46. save_count = count;
  47. }
  48. // functor
  49. {
  50. A_void_1 a0;
  51. #if defined BOOST_THREAD_PROVIDES_INVOKE
  52. boost::detail::invoke(a0, 4);
  53. BOOST_TEST(count == save_count+4);
  54. save_count = count;
  55. #endif
  56. boost::detail::invoke<void>(a0, 4);
  57. BOOST_TEST(count == save_count+4);
  58. save_count = count;
  59. }
  60. // member function pointer
  61. {
  62. #if defined BOOST_THREAD_PROVIDES_INVOKE
  63. void (A_void_1::*fp)() = &A_void_1::mem1;
  64. boost::detail::invoke(fp, A_void_1());
  65. BOOST_TEST(count == save_count+1);
  66. save_count = count;
  67. //BUG
  68. boost::detail::invoke<void>(fp, A_void_1());
  69. BOOST_TEST(count == save_count+1);
  70. save_count = count;
  71. #endif
  72. #if defined BOOST_THREAD_PROVIDES_INVOKE
  73. A_void_1 a;
  74. boost::detail::invoke(fp, &a);
  75. BOOST_TEST(count == save_count+1);
  76. save_count = count;
  77. //BUG
  78. boost::detail::invoke<int>(fp, &a);
  79. BOOST_TEST(count == save_count+1);
  80. save_count = count;
  81. #endif
  82. }
  83. // const member function pointer
  84. {
  85. void (A_void_1::*fp)() const = &A_void_1::mem2;
  86. boost::detail::invoke(fp, A_void_1());
  87. BOOST_TEST(count == save_count+2);
  88. save_count = count;
  89. A_void_1 a;
  90. boost::detail::invoke(fp, &a);
  91. BOOST_TEST(count == save_count+2);
  92. save_count = count;
  93. }
  94. }
  95. // 1 arg, return int
  96. int f_int_1(int i)
  97. {
  98. return i + 1;
  99. }
  100. struct A_int_1
  101. {
  102. A_int_1() : data_(5) {}
  103. int operator()(int i)
  104. {
  105. return i - 1;
  106. }
  107. int mem1() {return 3;}
  108. int mem2() const {return 4;}
  109. int data_;
  110. };
  111. void
  112. test_int_1()
  113. {
  114. // function
  115. {
  116. BOOST_TEST(boost::detail::invoke(f_int_1, 2) == 3);
  117. }
  118. // function pointer
  119. {
  120. int (*fp)(int) = f_int_1;
  121. BOOST_TEST(boost::detail::invoke(fp, 3) == 4);
  122. }
  123. // functor
  124. {
  125. #if defined BOOST_THREAD_PROVIDES_INVOKE
  126. BOOST_TEST(boost::detail::invoke(A_int_1(), 4) == 3);
  127. BOOST_TEST(boost::detail::invoke<int>(A_int_1(), 4) == 3);
  128. #endif
  129. }
  130. // member function pointer
  131. {
  132. #if defined BOOST_THREAD_PROVIDES_INVOKE
  133. BOOST_TEST(boost::detail::invoke(&A_int_1::mem1, A_int_1()) == 3);
  134. BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, A_int_1()) == 3);
  135. #endif
  136. A_int_1 a;
  137. BOOST_TEST(boost::detail::invoke(&A_int_1::mem1, &a) == 3);
  138. }
  139. // const member function pointer
  140. {
  141. BOOST_TEST(boost::detail::invoke(&A_int_1::mem2, A_int_1()) == 4);
  142. A_int_1 a;
  143. BOOST_TEST(boost::detail::invoke(&A_int_1::mem2, &a) == 4);
  144. }
  145. // member data pointer
  146. {
  147. #if defined BOOST_THREAD_PROVIDES_INVOKE
  148. BOOST_TEST(boost::detail::invoke(&A_int_1::data_, A_int_1()) == 5);
  149. BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, A_int_1()) == 5);
  150. A_int_1 a;
  151. BOOST_TEST(boost::detail::invoke(&A_int_1::data_, a) == 5);
  152. boost::detail::invoke(&A_int_1::data_, a) = 6;
  153. BOOST_TEST(boost::detail::invoke(&A_int_1::data_, a) == 6);
  154. BOOST_TEST(boost::detail::invoke(&A_int_1::data_, &a) == 6);
  155. boost::detail::invoke(&A_int_1::data_, &a) = 7;
  156. BOOST_TEST(boost::detail::invoke(&A_int_1::data_, &a) == 7);
  157. #endif
  158. }
  159. }
  160. // 2 arg, return void
  161. void f_void_2(int i, int j)
  162. {
  163. count += i+j;
  164. }
  165. struct A_void_2
  166. {
  167. void operator()(int i, int j)
  168. {
  169. count += i+j;
  170. }
  171. void mem1(int i) {count += i;}
  172. void mem2(int i) const {count += i;}
  173. };
  174. void
  175. test_void_2()
  176. {
  177. int save_count = count;
  178. // function
  179. {
  180. boost::detail::invoke(f_void_2, 2, 3);
  181. BOOST_TEST(count == save_count+5);
  182. save_count = count;
  183. }
  184. // member function pointer
  185. {
  186. #if defined BOOST_THREAD_PROVIDES_INVOKE
  187. boost::detail::invoke(&A_void_2::mem1, A_void_2(), 3);
  188. BOOST_TEST(count == save_count+3);
  189. save_count = count;
  190. boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), 3);
  191. BOOST_TEST(count == save_count+3);
  192. save_count = count;
  193. #endif
  194. }
  195. }
  196. int main()
  197. {
  198. test_void_1();
  199. test_int_1();
  200. test_void_2();
  201. return boost::report_errors();
  202. }