| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <expected> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
constexpr bool |
| 7 |
test_default() |
| 8 |
{ |
| 9 |
std::expected<int, int> e; |
| 10 |
VERIFY( e.has_value() ); |
| 11 |
VERIFY( *e == 0 ); |
| 12 |
|
| 13 |
std::expected<void, int> ev; |
| 14 |
VERIFY( ev.has_value() ); |
| 15 |
VERIFY( (ev.value(), true) ); |
| 16 |
|
| 17 |
return true; |
| 18 |
} |
| 19 |
|
| 20 |
constexpr bool |
| 21 |
test_val() |
| 22 |
{ |
| 23 |
std::expected<int, int> e1(1); |
| 24 |
VERIFY( e1.has_value() ); |
| 25 |
VERIFY( *e1 == 1 ); |
| 26 |
|
| 27 |
std::expected<int, int> e2(std::in_place, 2); |
| 28 |
VERIFY( e2.has_value() ); |
| 29 |
VERIFY( *e2 == 2 ); |
| 30 |
|
| 31 |
struct X |
| 32 |
{ |
| 33 |
constexpr X(std::initializer_list<int> l, void*) : n(l.size()) { } |
| 34 |
int n; |
| 35 |
}; |
| 36 |
|
| 37 |
std::expected<X, int> e3(X{{1, 2, 3}, nullptr}); |
| 38 |
VERIFY( e3.has_value() ); |
| 39 |
VERIFY( e3->n == 3 ); |
| 40 |
|
| 41 |
std::expected<X, int> e4(std::in_place, {1, 2, 3, 4}, nullptr); |
| 42 |
VERIFY( e4.has_value() ); |
| 43 |
VERIFY( e4->n == 4 ); |
| 44 |
|
| 45 |
std::expected<void, int> ev(std::in_place); |
| 46 |
VERIFY( ev.has_value() ); |
| 47 |
VERIFY( (ev.value(), true) ); |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
constexpr bool |
| 53 |
test_err() |
| 54 |
{ |
| 55 |
std::expected<int, int> e1(std::unexpected<int>(1)); |
| 56 |
VERIFY( ! e1.has_value() ); |
| 57 |
VERIFY( e1.error() == 1 ); |
| 58 |
|
| 59 |
const std::unexpected<int> u2(2); |
| 60 |
std::expected<int, int> e2(u2); |
| 61 |
VERIFY( ! e2.has_value() ); |
| 62 |
VERIFY( e2.error() == 2 ); |
| 63 |
|
| 64 |
std::expected<int, int> e3(std::unexpect, 3); |
| 65 |
VERIFY( ! e3.has_value() ); |
| 66 |
VERIFY( e3.error() == 3 ); |
| 67 |
|
| 68 |
struct X |
| 69 |
{ |
| 70 |
constexpr X(int i, int j) : n(i+j) { } |
| 71 |
constexpr X(std::initializer_list<int> l, void*) : n(l.size()) { } |
| 72 |
int n; |
| 73 |
}; |
| 74 |
|
| 75 |
std::expected<int, X> e4(std::unexpect, 1, 3); |
| 76 |
VERIFY( ! e4.has_value() ); |
| 77 |
VERIFY( e4.error().n == 4 ); |
| 78 |
|
| 79 |
std::expected<int, X> e5(std::unexpect, {1, 2, 3, 4, 5}, nullptr); |
| 80 |
VERIFY( ! e5.has_value() ); |
| 81 |
VERIFY( e5.error().n == 5 ); |
| 82 |
|
| 83 |
std::expected<const void, int> ev1(std::unexpected<int>(1)); |
| 84 |
VERIFY( ! ev1.has_value() ); |
| 85 |
VERIFY( ev1.error() == 1 ); |
| 86 |
|
| 87 |
std::expected<volatile void, int> ev2(u2); |
| 88 |
VERIFY( ! ev2.has_value() ); |
| 89 |
VERIFY( ev2.error() == 2 ); |
| 90 |
|
| 91 |
std::expected<const volatile void, int> ev3(std::unexpect, 3); |
| 92 |
VERIFY( ! ev3.has_value() ); |
| 93 |
VERIFY( ev3.error() == 3 ); |
| 94 |
|
| 95 |
std::expected<void, X> ev4(std::unexpect, 1, 3); |
| 96 |
VERIFY( ! ev4.has_value() ); |
| 97 |
VERIFY( ev4.error().n == 4 ); |
| 98 |
|
| 99 |
std::expected<void, X> ev5(std::unexpect, {1, 2, 3, 4, 5}, nullptr); |
| 100 |
VERIFY( ! ev5.has_value() ); |
| 101 |
VERIFY( ev5.error().n == 5 ); |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
constexpr bool |
| 107 |
test_copy() |
| 108 |
{ |
| 109 |
std::expected<int, int> e1(1); |
| 110 |
std::expected<int, int> e2(e1); |
| 111 |
VERIFY( e2.value() == 1 ); |
| 112 |
std::expected<int, int> e3(std::move(e2)); |
| 113 |
VERIFY( e2.value() == 1 ); |
| 114 |
VERIFY( e3.value() == 1 ); |
| 115 |
std::expected<short, short> e4(e1); |
| 116 |
VERIFY( e4.value() == 1 ); |
| 117 |
std::expected<short, short> e5(std::move(e4)); |
| 118 |
VERIFY( e4.value() == 1 ); |
| 119 |
VERIFY( e5.value() == 1 ); |
| 120 |
|
| 121 |
std::expected<int, int> u1(std::unexpect, 2); |
| 122 |
std::expected<int, int> u2(u1); |
| 123 |
VERIFY( ! u2.has_value() ); |
| 124 |
VERIFY( u2.error() == 2 ); |
| 125 |
std::expected<int, int> u3(std::move(u2)); |
| 126 |
VERIFY( ! u3.has_value() ); |
| 127 |
VERIFY( u3.error() == 2 ); |
| 128 |
std::expected<short, short> u4(u1); |
| 129 |
VERIFY( ! u4.has_value() ); |
| 130 |
VERIFY( u4.error() == 2 ); |
| 131 |
std::expected<short, short> u5(std::move(u4)); |
| 132 |
VERIFY( ! u5.has_value() ); |
| 133 |
VERIFY( u5.error() == 2 ); |
| 134 |
|
| 135 |
std::expected<void, int> ev1; |
| 136 |
std::expected<void, int> ev2(ev1); |
| 137 |
VERIFY( ev2.has_value() ); |
| 138 |
std::expected<void, int> ev3(std::move(ev2)); |
| 139 |
VERIFY( ev2.has_value() ); |
| 140 |
VERIFY( ev3.has_value() ); |
| 141 |
std::expected<volatile void, short> ev4(ev1); |
| 142 |
VERIFY( ev4.has_value() ); |
| 143 |
std::expected<const void, short> ev5(std::move(ev4)); |
| 144 |
VERIFY( ev4.has_value() ); |
| 145 |
VERIFY( ev5.has_value() ); |
| 146 |
|
| 147 |
std::expected<void, int> uv1(std::unexpect, 2); |
| 148 |
std::expected<void, int> uv2(uv1); |
| 149 |
VERIFY( ! uv2.has_value() ); |
| 150 |
VERIFY( uv2.error() == 2 ); |
| 151 |
std::expected<void, int> uv3(std::move(uv2)); |
| 152 |
VERIFY( ! uv3.has_value() ); |
| 153 |
VERIFY( uv3.error() == 2 ); |
| 154 |
std::expected<const void, short> uv4(uv1); |
| 155 |
VERIFY( ! uv4.has_value() ); |
| 156 |
VERIFY( uv4.error() == 2 ); |
| 157 |
std::expected<volatile void, short> uv5(std::move(uv4)); |
| 158 |
VERIFY( ! uv5.has_value() ); |
| 159 |
VERIFY( uv5.error() == 2 ); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
constexpr bool |
| 165 |
test_pr105153() |
| 166 |
{ |
| 167 |
struct E { |
| 168 |
E(int&&) = delete; |
| 169 |
E(const int&); |
| 170 |
}; |
| 171 |
|
| 172 |
std::expected<void, E> e(std::expected<void, int>{}); |
| 173 |
|
| 174 |
static_assert( ! std::is_constructible_v<std::expected<void, int>, |
| 175 |
std::expected<int, int>> ); |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
int main() |
| 181 |
{ |
| 182 |
test_default(); |
| 183 |
static_assert( test_default() ); |
| 184 |
test_val(); |
| 185 |
static_assert( test_val() ); |
| 186 |
test_err(); |
| 187 |
static_assert( test_err() ); |
| 188 |
test_copy(); |
| 189 |
static_assert( test_copy() ); |
| 190 |
test_pr105153(); |
| 191 |
static_assert( test_pr105153() ); |
| 192 |
} |