1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/function_ref.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #include <functional>
22*8f0ba417SAndroid Build Coastguard Worker #include <string>
23*8f0ba417SAndroid Build Coastguard Worker
24*8f0ba417SAndroid Build Coastguard Worker namespace android::base {
25*8f0ba417SAndroid Build Coastguard Worker
TEST(function_ref,Ctor)26*8f0ba417SAndroid Build Coastguard Worker TEST(function_ref, Ctor) {
27*8f0ba417SAndroid Build Coastguard Worker // Making sure it can be constructed in all meaningful ways
28*8f0ba417SAndroid Build Coastguard Worker
29*8f0ba417SAndroid Build Coastguard Worker using EmptyFunc = function_ref<void()>;
30*8f0ba417SAndroid Build Coastguard Worker
31*8f0ba417SAndroid Build Coastguard Worker EmptyFunc f1([] {});
32*8f0ba417SAndroid Build Coastguard Worker
33*8f0ba417SAndroid Build Coastguard Worker struct Functor {
34*8f0ba417SAndroid Build Coastguard Worker void operator()() const {}
35*8f0ba417SAndroid Build Coastguard Worker };
36*8f0ba417SAndroid Build Coastguard Worker EmptyFunc f2(Functor{});
37*8f0ba417SAndroid Build Coastguard Worker Functor fctr;
38*8f0ba417SAndroid Build Coastguard Worker EmptyFunc f3(fctr);
39*8f0ba417SAndroid Build Coastguard Worker
40*8f0ba417SAndroid Build Coastguard Worker EmptyFunc f4(std::function<void()>([f1, f2, f3] {
41*8f0ba417SAndroid Build Coastguard Worker (void)f1;
42*8f0ba417SAndroid Build Coastguard Worker (void)f2;
43*8f0ba417SAndroid Build Coastguard Worker (void)f3;
44*8f0ba417SAndroid Build Coastguard Worker }));
45*8f0ba417SAndroid Build Coastguard Worker
46*8f0ba417SAndroid Build Coastguard Worker const std::function<void()> func = [] {};
47*8f0ba417SAndroid Build Coastguard Worker EmptyFunc f5(func);
48*8f0ba417SAndroid Build Coastguard Worker
49*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(f1) <= 2 * sizeof(void*), "Too big function_ref");
50*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(f2) <= 2 * sizeof(void*), "Too big function_ref");
51*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(f3) <= 2 * sizeof(void*), "Too big function_ref");
52*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(f4) <= 2 * sizeof(void*), "Too big function_ref");
53*8f0ba417SAndroid Build Coastguard Worker static_assert(sizeof(f5) <= 2 * sizeof(void*), "Too big function_ref");
54*8f0ba417SAndroid Build Coastguard Worker }
55*8f0ba417SAndroid Build Coastguard Worker
TEST(function_ref,Call)56*8f0ba417SAndroid Build Coastguard Worker TEST(function_ref, Call) {
57*8f0ba417SAndroid Build Coastguard Worker function_ref<int(int)> view = [](int i) { return i + 1; };
58*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(1, view(0));
59*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(-1, view(-2));
60*8f0ba417SAndroid Build Coastguard Worker
61*8f0ba417SAndroid Build Coastguard Worker function_ref<std::string(std::string)> fs = [](const std::string& s) { return s + "1"; };
62*8f0ba417SAndroid Build Coastguard Worker EXPECT_STREQ("s1", fs("s").c_str());
63*8f0ba417SAndroid Build Coastguard Worker EXPECT_STREQ("ssss1", fs("ssss").c_str());
64*8f0ba417SAndroid Build Coastguard Worker
65*8f0ba417SAndroid Build Coastguard Worker std::string base;
66*8f0ba417SAndroid Build Coastguard Worker auto lambda = [&base]() { return base + "1"; };
67*8f0ba417SAndroid Build Coastguard Worker function_ref<std::string()> fs2 = lambda;
68*8f0ba417SAndroid Build Coastguard Worker base = "one";
69*8f0ba417SAndroid Build Coastguard Worker EXPECT_STREQ("one1", fs2().c_str());
70*8f0ba417SAndroid Build Coastguard Worker base = "forty two";
71*8f0ba417SAndroid Build Coastguard Worker EXPECT_STREQ("forty two1", fs2().c_str());
72*8f0ba417SAndroid Build Coastguard Worker }
73*8f0ba417SAndroid Build Coastguard Worker
TEST(function_ref,CopyAndAssign)74*8f0ba417SAndroid Build Coastguard Worker TEST(function_ref, CopyAndAssign) {
75*8f0ba417SAndroid Build Coastguard Worker function_ref<int(int)> view = [](int i) { return i + 1; };
76*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(1, view(0));
77*8f0ba417SAndroid Build Coastguard Worker view = [](int i) { return i - 1; };
78*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(0, view(1));
79*8f0ba417SAndroid Build Coastguard Worker
80*8f0ba417SAndroid Build Coastguard Worker function_ref<int(int)> view2 = view;
81*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(view(10), view2(10));
82*8f0ba417SAndroid Build Coastguard Worker
83*8f0ba417SAndroid Build Coastguard Worker view = view2;
84*8f0ba417SAndroid Build Coastguard Worker EXPECT_EQ(view(10), view2(10));
85*8f0ba417SAndroid Build Coastguard Worker }
86*8f0ba417SAndroid Build Coastguard Worker
87*8f0ba417SAndroid Build Coastguard Worker } // namespace android::base
88