2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000-2025 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>
51 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
56 # define _VERIFY_PRINT(S, F, L, P, C) __builtin_fprintf(stderr, S, F, L, P, C)
58 # define _VERIFY_PRINT(S, F, L, P, C) __builtin_printf(S, F, L, P, C)
66 _VERIFY_PRINT("%s:%d: %s: Assertion '%s' failed.\n", \
67 __FILE__, __LINE__, __PRETTY_FUNCTION__, #fn); \
72 #ifdef _GLIBCXX_HAVE_UNISTD_H
78 #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
79 # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
81 # define ISO_8859(part,langTERR) ((part) == 15 ?\
82 #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
85 #if __cplusplus < 201103L
86 # define THROW(X) throw(X)
88 # define THROW(X) noexcept(false)
91 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT || _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
92 // Correct order of thread_local destruction needs __cxa_thread_atexit_impl
93 // or similar support from libc.
94 # define CORRECT_THREAD_LOCAL_DTORS 1
99 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
102 // Set memory limits if possible, if not set to 0.
103 #ifndef _GLIBCXX_RES_LIMITS
104 # define MEMLIMIT_MB 0
107 # define MEMLIMIT_MB 16.0
111 set_memory_limits(float __size
= MEMLIMIT_MB
);
114 set_file_limit(unsigned long __size
);
116 // Check mangled name demangles (using __cxa_demangle) as expected.
118 verify_demangle(const char* mangled
, const char* wanted
);
120 // Simple callback structure for variable numbers of tests (all with
121 // same signature). Assume all unit tests are of the signature
126 typedef void (*test_type
) (void);
130 test_type _M_tests
[15];
133 operator=(const func_callback
&);
135 func_callback(const func_callback
&);
138 func_callback(): _M_size(0) { }
141 size() const { return _M_size
; }
144 tests() const { return _M_tests
; }
147 push_back(test_type test
)
149 _M_tests
[_M_size
] = test
;
155 // Run select unit tests after setting global locale.
157 run_tests_wrapped_locale(const char*, const func_callback
&);
159 // Run select unit tests after setting environment variables.
161 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
164 struct object_counter
166 // Specifically and glaringly-obviously marked 'signed' so that
167 // when COUNT mistakenly goes negative, we can track the patterns
168 // of deletions more easily.
169 typedef signed int size_type
;
170 static size_type count
;
171 object_counter() { ++count
; }
172 object_counter (const object_counter
&) { ++count
; }
173 ~object_counter() { --count
; }
176 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
178 // A (static) class for counting copy constructors and possibly throwing an
179 // exception on a desired count.
180 class copy_constructor
184 count() { return count_
; }
190 if (count_
== throw_on_
)
191 std::__throw_runtime_error("copy_constructor::mark_call");
202 throw_on(unsigned int count
) { throw_on_
= count
; }
205 static unsigned int count_
;
206 static unsigned int throw_on_
;
209 // A (static) class for counting assignment operator calls and
210 // possibly throwing an exception on a desired count.
211 class assignment_operator
215 count() { return count_
; }
221 if (count_
== throw_on_
)
222 std::__throw_runtime_error("assignment_operator::mark_call");
233 throw_on(unsigned int count
) { throw_on_
= count
; }
236 static unsigned int count_
;
237 static unsigned int throw_on_
;
240 // A (static) class for tracking calls to an object's destructor.
245 count() { return _M_count
; }
248 mark_call() { _M_count
++; }
251 reset() { _M_count
= 0; }
254 static unsigned int _M_count
;
257 // A class of objects that can be used for validating various
258 // behaviors and guarantees of containers and algorithms defined in
259 // the standard library.
263 // Creates a copy-tracking object with the given ID number. If
264 // "throw_on_copy" is set, an exception will be thrown if an
265 // attempt is made to copy this object.
266 copy_tracker(int id
= next_id_
--, bool throw_on_copy
= false)
267 : id_(id
) , throw_on_copy_(throw_on_copy
) { }
269 // Copy-constructs the object, marking a call to the copy
270 // constructor and forcing an exception if indicated.
271 copy_tracker(const copy_tracker
& rhs
)
272 : id_(rhs
.id()), throw_on_copy_(rhs
.throw_on_copy_
)
275 copy_constructor::throw_on(copy_constructor::count() + 1);
276 copy_constructor::mark_call();
279 // Assigns the value of another object to this one, tracking the
280 // number of times this member function has been called and if the
281 // other object is supposed to throw an exception when it is
282 // copied, well, make it so.
284 operator=(const copy_tracker
& rhs
)
287 if (rhs
.throw_on_copy_
)
288 assignment_operator::throw_on(assignment_operator::count() + 1);
289 assignment_operator::mark_call();
294 { destructor::mark_call(); }
297 id() const { return id_
; }
302 copy_constructor::reset();
303 assignment_operator::reset();
309 const bool throw_on_copy_
;
314 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
315 { return lhs
.id() == rhs
.id(); }
318 operator<(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
319 { return lhs
.id() < rhs
.id(); }
321 // Class for checking required type conversions, implicit and
322 // explicit for given library data structures.
323 template<typename _Container
>
326 typedef typename
_Container::const_iterator const_iterator
;
328 // Implicit conversion iterator to const_iterator.
330 iterator_to_const_iterator()
333 const_iterator i
__attribute__((unused
)) = const_iterator(v
.begin());
334 const_iterator j
__attribute__((unused
)) = true ? i
: v
.begin();
335 #if __cplusplus >= 201103L
336 const_iterator k
__attribute__((unused
)) { v
.begin() };
341 // A binary semaphore for use across multiple processes.
345 // Creates a binary semaphore. The semaphore is initially in the
349 // Destroy the semaphore.
352 // Signal the semaphore. If there are processes blocked in
353 // "wait", exactly one will be permitted to proceed.
356 // Wait until the semaphore is signaled.
365 // For use in 22_locale/time_get and time_put.
366 std::tm
test_tm(int sec
, int min
, int hour
, int mday
, int mon
,
367 int year
, int wday
, int yday
, int isdst
);
369 } // namespace __gnu_test
371 #endif // _GLIBCXX_TESTSUITE_HOOKS_H