]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/experimental/optional/optional.object/optional.object.assign/copy.pass.cpp
Vendor import of libc++ release_39 branch r276489:
[FreeBSD/FreeBSD.git] / test / std / experimental / optional / optional.object / optional.object.assign / copy.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11
11 // XFAIL: libcpp-no-exceptions
12 // <optional>
13
14 // optional<T>& operator=(const optional<T>& rhs);
15
16 #include <experimental/optional>
17 #include <type_traits>
18 #include <cassert>
19
20 using std::experimental::optional;
21
22 struct X
23 {
24     static bool throw_now;
25
26     X() = default;
27     X(const X&)
28     {
29         if (throw_now)
30             throw 6;
31     }
32 };
33
34 bool X::throw_now = false;
35
36 int main()
37 {
38     {
39         optional<int> opt;
40         constexpr optional<int> opt2;
41         opt = opt2;
42         static_assert(static_cast<bool>(opt2) == false, "");
43         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
44     }
45     {
46         optional<int> opt;
47         constexpr optional<int> opt2(2);
48         opt = opt2;
49         static_assert(static_cast<bool>(opt2) == true, "");
50         static_assert(*opt2 == 2, "");
51         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
52         assert(*opt == *opt2);
53     }
54     {
55         optional<int> opt(3);
56         constexpr optional<int> opt2;
57         opt = opt2;
58         static_assert(static_cast<bool>(opt2) == false, "");
59         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
60     }
61     {
62         optional<int> opt(3);
63         constexpr optional<int> opt2(2);
64         opt = opt2;
65         static_assert(static_cast<bool>(opt2) == true, "");
66         static_assert(*opt2 == 2, "");
67         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
68         assert(*opt == *opt2);
69     }
70     {
71         optional<X> opt;
72         optional<X> opt2(X{});
73         assert(static_cast<bool>(opt2) == true);
74         try
75         {
76             X::throw_now = true;
77             opt = opt2;
78             assert(false);
79         }
80         catch (int i)
81         {
82             assert(i == 6);
83             assert(static_cast<bool>(opt) == false);
84         }
85     }
86 }