diff options
author | 2021-09-22 14:48:36 -0700 | |
---|---|---|
committer | 2021-10-14 20:28:52 +0000 | |
commit | 401284465b58abe8169b9b9786921dd652a52020 (patch) | |
tree | 582f612cdc9542010239268f4e1eb2787c6ba0c7 | |
parent | 756a12ddf277e6598b4c637776f072b23e5b0c8e (diff) |
legacy: Add common/base_bind_unittest.cc
Bug: 188077107
Tag: #refactor
Test: gd/cert/run
Change-Id: I17bd19f50dab86849feb38654d26fad3acb906ea
-rw-r--r-- | system/common/Android.bp | 1 | ||||
-rw-r--r-- | system/common/base_bind_unittest.cc | 120 |
2 files changed, 121 insertions, 0 deletions
diff --git a/system/common/Android.bp b/system/common/Android.bp index 3cb77b21f2..e23b8f17ec 100644 --- a/system/common/Android.bp +++ b/system/common/Android.bp @@ -66,6 +66,7 @@ cc_test { ], srcs: [ "address_obfuscator_unittest.cc", + "base_bind_unittest.cc", "leaky_bonded_queue_unittest.cc", "lru_unittest.cc", "message_loop_thread_unittest.cc", diff --git a/system/common/base_bind_unittest.cc b/system/common/base_bind_unittest.cc new file mode 100644 index 0000000000..dc4d0682ed --- /dev/null +++ b/system/common/base_bind_unittest.cc @@ -0,0 +1,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); +} |