| // Copyright 2022 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "base/functional/function_ref.h" |
| |
| #include "testing/gtest/include/gtest/gtest.h" |
| |
| namespace base { |
| |
| namespace { |
| |
| char Moo(float) { |
| return 'a'; |
| } |
| |
| struct C { |
| long Method() { return value; } |
| long value; |
| }; |
| |
| } // namespace |
| |
| TEST(FunctionRefTest, FreeFunction) { |
| [](FunctionRef<char(float)> ref) { EXPECT_EQ('a', ref(1.0)); }(&Moo); |
| } |
| |
| TEST(FunctionRefTest, Method) { |
| [](FunctionRef<long(C*)> ref) { |
| #if defined(STARBOARD) |
| C c = {25L}; |
| #else |
| C c = {.value = 25L}; |
| #endif |
| EXPECT_EQ(25L, ref(&c)); |
| }(&C::Method); |
| } |
| |
| TEST(FunctionRefTest, Lambda) { |
| int x = 3; |
| auto lambda = [&x]() { return x; }; |
| [](FunctionRef<int()> ref) { EXPECT_EQ(3, ref()); }(lambda); |
| } |
| |
| // Tests for passing a `base::FunctionRef` as an `absl::FunctionRef`. |
| TEST(FunctionRefTest, AbslConversion) { |
| // Matching signatures should work. |
| { |
| bool called = false; |
| auto lambda = [&called](float) { |
| called = true; |
| return 'a'; |
| }; |
| FunctionRef<char(float)> ref(lambda); |
| [](absl::FunctionRef<char(float)> absl_ref) { |
| absl_ref(1.0); |
| }(ref.ToAbsl()); |
| EXPECT_TRUE(called); |
| } |
| |
| // `absl::FunctionRef` should be able to adapt "similar enough" signatures. |
| { |
| bool called = false; |
| auto lambda = [&called](float) { |
| called = true; |
| return 'a'; |
| }; |
| FunctionRef<char(float)> ref(lambda); |
| [](absl::FunctionRef<void(float)> absl_ref) { |
| absl_ref(1.0); |
| }(ref.ToAbsl()); |
| EXPECT_TRUE(called); |
| } |
| } |
| |
| // base::FunctionRef allows functors with convertible return types to be |
| // adapted. |
| TEST(FunctionRefTest, ConvertibleReturnTypes) { |
| // Hopefully this never results in a postmorterm-worthy bug... |
| { |
| auto lambda = []() -> bool { return true; }; |
| [](FunctionRef<int()> ref) { EXPECT_EQ(1, ref()); }(lambda); |
| } |
| |
| { |
| class Base {}; |
| class Derived : public Base {}; |
| |
| auto lambda = []() -> Derived* { return nullptr; }; |
| [](FunctionRef<Base*()> ref) { EXPECT_EQ(nullptr, ref()); }(lambda); |
| } |
| } |
| |
| } // namespace base |