• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "os/log.h"
28 
29 class BaseBindThreadTest : public ::testing::Test {
30  public:
31  protected:
32 };
33 
34 namespace {
35 struct Vars {
36   int a{0};
37   int b{0};
38   int c{0};
39 
operator ==__anonbcd6a7040111::Vars40   bool operator==(const Vars& rhs) const {
41     return (a == rhs.a && b == rhs.b && c == rhs.c);
42   }
43 
44 } g_vars;
45 
func()46 void func() {}
func_a(int a)47 void func_a(int a) { g_vars.a = a; }
func_ab(int a,int b)48 void func_ab(int a, int b) {
49   func_a(a);
50   g_vars.b = b;
51 }
func_abc(int a,int b,int c)52 void func_abc(int a, int b, int c) {
53   func_ab(a, b);
54   g_vars.c = c;
55 }
56 }  // namespace
57 
TEST_F(BaseBindThreadTest,simple)58 TEST_F(BaseBindThreadTest, simple) {
59   struct Vars v;
60   g_vars = {};
61   base::Callback<void()> cb0 = base::Bind(&func);
62   cb0.Run();
63   ASSERT_EQ(g_vars, v);
64 
65   v = {};
66   v.a = 1;
67   g_vars = {};
68   base::Callback<void()> cb1 = base::Bind(&func_a, 1);
69   cb1.Run();
70   ASSERT_EQ(g_vars, v);
71 
72   v = {};
73   v.a = 1;
74   v.b = 2;
75   g_vars = {};
76   base::Callback<void()> cb2 = base::Bind(&func_ab, 1, 2);
77   cb2.Run();
78   ASSERT_EQ(g_vars, v);
79 
80   v = {};
81   v.a = 1;
82   v.b = 2;
83   v.c = 3;
84   g_vars = {};
85   base::Callback<void()> cb3 = base::Bind(&func_abc, 1, 2, 3);
86   cb3.Run();
87   ASSERT_EQ(g_vars, v);
88 }
89 
TEST_F(BaseBindThreadTest,bind_first_arg)90 TEST_F(BaseBindThreadTest, bind_first_arg) {
91   struct Vars v;
92   g_vars = {};
93   base::Callback<void()> cb0 = base::Bind(&func);
94   cb0.Run();
95   ASSERT_EQ(g_vars, v);
96 
97   v = {};
98   v.a = 1;
99   g_vars = {};
100   base::Callback<void()> cb1 = base::Bind(&func_a, 1);
101   cb1.Run();
102   ASSERT_EQ(g_vars, v);
103 
104   v = {};
105   v.a = 1;
106   v.b = 2;
107   g_vars = {};
108   base::Callback<void(int)> cb2 = base::Bind(&func_ab, 1);
109   cb2.Run(2);
110   ASSERT_EQ(g_vars, v);
111 
112   v = {};
113   v.a = 1;
114   v.b = 2;
115   v.c = 3;
116   g_vars = {};
117   base::Callback<void(int, int)> cb3 = base::Bind(&func_abc, 1);
118   cb3.Run(2, 3);
119   ASSERT_EQ(g_vars, v);
120 }
121