mirror of
https://github.com/XRPLF/rippled.git
synced 2026-01-21 07:05:23 +00:00
This change renames all occurrences of `namespace ripple` and `ripple::` to `namespace xrpl` and `xrpl::`, respectively, as well as the names of test suites. It also provides a script to allow developers to replicate the changes in their local branch or fork to avoid conflicts.
156 lines
2.8 KiB
C++
156 lines
2.8 KiB
C++
#include <xrpl/basics/scope.h>
|
|
|
|
#include <doctest/doctest.h>
|
|
|
|
using namespace xrpl;
|
|
|
|
TEST_CASE("scope_exit")
|
|
{
|
|
// scope_exit always executes the functor on destruction,
|
|
// unless release() is called
|
|
int i = 0;
|
|
{
|
|
scope_exit x{[&i]() { i = 1; }};
|
|
}
|
|
CHECK(i == 1);
|
|
{
|
|
scope_exit x{[&i]() { i = 2; }};
|
|
x.release();
|
|
}
|
|
CHECK(i == 1);
|
|
{
|
|
scope_exit x{[&i]() { i += 2; }};
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 3);
|
|
{
|
|
scope_exit x{[&i]() { i = 4; }};
|
|
x.release();
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 3);
|
|
{
|
|
try
|
|
{
|
|
scope_exit x{[&i]() { i = 5; }};
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 5);
|
|
{
|
|
try
|
|
{
|
|
scope_exit x{[&i]() { i = 6; }};
|
|
x.release();
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 5);
|
|
}
|
|
|
|
TEST_CASE("scope_fail")
|
|
{
|
|
// scope_fail executes the functor on destruction only
|
|
// if an exception is unwinding, unless release() is called
|
|
int i = 0;
|
|
{
|
|
scope_fail x{[&i]() { i = 1; }};
|
|
}
|
|
CHECK(i == 0);
|
|
{
|
|
scope_fail x{[&i]() { i = 2; }};
|
|
x.release();
|
|
}
|
|
CHECK(i == 0);
|
|
{
|
|
scope_fail x{[&i]() { i = 3; }};
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 0);
|
|
{
|
|
scope_fail x{[&i]() { i = 4; }};
|
|
x.release();
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 0);
|
|
{
|
|
try
|
|
{
|
|
scope_fail x{[&i]() { i = 5; }};
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 5);
|
|
{
|
|
try
|
|
{
|
|
scope_fail x{[&i]() { i = 6; }};
|
|
x.release();
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 5);
|
|
}
|
|
|
|
TEST_CASE("scope_success")
|
|
{
|
|
// scope_success executes the functor on destruction only
|
|
// if an exception is not unwinding, unless release() is called
|
|
int i = 0;
|
|
{
|
|
scope_success x{[&i]() { i = 1; }};
|
|
}
|
|
CHECK(i == 1);
|
|
{
|
|
scope_success x{[&i]() { i = 2; }};
|
|
x.release();
|
|
}
|
|
CHECK(i == 1);
|
|
{
|
|
scope_success x{[&i]() { i += 2; }};
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 3);
|
|
{
|
|
scope_success x{[&i]() { i = 4; }};
|
|
x.release();
|
|
auto x2 = std::move(x);
|
|
}
|
|
CHECK(i == 3);
|
|
{
|
|
try
|
|
{
|
|
scope_success x{[&i]() { i = 5; }};
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 3);
|
|
{
|
|
try
|
|
{
|
|
scope_success x{[&i]() { i = 6; }};
|
|
x.release();
|
|
throw 1;
|
|
}
|
|
catch (...)
|
|
{
|
|
}
|
|
}
|
|
CHECK(i == 3);
|
|
}
|