aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/unittests/function_view_ut.cpp
blob: 99af41793cc9266060210ac5e29385a0ccc60883 (plain) (blame)
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
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/memory/function_view.h>

#include <util/generic/string.h>
#include <util/string/cast.h>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

struct TNoCopy
{
    int Value = 42;

    TNoCopy() = default;

    TNoCopy(const TNoCopy&) = delete;

    TNoCopy(TNoCopy&&)
    { }
};

int Foo()
{
    return 42;
}

int& Bar()
{
    static int bar = 0;
    return bar;
}

const TNoCopy& ImmutBar()
{
    static TNoCopy bar = {};
    return bar;
}

TString Baz(const int& x)
{
    return ToString(x);
}

void NoExFoo() noexcept
{ }

struct TCallable
{
    int InvocationCount = 0;
    mutable int ConstInvocationCount = 0;

    void operator()() &
    {
        ++InvocationCount;
    }

    void operator()() const &
    {
        ++ConstInvocationCount;
    }
};

////////////////////////////////////////////////////////////////////////////////

TEST(TFunctionViewTest, JustWorks)
{
    auto stackLambda = [] (int val) {
        return val + 1;
    };

    {
        TFunctionView<int(int)> view(stackLambda);

        EXPECT_EQ(view(42), 43);
    }
}

TEST(TFunctionViewTest, FreeFunction)
{
    TFunctionView<int()> view(Foo);
    EXPECT_EQ(view(), 42);
}

TEST(TFunctionViewTest, RefReturn)
{
    TFunctionView<int&()> view(Bar);
    ++view();
    EXPECT_EQ(view(), 1);

    TFunctionView<const TNoCopy&()> immut_view(ImmutBar);
    EXPECT_EQ(immut_view().Value, 42);
}

TEST(TFunctionViewTest, RefArgument)
{
    TFunctionView<TString(const int&)> view(Baz);
    EXPECT_EQ(view(77), TString("77"));
}

TEST(TFunctionViewTest, NoExcept)
{
    TFunctionView<void() noexcept> view(NoExFoo);
    static_assert(std::is_nothrow_invocable_r_v<void, decltype(view)>);

    view();
}

TEST(TFunctionViewTest, CVOverloads)
{
    TCallable callable;

    TFunctionView<void()> view(callable);
    // NB: & overload overshadows every other overload.
    // const auto& viewRef = view;
    // viewRef();

    view();
    EXPECT_EQ(callable.InvocationCount, 1);
    EXPECT_EQ(callable.ConstInvocationCount, 0);
}

TEST(TFunctionViewTest, CopyView)
{
    int counter = 0;
    auto lambda = [&counter] {
        ++counter;
    };

    TFunctionView<void()> view1(lambda);
    TFunctionView<void()> view2 = view1;

    view1();
    EXPECT_EQ(counter, 1);
    view2();
    EXPECT_EQ(counter, 2);
    view1();
    EXPECT_EQ(counter, 3);
}

TEST(TFunctionViewTest, AssignView)
{
    int counter = 0;
    auto lambda = [&counter] {
        ++counter;
    };

    TFunctionView<void()> view(lambda);
    view();
    EXPECT_EQ(counter, 1);

    {
        auto innerCounter = 0;
        auto lambda = [&innerCounter] {
            ++innerCounter;
        };

        view = lambda;
        view();
        EXPECT_EQ(counter, 1);
        EXPECT_EQ(innerCounter, 1);
    }

    // NB: Even though object is dead view will remain "valid".
    // Be careful with lifetimes!
    EXPECT_TRUE(view.IsValid());
}

TEST(TFunctionViewTest, ReleaseSemantics)
{
    int counter = 0;
    auto lambda = [&counter] {
        ++counter;
    };

    TFunctionView<void()> view1(lambda);
    view1();
    EXPECT_EQ(counter, 1);

    TFunctionView view2 = view1.Release();
    EXPECT_FALSE(view1.IsValid());

    EXPECT_TRUE(view2.IsValid());

    view2();
    EXPECT_EQ(counter, 2);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT