Yurii Zubrytskyi | 68ce05e | 2021-02-19 00:07:31 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 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 | |
| 17 | #include "android-base/function_ref.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <functional> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace android::base { |
| 25 | |
| 26 | TEST(function_ref, Ctor) { |
| 27 | // Making sure it can be constructed in all meaningful ways |
| 28 | |
| 29 | using EmptyFunc = function_ref<void()>; |
| 30 | |
| 31 | EmptyFunc f1([] {}); |
| 32 | |
| 33 | struct Functor { |
| 34 | void operator()() const {} |
| 35 | }; |
| 36 | EmptyFunc f2(Functor{}); |
| 37 | Functor fctr; |
| 38 | EmptyFunc f3(fctr); |
| 39 | |
| 40 | EmptyFunc f4(std::function<void()>([f1, f2, f3] { |
| 41 | (void)f1; |
| 42 | (void)f2; |
| 43 | (void)f3; |
| 44 | })); |
| 45 | |
| 46 | const std::function<void()> func = [] {}; |
| 47 | EmptyFunc f5(func); |
| 48 | |
| 49 | static_assert(sizeof(f1) <= 2 * sizeof(void*), "Too big function_ref"); |
| 50 | static_assert(sizeof(f2) <= 2 * sizeof(void*), "Too big function_ref"); |
| 51 | static_assert(sizeof(f3) <= 2 * sizeof(void*), "Too big function_ref"); |
| 52 | static_assert(sizeof(f4) <= 2 * sizeof(void*), "Too big function_ref"); |
| 53 | static_assert(sizeof(f5) <= 2 * sizeof(void*), "Too big function_ref"); |
| 54 | } |
| 55 | |
| 56 | TEST(function_ref, Call) { |
| 57 | function_ref<int(int)> view = [](int i) { return i + 1; }; |
| 58 | EXPECT_EQ(1, view(0)); |
| 59 | EXPECT_EQ(-1, view(-2)); |
| 60 | |
| 61 | function_ref<std::string(std::string)> fs = [](const std::string& s) { return s + "1"; }; |
| 62 | EXPECT_STREQ("s1", fs("s").c_str()); |
| 63 | EXPECT_STREQ("ssss1", fs("ssss").c_str()); |
| 64 | |
| 65 | std::string base; |
| 66 | auto lambda = [&base]() { return base + "1"; }; |
| 67 | function_ref<std::string()> fs2 = lambda; |
| 68 | base = "one"; |
| 69 | EXPECT_STREQ("one1", fs2().c_str()); |
| 70 | base = "forty two"; |
| 71 | EXPECT_STREQ("forty two1", fs2().c_str()); |
| 72 | } |
| 73 | |
| 74 | TEST(function_ref, CopyAndAssign) { |
| 75 | function_ref<int(int)> view = [](int i) { return i + 1; }; |
| 76 | EXPECT_EQ(1, view(0)); |
| 77 | view = [](int i) { return i - 1; }; |
| 78 | EXPECT_EQ(0, view(1)); |
| 79 | |
| 80 | function_ref<int(int)> view2 = view; |
| 81 | EXPECT_EQ(view(10), view2(10)); |
| 82 | |
| 83 | view = view2; |
| 84 | EXPECT_EQ(view(10), view2(10)); |
| 85 | } |
| 86 | |
| 87 | } // namespace android::base |