]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_hooks.h
c6f020c34dbca880c541132c4ab255c8870e279b
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.h
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000-2016 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.
11 //
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.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20 //
21
22 // This file provides the following:
23 //
24 // 1) VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
25 // This file is included in the various testsuite programs to provide
26 // #define(able) assert() behavior for debugging/testing. It may be
27 // a suitable location for other furry woodland creatures as well.
28 //
29 // 2) set_memory_limits()
30 // set_memory_limits() uses setrlimit() to restrict dynamic memory
31 // allocation. We provide a default memory limit if none is passed by the
32 // calling application. The argument to set_memory_limits() is the
33 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
34 // not #defined before including this header, then no limiting is attempted.
35 //
36 // 3) object_counter
37 // This is a POD with a static data member, object_counter::count,
38 // which starts at zero, increments on instance construction, and decrements
39 // on instance destruction. "assert_count(n)" can be called to VERIFY()
40 // that the count equals N.
41 //
42 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
43 // A class with nontrivial ctor/dtor that provides the ability to track the
44 // number of copy ctors and dtors, and will throw on demand during copy.
45
46 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
47 #define _GLIBCXX_TESTSUITE_HOOKS_H
48
49 #include <bits/c++config.h>
50 #include <bits/functexcept.h>
51 #include <ctime>
52
53 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
54 #include <sys/stat.h>
55 #endif
56
57 #ifdef _GLIBCXX_ASSERT
58 # include <cassert>
59 # define VERIFY(fn) assert(fn)
60 #else
61 # define VERIFY(fn) test &= bool(fn)
62 #endif
63
64 #ifdef _GLIBCXX_HAVE_UNISTD_H
65 # include <unistd.h>
66 #else
67 # define unlink(x)
68 #endif
69
70 #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
71 # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
72 #else
73 # define ISO_8859(part,langTERR) ((part) == 15 ?\
74 #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
75 #endif
76
77 namespace __gnu_test
78 {
79 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
80 // from c++config.h
81
82 // Set memory limits if possible, if not set to 0.
83 #ifndef _GLIBCXX_RES_LIMITS
84 # define MEMLIMIT_MB 0
85 #else
86 # ifndef MEMLIMIT_MB
87 # define MEMLIMIT_MB 16.0
88 # endif
89 #endif
90 extern void
91 set_memory_limits(float __size = MEMLIMIT_MB);
92
93 extern void
94 set_file_limit(unsigned long __size);
95
96 // Check mangled name demangles (using __cxa_demangle) as expected.
97 void
98 verify_demangle(const char* mangled, const char* wanted);
99
100 // Simple callback structure for variable numbers of tests (all with
101 // same signature). Assume all unit tests are of the signature
102 // void test01();
103 class func_callback
104 {
105 public:
106 typedef void (*test_type) (void);
107
108 private:
109 int _M_size;
110 test_type _M_tests[15];
111
112 func_callback&
113 operator=(const func_callback&);
114
115 func_callback(const func_callback&);
116
117 public:
118 func_callback(): _M_size(0) { }
119
120 int
121 size() const { return _M_size; }
122
123 const test_type*
124 tests() const { return _M_tests; }
125
126 void
127 push_back(test_type test)
128 {
129 _M_tests[_M_size] = test;
130 ++_M_size;
131 }
132 };
133
134
135 // Run select unit tests after setting global locale.
136 void
137 run_tests_wrapped_locale(const char*, const func_callback&);
138
139 // Run select unit tests after setting environment variables.
140 void
141 run_tests_wrapped_env(const char*, const char*, const func_callback&);
142
143 // Counting.
144 struct object_counter
145 {
146 // Specifically and glaringly-obviously marked 'signed' so that
147 // when COUNT mistakenly goes negative, we can track the patterns
148 // of deletions more easily.
149 typedef signed int size_type;
150 static size_type count;
151 object_counter() { ++count; }
152 object_counter (const object_counter&) { ++count; }
153 ~object_counter() { --count; }
154 };
155
156 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
157
158 // A (static) class for counting copy constructors and possibly throwing an
159 // exception on a desired count.
160 class copy_constructor
161 {
162 public:
163 static unsigned int
164 count() { return count_; }
165
166 static void
167 mark_call()
168 {
169 count_++;
170 if (count_ == throw_on_)
171 std::__throw_runtime_error("copy_constructor::mark_call");
172 }
173
174 static void
175 reset()
176 {
177 count_ = 0;
178 throw_on_ = 0;
179 }
180
181 static void
182 throw_on(unsigned int count) { throw_on_ = count; }
183
184 private:
185 static unsigned int count_;
186 static unsigned int throw_on_;
187 };
188
189 // A (static) class for counting assignment operator calls and
190 // possibly throwing an exception on a desired count.
191 class assignment_operator
192 {
193 public:
194 static unsigned int
195 count() { return count_; }
196
197 static void
198 mark_call()
199 {
200 count_++;
201 if (count_ == throw_on_)
202 std::__throw_runtime_error("assignment_operator::mark_call");
203 }
204
205 static void
206 reset()
207 {
208 count_ = 0;
209 throw_on_ = 0;
210 }
211
212 static void
213 throw_on(unsigned int count) { throw_on_ = count; }
214
215 private:
216 static unsigned int count_;
217 static unsigned int throw_on_;
218 };
219
220 // A (static) class for tracking calls to an object's destructor.
221 class destructor
222 {
223 public:
224 static unsigned int
225 count() { return _M_count; }
226
227 static void
228 mark_call() { _M_count++; }
229
230 static void
231 reset() { _M_count = 0; }
232
233 private:
234 static unsigned int _M_count;
235 };
236
237 // An class of objects that can be used for validating various
238 // behaviours and guarantees of containers and algorithms defined in
239 // the standard library.
240 class copy_tracker
241 {
242 public:
243 // Creates a copy-tracking object with the given ID number. If
244 // "throw_on_copy" is set, an exception will be thrown if an
245 // attempt is made to copy this object.
246 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
247 : id_(id) , throw_on_copy_(throw_on_copy) { }
248
249 // Copy-constructs the object, marking a call to the copy
250 // constructor and forcing an exception if indicated.
251 copy_tracker(const copy_tracker& rhs)
252 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
253 {
254 if (throw_on_copy_)
255 copy_constructor::throw_on(copy_constructor::count() + 1);
256 copy_constructor::mark_call();
257 }
258
259 // Assigns the value of another object to this one, tracking the
260 // number of times this member function has been called and if the
261 // other object is supposed to throw an exception when it is
262 // copied, well, make it so.
263 copy_tracker&
264 operator=(const copy_tracker& rhs)
265 {
266 id_ = rhs.id();
267 if (rhs.throw_on_copy_)
268 assignment_operator::throw_on(assignment_operator::count() + 1);
269 assignment_operator::mark_call();
270 return *this;
271 }
272
273 ~copy_tracker()
274 { destructor::mark_call(); }
275
276 int
277 id() const { return id_; }
278
279 static void
280 reset()
281 {
282 copy_constructor::reset();
283 assignment_operator::reset();
284 destructor::reset();
285 }
286
287 private:
288 int id_;
289 const bool throw_on_copy_;
290 static int next_id_;
291 };
292
293 inline bool
294 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
295 { return lhs.id() == rhs.id(); }
296
297 inline bool
298 operator<(const copy_tracker& lhs, const copy_tracker& rhs)
299 { return lhs.id() < rhs.id(); }
300
301 // Class for checking required type conversions, implicit and
302 // explicit for given library data structures.
303 template<typename _Container>
304 struct conversion
305 {
306 typedef typename _Container::const_iterator const_iterator;
307
308 // Implicit conversion iterator to const_iterator.
309 static const_iterator
310 iterator_to_const_iterator()
311 {
312 _Container v;
313 const_iterator it = v.begin();
314 const_iterator end = v.end();
315 return it == end ? v.end() : it;
316 }
317 };
318
319 // A binary semaphore for use across multiple processes.
320 class semaphore
321 {
322 public:
323 // Creates a binary semaphore. The semaphore is initially in the
324 // unsignaled state.
325 semaphore();
326
327 // Destroy the semaphore.
328 ~semaphore();
329
330 // Signal the semaphore. If there are processes blocked in
331 // "wait", exactly one will be permitted to proceed.
332 void signal();
333
334 // Wait until the semaphore is signaled.
335 void wait();
336
337 private:
338 int sem_set_;
339
340 pid_t pid_;
341 };
342
343 // For use in 22_locale/time_get and time_put.
344 std::tm test_tm(int sec, int min, int hour, int mday, int mon,
345 int year, int wday, int yday, int isdst);
346
347 } // namespace __gnu_test
348
349 #endif // _GLIBCXX_TESTSUITE_HOOKS_H
350