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