2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000-2018 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:
26 // 2) set_memory_limits()
27 // set_memory_limits() uses setrlimit() to restrict dynamic memory
28 // allocation. We provide a default memory limit if none is passed by the
29 // calling application. The argument to set_memory_limits() is the
30 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
31 // not #defined before including this header, then no limiting is attempted.
34 // This is a POD with a static data member, object_counter::count,
35 // which starts at zero, increments on instance construction, and decrements
36 // on instance destruction. "assert_count(n)" can be called to VERIFY()
37 // that the count equals N.
39 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
40 // A class with nontrivial ctor/dtor that provides the ability to track the
41 // number of copy ctors and dtors, and will throw on demand during copy.
43 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
44 #define _GLIBCXX_TESTSUITE_HOOKS_H
46 #include <bits/c++config.h>
47 #include <bits/functexcept.h>
50 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
59 __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", \
60 __FILE__, __LINE__, __PRETTY_FUNCTION__, #fn); \
65 #ifdef _GLIBCXX_HAVE_UNISTD_H
71 #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
72 # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
74 # define ISO_8859(part,langTERR) ((part) == 15 ?\
75 #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
78 #if __cplusplus < 201103L
79 # define THROW(X) throw(X)
81 # define THROW(X) noexcept(false)
84 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT || _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
85 // Correct order of thread_local destruction needs __cxa_thread_atexit_impl
86 // or similar support from libc.
87 # define CORRECT_THREAD_LOCAL_DTORS 1
92 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
95 // Set memory limits if possible, if not set to 0.
96 #ifndef _GLIBCXX_RES_LIMITS
97 # define MEMLIMIT_MB 0
100 # define MEMLIMIT_MB 16.0
104 set_memory_limits(float __size
= MEMLIMIT_MB
);
107 set_file_limit(unsigned long __size
);
109 // Check mangled name demangles (using __cxa_demangle) as expected.
111 verify_demangle(const char* mangled
, const char* wanted
);
113 // Simple callback structure for variable numbers of tests (all with
114 // same signature). Assume all unit tests are of the signature
119 typedef void (*test_type
) (void);
123 test_type _M_tests
[15];
126 operator=(const func_callback
&);
128 func_callback(const func_callback
&);
131 func_callback(): _M_size(0) { }
134 size() const { return _M_size
; }
137 tests() const { return _M_tests
; }
140 push_back(test_type test
)
142 _M_tests
[_M_size
] = test
;
148 // Run select unit tests after setting global locale.
150 run_tests_wrapped_locale(const char*, const func_callback
&);
152 // Run select unit tests after setting environment variables.
154 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
157 struct object_counter
159 // Specifically and glaringly-obviously marked 'signed' so that
160 // when COUNT mistakenly goes negative, we can track the patterns
161 // of deletions more easily.
162 typedef signed int size_type
;
163 static size_type count
;
164 object_counter() { ++count
; }
165 object_counter (const object_counter
&) { ++count
; }
166 ~object_counter() { --count
; }
169 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
171 // A (static) class for counting copy constructors and possibly throwing an
172 // exception on a desired count.
173 class copy_constructor
177 count() { return count_
; }
183 if (count_
== throw_on_
)
184 std::__throw_runtime_error("copy_constructor::mark_call");
195 throw_on(unsigned int count
) { throw_on_
= count
; }
198 static unsigned int count_
;
199 static unsigned int throw_on_
;
202 // A (static) class for counting assignment operator calls and
203 // possibly throwing an exception on a desired count.
204 class assignment_operator
208 count() { return count_
; }
214 if (count_
== throw_on_
)
215 std::__throw_runtime_error("assignment_operator::mark_call");
226 throw_on(unsigned int count
) { throw_on_
= count
; }
229 static unsigned int count_
;
230 static unsigned int throw_on_
;
233 // A (static) class for tracking calls to an object's destructor.
238 count() { return _M_count
; }
241 mark_call() { _M_count
++; }
244 reset() { _M_count
= 0; }
247 static unsigned int _M_count
;
250 // A class of objects that can be used for validating various
251 // behaviors and guarantees of containers and algorithms defined in
252 // the standard library.
256 // Creates a copy-tracking object with the given ID number. If
257 // "throw_on_copy" is set, an exception will be thrown if an
258 // attempt is made to copy this object.
259 copy_tracker(int id
= next_id_
--, bool throw_on_copy
= false)
260 : id_(id
) , throw_on_copy_(throw_on_copy
) { }
262 // Copy-constructs the object, marking a call to the copy
263 // constructor and forcing an exception if indicated.
264 copy_tracker(const copy_tracker
& rhs
)
265 : id_(rhs
.id()), throw_on_copy_(rhs
.throw_on_copy_
)
268 copy_constructor::throw_on(copy_constructor::count() + 1);
269 copy_constructor::mark_call();
272 // Assigns the value of another object to this one, tracking the
273 // number of times this member function has been called and if the
274 // other object is supposed to throw an exception when it is
275 // copied, well, make it so.
277 operator=(const copy_tracker
& rhs
)
280 if (rhs
.throw_on_copy_
)
281 assignment_operator::throw_on(assignment_operator::count() + 1);
282 assignment_operator::mark_call();
287 { destructor::mark_call(); }
290 id() const { return id_
; }
295 copy_constructor::reset();
296 assignment_operator::reset();
302 const bool throw_on_copy_
;
307 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
308 { return lhs
.id() == rhs
.id(); }
311 operator<(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
312 { return lhs
.id() < rhs
.id(); }
314 // Class for checking required type conversions, implicit and
315 // explicit for given library data structures.
316 template<typename _Container
>
319 typedef typename
_Container::const_iterator const_iterator
;
321 // Implicit conversion iterator to const_iterator.
322 static const_iterator
323 iterator_to_const_iterator()
326 const_iterator it
= v
.begin();
327 const_iterator end
= v
.end();
328 return it
== end
? v
.end() : it
;
332 // A binary semaphore for use across multiple processes.
336 // Creates a binary semaphore. The semaphore is initially in the
340 // Destroy the semaphore.
343 // Signal the semaphore. If there are processes blocked in
344 // "wait", exactly one will be permitted to proceed.
347 // Wait until the semaphore is signaled.
356 // For use in 22_locale/time_get and time_put.
357 std::tm
test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
358 int year
, int wday
, int yday
, int isdst
);
360 } // namespace __gnu_test
362 #endif // _GLIBCXX_TESTSUITE_HOOKS_H