2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000, 2001, 2002, 2003 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 2, 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 COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // This file provides the following:
33 // 1) VERIFY(), via DEBUG_ASSERT, from Brent Verner <brent@rcfile.org>.
34 // This file is included in the various testsuite programs to provide
35 // #define(able) assert() behavior for debugging/testing. It may be
36 // a suitable location for other furry woodland creatures as well.
38 // 2) set_memory_limits()
39 // set_memory_limits() uses setrlimit() to restrict dynamic memory
40 // allocation. We provide a default memory limit if none is passed by the
41 // calling application. The argument to set_memory_limits() is the
42 // limit in megabytes (a floating-point number). If _GLIBCPP_MEM_LIMITS is
43 // not #defined before including this header, then no limiting is attempted.
46 // This is a POD with a static data member, gnu_counting_struct::count,
47 // which starts at zero, increments on instance construction, and decrements
48 // on instance destruction. "assert_count(n)" can be called to VERIFY()
49 // that the count equals N.
51 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
52 // A class with nontrivial ctor/dtor that provides the ability to track the
53 // number of copy ctors and dtors, and will throw on demand during copy.
55 // 5) pod_char, pod_int, , abstract character classes and
56 // char_traits specializations for testing instantiations.
58 #ifndef _GLIBCPP_TESTSUITE_HOOKS_H
59 #define _GLIBCPP_TESTSUITE_HOOKS_H
61 #include <bits/c++config.h>
62 #include <bits/functexcept.h>
66 # define VERIFY(fn) assert(fn)
68 # define VERIFY(fn) test &= (fn)
73 namespace __gnu_cxx_test
75 // All macros are defined in GLIBCPP_CONFIGURE_TESTSUITE and imported
78 // Set memory limits if possible, if not set to 0.
79 #ifndef _GLIBCPP_MEM_LIMITS
80 # define MEMLIMIT_MB 0
83 # define MEMLIMIT_MB 16.0
87 set_memory_limits(float __size = MEMLIMIT_MB);
90 // Check mangled name demangles (using __cxa_demangle) as expected.
92 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
98 typedef void (*test_func) (void);
99 typedef std::list<test_func> func_callback;
101 // Run select unit tests after setting global locale.
103 run_tests_wrapped_locale(const char*, const func_callback&);
105 // Run select unit tests after setting environment variables.
107 run_tests_wrapped_env(const char*, const char*, const func_callback&);
109 // Try to create a locale with the given name. If it fails, bail.
111 try_named_locale(const char* name);
124 struct pod_unsigned_int
143 // Specifically and glaringly-obviously marked 'signed' so that when
144 // COUNT mistakenly goes negative, we can track the patterns of
145 // deletions more easily.
146 typedef signed int size_type;
147 static size_type count;
148 counter() { ++count; }
149 counter (const counter&) { ++count; }
150 ~counter() { --count; }
153 #define assert_count(n) VERIFY(__gnu_cxx_test::counter::count == n)
155 // A (static) class for counting copy constructors and possibly throwing an
156 // exception on a desired count.
157 class copy_constructor
161 count() { return count_; }
167 if (count_ == throw_on_)
168 __throw_exception_again "copy constructor exception";
179 throw_on(unsigned int count) { throw_on_ = count; }
182 static unsigned int count_;
183 static unsigned int throw_on_;
186 // A (static) class for counting assignment operator calls and
187 // possibly throwing an exception on a desired count.
188 class assignment_operator
192 count() { return count_; }
198 if (count_ == throw_on_)
199 __throw_exception_again "assignment operator exception";
210 throw_on(unsigned int count) { throw_on_ = count; }
213 static unsigned int count_;
214 static unsigned int throw_on_;
217 // A (static) class for tracking calls to an object's destructor.
222 count() { return _M_count; }
225 mark_call() { _M_count++; }
228 reset() { _M_count = 0; }
231 static unsigned int _M_count;
234 // An class of objects that can be used for validating various
235 // behaviours and guarantees of containers and algorithms defined in
236 // the standard library.
240 // Creates a copy-tracking object with the given ID number. If
241 // "throw_on_copy" is set, an exception will be thrown if an
242 // attempt is made to copy this object.
243 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
244 : id_(id) , throw_on_copy_(throw_on_copy) { }
246 // Copy-constructs the object, marking a call to the copy
247 // constructor and forcing an exception if indicated.
248 copy_tracker(const copy_tracker& rhs)
249 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
252 copy_constructor::throw_on(copy_constructor::count() + 1);
253 copy_constructor::mark_call();
256 // Assigns the value of another object to this one, tracking the
257 // number of times this member function has been called and if the
258 // other object is supposed to throw an exception when it is
259 // copied, well, make it so.
261 operator=(const copy_tracker& rhs)
264 if (rhs.throw_on_copy_)
265 assignment_operator::throw_on(assignment_operator::count() + 1);
266 assignment_operator::mark_call();
271 { destructor::mark_call(); }
274 id() const { return id_; }
278 const bool throw_on_copy_;
284 copy_constructor::reset();
285 assignment_operator::reset();
289 // for backwards-compatibility
292 { return copy_constructor::count(); }
294 // for backwards-compatibility
297 { return destructor::count(); }
304 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
305 { return lhs.id() == rhs.id(); }
306 } // namespace __gnu_cxx_test
310 template<class _CharT>
313 // char_traits specialization
315 struct char_traits<__gnu_cxx_test::pod_char>
317 typedef __gnu_cxx_test::pod_char char_type;
318 typedef __gnu_cxx_test::pod_int int_type;
319 typedef long pos_type;
320 typedef unsigned long off_type;
321 typedef __gnu_cxx_test::state state_type;
324 assign(char_type& __c1, const char_type& __c2);
327 eq(const char_type& __c1, const char_type& __c2);
330 lt(const char_type& __c1, const char_type& __c2);
333 compare(const char_type* __s1, const char_type* __s2, size_t __n);
336 length(const char_type* __s);
338 static const char_type*
339 find(const char_type* __s, size_t __n, const char_type& __a);
342 move(char_type* __s1, const char_type* __s2, size_t __n);
345 copy(char_type* __s1, const char_type* __s2, size_t __n);
348 assign(char_type* __s, size_t __n, char_type __a);
351 to_char_type(const int_type& __c);
354 to_int_type(const char_type& __c);
357 eq_int_type(const int_type& __c1, const int_type& __c2);
363 not_eof(const int_type& __c);
367 #endif // _GLIBCPP_TESTSUITE_HOOKS_H