2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000-2016 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
70 #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
71 # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
73 # define ISO_8859(part,langTERR) ((part) == 15 ?\
74 #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
79 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
82 // Set memory limits if possible, if not set to 0.
83 #ifndef _GLIBCXX_RES_LIMITS
84 # define MEMLIMIT_MB 0
87 # define MEMLIMIT_MB 16.0
91 set_memory_limits(float __size
= MEMLIMIT_MB
);
94 set_file_limit(unsigned long __size
);
96 // Check mangled name demangles (using __cxa_demangle) as expected.
98 verify_demangle(const char* mangled
, const char* wanted
);
100 // Simple callback structure for variable numbers of tests (all with
101 // same signature). Assume all unit tests are of the signature
106 typedef void (*test_type
) (void);
110 test_type _M_tests
[15];
113 operator=(const func_callback
&);
115 func_callback(const func_callback
&);
118 func_callback(): _M_size(0) { }
121 size() const { return _M_size
; }
124 tests() const { return _M_tests
; }
127 push_back(test_type test
)
129 _M_tests
[_M_size
] = test
;
135 // Run select unit tests after setting global locale.
137 run_tests_wrapped_locale(const char*, const func_callback
&);
139 // Run select unit tests after setting environment variables.
141 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
144 struct object_counter
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
; }
156 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
158 // A (static) class for counting copy constructors and possibly throwing an
159 // exception on a desired count.
160 class copy_constructor
164 count() { return count_
; }
170 if (count_
== throw_on_
)
171 std::__throw_runtime_error("copy_constructor::mark_call");
182 throw_on(unsigned int count
) { throw_on_
= count
; }
185 static unsigned int count_
;
186 static unsigned int throw_on_
;
189 // A (static) class for counting assignment operator calls and
190 // possibly throwing an exception on a desired count.
191 class assignment_operator
195 count() { return count_
; }
201 if (count_
== throw_on_
)
202 std::__throw_runtime_error("assignment_operator::mark_call");
213 throw_on(unsigned int count
) { throw_on_
= count
; }
216 static unsigned int count_
;
217 static unsigned int throw_on_
;
220 // A (static) class for tracking calls to an object's destructor.
225 count() { return _M_count
; }
228 mark_call() { _M_count
++; }
231 reset() { _M_count
= 0; }
234 static unsigned int _M_count
;
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.
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
) { }
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_
)
255 copy_constructor::throw_on(copy_constructor::count() + 1);
256 copy_constructor::mark_call();
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.
264 operator=(const copy_tracker
& rhs
)
267 if (rhs
.throw_on_copy_
)
268 assignment_operator::throw_on(assignment_operator::count() + 1);
269 assignment_operator::mark_call();
274 { destructor::mark_call(); }
277 id() const { return id_
; }
282 copy_constructor::reset();
283 assignment_operator::reset();
289 const bool throw_on_copy_
;
294 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
295 { return lhs
.id() == rhs
.id(); }
298 operator<(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
299 { return lhs
.id() < rhs
.id(); }
301 // Class for checking required type conversions, implicit and
302 // explicit for given library data structures.
303 template<typename _Container
>
306 typedef typename
_Container::const_iterator const_iterator
;
308 // Implicit conversion iterator to const_iterator.
309 static const_iterator
310 iterator_to_const_iterator()
313 const_iterator it
= v
.begin();
314 const_iterator end
= v
.end();
315 return it
== end
? v
.end() : it
;
319 // A binary semaphore for use across multiple processes.
323 // Creates a binary semaphore. The semaphore is initially in the
327 // Destroy the semaphore.
330 // Signal the semaphore. If there are processes blocked in
331 // "wait", exactly one will be permitted to proceed.
334 // Wait until the semaphore is signaled.
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
);
347 } // namespace __gnu_test
349 #endif // _GLIBCXX_TESTSUITE_HOOKS_H