2003-06-14 Nick Clifton <nickc@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blob719135caa582be51424ea52d19ca25b9ec84a581
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
45 // 3) counter
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>
63 #include <cstddef>
64 #ifdef DEBUG_ASSERT
65 # include <cassert>
66 # define VERIFY(fn) assert(fn)
67 #else
68 # define VERIFY(fn) test &= (fn)
69 #endif
70 #include <list>
71 #include <locale>
73 namespace __gnu_cxx_test
75 // All macros are defined in GLIBCPP_CONFIGURE_TESTSUITE and imported
76 // from c++config.h
78 // Set memory limits if possible, if not set to 0.
79 #ifndef _GLIBCPP_MEM_LIMITS
80 # define MEMLIMIT_MB 0
81 #else
82 # ifndef MEMLIMIT_MB
83 # define MEMLIMIT_MB 16.0
84 # endif
85 #endif
86 extern void
87 set_memory_limits(float __size = MEMLIMIT_MB);
90 // Check mangled name demangles (using __cxa_demangle) as expected.
91 void
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
97 // void test01();
98 typedef void (*test_func) (void);
99 typedef std::list<test_func> func_callback;
101 // Run select unit tests after setting global locale.
102 void
103 run_tests_wrapped_locale(const char*, const func_callback&);
105 // Run select unit tests after setting environment variables.
106 void
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.
110 std::locale
111 try_named_locale(const char* name);
114 // Test data types.
115 struct pod_char
117 unsigned char c;
120 struct pod_int
122 int i;
125 struct pod_unsigned_int
127 unsigned int i;
130 struct pod_long
132 unsigned long i;
135 struct state
137 unsigned long l;
138 unsigned long l2;
142 // Counting.
143 struct counter
145 // Specifically and glaringly-obviously marked 'signed' so that when
146 // COUNT mistakenly goes negative, we can track the patterns of
147 // deletions more easily.
148 typedef signed int size_type;
149 static size_type count;
150 counter() { ++count; }
151 counter (const counter&) { ++count; }
152 ~counter() { --count; }
155 #define assert_count(n) VERIFY(__gnu_cxx_test::counter::count == n)
157 // A (static) class for counting copy constructors and possibly throwing an
158 // exception on a desired count.
159 class copy_constructor
161 public:
162 static unsigned int
163 count() { return count_; }
165 static void
166 mark_call()
168 count_++;
169 if (count_ == throw_on_)
170 __throw_exception_again "copy constructor exception";
173 static void
174 reset()
176 count_ = 0;
177 throw_on_ = 0;
180 static void
181 throw_on(unsigned int count) { throw_on_ = count; }
183 private:
184 static unsigned int count_;
185 static unsigned int throw_on_;
188 // A (static) class for counting assignment operator calls and
189 // possibly throwing an exception on a desired count.
190 class assignment_operator
192 public:
193 static unsigned int
194 count() { return count_; }
196 static void
197 mark_call()
199 count_++;
200 if (count_ == throw_on_)
201 __throw_exception_again "assignment operator exception";
204 static void
205 reset()
207 count_ = 0;
208 throw_on_ = 0;
211 static void
212 throw_on(unsigned int count) { throw_on_ = count; }
214 private:
215 static unsigned int count_;
216 static unsigned int throw_on_;
219 // A (static) class for tracking calls to an object's destructor.
220 class destructor
222 public:
223 static unsigned int
224 count() { return _M_count; }
226 static void
227 mark_call() { _M_count++; }
229 static void
230 reset() { _M_count = 0; }
232 private:
233 static unsigned int _M_count;
236 // An class of objects that can be used for validating various
237 // behaviours and guarantees of containers and algorithms defined in
238 // the standard library.
239 class copy_tracker
241 public:
242 // Creates a copy-tracking object with the given ID number. If
243 // "throw_on_copy" is set, an exception will be thrown if an
244 // attempt is made to copy this object.
245 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
246 : id_(id) , throw_on_copy_(throw_on_copy) { }
248 // Copy-constructs the object, marking a call to the copy
249 // constructor and forcing an exception if indicated.
250 copy_tracker(const copy_tracker& rhs)
251 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
253 if (throw_on_copy_)
254 copy_constructor::throw_on(copy_constructor::count() + 1);
255 copy_constructor::mark_call();
258 // Assigns the value of another object to this one, tracking the
259 // number of times this member function has been called and if the
260 // other object is supposed to throw an exception when it is
261 // copied, well, make it so.
262 copy_tracker&
263 operator=(const copy_tracker& rhs)
265 id_ = rhs.id();
266 if (rhs.throw_on_copy_)
267 assignment_operator::throw_on(assignment_operator::count() + 1);
268 assignment_operator::mark_call();
269 return *this;
272 ~copy_tracker()
273 { destructor::mark_call(); }
276 id() const { return id_; }
278 private:
279 int id_;
280 const bool throw_on_copy_;
282 public:
283 static void
284 reset()
286 copy_constructor::reset();
287 assignment_operator::reset();
288 destructor::reset();
291 // for backwards-compatibility
292 static int
293 copyCount()
294 { return copy_constructor::count(); }
296 // for backwards-compatibility
297 static int
298 dtorCount()
299 { return destructor::count(); }
301 private:
302 static int next_id_;
305 inline bool
306 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
307 { return lhs.id() == rhs.id(); }
308 } // namespace __gnu_cxx_test
310 namespace std
312 template<class _CharT>
313 struct char_traits;
315 // char_traits specialization
316 template<>
317 struct char_traits<__gnu_cxx_test::pod_char>
319 typedef __gnu_cxx_test::pod_char char_type;
320 typedef __gnu_cxx_test::pod_int int_type;
321 typedef long pos_type;
322 typedef unsigned long off_type;
323 typedef __gnu_cxx_test::state state_type;
325 static void
326 assign(char_type& __c1, const char_type& __c2);
328 static bool
329 eq(const char_type& __c1, const char_type& __c2);
331 static bool
332 lt(const char_type& __c1, const char_type& __c2);
334 static int
335 compare(const char_type* __s1, const char_type* __s2, size_t __n);
337 static size_t
338 length(const char_type* __s);
340 static const char_type*
341 find(const char_type* __s, size_t __n, const char_type& __a);
343 static char_type*
344 move(char_type* __s1, const char_type* __s2, size_t __n);
346 static char_type*
347 copy(char_type* __s1, const char_type* __s2, size_t __n);
349 static char_type*
350 assign(char_type* __s, size_t __n, char_type __a);
352 static char_type
353 to_char_type(const int_type& __c);
355 static int_type
356 to_int_type(const char_type& __c);
358 static bool
359 eq_int_type(const int_type& __c1, const int_type& __c2);
361 static int_type
362 eof();
364 static int_type
365 not_eof(const int_type& __c);
367 } // namespace std
369 #endif // _GLIBCPP_TESTSUITE_HOOKS_H