2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blob8a5276f50c243b93a9c09f80d1ea93cabf027aca
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
57 #define _GLIBCXX_TESTSUITE_HOOKS_H
59 #include <bits/c++config.h>
60 #include <bits/functexcept.h>
61 #include <cstddef>
62 #include <locale>
63 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
64 #include <sys/stat.h>
65 #endif
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_RES_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);
96 extern void
97 set_file_limit(unsigned long __size);
99 // Check mangled name demangles (using __cxa_demangle) as expected.
100 void
101 verify_demangle(const char* mangled, const char* wanted);
103 // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
104 // bitmask_operators
105 template<typename bitmask_type>
106 void
107 bitmask_operators()
109 bitmask_type a;
110 bitmask_type b;
111 a | b;
112 a & b;
113 a ^ b;
115 a |= b; // set
116 a &= ~b; // clear
117 a ^= b;
120 // Simple callback structure for variable numbers of tests (all with
121 // same signature). Assume all unit tests are of the signature
122 // void test01();
123 class func_callback
125 public:
126 typedef void (*test_type) (void);
128 private:
129 int _M_size;
130 test_type _M_tests[15];
132 func_callback&
133 operator=(const func_callback&);
135 func_callback(const func_callback&);
137 public:
138 func_callback(): _M_size(0) { };
141 size() const { return _M_size; }
143 const test_type*
144 tests() const { return _M_tests; }
146 void
147 push_back(test_type test)
149 _M_tests[_M_size] = test;
150 ++_M_size;
155 // Run select unit tests after setting global locale.
156 void
157 run_tests_wrapped_locale(const char*, const func_callback&);
159 // Run select unit tests after setting environment variables.
160 void
161 run_tests_wrapped_env(const char*, const char*, const func_callback&);
164 // For containers (23.1/3).
165 struct NonDefaultConstructible
167 NonDefaultConstructible(int) { }
170 inline bool
171 operator==(const NonDefaultConstructible& lhs,
172 const NonDefaultConstructible& rhs)
173 { return false; }
175 inline bool
176 operator<(const NonDefaultConstructible& lhs,
177 const NonDefaultConstructible& rhs)
178 { return false; }
181 // Counting.
182 struct counter
184 // Specifically and glaringly-obviously marked 'signed' so that when
185 // COUNT mistakenly goes negative, we can track the patterns of
186 // deletions more easily.
187 typedef signed int size_type;
188 static size_type count;
189 counter() { ++count; }
190 counter (const counter&) { ++count; }
191 ~counter() { --count; }
194 #define assert_count(n) VERIFY(__gnu_test::counter::count == n)
196 // A (static) class for counting copy constructors and possibly throwing an
197 // exception on a desired count.
198 class copy_constructor
200 public:
201 static unsigned int
202 count() { return count_; }
204 static void
205 mark_call()
207 count_++;
208 if (count_ == throw_on_)
209 std::__throw_runtime_error("copy_constructor::mark_call");
212 static void
213 reset()
215 count_ = 0;
216 throw_on_ = 0;
219 static void
220 throw_on(unsigned int count) { throw_on_ = count; }
222 private:
223 static unsigned int count_;
224 static unsigned int throw_on_;
227 // A (static) class for counting assignment operator calls and
228 // possibly throwing an exception on a desired count.
229 class assignment_operator
231 public:
232 static unsigned int
233 count() { return count_; }
235 static void
236 mark_call()
238 count_++;
239 if (count_ == throw_on_)
240 std::__throw_runtime_error("assignment_operator::mark_call");
243 static void
244 reset()
246 count_ = 0;
247 throw_on_ = 0;
250 static void
251 throw_on(unsigned int count) { throw_on_ = count; }
253 private:
254 static unsigned int count_;
255 static unsigned int throw_on_;
258 // A (static) class for tracking calls to an object's destructor.
259 class destructor
261 public:
262 static unsigned int
263 count() { return _M_count; }
265 static void
266 mark_call() { _M_count++; }
268 static void
269 reset() { _M_count = 0; }
271 private:
272 static unsigned int _M_count;
275 // An class of objects that can be used for validating various
276 // behaviours and guarantees of containers and algorithms defined in
277 // the standard library.
278 class copy_tracker
280 public:
281 // Creates a copy-tracking object with the given ID number. If
282 // "throw_on_copy" is set, an exception will be thrown if an
283 // attempt is made to copy this object.
284 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
285 : id_(id) , throw_on_copy_(throw_on_copy) { }
287 // Copy-constructs the object, marking a call to the copy
288 // constructor and forcing an exception if indicated.
289 copy_tracker(const copy_tracker& rhs)
290 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
292 if (throw_on_copy_)
293 copy_constructor::throw_on(copy_constructor::count() + 1);
294 copy_constructor::mark_call();
297 // Assigns the value of another object to this one, tracking the
298 // number of times this member function has been called and if the
299 // other object is supposed to throw an exception when it is
300 // copied, well, make it so.
301 copy_tracker&
302 operator=(const copy_tracker& rhs)
304 id_ = rhs.id();
305 if (rhs.throw_on_copy_)
306 assignment_operator::throw_on(assignment_operator::count() + 1);
307 assignment_operator::mark_call();
308 return *this;
311 ~copy_tracker()
312 { destructor::mark_call(); }
315 id() const { return id_; }
317 private:
318 int id_;
319 const bool throw_on_copy_;
321 public:
322 static void
323 reset()
325 copy_constructor::reset();
326 assignment_operator::reset();
327 destructor::reset();
330 // for backwards-compatibility
331 static int
332 copyCount()
333 { return copy_constructor::count(); }
335 // for backwards-compatibility
336 static int
337 dtorCount()
338 { return destructor::count(); }
340 private:
341 static int next_id_;
344 inline bool
345 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
346 { return lhs.id() == rhs.id(); }
348 // Class for checking required type conversions, implicit and
349 // explicit for given library data structures.
350 template<typename _Container>
351 struct conversion
353 typedef typename _Container::const_iterator const_iterator;
355 // Implicit conversion iterator to const_iterator.
356 static const_iterator
357 iterator_to_const_iterator()
359 _Container v;
360 const_iterator it = v.begin();
361 const_iterator end = v.end();
362 return it == end ? v.end() : it;
366 // A binary semaphore for use across multiple processes.
367 class semaphore
369 public:
370 // Creates a binary semaphore. The semaphore is initially in the
371 // unsignaled state.
372 semaphore();
374 // Destroy the semaphore.
375 ~semaphore();
377 // Signal the semaphore. If there are processes blocked in
378 // "wait", exactly one will be permitted to proceed.
379 void signal();
381 // Wait until the semaphore is signaled.
382 void wait();
384 private:
385 int sem_set_;
387 pid_t pid_;
389 } // namespace __gnu_test
391 #endif // _GLIBCXX_TESTSUITE_HOOKS_H