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