-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdefer.test.cc
49 lines (36 loc) · 963 Bytes
/
defer.test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
#include "snn-core/defer.hh"
#include "snn-core/unittest.hh"
namespace snn::app
{
namespace
{
constexpr bool example()
{
int i = 40;
{
defer add_three{[&]() { i += 3; }};
defer divide_by_two{[&]() { i /= 2; }};
snn_require(i == 40);
{
defer add_eight{[&]() { i += 8; }};
snn_require(i == 40);
// "add_eight" goes out of scope.
}
snn_require(i == 48);
// "divide_by_two" goes out of scope.
// "add_three" goes out of scope.
}
snn_require(i == 27);
return true;
}
}
}
namespace snn
{
void unittest()
{
snn_static_require(app::example());
}
}