• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // <optional>
12 
13 // optional(const optional<T>& rhs);
14 
15 #include <experimental/optional>
16 #include <type_traits>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 using std::experimental::optional;
22 
23 template <class T>
24 void
test(const optional<T> & rhs,bool is_going_to_throw=false)25 test(const optional<T>& rhs, bool is_going_to_throw = false)
26 {
27     bool rhs_engaged = static_cast<bool>(rhs);
28 #ifdef TEST_HAS_NO_EXCEPTIONS
29     if (is_going_to_throw)
30         return;
31 #else
32     try
33 #endif
34     {
35         optional<T> lhs = rhs;
36         assert(is_going_to_throw == false);
37         assert(static_cast<bool>(lhs) == rhs_engaged);
38         if (rhs_engaged)
39             assert(*lhs == *rhs);
40     }
41 #ifndef TEST_HAS_NO_EXCEPTIONS
42     catch (int i)
43     {
44         assert(i == 6);
45         assert(is_going_to_throw);
46     }
47 #endif
48 }
49 
50 class X
51 {
52     int i_;
53 public:
X(int i)54     X(int i) : i_(i) {}
X(const X & x)55     X(const X& x) : i_(x.i_) {}
~X()56     ~X() {i_ = 0;}
operator ==(const X & x,const X & y)57     friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
58 };
59 
60 class Y
61 {
62     int i_;
63 public:
Y(int i)64     Y(int i) : i_(i) {}
Y(const Y & x)65     Y(const Y& x) : i_(x.i_) {}
66 
operator ==(const Y & x,const Y & y)67     friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
68 };
69 
70 int count = 0;
71 
72 class Z
73 {
74     int i_;
75 public:
Z(int i)76     Z(int i) : i_(i) {}
Z(const Z &)77     Z(const Z&)
78     {
79         if (++count == 2)
80             TEST_THROW(6);
81     }
82 
operator ==(const Z & x,const Z & y)83     friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
84 };
85 
86 
main()87 int main()
88 {
89     {
90         typedef int T;
91         optional<T> rhs;
92         test(rhs);
93     }
94     {
95         typedef int T;
96         optional<T> rhs(3);
97         test(rhs);
98     }
99     {
100         typedef const int T;
101         optional<T> rhs(3);
102         test(rhs);
103     }
104     {
105         typedef X T;
106         optional<T> rhs;
107         test(rhs);
108     }
109     {
110         typedef X T;
111         optional<T> rhs(X(3));
112         test(rhs);
113     }
114     {
115         typedef const X T;
116         optional<T> rhs(X(3));
117         test(rhs);
118     }
119     {
120         typedef Y T;
121         optional<T> rhs;
122         test(rhs);
123     }
124     {
125         typedef Y T;
126         optional<T> rhs(Y(3));
127         test(rhs);
128     }
129     {
130         typedef Z T;
131         optional<T> rhs;
132         test(rhs);
133     }
134     {
135         typedef Z T;
136         optional<T> rhs(Z(3));
137         test(rhs, true);
138     }
139 }
140