Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blob171cdc6cee6a4ac4ba1b3bb137dbccc4a4fc4939
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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,
21 // USA.
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.
46 // 3) counter
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>
64 #include <cstddef>
65 #include <locale>
66 #include <ext/pod_char_traits.h>
67 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
68 #include <sys/stat.h>
69 #endif
71 #ifdef _GLIBCXX_ASSERT
72 # include <cassert>
73 # define VERIFY(fn) assert(fn)
74 #else
75 # define VERIFY(fn) test &= (fn)
76 #endif
78 #ifdef _GLIBCXX_HAVE_UNISTD_H
79 # include <unistd.h>
80 #else
81 # define unlink(x)
82 #endif
84 namespace __gnu_test
86 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
87 // from c++config.h
89 // Set memory limits if possible, if not set to 0.
90 #ifndef _GLIBCXX_RES_LIMITS
91 # define MEMLIMIT_MB 0
92 #else
93 # ifndef MEMLIMIT_MB
94 # define MEMLIMIT_MB 16.0
95 # endif
96 #endif
97 extern void
98 set_memory_limits(float __size = MEMLIMIT_MB);
100 extern void
101 set_file_limit(unsigned long __size);
103 // Check mangled name demangles (using __cxa_demangle) as expected.
104 void
105 verify_demangle(const char* mangled, const char* wanted);
107 // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
108 // bitmask_operators
109 template<typename bitmask_type>
110 void
111 bitmask_operators()
113 bitmask_type a;
114 bitmask_type b;
115 a | b;
116 a & b;
117 a ^ b;
119 a |= b; // set
120 a &= ~b; // clear
121 a ^= b;
124 // Simple callback structure for variable numbers of tests (all with
125 // same signature). Assume all unit tests are of the signature
126 // void test01();
127 class func_callback
129 public:
130 typedef void (*test_type) (void);
132 private:
133 int _M_size;
134 test_type _M_tests[15];
136 func_callback&
137 operator=(const func_callback&);
139 func_callback(const func_callback&);
141 public:
142 func_callback(): _M_size(0) { };
145 size() const { return _M_size; }
147 const test_type*
148 tests() const { return _M_tests; }
150 void
151 push_back(test_type test)
153 _M_tests[_M_size] = test;
154 ++_M_size;
159 // Run select unit tests after setting global locale.
160 void
161 run_tests_wrapped_locale(const char*, const func_callback&);
163 // Run select unit tests after setting environment variables.
164 void
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.
168 std::locale
169 try_named_locale(const char* name);
172 try_mkfifo (const char* filename, mode_t mode);
174 // Test data types.
175 struct pod_char
177 unsigned char c;
180 inline bool
181 operator==(const pod_char& lhs, const pod_char& rhs)
182 { return lhs.c == rhs.c; }
184 struct pod_int
186 int i;
189 struct state
191 unsigned long l;
192 unsigned long l2;
195 typedef unsigned short value_type;
196 typedef unsigned int int_type;
197 typedef __gnu_cxx::character<value_type, int_type> pod_type;
200 // Counting.
201 struct counter
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
219 public:
220 static unsigned int
221 count() { return count_; }
223 static void
224 mark_call()
226 count_++;
227 if (count_ == throw_on_)
228 std::__throw_runtime_error("copy_constructor::mark_call");
231 static void
232 reset()
234 count_ = 0;
235 throw_on_ = 0;
238 static void
239 throw_on(unsigned int count) { throw_on_ = count; }
241 private:
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
250 public:
251 static unsigned int
252 count() { return count_; }
254 static void
255 mark_call()
257 count_++;
258 if (count_ == throw_on_)
259 std::__throw_runtime_error("assignment_operator::mark_call");
262 static void
263 reset()
265 count_ = 0;
266 throw_on_ = 0;
269 static void
270 throw_on(unsigned int count) { throw_on_ = count; }
272 private:
273 static unsigned int count_;
274 static unsigned int throw_on_;
277 // A (static) class for tracking calls to an object's destructor.
278 class destructor
280 public:
281 static unsigned int
282 count() { return _M_count; }
284 static void
285 mark_call() { _M_count++; }
287 static void
288 reset() { _M_count = 0; }
290 private:
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.
297 class copy_tracker
299 public:
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_)
311 if (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.
320 copy_tracker&
321 operator=(const copy_tracker& rhs)
323 id_ = rhs.id();
324 if (rhs.throw_on_copy_)
325 assignment_operator::throw_on(assignment_operator::count() + 1);
326 assignment_operator::mark_call();
327 return *this;
330 ~copy_tracker()
331 { destructor::mark_call(); }
334 id() const { return id_; }
336 private:
337 int id_;
338 const bool throw_on_copy_;
340 public:
341 static void
342 reset()
344 copy_constructor::reset();
345 assignment_operator::reset();
346 destructor::reset();
349 // for backwards-compatibility
350 static int
351 copyCount()
352 { return copy_constructor::count(); }
354 // for backwards-compatibility
355 static int
356 dtorCount()
357 { return destructor::count(); }
359 private:
360 static int next_id_;
363 inline bool
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>
370 struct conversion
372 typedef typename _Container::const_iterator const_iterator;
374 // Implicit conversion iterator to const_iterator.
375 static const_iterator
376 iterator_to_const_iterator()
378 _Container v;
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.
386 class semaphore
388 public:
389 // Creates a binary semaphore. The semaphore is initially in the
390 // unsignaled state.
391 semaphore();
393 // Destroy the semaphore.
394 ~semaphore();
396 // Signal the semaphore. If there are processes blocked in
397 // "wait", exactly one will be permitted to proceed.
398 void signal();
400 // Wait until the semaphore is signaled.
401 void wait();
403 private:
404 int sem_set_;
406 pid_t pid_;
408 } // namespace __gnu_test
410 namespace std
412 template<class _CharT>
413 struct char_traits;
415 // char_traits specialization
416 template<>
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;
425 static void
426 assign(char_type& c1, const char_type& c2)
427 { c1.c = c2.c; }
429 static bool
430 eq(const char_type& c1, const char_type& c2)
431 { return c1.c == c2.c; }
433 static bool
434 lt(const char_type& c1, const char_type& c2)
435 { return c1.c < c2.c; }
437 static int
438 compare(const char_type* s1, const char_type* s2, size_t n)
439 { return memcmp(s1, s2, n); }
441 static size_t
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)); }
449 static char_type*
450 move(char_type* s1, const char_type* s2, size_t n)
452 memmove(s1, s2, n);
453 return s1;
456 static char_type*
457 copy(char_type* s1, const char_type* s2, size_t n)
459 memcpy(s1, s2, n);
460 return s1;
463 static char_type*
464 assign(char_type* s, size_t n, char_type a)
466 memset(s, a.c, n);
467 return s;
470 static char_type
471 to_char_type(const int_type& c)
473 char_type ret;
474 ret.c = static_cast<unsigned char>(c.i);
475 return ret;
478 static int_type
479 to_int_type(const char_type& c)
481 int_type ret;
482 ret.i = c.c;
483 return ret;
486 static bool
487 eq_int_type(const int_type& c1, const int_type& c2)
488 { return c1.i == c2.i; }
490 static int_type
491 eof()
493 int_type n;
494 n.i = -10;
495 return n;
498 static int_type
499 not_eof(const int_type& c)
501 if (eq_int_type(c, eof()))
502 return int_type();
503 return c;
506 } // namespace std
508 #endif // _GLIBCXX_TESTSUITE_HOOKS_H