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