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 _GLIBCXX_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 _GLIBCXX_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 _GLIBCXX_TESTSUITE_HOOKS_H
59 #define _GLIBCXX_TESTSUITE_HOOKS_H
61 #include <bits/c++config.h>
62 #include <bits/functexcept.h>
65 #include <ext/pod_char_traits.h>
66 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
70 #ifdef _GLIBCXX_ASSERT
72 # define VERIFY(fn) assert(fn)
74 # define VERIFY(fn) test &= (fn)
77 #ifdef _GLIBCXX_HAVE_UNISTD_H
85 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
88 // Set memory limits if possible, if not set to 0.
89 #ifndef _GLIBCXX_MEM_LIMITS
90 # define MEMLIMIT_MB 0
93 # define MEMLIMIT_MB 16.0
97 set_memory_limits(float __size = MEMLIMIT_MB);
100 // Check mangled name demangles (using __cxa_demangle) as expected.
102 verify_demangle(const char* mangled, const char* wanted);
105 // Simple callback structure for variable numbers of tests (all with
106 // same signature). Assume all unit tests are of the signature
111 typedef void (*test_type) (void);
115 test_type _M_tests[15];
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&);
143 // Try to create a locale with the given name. If it fails, bail.
145 try_named_locale(const char* name);
148 try_mkfifo (const char* filename, mode_t mode);
167 typedef unsigned short value_type;
168 typedef unsigned int int_type;
169 typedef __gnu_cxx::character<value_type, int_type> pod_type;
175 // Specifically and glaringly-obviously marked 'signed' so that when
176 // COUNT mistakenly goes negative, we can track the patterns of
177 // deletions more easily.
178 typedef signed int size_type;
179 static size_type count;
180 counter() { ++count; }
181 counter (const counter&) { ++count; }
182 ~counter() { --count; }
185 #define assert_count(n) VERIFY(__gnu_test::counter::count == n)
187 // A (static) class for counting copy constructors and possibly throwing an
188 // exception on a desired count.
189 class copy_constructor
193 count() { return count_; }
199 if (count_ == throw_on_)
200 __throw_exception_again "copy constructor exception";
211 throw_on(unsigned int count) { throw_on_ = count; }
214 static unsigned int count_;
215 static unsigned int throw_on_;
218 // A (static) class for counting assignment operator calls and
219 // possibly throwing an exception on a desired count.
220 class assignment_operator
224 count() { return count_; }
230 if (count_ == throw_on_)
231 __throw_exception_again "assignment operator exception";
242 throw_on(unsigned int count) { throw_on_ = count; }
245 static unsigned int count_;
246 static unsigned int throw_on_;
249 // A (static) class for tracking calls to an object's destructor.
254 count() { return _M_count; }
257 mark_call() { _M_count++; }
260 reset() { _M_count = 0; }
263 static unsigned int _M_count;
266 // An class of objects that can be used for validating various
267 // behaviours and guarantees of containers and algorithms defined in
268 // the standard library.
272 // Creates a copy-tracking object with the given ID number. If
273 // "throw_on_copy" is set, an exception will be thrown if an
274 // attempt is made to copy this object.
275 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
276 : id_(id) , throw_on_copy_(throw_on_copy) { }
278 // Copy-constructs the object, marking a call to the copy
279 // constructor and forcing an exception if indicated.
280 copy_tracker(const copy_tracker& rhs)
281 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
284 copy_constructor::throw_on(copy_constructor::count() + 1);
285 copy_constructor::mark_call();
288 // Assigns the value of another object to this one, tracking the
289 // number of times this member function has been called and if the
290 // other object is supposed to throw an exception when it is
291 // copied, well, make it so.
293 operator=(const copy_tracker& rhs)
296 if (rhs.throw_on_copy_)
297 assignment_operator::throw_on(assignment_operator::count() + 1);
298 assignment_operator::mark_call();
303 { destructor::mark_call(); }
306 id() const { return id_; }
310 const bool throw_on_copy_;
316 copy_constructor::reset();
317 assignment_operator::reset();
321 // for backwards-compatibility
324 { return copy_constructor::count(); }
326 // for backwards-compatibility
329 { return destructor::count(); }
336 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
337 { return lhs.id() == rhs.id(); }
338 } // namespace __gnu_test
342 template<class _CharT>
345 // char_traits specialization
347 struct char_traits<__gnu_test::pod_char>
349 typedef __gnu_test::pod_char char_type;
350 typedef __gnu_test::pod_int int_type;
351 typedef __gnu_test::state state_type;
352 typedef fpos<state_type> pos_type;
353 typedef streamoff off_type;
356 assign(char_type& c1, const char_type& c2)
360 eq(const char_type& c1, const char_type& c2)
361 { return c1.c == c2.c; }
364 lt(const char_type& c1, const char_type& c2)
365 { return c1.c < c2.c; }
368 compare(const char_type* s1, const char_type* s2, size_t n)
369 { return memcmp(s1, s2, n); }
372 length(const char_type* s)
373 { return strlen(reinterpret_cast<const char*>(s)); }
375 static const char_type*
376 find(const char_type* s, size_t n, const char_type& a)
377 { return static_cast<const char_type*>(memchr(s, a.c, n)); }
380 move(char_type* s1, const char_type* s2, size_t n)
387 copy(char_type* s1, const char_type* s2, size_t n)
394 assign(char_type* s, size_t n, char_type a)
401 to_char_type(const int_type& c)
404 ret.c = static_cast<unsigned char>(c.i);
409 to_int_type(const char_type& c)
417 eq_int_type(const int_type& c1, const int_type& c2)
418 { return c1.i == c2.i; }
429 not_eof(const int_type& c)
431 if (eq_int_type(c, eof()))
438 #endif // _GLIBCXX_TESTSUITE_HOOKS_H