]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/optional/assignment/1.cc
1 // { dg-options "-std=gnu++1y" }
4 // Copyright (C) 2013 Free Software Foundation, Inc.
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)
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 moved_to 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 <experimental/optional>
22 #include <testsuite_hooks.h>
30 mixin_counter() { ++counter
; }
31 mixin_counter(mixin_counter
const&) { ++counter
; }
32 ~mixin_counter() { --counter
; }
35 struct value_type
: private mixin_counter
41 throwing_construction
,
43 throwing_copy_assignment
,
45 throwing_move_assignment
,
49 value_type() = default;
51 explicit value_type(state_type state_
)
54 throw_if(throwing_construction
);
57 value_type(value_type
const& other
)
60 throw_if(throwing_copy
);
64 operator=(value_type
const& other
)
67 throw_if(throwing_copy_assignment
);
71 value_type(value_type
&& other
)
74 other
.state
= moved_from
;
75 throw_if(throwing_move
);
79 operator=(value_type
&& other
)
82 other
.state
= moved_from
;
83 throw_if(throwing_move_assignment
);
87 void throw_if(state_type match
)
96 state_type state
= zero
;
101 using O
= std::experimental::optional
<value_type
>;
102 using S
= value_type::state_type
;
103 auto const make
= [](S s
= S::zero
) { return O
{ std::experimental::in_place
, s
}; };
105 enum outcome_type
{ nothrow
, caught
, bad_catch
};
107 // Check copy/move assignment for disengaged optional
109 // From disengaged optional
135 // From engaged optional
139 O p
= make(S::throwing_copy_assignment
);
141 VERIFY( o
&& o
->state
== S::throwing_copy_assignment
);
142 VERIFY( p
&& p
->state
== S::throwing_copy_assignment
);
148 O p
= make(S::throwing_move_assignment
);
150 VERIFY( o
&& o
->state
== S::throwing_move_assignment
);
151 VERIFY( p
&& p
->state
== S::moved_from
);
155 outcome_type outcome
{};
158 O p
= make(S::throwing_copy
);
164 catch(exception
const&)
165 { outcome
= caught
; }
167 { outcome
= bad_catch
; }
169 VERIFY( outcome
== caught
);
171 VERIFY( p
&& p
->state
== S::throwing_copy
);
175 outcome_type outcome
{};
178 O p
= make(S::throwing_move
);
184 catch(exception
const&)
185 { outcome
= caught
; }
187 { outcome
= bad_catch
; }
189 VERIFY( outcome
== caught
);
191 VERIFY( p
&& p
->state
== S::moved_from
);
194 VERIFY( counter
== 0 );