forked from project-arcana/clean-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional.cc
More file actions
117 lines (86 loc) · 2.11 KB
/
Copy pathoptional.cc
File metadata and controls
117 lines (86 loc) · 2.11 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <nexus/test.hh>
#include <clean-core/optional.hh>
#include <clean-core/string.hh>
#include <clean-core/to_string.hh>
#include <typed-geometry/feature/basic.hh>
TEST("cc::optional basics")
{
cc::optional<int> v;
CHECK(!v.has_value());
CHECK(v != 0);
v = 7;
CHECK(v == 7);
CHECK(v != 8);
CHECK(v != cc::nullopt);
auto vv = cc::make_optional(13);
CHECK(v != vv);
vv = 7;
CHECK(v == vv);
auto vf = cc::make_optional(7.f);
CHECK(v == vf);
vf = 8;
CHECK(v != vf);
v = vf;
CHECK(v == 8);
v = {};
CHECK(!v.has_value());
v = 3;
CHECK(v.has_value());
v = cc::nullopt;
CHECK(!v.has_value());
}
TEST("cc::optional string")
{
cc::optional<cc::string> v;
CHECK(!v.has_value());
v = "hello";
CHECK(v == "hello");
v = {};
CHECK(v != "hello");
CHECK(!v.has_value());
}
TEST("cc::optional map")
{
cc::optional<int> i = 17;
CHECK(i == 17);
i = i.map([](int x) { return -x * 2; });
CHECK(i == -34);
auto iabs = [](int x) { return tg::abs(x); };
i = i.map(iabs);
CHECK(i == 34);
auto s = i.map([](int x) { return cc::to_string(x); });
CHECK(s == "34");
i = cc::nullopt;
CHECK(!i.has_value());
s = i.map([](int x) { return cc::to_string(x); });
CHECK(!s.has_value());
i = i.map(iabs);
CHECK(!i.has_value());
i = 123;
s = i.map([](int x) { return cc::to_string(x); });
CHECK(s == "123");
i = s.map(&cc::string::size);
CHECK(i == 3);
cc::optional<tg::pos3> p = tg::pos3(1, 2, 3);
i = p.map(&tg::pos3::y);
CHECK(i == 2);
}
TEST("cc::optional transform")
{
cc::optional<int> i = 17;
CHECK(i == 17);
i.transform([](int& x) { x *= 2; });
CHECK(i == 34);
i = cc::nullopt;
CHECK(!i.has_value());
i.transform([](int& x) { x *= 2; });
CHECK(!i.has_value());
cc::optional<cc::string> s = cc::string("hello");
CHECK(s == "hello");
s.transform(&cc::string::clear);
CHECK(s == "");
s = cc::nullopt;
CHECK(!s.has_value());
s.transform(&cc::string::clear);
CHECK(!s.has_value());
}