]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_rvalref.h
i386.c (ix86_trampoline_init): Switch arms of if expr.
[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( in.valid == true );
72 val = in.val;
73 in.valid = false;
74 valid = true;
75 return *this;
76 }
77 };
78
79 inline bool
80 operator==(const rvalstruct& lhs, const rvalstruct& rhs)
81 { return lhs.val == rhs.val; }
82
83 inline bool
84 operator<(const rvalstruct& lhs, const rvalstruct& rhs)
85 { return lhs.val < rhs.val; }
86
87 void
88 swap(rvalstruct& lhs, rvalstruct& rhs)
89 {
90 bool test __attribute__((unused)) = true;
91 VERIFY( lhs.valid && rhs.valid );
92 int temp = lhs.val;
93 lhs.val = rhs.val;
94 rhs.val = temp;
95 }
96
97 // This is a moveable class which copies how many times it is copied.
98 // This is mainly of use in the containers, where the an element inserted
99 // into a container has to be copied once to get there, but we want to check
100 // nothing else is copied.
101 struct copycounter
102 {
103 static int copycount;
104 int val;
105 bool valid;
106
107 copycounter() : val(0), valid(true)
108 { }
109
110 copycounter(int inval) : val(inval), valid(true)
111 { }
112
113 copycounter(const copycounter& in) : val(in.val), valid(true)
114 {
115 bool test __attribute__((unused)) = true;
116 VERIFY( in.valid == true );
117 ++copycount;
118 }
119
120 copycounter(copycounter&& in) noexcept
121 {
122 bool test __attribute__((unused)) = true;
123 VERIFY( in.valid == true );
124 val = in.val;
125 in.valid = false;
126 valid = true;
127 }
128
129 copycounter&
130 operator=(int newval)
131 {
132 val = newval;
133 valid = true;
134 return *this;
135 }
136
137 bool
138 operator=(const copycounter& in)
139 {
140 bool test __attribute__((unused)) = true;
141 VERIFY( in.valid == true );
142 ++copycount;
143 val = in.val;
144 valid = true;
145 return true;
146 }
147
148 copycounter&
149 operator=(copycounter&& in)
150 {
151 bool test __attribute__((unused)) = true;
152 VERIFY(in.valid == true);
153 val = in.val;
154 in.valid = false;
155 valid = true;
156 return *this;
157 }
158
159 ~copycounter() noexcept
160 { valid = false; }
161 };
162
163 int copycounter::copycount = 0;
164
165 inline bool
166 operator==(const copycounter& lhs, const copycounter& rhs)
167 { return lhs.val == rhs.val; }
168
169 inline bool
170 operator<(const copycounter& lhs, const copycounter& rhs)
171 { return lhs.val < rhs.val; }
172
173 inline void
174 swap(copycounter& lhs, copycounter& rhs)
175 {
176 bool test __attribute__((unused)) = true;
177 VERIFY( lhs.valid && rhs.valid );
178 int temp = lhs.val;
179 lhs.val = rhs.val;
180 rhs.val = temp;
181 }
182
183 // In the occasion of libstdc++/48038.
184 struct rvalstruct_compare_by_value
185 {
186 int val;
187 bool ok;
188
189 rvalstruct_compare_by_value(int v)
190 : val(v), ok(true) { }
191
192 rvalstruct_compare_by_value(const rvalstruct_compare_by_value& rh)
193 : val(rh.val), ok(rh.ok)
194 {
195 bool test __attribute__((unused)) = true;
196 VERIFY(rh.ok);
197 }
198
199 rvalstruct_compare_by_value&
200 operator=(const rvalstruct_compare_by_value& rh)
201 {
202 bool test __attribute__((unused)) = true;
203 VERIFY( rh.ok );
204 val = rh.val;
205 ok = rh.ok;
206 return *this;
207 }
208
209 rvalstruct_compare_by_value(rvalstruct_compare_by_value&& rh)
210 : val(rh.val), ok(rh.ok)
211 {
212 bool test __attribute__((unused)) = true;
213 VERIFY( rh.ok );
214 rh.ok = false;
215 }
216
217 rvalstruct_compare_by_value&
218 operator=(rvalstruct_compare_by_value&& rh)
219 {
220 bool test __attribute__((unused)) = true;
221 VERIFY( rh.ok );
222 val = rh.val;
223 ok = rh.ok;
224 rh.ok = false;
225 return *this;
226 }
227 };
228
229 inline bool
230 operator<(rvalstruct_compare_by_value lh,
231 rvalstruct_compare_by_value rh)
232 {
233 bool test __attribute__((unused)) = true;
234 VERIFY( rh.ok );
235 VERIFY( lh.ok );
236 return lh.val < rh.val;
237 }
238
239 inline bool
240 order(rvalstruct_compare_by_value lh,
241 rvalstruct_compare_by_value rh)
242 {
243 bool test __attribute__((unused)) = true;
244 VERIFY( rh.ok );
245 VERIFY( lh.ok );
246 return lh.val < rh.val;
247 }
248
249 struct throwing_move_constructor
250 {
251 throwing_move_constructor() = default;
252
253 throwing_move_constructor(throwing_move_constructor&&)
254 { throw 1; }
255
256 throwing_move_constructor(const throwing_move_constructor&) = default;
257 };
258
259 } // namespace __gnu_test
260
261 namespace std
262 {
263 /// std::hash specialization for __gnu_test::rvalstruct.
264 template<>
265 struct hash<__gnu_test::rvalstruct>
266 {
267 typedef size_t result_type;
268 typedef __gnu_test::rvalstruct argument_type;
269
270 size_t
271 operator()(const __gnu_test::rvalstruct& __rvs) const
272 { return __rvs.val; }
273 };
274 }
275
276 #endif // _GLIBCXX_TESTSUITE_TR1_H