2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000-2014 Free Software Foundation, Inc.
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)
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 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/>.
22 // This file provides the following:
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.
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.
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.
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.
46 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
47 #define _GLIBCXX_TESTSUITE_HOOKS_H
49 #include <bits/c++config.h>
50 #include <bits/functexcept.h>
53 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
57 #ifdef _GLIBCXX_ASSERT
59 # define VERIFY(fn) assert(fn)
61 # define VERIFY(fn) test &= bool(fn)
64 #ifdef _GLIBCXX_HAVE_UNISTD_H
72 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
75 // Set memory limits if possible, if not set to 0.
76 #ifndef _GLIBCXX_RES_LIMITS
77 # define MEMLIMIT_MB 0
80 # define MEMLIMIT_MB 16.0
84 set_memory_limits(float __size
= MEMLIMIT_MB
);
87 set_file_limit(unsigned long __size
);
89 // Check mangled name demangles (using __cxa_demangle) as expected.
91 verify_demangle(const char* mangled
, const char* wanted
);
93 // Simple callback structure for variable numbers of tests (all with
94 // same signature). Assume all unit tests are of the signature
99 typedef void (*test_type
) (void);
103 test_type _M_tests
[15];
106 operator=(const func_callback
&);
108 func_callback(const func_callback
&);
111 func_callback(): _M_size(0) { }
114 size() const { return _M_size
; }
117 tests() const { return _M_tests
; }
120 push_back(test_type test
)
122 _M_tests
[_M_size
] = test
;
128 // Run select unit tests after setting global locale.
130 run_tests_wrapped_locale(const char*, const func_callback
&);
132 // Run select unit tests after setting environment variables.
134 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
137 struct object_counter
139 // Specifically and glaringly-obviously marked 'signed' so that
140 // when COUNT mistakenly goes negative, we can track the patterns
141 // of deletions more easily.
142 typedef signed int size_type
;
143 static size_type count
;
144 object_counter() { ++count
; }
145 object_counter (const object_counter
&) { ++count
; }
146 ~object_counter() { --count
; }
149 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
151 // A (static) class for counting copy constructors and possibly throwing an
152 // exception on a desired count.
153 class copy_constructor
157 count() { return count_
; }
163 if (count_
== throw_on_
)
164 std::__throw_runtime_error("copy_constructor::mark_call");
175 throw_on(unsigned int count
) { throw_on_
= count
; }
178 static unsigned int count_
;
179 static unsigned int throw_on_
;
182 // A (static) class for counting assignment operator calls and
183 // possibly throwing an exception on a desired count.
184 class assignment_operator
188 count() { return count_
; }
194 if (count_
== throw_on_
)
195 std::__throw_runtime_error("assignment_operator::mark_call");
206 throw_on(unsigned int count
) { throw_on_
= count
; }
209 static unsigned int count_
;
210 static unsigned int throw_on_
;
213 // A (static) class for tracking calls to an object's destructor.
218 count() { return _M_count
; }
221 mark_call() { _M_count
++; }
224 reset() { _M_count
= 0; }
227 static unsigned int _M_count
;
230 // An class of objects that can be used for validating various
231 // behaviours and guarantees of containers and algorithms defined in
232 // the standard library.
236 // Creates a copy-tracking object with the given ID number. If
237 // "throw_on_copy" is set, an exception will be thrown if an
238 // attempt is made to copy this object.
239 copy_tracker(int id
= next_id_
--, bool throw_on_copy
= false)
240 : id_(id
) , throw_on_copy_(throw_on_copy
) { }
242 // Copy-constructs the object, marking a call to the copy
243 // constructor and forcing an exception if indicated.
244 copy_tracker(const copy_tracker
& rhs
)
245 : id_(rhs
.id()), throw_on_copy_(rhs
.throw_on_copy_
)
248 copy_constructor::throw_on(copy_constructor::count() + 1);
249 copy_constructor::mark_call();
252 // Assigns the value of another object to this one, tracking the
253 // number of times this member function has been called and if the
254 // other object is supposed to throw an exception when it is
255 // copied, well, make it so.
257 operator=(const copy_tracker
& rhs
)
260 if (rhs
.throw_on_copy_
)
261 assignment_operator::throw_on(assignment_operator::count() + 1);
262 assignment_operator::mark_call();
267 { destructor::mark_call(); }
270 id() const { return id_
; }
275 copy_constructor::reset();
276 assignment_operator::reset();
282 const bool throw_on_copy_
;
287 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
288 { return lhs
.id() == rhs
.id(); }
291 operator<(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
292 { return lhs
.id() < rhs
.id(); }
294 // Class for checking required type conversions, implicit and
295 // explicit for given library data structures.
296 template<typename _Container
>
299 typedef typename
_Container::const_iterator const_iterator
;
301 // Implicit conversion iterator to const_iterator.
302 static const_iterator
303 iterator_to_const_iterator()
306 const_iterator it
= v
.begin();
307 const_iterator end
= v
.end();
308 return it
== end
? v
.end() : it
;
312 // A binary semaphore for use across multiple processes.
316 // Creates a binary semaphore. The semaphore is initially in the
320 // Destroy the semaphore.
323 // Signal the semaphore. If there are processes blocked in
324 // "wait", exactly one will be permitted to proceed.
327 // Wait until the semaphore is signaled.
336 // For use in 22_locale/time_get and time_put.
337 std::tm
test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
338 int year
, int wday
, int yday
, int isdst
);
340 } // namespace __gnu_test
342 #endif // _GLIBCXX_TESTSUITE_HOOKS_H