Reverting merge from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / experimental / optional / assignment / 3.cc
blobd631886269cc11c42059c0bb46ff68359e1812b4
1 // { dg-options "-std=gnu++1y" }
2 // { dg-do run }
4 // Copyright (C) 2013 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 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>
24 struct exception {};
26 int counter = 0;
28 struct mixin_counter
30 mixin_counter() { ++counter; }
31 mixin_counter(mixin_counter const&) { ++counter; }
32 ~mixin_counter() { --counter; }
35 struct value_type : private mixin_counter
37 enum state_type
39 zero,
40 moved_from,
41 throwing_construction,
42 throwing_copy,
43 throwing_copy_assignment,
44 throwing_move,
45 throwing_move_assignment,
46 threw,
49 value_type() = default;
51 explicit value_type(state_type state_)
52 : state(state_)
54 throw_if(throwing_construction);
57 value_type(value_type const& other)
58 : state(other.state)
60 throw_if(throwing_copy);
63 value_type&
64 operator=(value_type const& other)
66 state = other.state;
67 throw_if(throwing_copy_assignment);
68 return *this;
71 value_type(value_type&& other)
72 : state(other.state)
74 other.state = moved_from;
75 throw_if(throwing_move);
78 value_type&
79 operator=(value_type&& other)
81 state = other.state;
82 other.state = moved_from;
83 throw_if(throwing_move_assignment);
84 return *this;
87 void throw_if(state_type match)
89 if(state == match)
91 state = threw;
92 throw exception {};
96 state_type state = zero;
99 int main()
101 using O = std::experimental::optional<value_type>;
102 using S = value_type::state_type;
103 auto const make = [](S s = S::zero) { return value_type { s }; };
105 enum outcome_type { nothrow, caught, bad_catch };
107 // Check value assignment for disengaged optional
110 O o;
111 value_type v = make(S::throwing_copy_assignment);
112 o = v;
113 VERIFY( o && o->state == S::throwing_copy_assignment );
117 O o;
118 value_type v = make(S::throwing_move_assignment);
119 o = std::move(v);
120 VERIFY( o && o->state == S::throwing_move_assignment );
124 outcome_type outcome {};
125 O o;
126 value_type v = make(S::throwing_copy);
130 o = v;
132 catch(exception const&)
133 { outcome = caught; }
134 catch(...)
135 { outcome = bad_catch; }
137 VERIFY( !o );
141 outcome_type outcome {};
142 O o;
143 value_type v = make(S::throwing_move);
147 o = std::move(v);
149 catch(exception const&)
150 { outcome = caught; }
151 catch(...)
152 { outcome = bad_catch; }
154 VERIFY( !o );
157 VERIFY( counter == 0 );