rippled
test
basics
scope_test.cpp
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of rippled: https://github0.com/ripple/rippled
4
Copyright (c) 2021 Ripple Inc.
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
20
#include <ripple/basics/scope.h>
21
#include <ripple/beast/unit_test.h>
22
23
namespace
ripple
{
24
namespace
test {
25
26
struct
scope_test
: beast::unit_test::suite
27
{
28
void
29
test_scope_exit
()
30
{
31
// scope_exit always executes the functor on destruction,
32
// unless release() is called
33
int
i = 0;
34
{
35
scope_exit
x{[&i]() { i = 1; }};
36
}
37
BEAST_EXPECT(i == 1);
38
{
39
scope_exit
x{[&i]() { i = 2; }};
40
x.
release
();
41
}
42
BEAST_EXPECT(i == 1);
43
{
44
scope_exit
x{[&i]() { i += 2; }};
45
auto
x2 = std::move(x);
46
}
47
BEAST_EXPECT(i == 3);
48
{
49
scope_exit
x{[&i]() { i = 4; }};
50
x.
release
();
51
auto
x2 = std::move(x);
52
}
53
BEAST_EXPECT(i == 3);
54
{
55
try
56
{
57
scope_exit
x{[&i]() { i = 5; }};
58
throw
1;
59
}
60
catch
(...)
61
{
62
}
63
}
64
BEAST_EXPECT(i == 5);
65
{
66
try
67
{
68
scope_exit
x{[&i]() { i = 6; }};
69
x.
release
();
70
throw
1;
71
}
72
catch
(...)
73
{
74
}
75
}
76
BEAST_EXPECT(i == 5);
77
}
78
79
void
80
test_scope_fail
()
81
{
82
// scope_fail executes the functor on destruction only
83
// if an exception is unwinding, unless release() is called
84
int
i = 0;
85
{
86
scope_fail
x{[&i]() { i = 1; }};
87
}
88
BEAST_EXPECT(i == 0);
89
{
90
scope_fail
x{[&i]() { i = 2; }};
91
x.
release
();
92
}
93
BEAST_EXPECT(i == 0);
94
{
95
scope_fail
x{[&i]() { i = 3; }};
96
auto
x2 = std::move(x);
97
}
98
BEAST_EXPECT(i == 0);
99
{
100
scope_fail
x{[&i]() { i = 4; }};
101
x.
release
();
102
auto
x2 = std::move(x);
103
}
104
BEAST_EXPECT(i == 0);
105
{
106
try
107
{
108
scope_fail
x{[&i]() { i = 5; }};
109
throw
1;
110
}
111
catch
(...)
112
{
113
}
114
}
115
BEAST_EXPECT(i == 5);
116
{
117
try
118
{
119
scope_fail
x{[&i]() { i = 6; }};
120
x.
release
();
121
throw
1;
122
}
123
catch
(...)
124
{
125
}
126
}
127
BEAST_EXPECT(i == 5);
128
}
129
130
void
131
test_scope_success
()
132
{
133
// scope_success executes the functor on destruction only
134
// if an exception is not unwinding, unless release() is called
135
int
i = 0;
136
{
137
scope_success
x{[&i]() { i = 1; }};
138
}
139
BEAST_EXPECT(i == 1);
140
{
141
scope_success
x{[&i]() { i = 2; }};
142
x.
release
();
143
}
144
BEAST_EXPECT(i == 1);
145
{
146
scope_success
x{[&i]() { i += 2; }};
147
auto
x2 = std::move(x);
148
}
149
BEAST_EXPECT(i == 3);
150
{
151
scope_success
x{[&i]() { i = 4; }};
152
x.
release
();
153
auto
x2 = std::move(x);
154
}
155
BEAST_EXPECT(i == 3);
156
{
157
try
158
{
159
scope_success
x{[&i]() { i = 5; }};
160
throw
1;
161
}
162
catch
(...)
163
{
164
}
165
}
166
BEAST_EXPECT(i == 3);
167
{
168
try
169
{
170
scope_success
x{[&i]() { i = 6; }};
171
x.
release
();
172
throw
1;
173
}
174
catch
(...)
175
{
176
}
177
}
178
BEAST_EXPECT(i == 3);
179
}
180
181
void
182
run
()
override
183
{
184
test_scope_exit
();
185
test_scope_fail
();
186
test_scope_success
();
187
}
188
};
189
190
BEAST_DEFINE_TESTSUITE
(scope, ripple_basics,
ripple
);
191
192
}
// namespace test
193
}
// namespace ripple
ripple::scope_success
Definition:
scope.h:141
ripple::test::scope_test::test_scope_success
void test_scope_success()
Definition:
scope_test.cpp:131
ripple::test::scope_test::test_scope_fail
void test_scope_fail()
Definition:
scope_test.cpp:80
ripple::test::scope_test
Definition:
scope_test.cpp:26
ripple::test::scope_test::run
void run() override
Definition:
scope_test.cpp:182
ripple::scope_fail
Definition:
scope.h:90
ripple::scope_exit
Definition:
scope.h:42
ripple::scope_success::release
void release() noexcept
Definition:
scope.h:180
ripple::scope_exit::release
void release() noexcept
Definition:
scope.h:80
ripple::test::scope_test::test_scope_exit
void test_scope_exit()
Definition:
scope_test.cpp:29
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
ripple::scope_fail::release
void release() noexcept
Definition:
scope.h:131
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)
Generated by
1.8.17