20 #include <ripple/beast/unit_test.h>
21 #include <ripple/core/ClosureCounter.h>
24 #include <test/jtx/Env.h>
45 BEAST_EXPECT(voidCounter.
count() == 0);
49 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
50 BEAST_EXPECT(voidCounter.
count() == 1);
51 BEAST_EXPECT(evidence == 0);
52 BEAST_EXPECT(wrapped);
56 BEAST_EXPECT(evidence == 1);
58 BEAST_EXPECT(evidence == 2);
61 wrapped = std::nullopt;
62 BEAST_EXPECT(voidCounter.
count() == 0);
67 BEAST_EXPECT(setCounter.
count() == 0);
71 auto setInt = [&evidence](
int i) { evidence = i; };
72 auto wrapped = setCounter.
wrap(setInt);
74 BEAST_EXPECT(setCounter.
count() == 1);
75 BEAST_EXPECT(evidence == 0);
76 BEAST_EXPECT(wrapped);
80 BEAST_EXPECT(evidence == 5);
82 BEAST_EXPECT(evidence == 11);
85 wrapped = std::nullopt;
86 BEAST_EXPECT(setCounter.
count() == 0);
91 BEAST_EXPECT(sumCounter.
count() == 0);
94 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
95 auto wrapped = sumCounter.
wrap(
sum);
97 BEAST_EXPECT(sumCounter.
count() == 1);
98 BEAST_EXPECT(wrapped);
101 BEAST_EXPECT((*wrapped)(5, 2) == 7);
102 BEAST_EXPECT((*wrapped)(2, -8) == -6);
105 wrapped = std::nullopt;
106 BEAST_EXPECT(sumCounter.
count() == 0);
132 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
165 BEAST_EXPECT(strCounter.
count() == 0);
170 BEAST_EXPECT(strCounter.
count() == 1);
171 BEAST_EXPECT(wrapped);
175 BEAST_EXPECT(result.
copies == 2);
176 BEAST_EXPECT(result.
moves == 1);
177 BEAST_EXPECT(result.
str ==
"value!");
178 BEAST_EXPECT(strValue.
str.
size() == 5);
183 BEAST_EXPECT(strCounter.
count() == 0);
185 auto wrapped = strCounter.
wrap(
188 BEAST_EXPECT(strCounter.
count() == 1);
189 BEAST_EXPECT(wrapped);
193 BEAST_EXPECT(result.
copies == 1);
195 BEAST_EXPECT(result.
str ==
"const lvalue!");
196 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
201 BEAST_EXPECT(strCounter.
count() == 0);
206 BEAST_EXPECT(strCounter.
count() == 1);
207 BEAST_EXPECT(wrapped);
211 BEAST_EXPECT(result.
copies == 1);
212 BEAST_EXPECT(result.
moves == 0);
213 BEAST_EXPECT(result.
str ==
"lvalue!");
214 BEAST_EXPECT(strLValue.
str == result.
str);
219 BEAST_EXPECT(strCounter.
count() == 0);
226 return std::move(
in +=
"!");
229 BEAST_EXPECT(strCounter.
count() == 1);
230 BEAST_EXPECT(wrapped);
234 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
235 TrackedString const result = (*wrapped)(std::move(strRValue));
236 BEAST_EXPECT(result.
copies == 0);
237 BEAST_EXPECT(result.
moves == 1);
238 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
239 BEAST_EXPECT(strRValue.
str.
size() == 0);
248 BEAST_EXPECT(voidCounter.
count() == 0);
250 auto wrapped1 = voidCounter.
wrap([]() {});
251 BEAST_EXPECT(voidCounter.
count() == 1);
254 auto wrapped2(wrapped1);
255 BEAST_EXPECT(voidCounter.
count() == 2);
258 auto wrapped3(std::move(wrapped2));
259 BEAST_EXPECT(voidCounter.
count() == 3);
262 auto wrapped4 = voidCounter.
wrap([]() {});
263 BEAST_EXPECT(voidCounter.
count() == 4);
265 BEAST_EXPECT(voidCounter.
count() == 3);
267 BEAST_EXPECT(voidCounter.
count() == 2);
269 BEAST_EXPECT(voidCounter.
count() == 1);
271 BEAST_EXPECT(voidCounter.
count() == 0);
274 using namespace std::chrono_literals;
275 voidCounter.
join(
"testWrap", 1ms,
j);
278 BEAST_EXPECT(voidCounter.
wrap([]() {}) == std::nullopt);
286 BEAST_EXPECT(voidCounter.
count() == 0);
288 auto wrapped = (voidCounter.
wrap([]() {}));
289 BEAST_EXPECT(voidCounter.
count() == 1);
293 std::thread localThread([&voidCounter, &threadExited,
this]() {
295 using namespace std::chrono_literals;
296 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
297 threadExited.store(
true);
301 while (!voidCounter.
joined())
307 using namespace std::chrono_literals;
309 BEAST_EXPECT(threadExited ==
false);
313 wrapped = std::nullopt;
314 BEAST_EXPECT(voidCounter.
count() == 0);
317 while (threadExited ==
false)