2 // Utility subroutines for the C++ library testsuite.
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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 2, 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 COPYING. If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 // This file provides the following:
34 // 1) VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
35 // This file is included in the various testsuite programs to provide
36 // #define(able) assert() behavior for debugging/testing. It may be
37 // a suitable location for other furry woodland creatures as well.
39 // 2) set_memory_limits()
40 // set_memory_limits() uses setrlimit() to restrict dynamic memory
41 // allocation. We provide a default memory limit if none is passed by the
42 // calling application. The argument to set_memory_limits() is the
43 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
44 // not #defined before including this header, then no limiting is attempted.
47 // This is a POD with a static data member, gnu_counting_struct::count,
48 // which starts at zero, increments on instance construction, and decrements
49 // on instance destruction. "assert_count(n)" can be called to VERIFY()
50 // that the count equals N.
52 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
53 // A class with nontrivial ctor/dtor that provides the ability to track the
54 // number of copy ctors and dtors, and will throw on demand during copy.
56 // 5) pod_char, pod_int, , abstract character classes and
57 // char_traits specializations for testing instantiations.
59 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
60 #define _GLIBCXX_TESTSUITE_HOOKS_H
62 #include <bits/c++config.h>
63 #include <bits/functexcept.h>
66 #include <ext/pod_char_traits.h>
67 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
71 #ifdef _GLIBCXX_ASSERT
73 # define VERIFY(fn) assert(fn)
75 # define VERIFY(fn) test &= (fn)
78 #ifdef _GLIBCXX_HAVE_UNISTD_H
86 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
89 // Set memory limits if possible, if not set to 0.
90 #ifndef _GLIBCXX_RES_LIMITS
91 # define MEMLIMIT_MB 0
94 # define MEMLIMIT_MB 16.0
98 set_memory_limits(float __size
= MEMLIMIT_MB
);
101 set_file_limit(unsigned long __size
);
103 // Check mangled name demangles (using __cxa_demangle) as expected.
105 verify_demangle(const char* mangled
, const char* wanted
);
107 // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
109 template<typename bitmask_type
>
124 // Simple callback structure for variable numbers of tests (all with
125 // same signature). Assume all unit tests are of the signature
130 typedef void (*test_type
) (void);
134 test_type _M_tests
[15];
137 operator=(const func_callback
&);
139 func_callback(const func_callback
&);
142 func_callback(): _M_size(0) { };
145 size() const { return _M_size
; }
148 tests() const { return _M_tests
; }
151 push_back(test_type test
)
153 _M_tests
[_M_size
] = test
;
159 // Run select unit tests after setting global locale.
161 run_tests_wrapped_locale(const char*, const func_callback
&);
163 // Run select unit tests after setting environment variables.
165 run_tests_wrapped_env(const char*, const char*, const func_callback
&);
167 // Try to create a locale with the given name. If it fails, bail.
169 try_named_locale(const char* name
);
172 try_mkfifo (const char* filename
, mode_t mode
);
181 operator==(const pod_char
& lhs
, const pod_char
& rhs
)
182 { return lhs
.c
== rhs
.c
; }
195 typedef unsigned short value_type
;
196 typedef unsigned int int_type
;
197 typedef __gnu_cxx::character
<value_type
, int_type
> pod_type
;
203 // Specifically and glaringly-obviously marked 'signed' so that when
204 // COUNT mistakenly goes negative, we can track the patterns of
205 // deletions more easily.
206 typedef signed int size_type
;
207 static size_type count
;
208 counter() { ++count
; }
209 counter (const counter
&) { ++count
; }
210 ~counter() { --count
; }
213 #define assert_count(n) VERIFY(__gnu_test::counter::count == n)
215 // A (static) class for counting copy constructors and possibly throwing an
216 // exception on a desired count.
217 class copy_constructor
221 count() { return count_
; }
227 if (count_
== throw_on_
)
228 std::__throw_runtime_error("copy_constructor::mark_call");
239 throw_on(unsigned int count
) { throw_on_
= count
; }
242 static unsigned int count_
;
243 static unsigned int throw_on_
;
246 // A (static) class for counting assignment operator calls and
247 // possibly throwing an exception on a desired count.
248 class assignment_operator
252 count() { return count_
; }
258 if (count_
== throw_on_
)
259 std::__throw_runtime_error("assignment_operator::mark_call");
270 throw_on(unsigned int count
) { throw_on_
= count
; }
273 static unsigned int count_
;
274 static unsigned int throw_on_
;
277 // A (static) class for tracking calls to an object's destructor.
282 count() { return _M_count
; }
285 mark_call() { _M_count
++; }
288 reset() { _M_count
= 0; }
291 static unsigned int _M_count
;
294 // An class of objects that can be used for validating various
295 // behaviours and guarantees of containers and algorithms defined in
296 // the standard library.
300 // Creates a copy-tracking object with the given ID number. If
301 // "throw_on_copy" is set, an exception will be thrown if an
302 // attempt is made to copy this object.
303 copy_tracker(int id
= next_id_
--, bool throw_on_copy
= false)
304 : id_(id
) , throw_on_copy_(throw_on_copy
) { }
306 // Copy-constructs the object, marking a call to the copy
307 // constructor and forcing an exception if indicated.
308 copy_tracker(const copy_tracker
& rhs
)
309 : id_(rhs
.id()), throw_on_copy_(rhs
.throw_on_copy_
)
312 copy_constructor::throw_on(copy_constructor::count() + 1);
313 copy_constructor::mark_call();
316 // Assigns the value of another object to this one, tracking the
317 // number of times this member function has been called and if the
318 // other object is supposed to throw an exception when it is
319 // copied, well, make it so.
321 operator=(const copy_tracker
& rhs
)
324 if (rhs
.throw_on_copy_
)
325 assignment_operator::throw_on(assignment_operator::count() + 1);
326 assignment_operator::mark_call();
331 { destructor::mark_call(); }
334 id() const { return id_
; }
338 const bool throw_on_copy_
;
344 copy_constructor::reset();
345 assignment_operator::reset();
349 // for backwards-compatibility
352 { return copy_constructor::count(); }
354 // for backwards-compatibility
357 { return destructor::count(); }
364 operator==(const copy_tracker
& lhs
, const copy_tracker
& rhs
)
365 { return lhs
.id() == rhs
.id(); }
367 // Class for checking required type conversions, implicit and
368 // explicit for given library data structures.
369 template<typename _Container
>
372 typedef typename
_Container::const_iterator const_iterator
;
374 // Implicit conversion iterator to const_iterator.
375 static const_iterator
376 iterator_to_const_iterator()
379 const_iterator it
= v
.begin();
380 const_iterator end
= v
.end();
381 return it
== end
? v
.end() : it
;
385 // A binary semaphore for use across multiple processes.
389 // Creates a binary semaphore. The semaphore is initially in the
393 // Destroy the semaphore.
396 // Signal the semaphore. If there are processes blocked in
397 // "wait", exactly one will be permitted to proceed.
400 // Wait until the semaphore is signaled.
408 } // namespace __gnu_test
412 template<class _CharT
>
415 // char_traits specialization
417 struct char_traits
<__gnu_test::pod_char
>
419 typedef __gnu_test::pod_char char_type
;
420 typedef __gnu_test::pod_int int_type
;
421 typedef __gnu_test::state state_type
;
422 typedef fpos
<state_type
> pos_type
;
423 typedef streamoff off_type
;
426 assign(char_type
& c1
, const char_type
& c2
)
430 eq(const char_type
& c1
, const char_type
& c2
)
431 { return c1
.c
== c2
.c
; }
434 lt(const char_type
& c1
, const char_type
& c2
)
435 { return c1
.c
< c2
.c
; }
438 compare(const char_type
* s1
, const char_type
* s2
, size_t n
)
439 { return memcmp(s1
, s2
, n
); }
442 length(const char_type
* s
)
443 { return strlen(reinterpret_cast<const char*>(s
)); }
445 static const char_type
*
446 find(const char_type
* s
, size_t n
, const char_type
& a
)
447 { return static_cast<const char_type
*>(memchr(s
, a
.c
, n
)); }
450 move(char_type
* s1
, const char_type
* s2
, size_t n
)
457 copy(char_type
* s1
, const char_type
* s2
, size_t n
)
464 assign(char_type
* s
, size_t n
, char_type a
)
471 to_char_type(const int_type
& c
)
474 ret
.c
= static_cast<unsigned char>(c
.i
);
479 to_int_type(const char_type
& c
)
487 eq_int_type(const int_type
& c1
, const int_type
& c2
)
488 { return c1
.i
== c2
.i
; }
499 not_eof(const int_type
& c
)
501 if (eq_int_type(c
, eof()))
508 #endif // _GLIBCXX_TESTSUITE_HOOKS_H