with_lock_guard_simple.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // (C) Copyright 2013 Ruslan Baratov
  2. // (C) Copyright 2013 Ruslan Baratov
  3. //
  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. // See www.boost.org/libs/thread for documentation.
  7. #define BOOST_THREAD_VERSION 4
  8. #include <boost/detail/lightweight_test.hpp> // BOOST_TEST
  9. #include <boost/thread/mutex.hpp>
  10. #include <boost/thread/with_lock_guard.hpp>
  11. #include <boost/ref.hpp>
  12. void func_with_0_arg() {
  13. }
  14. void func_with_1_arg(int arg_1) {
  15. BOOST_TEST(arg_1 == 3);
  16. }
  17. bool func_with_2_arg(int arg_1, bool arg_2) {
  18. BOOST_TEST(arg_1 == 3);
  19. BOOST_TEST(arg_2 == true);
  20. return !arg_2;
  21. }
  22. int func_with_3_arg(int arg_1, bool arg_2, const char* arg_3) {
  23. BOOST_TEST(arg_1 == 13);
  24. BOOST_TEST(arg_2 == false);
  25. BOOST_TEST(std::string(arg_3) == "message for func with 3 arg");
  26. return 12;
  27. }
  28. const char* func_with_4_arg(int arg_1, bool arg_2, int* arg_3, int& arg_4) {
  29. BOOST_TEST(arg_1 == 23);
  30. BOOST_TEST(arg_2 == false);
  31. *arg_3 = 128;
  32. arg_4 = 456;
  33. return "hello";
  34. }
  35. void test_simple() {
  36. boost::mutex m;
  37. // #0
  38. boost::with_lock_guard(m, func_with_0_arg);
  39. // #1
  40. boost::with_lock_guard(m, func_with_1_arg, 3);
  41. // #2
  42. bool res2 = boost::with_lock_guard(m, func_with_2_arg, 3, true);
  43. BOOST_TEST(res2 == false);
  44. // #3
  45. int arg1 = 13;
  46. const char* mes = "message for func with 3 arg";
  47. int res3 = boost::with_lock_guard(m, func_with_3_arg, arg1, false, mes);
  48. BOOST_TEST(res3 == 12);
  49. // #4
  50. int arg3 = 0;
  51. int arg4 = 0;
  52. const char* res4 = boost::with_lock_guard(
  53. m,
  54. func_with_4_arg,
  55. 23,
  56. false,
  57. &arg3,
  58. boost::ref(arg4)
  59. );
  60. BOOST_TEST(arg3 == 128);
  61. BOOST_TEST(arg4 == 456);
  62. BOOST_TEST(std::string(res4) == "hello");
  63. }
  64. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  65. void test_variadic_templates() {
  66. std::cout << "C++11 variadic templates disabled" << std::endl;
  67. }
  68. #else
  69. int func_with_5_args(int a1, char a2, int& a3, bool* a4, bool a5) {
  70. BOOST_TEST(a1 == 12);
  71. BOOST_TEST(a2 == 'x');
  72. BOOST_TEST(a5 == false);
  73. a3 = 135;
  74. *a4 = false;
  75. return 45;
  76. }
  77. int func_with_6_args(int a1, char a2, int& a3, bool* a4, int&& a5, bool a6) {
  78. BOOST_TEST(a1 == 12);
  79. BOOST_TEST(a2 == 'N');
  80. BOOST_TEST(a5 == 2 || a5 == 13);
  81. BOOST_TEST(a6 == false);
  82. a3 = 200;
  83. *a4 = true;
  84. return 888;
  85. }
  86. void test_variadic_templates() {
  87. boost::mutex m;
  88. int a3 = 0;
  89. bool a4 = true;
  90. int res5 = boost::with_lock_guard(
  91. m, func_with_5_args, 12, 'x', a3, &a4, false
  92. );
  93. BOOST_TEST(a3 == 135);
  94. BOOST_TEST(a4 == false);
  95. BOOST_TEST(res5 == 45);
  96. int res6 = boost::with_lock_guard(
  97. m, func_with_6_args, 12, 'N', a3, &a4, 2, false
  98. );
  99. BOOST_TEST(a3 == 200);
  100. BOOST_TEST(a4 == true);
  101. BOOST_TEST(res6 == 888);
  102. a3 = 0;
  103. a4 = false;
  104. int a5 = 13;
  105. int res6_move = boost::with_lock_guard(
  106. m, func_with_6_args, 12, 'N', a3, &a4, boost::move(a5), false
  107. );
  108. BOOST_TEST(a3 == 200);
  109. BOOST_TEST(a4 == true);
  110. BOOST_TEST_EQ(res6_move, 888);
  111. }
  112. #endif
  113. int main() {
  114. test_simple();
  115. test_variadic_templates();
  116. return boost::report_errors();
  117. }