20 #include <ripple/beast/unit_test.h>
21 #include <ripple/core/ClosureCounter.h>
22 #include <test/jtx/Env.h>
51 BEAST_EXPECT(voidCounter.
count() == 0);
55 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
56 BEAST_EXPECT(voidCounter.
count() == 1);
57 BEAST_EXPECT(evidence == 0);
58 BEAST_EXPECT(wrapped);
62 BEAST_EXPECT(evidence == 1);
64 BEAST_EXPECT(evidence == 2);
67 wrapped = std::nullopt;
68 BEAST_EXPECT(voidCounter.
count() == 0);
73 BEAST_EXPECT(setCounter.
count() == 0);
77 auto setInt = [&evidence](
int i) { evidence = i; };
78 auto wrapped = setCounter.
wrap(setInt);
80 BEAST_EXPECT(setCounter.
count() == 1);
81 BEAST_EXPECT(evidence == 0);
82 BEAST_EXPECT(wrapped);
86 BEAST_EXPECT(evidence == 5);
88 BEAST_EXPECT(evidence == 11);
91 wrapped = std::nullopt;
92 BEAST_EXPECT(setCounter.
count() == 0);
97 BEAST_EXPECT(sumCounter.
count() == 0);
100 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
101 auto wrapped = sumCounter.
wrap(
sum);
103 BEAST_EXPECT(sumCounter.
count() == 1);
104 BEAST_EXPECT(wrapped);
107 BEAST_EXPECT((*wrapped)(5, 2) == 7);
108 BEAST_EXPECT((*wrapped)(2, -8) == -6);
111 wrapped = std::nullopt;
112 BEAST_EXPECT(sumCounter.
count() == 0);
138 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
171 BEAST_EXPECT(strCounter.
count() == 0);
176 BEAST_EXPECT(strCounter.
count() == 1);
177 BEAST_EXPECT(wrapped);
181 BEAST_EXPECT(result.
copies == 2);
182 BEAST_EXPECT(result.
moves == 1);
183 BEAST_EXPECT(result.
str ==
"value!");
184 BEAST_EXPECT(strValue.
str.
size() == 5);
189 BEAST_EXPECT(strCounter.
count() == 0);
191 auto wrapped = strCounter.
wrap(
194 BEAST_EXPECT(strCounter.
count() == 1);
195 BEAST_EXPECT(wrapped);
199 BEAST_EXPECT(result.
copies == 1);
201 BEAST_EXPECT(result.
str ==
"const lvalue!");
202 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
207 BEAST_EXPECT(strCounter.
count() == 0);
212 BEAST_EXPECT(strCounter.
count() == 1);
213 BEAST_EXPECT(wrapped);
217 BEAST_EXPECT(result.
copies == 1);
218 BEAST_EXPECT(result.
moves == 0);
219 BEAST_EXPECT(result.
str ==
"lvalue!");
220 BEAST_EXPECT(strLValue.
str == result.
str);
225 BEAST_EXPECT(strCounter.
count() == 0);
232 return std::move(
in +=
"!");
235 BEAST_EXPECT(strCounter.
count() == 1);
236 BEAST_EXPECT(wrapped);
240 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
241 TrackedString const result = (*wrapped)(std::move(strRValue));
242 BEAST_EXPECT(result.
copies == 0);
243 BEAST_EXPECT(result.
moves == 1);
244 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
245 BEAST_EXPECT(strRValue.
str.
size() == 0);
254 BEAST_EXPECT(voidCounter.
count() == 0);
256 auto wrapped1 = voidCounter.
wrap([]() {});
257 BEAST_EXPECT(voidCounter.
count() == 1);
260 auto wrapped2(wrapped1);
261 BEAST_EXPECT(voidCounter.
count() == 2);
264 auto wrapped3(std::move(wrapped2));
265 BEAST_EXPECT(voidCounter.
count() == 3);
268 auto wrapped4 = voidCounter.
wrap([]() {});
269 BEAST_EXPECT(voidCounter.
count() == 4);
271 BEAST_EXPECT(voidCounter.
count() == 3);
273 BEAST_EXPECT(voidCounter.
count() == 2);
275 BEAST_EXPECT(voidCounter.
count() == 1);
277 BEAST_EXPECT(voidCounter.
count() == 0);
280 using namespace std::chrono_literals;
281 voidCounter.
join(
"testWrap", 1ms,
j);
284 BEAST_EXPECT(voidCounter.
wrap([]() {}) == std::nullopt);
292 BEAST_EXPECT(voidCounter.
count() == 0);
294 auto wrapped = (voidCounter.
wrap([]() {}));
295 BEAST_EXPECT(voidCounter.
count() == 1);
299 std::thread localThread([&voidCounter, &threadExited,
this]() {
301 using namespace std::chrono_literals;
302 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
303 threadExited.store(
true);
307 while (!voidCounter.
joined())
313 using namespace std::chrono_literals;
315 BEAST_EXPECT(threadExited ==
false);
319 wrapped = std::nullopt;
320 BEAST_EXPECT(voidCounter.
count() == 0);
323 while (threadExited ==
false)