blob: 8697089f944f06004a74c6cb6f630eff17974753 (
plain) (
blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include <option.h>
#include <cstdlib>
#include <cstdio>
struct Checker {
static unsigned cnt;
Checker() {
++cnt;
}
Checker(const Checker&) {
++cnt;
}
Checker(Checker&&) {
++cnt;
}
~Checker() {
--cnt;
}
};
unsigned Checker::cnt = 0;
int main() {
using option::option;
auto check_cnt = [](unsigned expect) {
if (expect != Checker::cnt) {
std::printf("Checker: expect=%u cnt=%u\n", expect, Checker::cnt);
std::abort();
}
};
{
option<int> o1;
option<int> o2{::option::none{}};
assert(!o1.has_value());
assert(!o2.has_value());
}
// Assume we start test with cnt=0.
check_cnt(0);
{
option<Checker> o1(Checker{});
// copy construct
option<Checker> o2 = o1;
// move construct
option<Checker> o3 = o1.take();
assert(!o1.has_value());
assert(o2.has_value());
assert(o3.has_value());
// move option
option<Checker> o4 = std::move(o2);
assert(!o2.has_value());
assert(o4.has_value());
// take reference to inner
auto x = o3.value();
// take ownership of inner
auto y = o4.take();
assert(!o1.has_value());
assert(!o2.has_value());
assert(o3.has_value());
assert(!o4.has_value());
}
// Expect to finish test with cnt=0.
check_cnt(0);
{
option<Checker> o1;
assert(!o1.has_value());
o1.emplace();
assert(o1.has_value());
}
// Expect to finish test with cnt=0.
check_cnt(0);
return 0;
}
|