2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
6 // Free Software Foundation, Inc.
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
24 // This file provides the following:
26 // 1) VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
27 // This file is included in the various testsuite programs to provide
28 // #define(able) assert() behavior for debugging/testing. It may be
29 // a suitable location for other furry woodland creatures as well.
31 // 2) set_memory_limits()
32 // set_memory_limits() uses setrlimit() to restrict dynamic memory
33 // allocation. We provide a default memory limit if none is passed by the
34 // calling application. The argument to set_memory_limits() is the
35 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
36 // not #defined before including this header, then no limiting is attempted.
39 // This is a POD with a static data member, object_counter::count,
40 // which starts at zero, increments on instance construction, and decrements
41 // on instance destruction. "assert_count(n)" can be called to VERIFY()
42 // that the count equals N.
44 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
45 // A class with nontrivial ctor/dtor that provides the ability to track the
46 // number of copy ctors and dtors, and will throw on demand during copy.
48 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
49 #define _GLIBCXX_TESTSUITE_HOOKS_H
51 #include <bits/c++config.h>
52 #include <bits/functexcept.h>
55 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
59 #ifdef _GLIBCXX_ASSERT
61 # define VERIFY(fn) assert(fn)
63 # define VERIFY(fn) test &= bool(fn)
66 #ifdef _GLIBCXX_HAVE_UNISTD_H
74 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
77 // Set memory limits if possible, if not set to 0.
78 #ifndef _GLIBCXX_RES_LIMITS
79 # define MEMLIMIT_MB 0
82 # define MEMLIMIT_MB 16.0
86 set_memory_limits(float __size
= MEMLIMIT_MB
);
89 set_file_limit(unsigned long __size
);
91 // Check mangled name demangles (using __cxa_demangle) as expected.
93 verify_demangle(const char* mangled
, const char* wanted
);
95 // Simple callback structure for variable numbers of tests (all with
96 // same signature). Assume all unit tests are of the signature
101 typedef void (*test_type
) (void);
105 test_type _M_tests
[15];
108 operator=(const func_callback
&);
110 func_callback(const func_callback
&);
113 func_callback(): _M_size(0) { }
116 size() const { return _M_size
; }
119 tests() const { return _M_tests
; }
122 push_back(test_type test
)
124 _M_tests
[_M_size
] = test
;
130 // Run select unit tests after setting global locale.
132 run_tests_wrapped_locale(const char*, const func_callback
&);
134 // Run select unit tests after setting environment variables.
136 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
139 struct object_counter
141 // Specifically and glaringly-obviously marked 'signed' so that
142 // when COUNT mistakenly goes negative, we can track the patterns
143 // of deletions more easily.
144 typedef signed int size_type
;
145 static size_type count
;
146 object_counter() { ++count
; }
147 object_counter (const object_counter
&) { ++count
; }
148 ~object_counter() { --count
; }
151 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
153 // A (static) class for counting copy constructors and possibly throwing an
154 // exception on a desired count.
155 class copy_constructor
159 count() { return count_
; }
165 if (count_
== throw_on_
)
166 std::__throw_runtime_error("copy_constructor::mark_call");
177 throw_on(unsigned int count
) { throw_on_
= count
; }
180 static unsigned int count_
;
181 static unsigned int throw_on_
;
184 // A (static) class for counting assignment operator calls and
185 // possibly throwing an exception on a desired count.
186 class assignment_operator
190 count() { return count_
; }
196 if (count_
== throw_on_
)
197 std::__throw_runtime_error("assignment_operator::mark_call");
208 throw_on(unsigned int count
) { throw_on_
= count
; }
211 static unsigned int count_
;
212 static unsigned int throw_on_
;
215 // A (static) class for tracking calls to an object's destructor.
220 count() { return _M_count
; }
223 mark_call() { _M_count
++; }
226 reset() { _M_count
= 0; }
229 static unsigned int _M_count
;
232 // An class of objects that can be used for validating various
233 // behaviours and guarantees of containers and algorithms defined in
234 // the standard library.
238 // Creates a copy-tracking object with the given ID number. If
239 // "throw_on_copy" is set, an exception will be thrown if an
240 // attempt is made to copy this object.
241 copy_tracker(int id
= next_id_
--, bool throw_on_copy
= false)
242 : id_(id
) , throw_on_copy_(throw_on_copy
) { }
244 // Copy-constructs the object, marking a call to the copy
245 // constructor and forcing an exception if indicated.
246 copy_tracker(const copy_tracker
& rhs
)
247 : id_(rhs
.id()), throw_on_copy_(rhs
.throw_on_copy_
)
250 copy_constructor::throw_on(copy_constructor::count() + 1);
251 copy_constructor::mark_call();
254 // Assigns the value of another object to this one, tracking the
255 // number of times this member function has been called and if the
256 // other object is supposed to throw an exception when it is
257 // copied, well, make it so.
259 operator=(const copy_tracker
& rhs
)
262 if (rhs
.throw_on_copy_
)
263 assignment_operator::throw_on(assignment_operator::count() + 1);
264 assignment_operator::mark_call();
269 { destructor::mark_call(); }
272 id() const { return id_
; }
277 copy_constructor::reset();
278 assignment_operator::reset();
284 const bool throw_on_copy_
;
289 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
290 { return lhs
.id() == rhs
.id(); }
293 operator<(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
294 { return lhs
.id() < rhs
.id(); }
296 // Class for checking required type conversions, implicit and
297 // explicit for given library data structures.
298 template<typename _Container
>
301 typedef typename
_Container::const_iterator const_iterator
;
303 // Implicit conversion iterator to const_iterator.
304 static const_iterator
305 iterator_to_const_iterator()
308 const_iterator it
= v
.begin();
309 const_iterator end
= v
.end();
310 return it
== end
? v
.end() : it
;
314 // A binary semaphore for use across multiple processes.
318 // Creates a binary semaphore. The semaphore is initially in the
322 // Destroy the semaphore.
325 // Signal the semaphore. If there are processes blocked in
326 // "wait", exactly one will be permitted to proceed.
329 // Wait until the semaphore is signaled.
338 // For use in 22_locale/time_get and time_put.
339 std::tm
test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
340 int year
, int wday
, int yday
, int isdst
);
342 } // namespace __gnu_test
344 #endif // _GLIBCXX_TESTSUITE_HOOKS_H