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
|
#include "function_ref.h"
#include <library/cpp/testing/unittest/registar.h>
Y_UNIT_TEST_SUITE(TestFunctionRef) {
template <typename Signature>
struct TTestFunction;
template <typename Ret, typename... Args, bool IsNoexcept>
struct TTestFunction<Ret(Args...) noexcept(IsNoexcept)> {
Ret operator()(Args...) const noexcept(IsNoexcept) {
return {};
}
};
Y_UNIT_TEST(NonDefaultConstructible) {
static_assert(!std::is_default_constructible_v<TFunctionRef<void()>>);
static_assert(!std::is_default_constructible_v<TFunctionRef<void() noexcept>>);
static_assert(!std::is_default_constructible_v<TFunctionRef<int(double, void********* megaptr, TTestFunction<void(int)>)>>);
}
int F1(bool x) {
if (x)
throw 19;
return 42;
}
int F2(bool x) noexcept {
return 42 + x;
}
static const TTestFunction<int(bool)> C1;
static const TTestFunction<int(bool) noexcept> C2;
Y_UNIT_TEST(Noexcept) {
static_assert(std::is_constructible_v<TFunctionRef<int(bool)>, decltype(F1)>);
static_assert(std::is_constructible_v<TFunctionRef<int(bool)>, decltype(F2)>);
static_assert(!std::is_constructible_v<TFunctionRef<int(bool) noexcept>, decltype(F1)>);
static_assert(std::is_constructible_v<TFunctionRef<int(bool) noexcept>, decltype(F2)>);
static_assert(std::is_constructible_v<TFunctionRef<int(bool)>, decltype(C1)>);
static_assert(std::is_constructible_v<TFunctionRef<int(bool)>, decltype(C2)>);
static_assert(!std::is_constructible_v<TFunctionRef<int(bool) noexcept>, decltype(C1)>);
static_assert(std::is_constructible_v<TFunctionRef<int(bool) noexcept>, decltype(C2)>);
}
Y_UNIT_TEST(Deduction) {
TFunctionRef ref1(F1);
TFunctionRef ref2(F2);
TFunctionRef ref3(C1);
TFunctionRef ref4(C2);
static_assert(!std::is_nothrow_invocable_r_v<int, decltype(ref1), bool>);
static_assert(std::is_nothrow_invocable_r_v<int, decltype(ref2), bool>);
static_assert(std::is_same_v<decltype(ref1)::TSignature, int(bool)>);
static_assert(std::is_same_v<decltype(ref2)::TSignature, int(bool) noexcept>);
}
void WithCallback(TFunctionRef<double(double, int) noexcept>);
void Iterate(int from, int to, TFunctionRef<void(int)> callback) {
while (from < to) {
callback(from++);
}
}
void IterateNoexcept(int from, int to, TFunctionRef<void(int) noexcept> callback) {
while (from < to) {
callback(from++);
}
}
Y_UNIT_TEST(AsArgument) {
int sum = 0;
Iterate(0, 10, [&](int x) { sum += x; });
UNIT_ASSERT_EQUAL(sum, 45);
Iterate(0, 10, [&](int x) noexcept { sum += x; });
UNIT_ASSERT_EQUAL(sum, 90);
IterateNoexcept(0, 10, [&](int x) noexcept { sum += x; });
UNIT_ASSERT_EQUAL(sum, 135);
auto summer = [&](int x) { sum += x; };
Iterate(0, 10, summer);
Iterate(0, 10, summer);
Iterate(0, 10, summer);
UNIT_ASSERT_EQUAL(sum, 270);
TFunctionRef ref = summer;
Iterate(0, 10, ref);
UNIT_ASSERT_EQUAL(sum, 315);
}
int GlobalSum = 0;
void AddToGlobalSum(int x) {
GlobalSum += x;
}
Y_UNIT_TEST(FunctionPointer) {
GlobalSum = 0;
Iterate(0, 10, AddToGlobalSum);
UNIT_ASSERT_EQUAL(GlobalSum, 45);
TFunctionRef ref1 = AddToGlobalSum;
Iterate(0, 10, ref1);
UNIT_ASSERT_EQUAL(GlobalSum, 90);
TFunctionRef ref2{AddToGlobalSum};
Iterate(0, 10, ref2);
UNIT_ASSERT_EQUAL(GlobalSum, 135);
}
Y_UNIT_TEST(Reassign) {
TFunctionRef kek = [](double) { return 42; };
kek = [](double) { return 19; };
kek = [](int) { return 22.8; };
}
const char* Greet() {
return "Hello, world!";
}
Y_UNIT_TEST(ImplicitCasts) {
TFunctionRef<void(int)> ref = [](int x) { return x; };
ref = [](double x) { return x; };
ref = [](char x) { return x; };
TFunctionRef<int()> ref1 = [] { return 0.5; };
ref1 = [] { return 'a'; };
ref1 = [] { return 124u; };
TFunctionRef<TStringBuf()> ref2{Greet};
}
Y_UNIT_TEST(StatelessLambdaLifetime) {
TFunctionRef<int(int, int)> ref{[](int a, int b) { return a + b; }};
UNIT_ASSERT_EQUAL(ref(5, 5), 10);
}
Y_UNIT_TEST(ForwardArguments) {
char x = 'x';
TFunctionRef<void(std::unique_ptr<int>, char&)> ref = [](std::unique_ptr<int> ptr, char& ch) {
UNIT_ASSERT_EQUAL(*ptr, 5);
ch = 'a';
};
ref(std::make_unique<int>(5), x);
UNIT_ASSERT_EQUAL(x, 'a');
}
}
|