Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.assign / copy.pass.cpp
blob9753fdb46b8624401e448b47bc7c86860bd430d6
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
10 // <optional>
12 // constexpr optional<T>& operator=(const optional<T>& rhs);
14 #include <optional>
15 #include <type_traits>
16 #include <cassert>
18 #include "test_macros.h"
19 #include "archetypes.h"
21 using std::optional;
23 struct X
25 static bool throw_now;
27 X() = default;
28 X(const X&)
30 if (throw_now)
31 TEST_THROW(6);
33 X& operator=(X const&) = default;
36 bool X::throw_now = false;
38 template <class Tp>
39 constexpr bool assign_empty(optional<Tp>&& lhs) {
40 const optional<Tp> rhs;
41 lhs = rhs;
42 return !lhs.has_value() && !rhs.has_value();
45 template <class Tp>
46 constexpr bool assign_value(optional<Tp>&& lhs) {
47 const optional<Tp> rhs(101);
48 lhs = rhs;
49 return lhs.has_value() && rhs.has_value() && *lhs == *rhs;
52 int main(int, char**)
55 using O = optional<int>;
56 static_assert(assign_empty(O{42}));
57 static_assert(assign_value(O{42}));
58 assert(assign_empty(O{42}));
59 assert(assign_value(O{42}));
62 using O = optional<TrivialTestTypes::TestType>;
63 static_assert(assign_empty(O{42}));
64 static_assert(assign_value(O{42}));
65 assert(assign_empty(O{42}));
66 assert(assign_value(O{42}));
69 using O = optional<TestTypes::TestType>;
70 assert(assign_empty(O{42}));
71 assert(assign_value(O{42}));
74 using T = TestTypes::TestType;
75 T::reset();
76 optional<T> opt(3);
77 const optional<T> opt2;
78 assert(T::alive == 1);
79 opt = opt2;
80 assert(T::alive == 0);
81 assert(!opt2.has_value());
82 assert(!opt.has_value());
84 #ifndef TEST_HAS_NO_EXCEPTIONS
86 optional<X> opt;
87 optional<X> opt2(X{});
88 assert(static_cast<bool>(opt2) == true);
89 try
91 X::throw_now = true;
92 opt = opt2;
93 assert(false);
95 catch (int i)
97 assert(i == 6);
98 assert(static_cast<bool>(opt) == false);
101 #endif
103 return 0;