]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_rvalref.h
re PR c++/50500 ([C++0x] [DR 1082] move constructor should cause copy constructor...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_rvalref.h
1 // -*- C++ -*-
2 // Testing utilities for the rvalue reference.
3 //
4 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21 //
22
23 #ifndef _GLIBCXX_TESTSUITE_RVALREF_H
24 #define _GLIBCXX_TESTSUITE_RVALREF_H 1
25
26 #include <testsuite_hooks.h>
27 #include <bits/functional_hash.h>
28
29 namespace __gnu_test
30 {
31 // This class is designed to test libstdc++'s template-based rvalue
32 // reference support. It should fail at compile-time if there is an
33 // attempt to copy it.
34 struct rvalstruct
35 {
36 int val;
37 bool valid;
38
39 rvalstruct() : val(0), valid(true)
40 { }
41
42 rvalstruct(int inval) : val(inval), valid(true)
43 { }
44
45 rvalstruct&
46 operator=(int newval)
47 {
48 val = newval;
49 valid = true;
50 return *this;
51 }
52
53 rvalstruct(const rvalstruct&) = delete;
54
55 rvalstruct(rvalstruct&& in)
56 {
57 bool test __attribute__((unused)) = true;
58 VERIFY( in.valid == true );
59 val = in.val;
60 in.valid = false;
61 valid = true;
62 }
63
64 rvalstruct&
65 operator=(const rvalstruct&) = delete;
66
67 rvalstruct&
68 operator=(rvalstruct&& in)
69 {
70 bool test __attribute__((unused)) = true;
71 VERIFY( this != &in );
72 VERIFY( in.valid == true );
73 val = in.val;
74 in.valid = false;
75 valid = true;
76 return *this;
77 }
78 };
79
80 inline bool
81 operator==(const rvalstruct& lhs, const rvalstruct& rhs)
82 { return lhs.val == rhs.val; }
83
84 inline bool
85 operator<(const rvalstruct& lhs, const rvalstruct& rhs)
86 { return lhs.val < rhs.val; }
87
88 void
89 swap(rvalstruct& lhs, rvalstruct& rhs)
90 {
91 bool test __attribute__((unused)) = true;
92 VERIFY( lhs.valid && rhs.valid );
93 int temp = lhs.val;
94 lhs.val = rhs.val;
95 rhs.val = temp;
96 }
97
98 // This is a moveable class which copies how many times it is copied.
99 // This is mainly of use in the containers, where the an element inserted
100 // into a container has to be copied once to get there, but we want to check
101 // nothing else is copied.
102 struct copycounter
103 {
104 static int copycount;
105 int val;
106 bool valid;
107
108 copycounter() : val(0), valid(true)
109 { }
110
111 copycounter(int inval) : val(inval), valid(true)
112 { }
113
114 copycounter(const copycounter& in) : val(in.val), valid(true)
115 {
116 bool test __attribute__((unused)) = true;
117 VERIFY( in.valid == true );
118 ++copycount;
119 }
120
121 copycounter(copycounter&& in) noexcept
122 {
123 bool test __attribute__((unused)) = true;
124 VERIFY( in.valid == true );
125 val = in.val;
126 in.valid = false;
127 valid = true;
128 }
129
130 copycounter&
131 operator=(int newval)
132 {
133 val = newval;
134 valid = true;
135 return *this;
136 }
137
138 bool
139 operator=(const copycounter& in)
140 {
141 bool test __attribute__((unused)) = true;
142 VERIFY( in.valid == true );
143 ++copycount;
144 val = in.val;
145 valid = true;
146 return true;
147 }
148
149 copycounter&
150 operator=(copycounter&& in)
151 {
152 bool test __attribute__((unused)) = true;
153 VERIFY(in.valid == true);
154 val = in.val;
155 in.valid = false;
156 valid = true;
157 return *this;
158 }
159
160 ~copycounter() noexcept
161 { valid = false; }
162 };
163
164 int copycounter::copycount = 0;
165
166 inline bool
167 operator==(const copycounter& lhs, const copycounter& rhs)
168 { return lhs.val == rhs.val; }
169
170 inline bool
171 operator<(const copycounter& lhs, const copycounter& rhs)
172 { return lhs.val < rhs.val; }
173
174 inline void
175 swap(copycounter& lhs, copycounter& rhs)
176 {
177 bool test __attribute__((unused)) = true;
178 VERIFY( lhs.valid && rhs.valid );
179 int temp = lhs.val;
180 lhs.val = rhs.val;
181 rhs.val = temp;
182 }
183
184 // In the occasion of libstdc++/48038.
185 struct rvalstruct_compare_by_value
186 {
187 int val;
188 bool ok;
189
190 rvalstruct_compare_by_value(int v)
191 : val(v), ok(true) { }
192
193 rvalstruct_compare_by_value(const rvalstruct_compare_by_value& rh)
194 : val(rh.val), ok(rh.ok)
195 {
196 bool test __attribute__((unused)) = true;
197 VERIFY(rh.ok);
198 }
199
200 rvalstruct_compare_by_value&
201 operator=(const rvalstruct_compare_by_value& rh)
202 {
203 bool test __attribute__((unused)) = true;
204 VERIFY( rh.ok );
205 val = rh.val;
206 ok = rh.ok;
207 return *this;
208 }
209
210 rvalstruct_compare_by_value(rvalstruct_compare_by_value&& rh)
211 : val(rh.val), ok(rh.ok)
212 {
213 bool test __attribute__((unused)) = true;
214 VERIFY( rh.ok );
215 rh.ok = false;
216 }
217
218 rvalstruct_compare_by_value&
219 operator=(rvalstruct_compare_by_value&& rh)
220 {
221 bool test __attribute__((unused)) = true;
222 VERIFY( rh.ok );
223 val = rh.val;
224 ok = rh.ok;
225 rh.ok = false;
226 return *this;
227 }
228 };
229
230 inline bool
231 operator<(rvalstruct_compare_by_value lh,
232 rvalstruct_compare_by_value rh)
233 {
234 bool test __attribute__((unused)) = true;
235 VERIFY( rh.ok );
236 VERIFY( lh.ok );
237 return lh.val < rh.val;
238 }
239
240 inline bool
241 order(rvalstruct_compare_by_value lh,
242 rvalstruct_compare_by_value rh)
243 {
244 bool test __attribute__((unused)) = true;
245 VERIFY( rh.ok );
246 VERIFY( lh.ok );
247 return lh.val < rh.val;
248 }
249
250 struct throwing_move_constructor
251 {
252 throwing_move_constructor() = default;
253
254 throwing_move_constructor(throwing_move_constructor&&)
255 { throw 1; }
256
257 throwing_move_constructor(const throwing_move_constructor&) = default;
258
259 throwing_move_constructor&
260 operator=(const throwing_move_constructor&) = default;
261 };
262
263 } // namespace __gnu_test
264
265 namespace std
266 {
267 /// std::hash specialization for __gnu_test::rvalstruct.
268 template<>
269 struct hash<__gnu_test::rvalstruct>
270 {
271 typedef size_t result_type;
272 typedef __gnu_test::rvalstruct argument_type;
273
274 size_t
275 operator()(const __gnu_test::rvalstruct& __rvs) const
276 { return __rvs.val; }
277 };
278 }
279
280 #endif // _GLIBCXX_TESTSUITE_TR1_H