* Makefile.in (WERROR_FLAGS): Renamed from WERROR.
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blob4b526e9b19f0bcc6e1526ace4fe11ae8d48b0eea
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004 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_RES_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>
66 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
67 #include <sys/stat.h>
68 #endif
70 #ifdef _GLIBCXX_ASSERT
71 # include <cassert>
72 # define VERIFY(fn) assert(fn)
73 #else
74 # define VERIFY(fn) test &= (fn)
75 #endif
77 #ifdef _GLIBCXX_HAVE_UNISTD_H
78 # include <unistd.h>
79 #else
80 # define unlink(x)
81 #endif
83 namespace __gnu_test
85 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
86 // from c++config.h
88 // Set memory limits if possible, if not set to 0.
89 #ifndef _GLIBCXX_RES_LIMITS
90 # define MEMLIMIT_MB 0
91 #else
92 # ifndef MEMLIMIT_MB
93 # define MEMLIMIT_MB 16.0
94 # endif
95 #endif
96 extern void
97 set_memory_limits(float __size = MEMLIMIT_MB);
99 extern void
100 set_file_limit(unsigned long __size);
102 // Check mangled name demangles (using __cxa_demangle) as expected.
103 void
104 verify_demangle(const char* mangled, const char* wanted);
106 // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
107 // bitmask_operators
108 template<typename bitmask_type>
109 void
110 bitmask_operators()
112 bitmask_type a;
113 bitmask_type b;
114 a | b;
115 a & b;
116 a ^ b;
118 a |= b; // set
119 a &= ~b; // clear
120 a ^= b;
123 // Simple callback structure for variable numbers of tests (all with
124 // same signature). Assume all unit tests are of the signature
125 // void test01();
126 class func_callback
128 public:
129 typedef void (*test_type) (void);
131 private:
132 int _M_size;
133 test_type _M_tests[15];
135 func_callback&
136 operator=(const func_callback&);
138 func_callback(const func_callback&);
140 public:
141 func_callback(): _M_size(0) { };
144 size() const { return _M_size; }
146 const test_type*
147 tests() const { return _M_tests; }
149 void
150 push_back(test_type test)
152 _M_tests[_M_size] = test;
153 ++_M_size;
158 // Run select unit tests after setting global locale.
159 void
160 run_tests_wrapped_locale(const char*, const func_callback&);
162 // Run select unit tests after setting environment variables.
163 void
164 run_tests_wrapped_env(const char*, const char*, const func_callback&);
166 // Try to create a locale with the given name. If it fails, bail.
167 std::locale
168 try_named_locale(const char* name);
171 try_mkfifo (const char* filename, mode_t mode);
173 // Test data types.
174 struct pod_char
176 unsigned char c;
179 inline bool
180 operator==(const pod_char& lhs, const pod_char& rhs)
181 { return lhs.c == rhs.c; }
183 struct pod_int
185 int i;
188 struct state
190 unsigned long l;
191 unsigned long l2;
194 typedef unsigned short value_type;
195 typedef unsigned int int_type;
196 typedef __gnu_cxx::character<value_type, int_type> pod_type;
199 // Counting.
200 struct counter
202 // Specifically and glaringly-obviously marked 'signed' so that when
203 // COUNT mistakenly goes negative, we can track the patterns of
204 // deletions more easily.
205 typedef signed int size_type;
206 static size_type count;
207 counter() { ++count; }
208 counter (const counter&) { ++count; }
209 ~counter() { --count; }
212 #define assert_count(n) VERIFY(__gnu_test::counter::count == n)
214 // A (static) class for counting copy constructors and possibly throwing an
215 // exception on a desired count.
216 class copy_constructor
218 public:
219 static unsigned int
220 count() { return count_; }
222 static void
223 mark_call()
225 count_++;
226 if (count_ == throw_on_)
227 __throw_exception_again "copy constructor exception";
230 static void
231 reset()
233 count_ = 0;
234 throw_on_ = 0;
237 static void
238 throw_on(unsigned int count) { throw_on_ = count; }
240 private:
241 static unsigned int count_;
242 static unsigned int throw_on_;
245 // A (static) class for counting assignment operator calls and
246 // possibly throwing an exception on a desired count.
247 class assignment_operator
249 public:
250 static unsigned int
251 count() { return count_; }
253 static void
254 mark_call()
256 count_++;
257 if (count_ == throw_on_)
258 __throw_exception_again "assignment operator exception";
261 static void
262 reset()
264 count_ = 0;
265 throw_on_ = 0;
268 static void
269 throw_on(unsigned int count) { throw_on_ = count; }
271 private:
272 static unsigned int count_;
273 static unsigned int throw_on_;
276 // A (static) class for tracking calls to an object's destructor.
277 class destructor
279 public:
280 static unsigned int
281 count() { return _M_count; }
283 static void
284 mark_call() { _M_count++; }
286 static void
287 reset() { _M_count = 0; }
289 private:
290 static unsigned int _M_count;
293 // An class of objects that can be used for validating various
294 // behaviours and guarantees of containers and algorithms defined in
295 // the standard library.
296 class copy_tracker
298 public:
299 // Creates a copy-tracking object with the given ID number. If
300 // "throw_on_copy" is set, an exception will be thrown if an
301 // attempt is made to copy this object.
302 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
303 : id_(id) , throw_on_copy_(throw_on_copy) { }
305 // Copy-constructs the object, marking a call to the copy
306 // constructor and forcing an exception if indicated.
307 copy_tracker(const copy_tracker& rhs)
308 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
310 if (throw_on_copy_)
311 copy_constructor::throw_on(copy_constructor::count() + 1);
312 copy_constructor::mark_call();
315 // Assigns the value of another object to this one, tracking the
316 // number of times this member function has been called and if the
317 // other object is supposed to throw an exception when it is
318 // copied, well, make it so.
319 copy_tracker&
320 operator=(const copy_tracker& rhs)
322 id_ = rhs.id();
323 if (rhs.throw_on_copy_)
324 assignment_operator::throw_on(assignment_operator::count() + 1);
325 assignment_operator::mark_call();
326 return *this;
329 ~copy_tracker()
330 { destructor::mark_call(); }
333 id() const { return id_; }
335 private:
336 int id_;
337 const bool throw_on_copy_;
339 public:
340 static void
341 reset()
343 copy_constructor::reset();
344 assignment_operator::reset();
345 destructor::reset();
348 // for backwards-compatibility
349 static int
350 copyCount()
351 { return copy_constructor::count(); }
353 // for backwards-compatibility
354 static int
355 dtorCount()
356 { return destructor::count(); }
358 private:
359 static int next_id_;
362 inline bool
363 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
364 { return lhs.id() == rhs.id(); }
366 // Class for checking required type conversions, implicit and
367 // explicit for given library data structures.
368 template<typename _Container>
369 struct conversion
371 typedef typename _Container::const_iterator const_iterator;
373 // Implicit conversion iterator to const_iterator.
374 static const_iterator
375 iterator_to_const_iterator()
377 _Container v;
378 const_iterator it = v.begin();
379 const_iterator end = v.end();
380 return it == end ? v.end() : it;
383 } // namespace __gnu_test
385 namespace std
387 template<class _CharT>
388 struct char_traits;
390 // char_traits specialization
391 template<>
392 struct char_traits<__gnu_test::pod_char>
394 typedef __gnu_test::pod_char char_type;
395 typedef __gnu_test::pod_int int_type;
396 typedef __gnu_test::state state_type;
397 typedef fpos<state_type> pos_type;
398 typedef streamoff off_type;
400 static void
401 assign(char_type& c1, const char_type& c2)
402 { c1.c = c2.c; }
404 static bool
405 eq(const char_type& c1, const char_type& c2)
406 { return c1.c == c2.c; }
408 static bool
409 lt(const char_type& c1, const char_type& c2)
410 { return c1.c < c2.c; }
412 static int
413 compare(const char_type* s1, const char_type* s2, size_t n)
414 { return memcmp(s1, s2, n); }
416 static size_t
417 length(const char_type* s)
418 { return strlen(reinterpret_cast<const char*>(s)); }
420 static const char_type*
421 find(const char_type* s, size_t n, const char_type& a)
422 { return static_cast<const char_type*>(memchr(s, a.c, n)); }
424 static char_type*
425 move(char_type* s1, const char_type* s2, size_t n)
427 memmove(s1, s2, n);
428 return s1;
431 static char_type*
432 copy(char_type* s1, const char_type* s2, size_t n)
434 memcpy(s1, s2, n);
435 return s1;
438 static char_type*
439 assign(char_type* s, size_t n, char_type a)
441 memset(s, a.c, n);
442 return s;
445 static char_type
446 to_char_type(const int_type& c)
448 char_type ret;
449 ret.c = static_cast<unsigned char>(c.i);
450 return ret;
453 static int_type
454 to_int_type(const char_type& c)
456 int_type ret;
457 ret.i = c.c;
458 return ret;
461 static bool
462 eq_int_type(const int_type& c1, const int_type& c2)
463 { return c1.i == c2.i; }
465 static int_type
466 eof()
468 int_type n;
469 n.i = -10;
470 return n;
473 static int_type
474 not_eof(const int_type& c)
476 if (eq_int_type(c, eof()))
477 return int_type();
478 return c;
481 } // namespace std
483 #endif // _GLIBCXX_TESTSUITE_HOOKS_H