2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blobd3755c66f6e1ea461ae0d868ffb1bfa4e60a8529
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 _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.
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 _GLIBCXX_TESTSUITE_HOOKS_H
59 #define _GLIBCXX_TESTSUITE_HOOKS_H
61 #include <bits/c++config.h>
62 #include <bits/functexcept.h>
63 #include <cstddef>
64 #include <locale>
65 #include <ext/pod_char_traits.h>
67 #ifdef _GLIBCXX_ASSERT
68 # include <cassert>
69 # define VERIFY(fn) assert(fn)
70 #else
71 # define VERIFY(fn) test &= (fn)
72 #endif
74 #ifdef _GLIBCXX_HAVE_UNISTD_H
75 # include <unistd.h>
76 #else
77 # define unlink(x)
78 #endif
80 namespace __gnu_test
82 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
83 // from c++config.h
85 // Set memory limits if possible, if not set to 0.
86 #ifndef _GLIBCXX_MEM_LIMITS
87 # define MEMLIMIT_MB 0
88 #else
89 # ifndef MEMLIMIT_MB
90 # define MEMLIMIT_MB 16.0
91 # endif
92 #endif
93 extern void
94 set_memory_limits(float __size = MEMLIMIT_MB);
97 // Check mangled name demangles (using __cxa_demangle) as expected.
98 void
99 verify_demangle(const char* mangled, const char* wanted);
102 // Simple callback structure for variable numbers of tests (all with
103 // same signature). Assume all unit tests are of the signature
104 // void test01();
105 class func_callback
107 public:
108 typedef void (*test_type) (void);
110 private:
111 int _M_size;
112 test_type _M_tests[15];
114 public:
115 func_callback(): _M_size(0) { };
118 size() const { return _M_size; }
120 const test_type*
121 tests() const { return _M_tests; }
123 void
124 push_back(test_type test)
126 _M_tests[_M_size] = test;
127 ++_M_size;
132 // Run select unit tests after setting global locale.
133 void
134 run_tests_wrapped_locale(const char*, const func_callback&);
136 // Run select unit tests after setting environment variables.
137 void
138 run_tests_wrapped_env(const char*, const char*, const func_callback&);
140 // Try to create a locale with the given name. If it fails, bail.
141 std::locale
142 try_named_locale(const char* name);
145 // Test data types.
146 struct pod_char
148 unsigned char c;
151 struct pod_int
153 int i;
156 struct state
158 unsigned long l;
159 unsigned long l2;
162 typedef unsigned short value_type;
163 typedef unsigned int int_type;
164 typedef __gnu_cxx::character<value_type, int_type> pod_type;
167 // Counting.
168 struct counter
170 // Specifically and glaringly-obviously marked 'signed' so that when
171 // COUNT mistakenly goes negative, we can track the patterns of
172 // deletions more easily.
173 typedef signed int size_type;
174 static size_type count;
175 counter() { ++count; }
176 counter (const counter&) { ++count; }
177 ~counter() { --count; }
180 #define assert_count(n) VERIFY(__gnu_test::counter::count == n)
182 // A (static) class for counting copy constructors and possibly throwing an
183 // exception on a desired count.
184 class copy_constructor
186 public:
187 static unsigned int
188 count() { return count_; }
190 static void
191 mark_call()
193 count_++;
194 if (count_ == throw_on_)
195 __throw_exception_again "copy constructor exception";
198 static void
199 reset()
201 count_ = 0;
202 throw_on_ = 0;
205 static void
206 throw_on(unsigned int count) { throw_on_ = count; }
208 private:
209 static unsigned int count_;
210 static unsigned int throw_on_;
213 // A (static) class for counting assignment operator calls and
214 // possibly throwing an exception on a desired count.
215 class assignment_operator
217 public:
218 static unsigned int
219 count() { return count_; }
221 static void
222 mark_call()
224 count_++;
225 if (count_ == throw_on_)
226 __throw_exception_again "assignment operator exception";
229 static void
230 reset()
232 count_ = 0;
233 throw_on_ = 0;
236 static void
237 throw_on(unsigned int count) { throw_on_ = count; }
239 private:
240 static unsigned int count_;
241 static unsigned int throw_on_;
244 // A (static) class for tracking calls to an object's destructor.
245 class destructor
247 public:
248 static unsigned int
249 count() { return _M_count; }
251 static void
252 mark_call() { _M_count++; }
254 static void
255 reset() { _M_count = 0; }
257 private:
258 static unsigned int _M_count;
261 // An class of objects that can be used for validating various
262 // behaviours and guarantees of containers and algorithms defined in
263 // the standard library.
264 class copy_tracker
266 public:
267 // Creates a copy-tracking object with the given ID number. If
268 // "throw_on_copy" is set, an exception will be thrown if an
269 // attempt is made to copy this object.
270 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
271 : id_(id) , throw_on_copy_(throw_on_copy) { }
273 // Copy-constructs the object, marking a call to the copy
274 // constructor and forcing an exception if indicated.
275 copy_tracker(const copy_tracker& rhs)
276 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
278 if (throw_on_copy_)
279 copy_constructor::throw_on(copy_constructor::count() + 1);
280 copy_constructor::mark_call();
283 // Assigns the value of another object to this one, tracking the
284 // number of times this member function has been called and if the
285 // other object is supposed to throw an exception when it is
286 // copied, well, make it so.
287 copy_tracker&
288 operator=(const copy_tracker& rhs)
290 id_ = rhs.id();
291 if (rhs.throw_on_copy_)
292 assignment_operator::throw_on(assignment_operator::count() + 1);
293 assignment_operator::mark_call();
294 return *this;
297 ~copy_tracker()
298 { destructor::mark_call(); }
301 id() const { return id_; }
303 private:
304 int id_;
305 const bool throw_on_copy_;
307 public:
308 static void
309 reset()
311 copy_constructor::reset();
312 assignment_operator::reset();
313 destructor::reset();
316 // for backwards-compatibility
317 static int
318 copyCount()
319 { return copy_constructor::count(); }
321 // for backwards-compatibility
322 static int
323 dtorCount()
324 { return destructor::count(); }
326 private:
327 static int next_id_;
330 inline bool
331 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
332 { return lhs.id() == rhs.id(); }
333 } // namespace __gnu_test
335 namespace std
337 template<class _CharT>
338 struct char_traits;
340 // char_traits specialization
341 template<>
342 struct char_traits<__gnu_test::pod_char>
344 typedef __gnu_test::pod_char char_type;
345 typedef __gnu_test::pod_int int_type;
346 typedef __gnu_test::state state_type;
347 typedef fpos<state_type> pos_type;
348 typedef streamoff off_type;
350 static void
351 assign(char_type& c1, const char_type& c2)
352 { c1.c = c2.c; }
354 static bool
355 eq(const char_type& c1, const char_type& c2)
356 { return c1.c == c2.c; }
358 static bool
359 lt(const char_type& c1, const char_type& c2)
360 { return c1.c < c2.c; }
362 static int
363 compare(const char_type* s1, const char_type* s2, size_t n)
364 { return memcmp(s1, s2, n); }
366 static size_t
367 length(const char_type* s)
368 { return strlen(reinterpret_cast<const char*>(s)); }
370 static const char_type*
371 find(const char_type* s, size_t n, const char_type& a)
372 { return static_cast<const char_type*>(memchr(s, a.c, n)); }
374 static char_type*
375 move(char_type* s1, const char_type* s2, size_t n)
377 memmove(s1, s2, n);
378 return s1;
381 static char_type*
382 copy(char_type* s1, const char_type* s2, size_t n)
384 memcpy(s1, s2, n);
385 return s1;
388 static char_type*
389 assign(char_type* s, size_t n, char_type a)
391 memset(s, a.c, n);
392 return s;
395 static char_type
396 to_char_type(const int_type& c)
398 char_type ret;
399 ret.c = static_cast<unsigned char>(c.i);
400 return ret;
403 static int_type
404 to_int_type(const char_type& c)
406 int_type ret;
407 ret.i = c.c;
408 return ret;
411 static bool
412 eq_int_type(const int_type& c1, const int_type& c2)
413 { return c1.i == c2.i; }
415 static int_type
416 eof()
418 int_type n;
419 n.i = -10;
420 return n;
423 static int_type
424 not_eof(const int_type& c)
426 if (eq_int_type(c, eof()))
427 return int_type();
428 return c;
431 } // namespace std
433 #endif // _GLIBCXX_TESTSUITE_HOOKS_H