1a32f894fSJonathan Roelofs //===- llvm/unittest/ADT/FunctionRefTest.cpp - function_ref unit tests ----===//
245dc480bSDavid Blaikie //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
645dc480bSDavid Blaikie //
745dc480bSDavid Blaikie //===----------------------------------------------------------------------===//
845dc480bSDavid Blaikie 
945dc480bSDavid Blaikie #include "llvm/ADT/STLExtras.h"
1045dc480bSDavid Blaikie #include "gtest/gtest.h"
1145dc480bSDavid Blaikie 
1245dc480bSDavid Blaikie using namespace llvm;
1345dc480bSDavid Blaikie 
1445dc480bSDavid Blaikie namespace {
1545dc480bSDavid Blaikie 
16a809f289SChandler Carruth // Ensure that there is a default constructor and we can test for a null
17a809f289SChandler Carruth // function_ref.
TEST(FunctionRefTest,Null)18a809f289SChandler Carruth TEST(FunctionRefTest, Null) {
19a809f289SChandler Carruth   function_ref<int()> F;
20a809f289SChandler Carruth   EXPECT_FALSE(F);
21a809f289SChandler Carruth 
22a809f289SChandler Carruth   auto L = [] { return 1; };
23a809f289SChandler Carruth   F = L;
24a809f289SChandler Carruth   EXPECT_TRUE(F);
25a809f289SChandler Carruth 
26a809f289SChandler Carruth   F = {};
27a809f289SChandler Carruth   EXPECT_FALSE(F);
28a809f289SChandler Carruth }
29a809f289SChandler Carruth 
3045dc480bSDavid Blaikie // Ensure that copies of a function_ref copy the underlying state rather than
3145dc480bSDavid Blaikie // causing one function_ref to chain to the next.
TEST(FunctionRefTest,Copy)3245dc480bSDavid Blaikie TEST(FunctionRefTest, Copy) {
3345dc480bSDavid Blaikie   auto A = [] { return 1; };
3445dc480bSDavid Blaikie   auto B = [] { return 2; };
3545dc480bSDavid Blaikie   function_ref<int()> X = A;
3645dc480bSDavid Blaikie   function_ref<int()> Y = X;
3745dc480bSDavid Blaikie   X = B;
3845dc480bSDavid Blaikie   EXPECT_EQ(1, Y());
3945dc480bSDavid Blaikie }
4045dc480bSDavid Blaikie 
TEST(FunctionRefTest,BadCopy)41cbce88ddSDavid Blaikie TEST(FunctionRefTest, BadCopy) {
42cbce88ddSDavid Blaikie   auto A = [] { return 1; };
43cbce88ddSDavid Blaikie   function_ref<int()> X;
44cbce88ddSDavid Blaikie   function_ref<int()> Y = A;
45cbce88ddSDavid Blaikie   function_ref<int()> Z = static_cast<const function_ref<int()> &&>(Y);
46cbce88ddSDavid Blaikie   X = Z;
47cbce88ddSDavid Blaikie   Y = nullptr;
48cbce88ddSDavid Blaikie   ASSERT_EQ(1, X());
4945dc480bSDavid Blaikie }
50cbce88ddSDavid Blaikie 
51b953a01bSSam McCall // Test that overloads on function_refs are resolved as expected.
returns(StringRef)52*1a1aad91SSam McCall std::string returns(StringRef) { return "not a function"; }
returns(function_ref<double ()> F)53*1a1aad91SSam McCall std::string returns(function_ref<double()> F) { return "number"; }
returns(function_ref<StringRef ()> F)54*1a1aad91SSam McCall std::string returns(function_ref<StringRef()> F) { return "string"; }
55b953a01bSSam McCall 
TEST(FunctionRefTest,SFINAE)56b953a01bSSam McCall TEST(FunctionRefTest, SFINAE) {
57b953a01bSSam McCall   EXPECT_EQ("not a function", returns("boo!"));
58b953a01bSSam McCall   EXPECT_EQ("number", returns([] { return 42; }));
59b953a01bSSam McCall   EXPECT_EQ("string", returns([] { return "hello"; }));
60b953a01bSSam McCall }
61b953a01bSSam McCall 
62cbce88ddSDavid Blaikie } // namespace
63