[analyzer][Z3] Restore the original timeout of 15s (#118291)
[llvm-project.git] / libcxx / test / std / utilities / expected / expected.void / swap / member.swap.pass.cpp
blob368a85b4f1a1ee8580d2da72a70632de0682e021
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // constexpr void swap(expected& rhs) noexcept(see below);
13 // Constraints:
14 // is_swappable_v<E> is true and is_move_constructible_v<E> is true.
16 // Throws: Any exception thrown by the expressions in the Effects.
18 // Remarks: The exception specification is equivalent to:
19 // is_nothrow_move_constructible_v<E> && is_nothrow_swappable_v<E>.
21 #include <cassert>
22 #include <expected>
23 #include <type_traits>
24 #include <utility>
26 #include "../../types.h"
27 #include "test_macros.h"
29 // Test Constraints:
30 template <class E>
31 concept HasMemberSwap = requires(std::expected<void, E> x, std::expected<void, E> y) { x.swap(y); };
33 static_assert(HasMemberSwap<int>);
35 struct NotSwappable {};
36 void swap(NotSwappable&, NotSwappable&) = delete;
38 // !is_swappable_v<E>
39 static_assert(!HasMemberSwap<NotSwappable>);
41 struct NotMoveConstructible {
42 NotMoveConstructible(NotMoveConstructible&&) = delete;
43 friend void swap(NotMoveConstructible&, NotMoveConstructible&) {}
46 // !is_move_constructible_v<E>
47 static_assert(!HasMemberSwap<NotMoveConstructible>);
49 // Test noexcept
50 struct MoveMayThrow {
51 MoveMayThrow(MoveMayThrow&&) noexcept(false);
52 friend void swap(MoveMayThrow&, MoveMayThrow&) noexcept {}
55 template <class E>
56 concept MemberSwapNoexcept = //
57 requires(std::expected<void, E> x, std::expected<void, E> y) {
58 { x.swap(y) } noexcept;
61 static_assert(MemberSwapNoexcept<int>);
63 // !is_nothrow_move_constructible_v<E>
64 static_assert(!MemberSwapNoexcept<MoveMayThrow>);
66 struct SwapMayThrow {
67 friend void swap(SwapMayThrow&, SwapMayThrow&) noexcept(false) {}
70 // !is_nothrow_swappable_v<E>
71 static_assert(!MemberSwapNoexcept<SwapMayThrow>);
73 constexpr bool test() {
74 // this->has_value() && rhs.has_value()
76 std::expected<void, int> x;
77 std::expected<void, int> y;
78 x.swap(y);
80 assert(x.has_value());
81 assert(y.has_value());
84 // !this->has_value() && !rhs.has_value()
86 std::expected<void, ADLSwap> x(std::unexpect, 5);
87 std::expected<void, ADLSwap> y(std::unexpect, 10);
88 x.swap(y);
90 assert(!x.has_value());
91 assert(x.error().i == 10);
92 assert(x.error().adlSwapCalled);
93 assert(!y.has_value());
94 assert(y.error().i == 5);
95 assert(y.error().adlSwapCalled);
98 // this->has_value() && !rhs.has_value()
100 Traced::state s{};
101 std::expected<void, Traced> e1(std::in_place);
102 std::expected<void, Traced> e2(std::unexpect, s, 10);
104 e1.swap(e2);
106 assert(!e1.has_value());
107 assert(e1.error().data_ == 10);
108 assert(e2.has_value());
110 assert(s.moveCtorCalled);
111 assert(s.dtorCalled);
114 // !this->has_value() && rhs.has_value()
116 Traced::state s{};
117 std::expected<void, Traced> e1(std::unexpect, s, 10);
118 std::expected<void, Traced> e2(std::in_place);
120 e1.swap(e2);
122 assert(e1.has_value());
123 assert(!e2.has_value());
124 assert(e2.error().data_ == 10);
126 assert(s.moveCtorCalled);
127 assert(s.dtorCalled);
130 // TailClobberer
132 std::expected<void, TailClobbererNonTrivialMove<1>> x(std::in_place);
133 std::expected<void, TailClobbererNonTrivialMove<1>> y(std::unexpect);
135 x.swap(y);
137 // The next line would fail if adjusting the "has value" flag happened
138 // _before_ constructing the member object inside the `swap`.
139 assert(!x.has_value());
140 assert(y.has_value());
143 // CheckForInvalidWrites
146 CheckForInvalidWrites<true, true> x(std::unexpect);
147 CheckForInvalidWrites<true, true> y;
149 x.swap(y);
151 assert(x.check());
152 assert(y.check());
155 CheckForInvalidWrites<false, true> x(std::unexpect);
156 CheckForInvalidWrites<false, true> y;
158 x.swap(y);
160 assert(x.check());
161 assert(y.check());
165 return true;
168 void testException() {
169 #ifndef TEST_HAS_NO_EXCEPTIONS
170 // !e1.has_value() && e2.has_value()
172 bool e1Destroyed = false;
173 std::expected<void, ThrowOnMove> e1(std::unexpect, e1Destroyed);
174 std::expected<void, ThrowOnMove> e2(std::in_place);
175 try {
176 e1.swap(e2);
177 assert(false);
178 } catch (Except) {
179 assert(!e1.has_value());
180 assert(e2.has_value());
181 assert(!e1Destroyed);
185 // e1.has_value() && !e2.has_value()
187 bool e2Destroyed = false;
188 std::expected<void, ThrowOnMove> e1(std::in_place);
189 std::expected<void, ThrowOnMove> e2(std::unexpect, e2Destroyed);
190 try {
191 e1.swap(e2);
192 assert(false);
193 } catch (Except) {
194 assert(e1.has_value());
195 assert(!e2.has_value());
196 assert(!e2Destroyed);
200 // TailClobberer
202 std::expected<void, TailClobbererNonTrivialMove<0, false, true>> x(std::in_place);
203 std::expected<void, TailClobbererNonTrivialMove<0, false, true>> y(std::unexpect);
204 try {
205 x.swap(y);
206 assert(false);
207 } catch (Except) {
208 // This would fail if `TailClobbererNonTrivialMove<0, false, true>`
209 // clobbered the flag before throwing the exception.
210 assert(x.has_value());
211 assert(!y.has_value());
214 #endif // TEST_HAS_NO_EXCEPTIONS
217 int main(int, char**) {
218 test();
219 static_assert(test());
220 testException();
221 return 0;