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
|
#include "lazy_value.h"
#include <library/cpp/testing/unittest/registar.h>
Y_UNIT_TEST_SUITE(TLazyValueTestSuite) {
Y_UNIT_TEST(TestLazyValue) {
TLazyValue<int> value([]() {
return 5;
});
UNIT_ASSERT(!value.WasLazilyInitialized());
UNIT_ASSERT_EQUAL(*value, 5);
UNIT_ASSERT(value.WasLazilyInitialized());
}
Y_UNIT_TEST(TestLazyValueInitialization) {
TLazyValue<int> value1([]() { return 5; });
TLazyValue<int> value2 = []() { return 5; };
TLazyValue<int> notInitialized{};
TLazyValue<int> copy1(value1);
copy1 = value2;
}
Y_UNIT_TEST(TestLazyValueCopy) {
TLazyValue<int> value([]() { return 5; });
UNIT_ASSERT(!value.WasLazilyInitialized());
TLazyValue<int> emptyCopy = value;
UNIT_ASSERT(!emptyCopy.WasLazilyInitialized());
UNIT_ASSERT_EQUAL(*emptyCopy, 5);
UNIT_ASSERT(emptyCopy.WasLazilyInitialized());
UNIT_ASSERT(!value.WasLazilyInitialized());
UNIT_ASSERT_EQUAL(*value, 5);
TLazyValue<int> notEmptyCopy = value;
UNIT_ASSERT(notEmptyCopy.WasLazilyInitialized());
UNIT_ASSERT_EQUAL(*notEmptyCopy, 5);
}
struct TCopyCounter {
TCopyCounter(size_t& numCopies)
: NumCopies(&numCopies)
{
}
TCopyCounter() = default;
TCopyCounter(const TCopyCounter& other)
: NumCopies(other.NumCopies)
{
++(*NumCopies);
}
TCopyCounter(TCopyCounter&&) = default;
TCopyCounter& operator=(const TCopyCounter& other) {
if (this == &other) {
return *this;
}
NumCopies = other.NumCopies;
++(*NumCopies);
return *this;
}
TCopyCounter& operator=(TCopyCounter&&) = default;
size_t* NumCopies = nullptr;
};
Y_UNIT_TEST(TestLazyValueMoveValueInitialization) {
size_t numCopies = 0;
TCopyCounter counter{numCopies};
TLazyValue<TCopyCounter> value{[v = std::move(counter)]() mutable { return std::move(v); }};
value.InitDefault();
UNIT_ASSERT_EQUAL(numCopies, 0);
}
Y_UNIT_TEST(TestLazyValueCopyValueInitialization) {
size_t numCopies = 0;
TCopyCounter counter{numCopies};
TLazyValue<TCopyCounter> value{[&counter]() { return counter; }};
UNIT_ASSERT_EQUAL(numCopies, 0);
value.InitDefault();
UNIT_ASSERT_EQUAL(numCopies, 1);
}
class TValueProvider {
public:
static size_t CountParseDataCalled;
TValueProvider()
: Data_([&] { return this->ParseData(); })
{
}
const TString& GetData() const {
return *Data_;
}
private:
TLazyValue<TString> Data_;
TString ParseData() {
CountParseDataCalled++;
return "hi";
}
};
size_t TValueProvider::CountParseDataCalled = 0;
Y_UNIT_TEST(TestValueProvider) {
TValueProvider provider;
UNIT_ASSERT(provider.GetData() == "hi");
}
Y_UNIT_TEST(TestValueProviderCopy) {
TValueProvider provider;
provider.GetData();
const auto countParsed = TValueProvider::CountParseDataCalled;
provider.GetData();
UNIT_ASSERT_EQUAL(countParsed, TValueProvider::CountParseDataCalled);
TValueProvider providerCopy;
providerCopy = provider;
providerCopy.GetData();
UNIT_ASSERT_EQUAL(countParsed, TValueProvider::CountParseDataCalled);
}
Y_UNIT_TEST(TestEmptyProviderCopy) {
TValueProvider provider;
TValueProvider copy(provider);
const auto countParsed = TValueProvider::CountParseDataCalled;
provider.GetData();
UNIT_ASSERT_EQUAL(countParsed + 1, TValueProvider::CountParseDataCalled);
copy.GetData();
UNIT_ASSERT_EQUAL(countParsed + 2, TValueProvider::CountParseDataCalled);
const TValueProvider notEmptyCopy(copy);
notEmptyCopy.GetData();
UNIT_ASSERT_EQUAL(countParsed + 2, TValueProvider::CountParseDataCalled);
}
Y_UNIT_TEST(TestMakeLazy) {
auto lv = MakeLazy([] {
return 100500;
});
UNIT_ASSERT(!lv.WasLazilyInitialized());
UNIT_ASSERT(lv.GetRef() == 100500);
UNIT_ASSERT(lv.WasLazilyInitialized());
}
}
|