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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
// { dg-do run { target c++23 } }
#include <expected>
#include <expected>
#include <testsuite_hooks.h>
struct X
{
constexpr int f() & { return 1; }
constexpr int f() const & { return 2; }
constexpr int f() && { return 3; }
constexpr int f() const && { return 4; }
};
constexpr bool
test_arrow()
{
std::expected<X, int> e1;
VERIFY( e1->f() == 1 );
const auto& e2 = e1;
VERIFY( e2->f() == 2 );
return true;
}
constexpr bool
test_star()
{
std::expected<X, int> e1;
VERIFY( (*e1).f() == 1 );
VERIFY( std::move(*e1).f() == 3 );
const auto& e2 = e1;
VERIFY( (*e2).f() == 2 );
VERIFY( std::move(*e2).f() == 4 );
std::expected<void, int> v;
*v;
return true;
}
constexpr bool
test_has_value()
{
std::expected<int, int> e;
VERIFY( e.has_value() );
VERIFY( e );
e = std::unexpected(1);
VERIFY( ! e.has_value() );
VERIFY( ! e );
std::expected<void, int> v;
VERIFY( v.has_value() );
VERIFY( v );
v = std::unexpected(1);
VERIFY( ! v.has_value() );
VERIFY( ! v );
return true;
}
constexpr bool
test_value()
{
std::expected<X, int> e1;
VERIFY( e1.value().f() == 1 );
VERIFY( std::move(e1).value().f() == 3 );
const auto& e2 = e1;
VERIFY( e2.value().f() == 2 );
VERIFY( std::move(e2).value().f() == 4 );
std::expected<void, int> v1;
v1.value();
std::move(v1).value();
return true;
}
void
test_value_throw()
{
std::expected<int, int> e1 = std::unexpected(9);
try {
e1.value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 9 );
}
try {
std::move(e1).value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 9 );
}
const auto& e2 = e1;
try {
e2.value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 9 );
}
try {
std::move(e2).value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 9 );
}
std::expected<void, int> v1 = std::unexpected(8);
try {
v1.value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 8 );
}
try {
std::move(v1).value();
VERIFY( false );
} catch (const std::bad_expected_access<int>& e) {
VERIFY( e.error() == 8 );
}
}
constexpr bool
test_error()
{
std::expected<int, X> e1(std::unexpect);
VERIFY( e1.error().f() == 1 );
VERIFY( std::move(e1).error().f() == 3 );
const auto& e2 = e1;
VERIFY( e2.error().f() == 2 );
VERIFY( std::move(e2).error().f() == 4 );
std::expected<void, X> v1(std::unexpect);
VERIFY( v1.error().f() == 1 );
VERIFY( std::move(v1).error().f() == 3 );
const auto& v2 = v1;
VERIFY( v2.error().f() == 2 );
VERIFY( std::move(v2).error().f() == 4 );
return true;
}
constexpr bool
test_value_or()
{
struct Movable
{
constexpr Movable(int i) : x(i) { }
constexpr Movable(const Movable&) = default;
constexpr Movable(Movable&& m) : x(m.x) { m.x = -1; }
int x;
constexpr bool operator==(int i) const { return x == i; }
};
std::expected<Movable, int> e1(1);
Movable m2(2);
VERIFY( e1.value_or(2) == 1 );
VERIFY( e1.value_or(m2) == 1 );
VERIFY( e1.value_or(std::move(m2)) == 1 );
VERIFY( m2 == 2 );
VERIFY( std::move(e1).value_or(m2) == 1 );
VERIFY( *e1 == -1 ); // moved
VERIFY( m2 == 2 );
e1 = std::unexpected(3);
VERIFY( e1.value_or(m2) == 2 );
VERIFY( m2 == 2 );
VERIFY( std::move(e1).value_or(m2) == 2 );
VERIFY( m2 == 2 );
VERIFY( e1.value_or(std::move(m2)) == 2 );
VERIFY( m2 == -1 );
m2.x = 4;
VERIFY( std::move(e1).value_or(std::move(m2)) == 4 );
VERIFY( m2 == -1 );
VERIFY( e1.value_or(5) == 5 );
VERIFY( std::move(e1).value_or(6) == 6 );
return true;
}
constexpr bool
test_error_or()
{
std::expected<int, int> e1(1), e2(std::unexpect, 3);
VERIFY( e1.error_or(2) == 2 );
VERIFY( std::move(e1).error_or(2) == 2 );
VERIFY( e2.error_or(2) == 3 );
VERIFY( std::move(e2).error_or(2) == 3 );
std::expected<void, int> e3, e4(std::unexpect, 3);
VERIFY( e3.error_or(2) == 2 );
VERIFY( std::move(e3).error_or(2) == 2 );
VERIFY( e4.error_or(2) == 3 );
VERIFY( std::move(e4).error_or(2) == 3 );
return true;
}
int main()
{
static_assert( test_arrow() );
test_arrow();
static_assert( test_star() );
test_star();
static_assert( test_has_value() );
test_has_value();
static_assert( test_value() );
test_value();
test_value_throw();
static_assert( test_error() );
test_error();
static_assert( test_value_or() );
test_value_or();
static_assert( test_error_or() );
test_error_or();
}
|