summaryrefslogtreecommitdiff
path: root/system/common/base_bind_unittest.cc
blob: dc4d0682edbc1a2d9fc0a89f4b2332b70a097717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <base/bind.h>
#include <base/callback.h>
#include <base/threading/platform_thread.h>
#include <gtest/gtest.h>
#include <sys/capability.h>
#include <syscall.h>

#include <condition_variable>
#include <memory>
#include <mutex>

#include "osi/include/log.h"

class BaseBindThreadTest : public ::testing::Test {
 public:
 protected:
};

namespace {
struct Vars {
  int a{0};
  int b{0};
  int c{0};

  bool operator==(const Vars& rhs) const {
    return (a == rhs.a && b == rhs.b && c == rhs.c);
  }

} g_vars;

void func() {}
void func_a(int a) { g_vars.a = a; }
void func_ab(int a, int b) {
  func_a(a);
  g_vars.b = b;
}
void func_abc(int a, int b, int c) {
  func_ab(a, b);
  g_vars.c = c;
}
}  // namespace

TEST_F(BaseBindThreadTest, simple) {
  struct Vars v;
  g_vars = {};
  base::Callback<void()> cb0 = base::Bind(&func);
  cb0.Run();
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  g_vars = {};
  base::Callback<void()> cb1 = base::Bind(&func_a, 1);
  cb1.Run();
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  v.b = 2;
  g_vars = {};
  base::Callback<void()> cb2 = base::Bind(&func_ab, 1, 2);
  cb2.Run();
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  v.b = 2;
  v.c = 3;
  g_vars = {};
  base::Callback<void()> cb3 = base::Bind(&func_abc, 1, 2, 3);
  cb3.Run();
  ASSERT_EQ(g_vars, v);
}

TEST_F(BaseBindThreadTest, bind_first_arg) {
  struct Vars v;
  g_vars = {};
  base::Callback<void()> cb0 = base::Bind(&func);
  cb0.Run();
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  g_vars = {};
  base::Callback<void()> cb1 = base::Bind(&func_a, 1);
  cb1.Run();
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  v.b = 2;
  g_vars = {};
  base::Callback<void(int)> cb2 = base::Bind(&func_ab, 1);
  cb2.Run(2);
  ASSERT_EQ(g_vars, v);

  v = {};
  v.a = 1;
  v.b = 2;
  v.c = 3;
  g_vars = {};
  base::Callback<void(int, int)> cb3 = base::Bind(&func_abc, 1);
  cb3.Run(2, 3);
  ASSERT_EQ(g_vars, v);
}