]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/optional/assignment/4.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / optional / assignment / 4.cc
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2013-2020 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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/>.
19
20 #include <experimental/optional>
21 #include <testsuite_hooks.h>
22
23 struct exception {};
24
25 int counter = 0;
26
27 struct mixin_counter
28 {
29 mixin_counter() { ++counter; }
30 mixin_counter(mixin_counter const&) { ++counter; }
31 ~mixin_counter() { --counter; }
32 };
33
34 struct value_type : private mixin_counter
35 {
36 enum state_type
37 {
38 zero,
39 moved_from,
40 throwing_construction,
41 throwing_copy,
42 throwing_copy_assignment,
43 throwing_move,
44 throwing_move_assignment,
45 threw,
46 };
47
48 value_type() = default;
49
50 explicit value_type(state_type state_)
51 : state(state_)
52 {
53 throw_if(throwing_construction);
54 }
55
56 value_type(value_type const& other)
57 : state(other.state)
58 {
59 throw_if(throwing_copy);
60 }
61
62 value_type&
63 operator=(value_type const& other)
64 {
65 state = other.state;
66 throw_if(throwing_copy_assignment);
67 return *this;
68 }
69
70 value_type(value_type&& other)
71 : state(other.state)
72 {
73 other.state = moved_from;
74 throw_if(throwing_move);
75 }
76
77 value_type&
78 operator=(value_type&& other)
79 {
80 state = other.state;
81 other.state = moved_from;
82 throw_if(throwing_move_assignment);
83 return *this;
84 }
85
86 void throw_if(state_type match)
87 {
88 if(state == match)
89 {
90 state = threw;
91 throw exception {};
92 }
93 }
94
95 state_type state = zero;
96 };
97
98 int main()
99 {
100 using O = std::experimental::optional<value_type>;
101 using S = value_type::state_type;
102 auto const make = [](S s = S::zero) { return value_type { s }; };
103
104 enum outcome_type { nothrow, caught, bad_catch };
105
106 // Check value assignment for engaged optional
107
108 {
109 O o = make();
110 value_type v = make(S::throwing_copy);
111 o = v;
112 VERIFY( o && o->state == S::throwing_copy);
113 }
114
115 {
116 O o = make();
117 value_type v = make(S::throwing_move);
118 o = std::move(v);
119 VERIFY( o && o->state == S::throwing_move);
120 }
121
122 {
123 outcome_type outcome {};
124 O o = make();
125 value_type v = make(S::throwing_copy_assignment);
126
127 try
128 {
129 o = v;
130 }
131 catch(exception const&)
132 { outcome = caught; }
133 catch(...)
134 { outcome = bad_catch; }
135
136 VERIFY( o && o->state == S::threw );
137 }
138
139 {
140 outcome_type outcome {};
141 O o = make();
142 value_type v = make(S::throwing_move_assignment);
143
144 try
145 {
146 o = std::move(v);
147 }
148 catch(exception const&)
149 { outcome = caught; }
150 catch(...)
151 { outcome = bad_catch; }
152
153 VERIFY( o && o->state == S::threw );
154 }
155
156 VERIFY( counter == 0 );
157 }