1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <base/functional/bind.h>
17 #include <base/functional/callback.h>
18 #include <base/threading/platform_thread.h>
19 #include <gtest/gtest.h>
20 #include <sys/capability.h>
21 #include <syscall.h>
22 
23 #include <condition_variable>
24 #include <memory>
25 #include <mutex>
26 
27 class BaseBindThreadTest : public ::testing::Test {
28 public:
29 protected:
30 };
31 
32 namespace {
33 struct Vars {
34   int a{0};
35   int b{0};
36   int c{0};
37 
operator ==__anon84fdfd260111::Vars38   bool operator==(const Vars& rhs) const { return a == rhs.a && b == rhs.b && c == rhs.c; }
39 } g_vars;
40 
func()41 void func() {}
func_a(int a)42 void func_a(int a) { g_vars.a = a; }
func_ab(int a,int b)43 void func_ab(int a, int b) {
44   func_a(a);
45   g_vars.b = b;
46 }
func_abc(int a,int b,int c)47 void func_abc(int a, int b, int c) {
48   func_ab(a, b);
49   g_vars.c = c;
50 }
51 }  // namespace
52 
TEST_F(BaseBindThreadTest,simple)53 TEST_F(BaseBindThreadTest, simple) {
54   struct Vars v;
55   g_vars = {};
56   base::Callback<void()> cb0 = base::Bind(&func);
57   cb0.Run();
58   ASSERT_EQ(g_vars, v);
59 
60   v = {};
61   v.a = 1;
62   g_vars = {};
63   base::Callback<void()> cb1 = base::Bind(&func_a, 1);
64   cb1.Run();
65   ASSERT_EQ(g_vars, v);
66 
67   v = {};
68   v.a = 1;
69   v.b = 2;
70   g_vars = {};
71   base::Callback<void()> cb2 = base::Bind(&func_ab, 1, 2);
72   cb2.Run();
73   ASSERT_EQ(g_vars, v);
74 
75   v = {};
76   v.a = 1;
77   v.b = 2;
78   v.c = 3;
79   g_vars = {};
80   base::Callback<void()> cb3 = base::Bind(&func_abc, 1, 2, 3);
81   cb3.Run();
82   ASSERT_EQ(g_vars, v);
83 }
84 
TEST_F(BaseBindThreadTest,bind_first_arg)85 TEST_F(BaseBindThreadTest, bind_first_arg) {
86   struct Vars v;
87   g_vars = {};
88   base::Callback<void()> cb0 = base::Bind(&func);
89   cb0.Run();
90   ASSERT_EQ(g_vars, v);
91 
92   v = {};
93   v.a = 1;
94   g_vars = {};
95   base::Callback<void()> cb1 = base::Bind(&func_a, 1);
96   cb1.Run();
97   ASSERT_EQ(g_vars, v);
98 
99   v = {};
100   v.a = 1;
101   v.b = 2;
102   g_vars = {};
103   base::Callback<void(int)> cb2 = base::Bind(&func_ab, 1);
104   cb2.Run(2);
105   ASSERT_EQ(g_vars, v);
106 
107   v = {};
108   v.a = 1;
109   v.b = 2;
110   v.c = 3;
111   g_vars = {};
112   base::Callback<void(int, int)> cb3 = base::Bind(&func_abc, 1);
113   cb3.Run(2, 3);
114   ASSERT_EQ(g_vars, v);
115 }
116