2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
23 // This file provides the following:
25 // 1) VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
26 // This file is included in the various testsuite programs to provide
27 // #define(able) assert() behavior for debugging/testing. It may be
28 // a suitable location for other furry woodland creatures as well.
30 // 2) set_memory_limits()
31 // set_memory_limits() uses setrlimit() to restrict dynamic memory
32 // allocation. We provide a default memory limit if none is passed by the
33 // calling application. The argument to set_memory_limits() is the
34 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
35 // not #defined before including this header, then no limiting is attempted.
38 // This is a POD with a static data member, object_counter::count,
39 // which starts at zero, increments on instance construction, and decrements
40 // on instance destruction. "assert_count(n)" can be called to VERIFY()
41 // that the count equals N.
43 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
44 // A class with nontrivial ctor/dtor that provides the ability to track the
45 // number of copy ctors and dtors, and will throw on demand during copy.
47 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
48 #define _GLIBCXX_TESTSUITE_HOOKS_H
50 #include <bits/c++config.h>
51 #include <bits/functexcept.h>
54 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
58 #ifdef _GLIBCXX_ASSERT
60 # define VERIFY(fn) assert(fn)
62 # define VERIFY(fn) test &= (fn)
65 #ifdef _GLIBCXX_HAVE_UNISTD_H
73 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
76 // Set memory limits if possible, if not set to 0.
77 #ifndef _GLIBCXX_RES_LIMITS
78 # define MEMLIMIT_MB 0
81 # define MEMLIMIT_MB 16.0
85 set_memory_limits(float __size = MEMLIMIT_MB);
88 set_file_limit(unsigned long __size);
90 // Check mangled name demangles (using __cxa_demangle) as expected.
92 verify_demangle(const char* mangled, const char* wanted);
94 // Simple callback structure for variable numbers of tests (all with
95 // same signature). Assume all unit tests are of the signature
100 typedef void (*test_type) (void);
104 test_type _M_tests[15];
107 operator=(const func_callback&);
109 func_callback(const func_callback&);
112 func_callback(): _M_size(0) { }
115 size() const { return _M_size; }
118 tests() const { return _M_tests; }
121 push_back(test_type test)
123 _M_tests[_M_size] = test;
129 // Run select unit tests after setting global locale.
131 run_tests_wrapped_locale(const char*, const func_callback&);
133 // Run select unit tests after setting environment variables.
135 run_tests_wrapped_env(const char*, const char*, const func_callback&);
138 struct object_counter
140 // Specifically and glaringly-obviously marked 'signed' so that
141 // when COUNT mistakenly goes negative, we can track the patterns
142 // of deletions more easily.
143 typedef signed int size_type;
144 static size_type count;
145 object_counter() { ++count; }
146 object_counter (const object_counter&) { ++count; }
147 ~object_counter() { --count; }
150 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
152 // A (static) class for counting copy constructors and possibly throwing an
153 // exception on a desired count.
154 class copy_constructor
158 count() { return count_; }
164 if (count_ == throw_on_)
165 std::__throw_runtime_error("copy_constructor::mark_call");
176 throw_on(unsigned int count) { throw_on_ = count; }
179 static unsigned int count_;
180 static unsigned int throw_on_;
183 // A (static) class for counting assignment operator calls and
184 // possibly throwing an exception on a desired count.
185 class assignment_operator
189 count() { return count_; }
195 if (count_ == throw_on_)
196 std::__throw_runtime_error("assignment_operator::mark_call");
207 throw_on(unsigned int count) { throw_on_ = count; }
210 static unsigned int count_;
211 static unsigned int throw_on_;
214 // A (static) class for tracking calls to an object's destructor.
219 count() { return _M_count; }
222 mark_call() { _M_count++; }
225 reset() { _M_count = 0; }
228 static unsigned int _M_count;
231 // An class of objects that can be used for validating various
232 // behaviours and guarantees of containers and algorithms defined in
233 // the standard library.
237 // Creates a copy-tracking object with the given ID number. If
238 // "throw_on_copy" is set, an exception will be thrown if an
239 // attempt is made to copy this object.
240 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
241 : id_(id) , throw_on_copy_(throw_on_copy) { }
243 // Copy-constructs the object, marking a call to the copy
244 // constructor and forcing an exception if indicated.
245 copy_tracker(const copy_tracker& rhs)
246 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
249 copy_constructor::throw_on(copy_constructor::count() + 1);
250 copy_constructor::mark_call();
253 // Assigns the value of another object to this one, tracking the
254 // number of times this member function has been called and if the
255 // other object is supposed to throw an exception when it is
256 // copied, well, make it so.
258 operator=(const copy_tracker& rhs)
261 if (rhs.throw_on_copy_)
262 assignment_operator::throw_on(assignment_operator::count() + 1);
263 assignment_operator::mark_call();
268 { destructor::mark_call(); }
271 id() const { return id_; }
276 copy_constructor::reset();
277 assignment_operator::reset();
283 const bool throw_on_copy_;
288 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
289 { return lhs.id() == rhs.id(); }
292 operator<(const copy_tracker& lhs, const copy_tracker& rhs)
293 { return lhs.id() < rhs.id(); }
295 // Class for checking required type conversions, implicit and
296 // explicit for given library data structures.
297 template<typename _Container>
300 typedef typename _Container::const_iterator const_iterator;
302 // Implicit conversion iterator to const_iterator.
303 static const_iterator
304 iterator_to_const_iterator()
307 const_iterator it = v.begin();
308 const_iterator end = v.end();
309 return it == end ? v.end() : it;
313 // A binary semaphore for use across multiple processes.
317 // Creates a binary semaphore. The semaphore is initially in the
321 // Destroy the semaphore.
324 // Signal the semaphore. If there are processes blocked in
325 // "wait", exactly one will be permitted to proceed.
328 // Wait until the semaphore is signaled.
337 // For use in 22_locale/time_get and time_put.
338 std::tm test_tm(int sec, int min, int hour, int mday, int mon,
339 int year, int wday, int yday, int isdst);
341 } // namespace __gnu_test
343 #endif // _GLIBCXX_TESTSUITE_HOOKS_H