rippled
Loading...
Searching...
No Matches
tests
libxrpl
basics
scope.cpp
1
#include <xrpl/basics/scope.h>
2
3
#include <doctest/doctest.h>
4
5
using namespace
ripple
;
6
7
TEST_CASE(
"scope_exit"
)
8
{
9
// scope_exit always executes the functor on destruction,
10
// unless release() is called
11
int
i = 0;
12
{
13
scope_exit
x{[&i]() { i = 1; }};
14
}
15
CHECK
(i == 1);
16
{
17
scope_exit
x{[&i]() { i = 2; }};
18
x.
release
();
19
}
20
CHECK
(i == 1);
21
{
22
scope_exit
x{[&i]() { i += 2; }};
23
auto
x2 = std::move(x);
24
}
25
CHECK
(i == 3);
26
{
27
scope_exit
x{[&i]() { i = 4; }};
28
x.
release
();
29
auto
x2 = std::move(x);
30
}
31
CHECK
(i == 3);
32
{
33
try
34
{
35
scope_exit
x{[&i]() { i = 5; }};
36
throw
1;
37
}
38
catch
(...)
39
{
40
}
41
}
42
CHECK
(i == 5);
43
{
44
try
45
{
46
scope_exit
x{[&i]() { i = 6; }};
47
x.
release
();
48
throw
1;
49
}
50
catch
(...)
51
{
52
}
53
}
54
CHECK
(i == 5);
55
}
56
57
TEST_CASE(
"scope_fail"
)
58
{
59
// scope_fail executes the functor on destruction only
60
// if an exception is unwinding, unless release() is called
61
int
i = 0;
62
{
63
scope_fail
x{[&i]() { i = 1; }};
64
}
65
CHECK
(i == 0);
66
{
67
scope_fail
x{[&i]() { i = 2; }};
68
x.
release
();
69
}
70
CHECK
(i == 0);
71
{
72
scope_fail
x{[&i]() { i = 3; }};
73
auto
x2 = std::move(x);
74
}
75
CHECK
(i == 0);
76
{
77
scope_fail
x{[&i]() { i = 4; }};
78
x.
release
();
79
auto
x2 = std::move(x);
80
}
81
CHECK
(i == 0);
82
{
83
try
84
{
85
scope_fail
x{[&i]() { i = 5; }};
86
throw
1;
87
}
88
catch
(...)
89
{
90
}
91
}
92
CHECK
(i == 5);
93
{
94
try
95
{
96
scope_fail
x{[&i]() { i = 6; }};
97
x.
release
();
98
throw
1;
99
}
100
catch
(...)
101
{
102
}
103
}
104
CHECK
(i == 5);
105
}
106
107
TEST_CASE(
"scope_success"
)
108
{
109
// scope_success executes the functor on destruction only
110
// if an exception is not unwinding, unless release() is called
111
int
i = 0;
112
{
113
scope_success
x{[&i]() { i = 1; }};
114
}
115
CHECK
(i == 1);
116
{
117
scope_success
x{[&i]() { i = 2; }};
118
x.
release
();
119
}
120
CHECK
(i == 1);
121
{
122
scope_success
x{[&i]() { i += 2; }};
123
auto
x2 = std::move(x);
124
}
125
CHECK
(i == 3);
126
{
127
scope_success
x{[&i]() { i = 4; }};
128
x.
release
();
129
auto
x2 = std::move(x);
130
}
131
CHECK
(i == 3);
132
{
133
try
134
{
135
scope_success
x{[&i]() { i = 5; }};
136
throw
1;
137
}
138
catch
(...)
139
{
140
}
141
}
142
CHECK
(i == 3);
143
{
144
try
145
{
146
scope_success
x{[&i]() { i = 6; }};
147
x.
release
();
148
throw
1;
149
}
150
catch
(...)
151
{
152
}
153
}
154
CHECK
(i == 3);
155
}
ripple::scope_exit
Definition
scope.h:27
ripple::scope_exit::release
void release() noexcept
Definition
scope.h:64
ripple::scope_fail
Definition
scope.h:75
ripple::scope_fail::release
void release() noexcept
Definition
scope.h:115
ripple::scope_success
Definition
scope.h:126
ripple::scope_success::release
void release() noexcept
Definition
scope.h:164
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:6
ripple::LedgerNameSpace::CHECK
@ CHECK
Generated by
1.9.8