* ipa-fnsummary.c (estimate_node_size_and_time): Be more tolerant for
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / optional / cons / value.cc
blobc5700578d02994c36d6521c4cfee8fe81a850a20
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run }
4 // Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 #include <optional>
22 #include <testsuite_hooks.h>
24 #include <vector>
25 #include <string>
27 struct tracker
29 tracker(int value) : value(value) { ++count; }
30 ~tracker() { --count; }
32 tracker(tracker const& other) : value(other.value) { ++count; }
33 tracker(tracker&& other) : value(other.value)
35 other.value = -1;
36 ++count;
39 tracker& operator=(tracker const&) = default;
40 tracker& operator=(tracker&&) = default;
42 int value;
44 static int count;
47 int tracker::count = 0;
49 struct exception { };
51 struct throwing_construction
53 explicit throwing_construction(bool propagate) : propagate(propagate) { }
55 throwing_construction(throwing_construction const& other)
56 : propagate(other.propagate)
58 if(propagate)
59 throw exception {};
62 bool propagate;
65 int main()
67 // [20.5.4.1] Constructors
70 auto i = 0x1234ABCD;
71 std::optional<long> o { i };
72 VERIFY( o );
73 VERIFY( *o == 0x1234ABCD );
74 VERIFY( i == 0x1234ABCD );
78 auto i = 0x1234ABCD;
79 std::optional<long> o = i;
80 VERIFY( o );
81 VERIFY( *o == 0x1234ABCD );
82 VERIFY( i == 0x1234ABCD );
86 auto i = 0x1234ABCD;
87 std::optional<long> o = { i };
88 VERIFY( o );
89 VERIFY( *o == 0x1234ABCD );
90 VERIFY( i == 0x1234ABCD );
94 auto i = 0x1234ABCD;
95 std::optional<long> o { std::move(i) };
96 VERIFY( o );
97 VERIFY( *o == 0x1234ABCD );
98 VERIFY( i == 0x1234ABCD );
102 auto i = 0x1234ABCD;
103 std::optional<long> o = std::move(i);
104 VERIFY( o );
105 VERIFY( *o == 0x1234ABCD );
106 VERIFY( i == 0x1234ABCD );
110 auto i = 0x1234ABCD;
111 std::optional<long> o = { std::move(i) };
112 VERIFY( o );
113 VERIFY( *o == 0x1234ABCD );
114 VERIFY( i == 0x1234ABCD );
118 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
119 std::optional<std::vector<int>> o { v };
120 VERIFY( !v.empty() );
121 VERIFY( o->size() == 6 );
125 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
126 std::optional<std::vector<int>> o = v;
127 VERIFY( !v.empty() );
128 VERIFY( o->size() == 6 );
132 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
133 std::optional<std::vector<int>> o { v };
134 VERIFY( !v.empty() );
135 VERIFY( o->size() == 6 );
139 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
140 std::optional<std::vector<int>> o { std::move(v) };
141 VERIFY( v.empty() );
142 VERIFY( o->size() == 6 );
146 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
147 std::optional<std::vector<int>> o = std::move(v);
148 VERIFY( v.empty() );
149 VERIFY( o->size() == 6 );
153 std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
154 std::optional<std::vector<int>> o { std::move(v) };
155 VERIFY( v.empty() );
156 VERIFY( o->size() == 6 );
160 tracker t { 333 };
161 std::optional<tracker> o = t;
162 VERIFY( o->value == 333 );
163 VERIFY( tracker::count == 2 );
164 VERIFY( t.value == 333 );
168 tracker t { 333 };
169 std::optional<tracker> o = std::move(t);
170 VERIFY( o->value == 333 );
171 VERIFY( tracker::count == 2 );
172 VERIFY( t.value == -1 );
175 enum outcome { nothrow, caught, bad_catch };
178 outcome result = nothrow;
179 throwing_construction t { false };
183 std::optional<throwing_construction> o { t };
185 catch(exception const&)
186 { result = caught; }
187 catch(...)
188 { result = bad_catch; }
190 VERIFY( result == nothrow );
194 outcome result = nothrow;
195 throwing_construction t { true };
199 std::optional<throwing_construction> o { t };
201 catch(exception const&)
202 { result = caught; }
203 catch(...)
204 { result = bad_catch; }
206 VERIFY( result == caught );
210 outcome result = nothrow;
211 throwing_construction t { false };
215 std::optional<throwing_construction> o { std::move(t) };
217 catch(exception const&)
218 { result = caught; }
219 catch(...)
220 { result = bad_catch; }
222 VERIFY( result == nothrow );
226 outcome result = nothrow;
227 throwing_construction t { true };
231 std::optional<throwing_construction> o { std::move(t) };
233 catch(exception const&)
234 { result = caught; }
235 catch(...)
236 { result = bad_catch; }
238 VERIFY( result == caught );
242 std::optional<std::string> os = "foo";
243 struct X
245 explicit X(int) {}
246 X& operator=(int) {return *this;}
248 std::optional<X> ox{42};
249 std::optional<int> oi{42};
250 std::optional<X> ox2{oi};
251 std::optional<std::string> os2;
252 os2 = "foo";
253 std::optional<X> ox3;
254 ox3 = 42;
255 std::optional<X> ox4;
256 ox4 = oi;
259 std::optional<int> oi = std::optional<short>();
260 VERIFY(!bool(oi));
261 std::optional<std::string> os = std::optional<const char*>();
262 VERIFY(!bool(os));
263 std::optional<std::optional<int>> ooi = std::optional<int>();
264 VERIFY(bool(ooi));
265 ooi = std::optional<int>();
266 VERIFY(bool(ooi));
267 ooi = std::optional<int>(42);
268 VERIFY(bool(ooi));
269 VERIFY(bool(*ooi));
270 std::optional<std::optional<int>> ooi2 = std::optional<short>();
271 VERIFY(bool(ooi2));
272 ooi2 = std::optional<short>();
273 VERIFY(bool(ooi2));
274 ooi2 = std::optional<short>(6);
275 VERIFY(bool(ooi2));
276 VERIFY(bool(*ooi2));
277 std::optional<std::optional<int>> ooi3 = std::optional<int>(42);
278 VERIFY(bool(ooi3));
279 VERIFY(bool(*ooi3));
280 std::optional<std::optional<int>> ooi4 = std::optional<short>(6);
281 VERIFY(bool(ooi4));
282 VERIFY(bool(*ooi4));