-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptional.test.cc
200 lines (162 loc) · 6.46 KB
/
optional.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
#include "snn-core/optional.hh"
#include "snn-core/unittest.hh"
namespace snn::app
{
namespace
{
constexpr bool example()
{
{
array a{123, 456, 789};
auto opt = a.at(1);
static_assert(std::is_same_v<decltype(opt), optional<int&>>);
snn_require(opt); // Has value.
snn_require(opt.has_value());
snn_require(opt.value() == 456);
snn_require(opt.value(promise::has_value) == 456);
snn_require(opt.value_or(-1) == 456);
}
{
array a{123, 456, 789};
auto opt = a.at(3); // Out of bounds.
static_assert(std::is_same_v<decltype(opt), optional<int&>>);
snn_require(!opt); // Has no value.
snn_require(!opt.has_value());
snn_require_throws_code(opt.value(), generic::error::no_value);
snn_require(opt.value_or(-1) == -1);
}
return true;
}
constexpr optional<i32> test_int_failure()
{
return nullopt;
}
constexpr optional<i32> test_int_success()
{
return 382;
}
inline optional<i32&> test_int_ref_failure()
{
return nullopt;
}
inline optional<i32&> test_int_ref_success(i32& i)
{
return i;
}
constexpr optional<void> test_void_failure()
{
return nullopt;
}
constexpr optional<void> test_void_success()
{
return {};
}
}
}
namespace snn
{
void unittest()
{
snn_static_require(app::example());
static_assert(sizeof(optional<u8>) == 2);
static_assert(sizeof(optional<u16>) == 4);
static_assert(sizeof(optional<i32>) == 8);
static_assert(sizeof(optional<usize>) == 16);
static_assert(sizeof(optional<str>) == 32);
static_assert(sizeof(optional<i32&>) == 8);
static_assert(sizeof(optional<void>) == 1);
static_assert(is_result_v<optional<int>>);
static_assert(is_result_v<optional<int&>>);
static_assert(is_result_v<optional<const int&>>);
static_assert(is_result_v<optional<void>>);
static_assert(is_result_v<const optional<int>>);
static_assert(is_result_v<const optional<int&>>);
static_assert(is_result_v<const optional<const int&>>);
static_assert(is_result_v<const optional<void>>);
static_assert(std::is_same_v<result_wrap_t<optional<int>>, optional<int>>);
static_assert(std::is_same_v<result_wrap_t<optional<int&>>, optional<int&>>);
static_assert(std::is_same_v<result_wrap_t<optional<const int&>>, optional<const int&>>);
static_assert(std::is_same_v<result_wrap_t<optional<void>>, optional<void>>);
static_assert(std::is_same_v<result_wrap_t<int, optional_error_storage>, optional<int>>);
static_assert(std::is_same_v<result_wrap_t<int&, optional_error_storage>, optional<int&>>);
static_assert(std::is_same_v<result_wrap_t<const int&, optional_error_storage>,
optional<const int&>>);
static_assert(std::is_same_v<result_wrap_t<void, optional_error_storage>, optional<void>>);
static_assert(
std::is_same_v<result_wrap_t<result<int>, optional_error_storage>, result<int>>);
static_assert(is_trivially_relocatable_v<optional<int>>);
static_assert(is_trivially_relocatable_v<optional<int&>>);
static_assert(is_trivially_relocatable_v<optional<const int&>>);
static_assert(is_trivially_relocatable_v<optional<void>>);
static_assert(is_trivially_relocatable_v<optional<str>>);
static_assert(optional_error_storage::has_boolean_state());
static_assert(optional_error_storage::default_error_code() == nullopt);
{
constexpr auto opt = app::test_int_failure();
static_assert(!opt);
static_assert(!opt.has_value());
snn_require_throws_code(opt.value(), generic::error::no_value);
}
{
auto [i, ec] = app::test_int_failure().unpack();
snn_require(ec);
snn_require(i == 0);
}
{
auto opt = app::test_int_ref_failure();
snn_require(!opt);
snn_require(!opt.has_value());
snn_require_throws_code(opt.value(), generic::error::no_value);
}
{
int i = 472;
auto opt = app::test_int_ref_success(i);
snn_require(opt);
snn_require(opt.has_value());
snn_require(opt.value() == 472);
snn_require(&opt.value() == &i);
snn_require(opt.value(promise::has_value) == 472);
snn_require(&opt.value(promise::has_value) == &i);
auto ec = opt.error_code();
snn_require(!ec);
snn_require(app::test_int_ref_success(i).value() == 472);
snn_require(&app::test_int_ref_success(i).value() == &i);
}
{
constexpr auto opt = app::test_int_success();
static_assert(opt);
static_assert(opt.has_value());
static_assert(opt.value() == 382);
static_assert(opt.value(promise::has_value) == 382);
constexpr auto ec = opt.error_code();
static_assert(!ec);
static_assert(app::test_int_success().value() == 382);
}
{
auto [i, ec] = app::test_int_success().unpack();
snn_require(!ec);
snn_require(i == 382);
}
{
constexpr auto opt = app::test_void_failure();
static_assert(!opt);
static_assert(!opt.has_value());
static_assert(opt.error_code() == generic::error::no_value);
snn_require_throws_code(app::test_void_failure().or_throw(), generic::error::no_value);
}
{
constexpr auto opt = app::test_void_success();
static_assert(opt);
static_assert(opt.has_value());
constexpr auto ec = opt.error_code();
static_assert(!ec);
}
{
constexpr auto opt =
app::test_void_failure().and_then([]() -> optional<int> { return 33; });
static_assert(!opt);
}
}
}