]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/optional/assignment/1.cc
1 // { dg-do run { target c++14 } }
3 // Copyright (C) 2013-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <experimental/optional>
21 #include <testsuite_hooks.h>
29 mixin_counter() { ++counter
; }
30 mixin_counter(mixin_counter
const&) { ++counter
; }
31 ~mixin_counter() { --counter
; }
34 struct value_type
: private mixin_counter
40 throwing_construction
,
42 throwing_copy_assignment
,
44 throwing_move_assignment
,
48 value_type() = default;
50 explicit value_type(state_type state_
)
53 throw_if(throwing_construction
);
56 value_type(value_type
const& other
)
59 throw_if(throwing_copy
);
63 operator=(value_type
const& other
)
66 throw_if(throwing_copy_assignment
);
70 value_type(value_type
&& other
)
73 other
.state
= moved_from
;
74 throw_if(throwing_move
);
78 operator=(value_type
&& other
)
81 other
.state
= moved_from
;
82 throw_if(throwing_move_assignment
);
86 void throw_if(state_type match
)
95 state_type state
= zero
;
100 using O
= std::experimental::optional
<value_type
>;
101 using S
= value_type::state_type
;
102 auto const make
= [](S s
= S::zero
) { return O
{ std::experimental::in_place
, s
}; };
104 enum outcome_type
{ nothrow
, caught
, bad_catch
};
106 // Check copy/move assignment for disengaged optional
108 // From disengaged optional
134 // From engaged optional
138 O p
= make(S::throwing_copy_assignment
);
140 VERIFY( o
&& o
->state
== S::throwing_copy_assignment
);
141 VERIFY( p
&& p
->state
== S::throwing_copy_assignment
);
147 O p
= make(S::throwing_move_assignment
);
149 VERIFY( o
&& o
->state
== S::throwing_move_assignment
);
150 VERIFY( p
&& p
->state
== S::moved_from
);
154 outcome_type outcome
{};
157 O p
= make(S::throwing_copy
);
163 catch(exception
const&)
164 { outcome
= caught
; }
166 { outcome
= bad_catch
; }
168 VERIFY( outcome
== caught
);
170 VERIFY( p
&& p
->state
== S::throwing_copy
);
174 outcome_type outcome
{};
177 O p
= make(S::throwing_move
);
183 catch(exception
const&)
184 { outcome
= caught
; }
186 { outcome
= bad_catch
; }
188 VERIFY( outcome
== caught
);
190 VERIFY( p
&& p
->state
== S::moved_from
);
193 VERIFY( counter
== 0 );