1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
//===- FunctionExtrasTest.cpp - Unit tests for function type erasure ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/FunctionExtras.h"
#include "gtest/gtest.h"
#include <memory>
#include <type_traits>
using namespace llvm;
namespace {
TEST(UniqueFunctionTest, Basic) {
unique_function<int(int, int)> Sum = [](int A, int B) { return A + B; };
EXPECT_EQ(Sum(1, 2), 3);
unique_function<int(int, int)> Sum2 = std::move(Sum);
EXPECT_EQ(Sum2(1, 2), 3);
unique_function<int(int, int)> Sum3 = [](int A, int B) { return A + B; };
Sum2 = std::move(Sum3);
EXPECT_EQ(Sum2(1, 2), 3);
Sum2 = unique_function<int(int, int)>([](int A, int B) { return A + B; });
EXPECT_EQ(Sum2(1, 2), 3);
// Explicit self-move test.
*&Sum2 = std::move(Sum2);
EXPECT_EQ(Sum2(1, 2), 3);
Sum2 = unique_function<int(int, int)>();
EXPECT_FALSE(Sum2);
// Make sure we can forward through l-value reference parameters.
unique_function<void(int &)> Inc = [](int &X) { ++X; };
int X = 42;
Inc(X);
EXPECT_EQ(X, 43);
// Make sure we can forward through r-value reference parameters with
// move-only types.
unique_function<int(std::unique_ptr<int> &&)> ReadAndDeallocByRef =
[](std::unique_ptr<int> &&Ptr) {
int V = *Ptr;
Ptr.reset();
return V;
};
std::unique_ptr<int> Ptr{new int(13)};
EXPECT_EQ(ReadAndDeallocByRef(std::move(Ptr)), 13);
EXPECT_FALSE((bool)Ptr);
// Make sure we can pass a move-only temporary as opposed to a local variable.
EXPECT_EQ(ReadAndDeallocByRef(std::unique_ptr<int>(new int(42))), 42);
// Make sure we can pass a move-only type by-value.
unique_function<int(std::unique_ptr<int>)> ReadAndDeallocByVal =
[](std::unique_ptr<int> Ptr) {
int V = *Ptr;
Ptr.reset();
return V;
};
Ptr.reset(new int(13));
EXPECT_EQ(ReadAndDeallocByVal(std::move(Ptr)), 13);
EXPECT_FALSE((bool)Ptr);
EXPECT_EQ(ReadAndDeallocByVal(std::unique_ptr<int>(new int(42))), 42);
}
TEST(UniqueFunctionTest, Captures) {
long A = 1, B = 2, C = 3, D = 4, E = 5;
unique_function<long()> Tmp;
unique_function<long()> C1 = [A]() { return A; };
EXPECT_EQ(C1(), 1);
Tmp = std::move(C1);
EXPECT_EQ(Tmp(), 1);
unique_function<long()> C2 = [A, B]() { return A + B; };
EXPECT_EQ(C2(), 3);
Tmp = std::move(C2);
EXPECT_EQ(Tmp(), 3);
unique_function<long()> C3 = [A, B, C]() { return A + B + C; };
EXPECT_EQ(C3(), 6);
Tmp = std::move(C3);
EXPECT_EQ(Tmp(), 6);
unique_function<long()> C4 = [A, B, C, D]() { return A + B + C + D; };
EXPECT_EQ(C4(), 10);
Tmp = std::move(C4);
EXPECT_EQ(Tmp(), 10);
unique_function<long()> C5 = [A, B, C, D, E]() { return A + B + C + D + E; };
EXPECT_EQ(C5(), 15);
Tmp = std::move(C5);
EXPECT_EQ(Tmp(), 15);
}
TEST(UniqueFunctionTest, MoveOnly) {
struct SmallCallable {
std::unique_ptr<int> A{new int(1)};
int operator()(int B) { return *A + B; }
};
unique_function<int(int)> Small = SmallCallable();
EXPECT_EQ(Small(2), 3);
unique_function<int(int)> Small2 = std::move(Small);
EXPECT_EQ(Small2(2), 3);
struct LargeCallable {
std::unique_ptr<int> A{new int(1)};
std::unique_ptr<int> B{new int(2)};
std::unique_ptr<int> C{new int(3)};
std::unique_ptr<int> D{new int(4)};
std::unique_ptr<int> E{new int(5)};
int operator()() { return *A + *B + *C + *D + *E; }
};
unique_function<int()> Large = LargeCallable();
EXPECT_EQ(Large(), 15);
unique_function<int()> Large2 = std::move(Large);
EXPECT_EQ(Large2(), 15);
}
TEST(UniqueFunctionTest, CountForwardingCopies) {
struct CopyCounter {
int &CopyCount;
CopyCounter(int &CopyCount) : CopyCount(CopyCount) {}
CopyCounter(const CopyCounter &Arg) : CopyCount(Arg.CopyCount) {
++CopyCount;
}
};
unique_function<void(CopyCounter)> ByValF = [](CopyCounter) {};
int CopyCount = 0;
ByValF(CopyCounter(CopyCount));
EXPECT_EQ(1, CopyCount);
CopyCount = 0;
{
CopyCounter Counter{CopyCount};
ByValF(Counter);
}
EXPECT_EQ(2, CopyCount);
// Check that we don't generate a copy at all when we can bind a reference all
// the way down, even if that reference could *in theory* allow copies.
unique_function<void(const CopyCounter &)> ByRefF = [](const CopyCounter &) {
};
CopyCount = 0;
ByRefF(CopyCounter(CopyCount));
EXPECT_EQ(0, CopyCount);
CopyCount = 0;
{
CopyCounter Counter{CopyCount};
ByRefF(Counter);
}
EXPECT_EQ(0, CopyCount);
// If we use a reference, we can make a stronger guarantee that *no* copy
// occurs.
struct Uncopyable {
Uncopyable() = default;
Uncopyable(const Uncopyable &) = delete;
};
unique_function<void(const Uncopyable &)> UncopyableF =
[](const Uncopyable &) {};
UncopyableF(Uncopyable());
Uncopyable X;
UncopyableF(X);
}
TEST(UniqueFunctionTest, CountForwardingMoves) {
struct MoveCounter {
int &MoveCount;
MoveCounter(int &MoveCount) : MoveCount(MoveCount) {}
MoveCounter(MoveCounter &&Arg) : MoveCount(Arg.MoveCount) { ++MoveCount; }
};
unique_function<void(MoveCounter)> ByValF = [](MoveCounter) {};
int MoveCount = 0;
ByValF(MoveCounter(MoveCount));
EXPECT_EQ(1, MoveCount);
MoveCount = 0;
{
MoveCounter Counter{MoveCount};
ByValF(std::move(Counter));
}
EXPECT_EQ(2, MoveCount);
// Check that when we use an r-value reference we get no spurious copies.
unique_function<void(MoveCounter &&)> ByRefF = [](MoveCounter &&) {};
MoveCount = 0;
ByRefF(MoveCounter(MoveCount));
EXPECT_EQ(0, MoveCount);
MoveCount = 0;
{
MoveCounter Counter{MoveCount};
ByRefF(std::move(Counter));
}
EXPECT_EQ(0, MoveCount);
// If we use an r-value reference we can in fact make a stronger guarantee
// with an unmovable type.
struct Unmovable {
Unmovable() = default;
Unmovable(Unmovable &&) = delete;
};
unique_function<void(const Unmovable &)> UnmovableF = [](const Unmovable &) {
};
UnmovableF(Unmovable());
Unmovable X;
UnmovableF(X);
}
TEST(UniqueFunctionTest, Const) {
// Can assign from const lambda.
unique_function<int(int) const> Plus2 = [X(std::make_unique<int>(2))](int Y) {
return *X + Y;
};
EXPECT_EQ(5, Plus2(3));
// Can call through a const ref.
const auto &Plus2Ref = Plus2;
EXPECT_EQ(5, Plus2Ref(3));
// Can move-construct and assign.
unique_function<int(int) const> Plus2A = std::move(Plus2);
EXPECT_EQ(5, Plus2A(3));
unique_function<int(int) const> Plus2B;
Plus2B = std::move(Plus2A);
EXPECT_EQ(5, Plus2B(3));
// Can convert to non-const function type, but not back.
unique_function<int(int)> Plus2C = std::move(Plus2B);
EXPECT_EQ(5, Plus2C(3));
// Overloaded call operator correctly resolved.
struct ChooseCorrectOverload {
StringRef operator()() { return "non-const"; }
StringRef operator()() const { return "const"; }
};
unique_function<StringRef()> ChooseMutable = ChooseCorrectOverload();
ChooseCorrectOverload A;
EXPECT_EQ("non-const", ChooseMutable());
EXPECT_EQ("non-const", A());
unique_function<StringRef() const> ChooseConst = ChooseCorrectOverload();
const ChooseCorrectOverload &X = A;
EXPECT_EQ("const", ChooseConst());
EXPECT_EQ("const", X());
}
// Test that overloads on unique_functions are resolved as expected.
std::string returns(StringRef) { return "not a function"; }
std::string returns(unique_function<double()> F) { return "number"; }
std::string returns(unique_function<StringRef()> F) { return "string"; }
TEST(UniqueFunctionTest, SFINAE) {
EXPECT_EQ("not a function", returns("boo!"));
EXPECT_EQ("number", returns([] { return 42; }));
EXPECT_EQ("string", returns([] { return "hello"; }));
}
// A forward declared type, and a templated type.
class Incomplete;
template <typename T> class Templated { T A; };
// Check that we can define unique_function that have references to
// incomplete types, even if those types are templated over an
// incomplete type.
TEST(UniqueFunctionTest, IncompleteTypes) {
unique_function<void(Templated<Incomplete> &&)>
IncompleteArgumentRValueReference;
unique_function<void(Templated<Incomplete> &)>
IncompleteArgumentLValueReference;
unique_function<void(Templated<Incomplete> *)> IncompleteArgumentPointer;
unique_function<Templated<Incomplete> &()> IncompleteResultLValueReference;
unique_function<Templated<Incomplete> && ()> IncompleteResultRValueReference2;
unique_function<Templated<Incomplete> *()> IncompleteResultPointer;
}
// Incomplete function returning an incomplete type
Incomplete incompleteFunction();
const Incomplete incompleteFunctionConst();
// Check that we can assign a callable to a unique_function when the
// callable return value is incomplete.
TEST(UniqueFunctionTest, IncompleteCallableType) {
unique_function<Incomplete()> IncompleteReturnInCallable{incompleteFunction};
unique_function<const Incomplete()> IncompleteReturnInCallableConst{
incompleteFunctionConst};
unique_function<const Incomplete()> IncompleteReturnInCallableConstConversion{
incompleteFunction};
}
// Define the incomplete function
class Incomplete {};
Incomplete incompleteFunction() { return {}; }
const Incomplete incompleteFunctionConst() { return {}; }
} // anonymous namespace
|