class.c (check_bases): Propagate non-literality.
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.h
blobfe41b1cb3e6e77de2743fe0d22b5334814406f7d
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 // 2009, 2010
6 // Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
24 // This file provides the following:
26 // 1) VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
27 // This file is included in the various testsuite programs to provide
28 // #define(able) assert() behavior for debugging/testing. It may be
29 // a suitable location for other furry woodland creatures as well.
31 // 2) set_memory_limits()
32 // set_memory_limits() uses setrlimit() to restrict dynamic memory
33 // allocation. We provide a default memory limit if none is passed by the
34 // calling application. The argument to set_memory_limits() is the
35 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
36 // not #defined before including this header, then no limiting is attempted.
38 // 3) object_counter
39 // This is a POD with a static data member, object_counter::count,
40 // which starts at zero, increments on instance construction, and decrements
41 // on instance destruction. "assert_count(n)" can be called to VERIFY()
42 // that the count equals N.
44 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
45 // A class with nontrivial ctor/dtor that provides the ability to track the
46 // number of copy ctors and dtors, and will throw on demand during copy.
48 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
49 #define _GLIBCXX_TESTSUITE_HOOKS_H
51 #include <bits/c++config.h>
52 #include <bits/functexcept.h>
53 #include <ctime>
55 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
56 #include <sys/stat.h>
57 #endif
59 #ifdef _GLIBCXX_ASSERT
60 # include <cassert>
61 # define VERIFY(fn) assert(fn)
62 #else
63 # define VERIFY(fn) test &= bool(fn)
64 #endif
66 #ifdef _GLIBCXX_HAVE_UNISTD_H
67 # include <unistd.h>
68 #else
69 # define unlink(x)
70 #endif
72 namespace __gnu_test
74 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
75 // from c++config.h
77 // Set memory limits if possible, if not set to 0.
78 #ifndef _GLIBCXX_RES_LIMITS
79 # define MEMLIMIT_MB 0
80 #else
81 # ifndef MEMLIMIT_MB
82 # define MEMLIMIT_MB 16.0
83 # endif
84 #endif
85 extern void
86 set_memory_limits(float __size = MEMLIMIT_MB);
88 extern void
89 set_file_limit(unsigned long __size);
91 // Check mangled name demangles (using __cxa_demangle) as expected.
92 void
93 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 class func_callback
100 public:
101 typedef void (*test_type) (void);
103 private:
104 int _M_size;
105 test_type _M_tests[15];
107 func_callback&
108 operator=(const func_callback&);
110 func_callback(const func_callback&);
112 public:
113 func_callback(): _M_size(0) { }
116 size() const { return _M_size; }
118 const test_type*
119 tests() const { return _M_tests; }
121 void
122 push_back(test_type test)
124 _M_tests[_M_size] = test;
125 ++_M_size;
130 // Run select unit tests after setting global locale.
131 void
132 run_tests_wrapped_locale(const char*, const func_callback&);
134 // Run select unit tests after setting environment variables.
135 void
136 run_tests_wrapped_env(const char*, const char*, const func_callback&);
138 // Counting.
139 struct object_counter
141 // Specifically and glaringly-obviously marked 'signed' so that
142 // when COUNT mistakenly goes negative, we can track the patterns
143 // of deletions more easily.
144 typedef signed int size_type;
145 static size_type count;
146 object_counter() { ++count; }
147 object_counter (const object_counter&) { ++count; }
148 ~object_counter() { --count; }
151 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
153 // A (static) class for counting copy constructors and possibly throwing an
154 // exception on a desired count.
155 class copy_constructor
157 public:
158 static unsigned int
159 count() { return count_; }
161 static void
162 mark_call()
164 count_++;
165 if (count_ == throw_on_)
166 std::__throw_runtime_error("copy_constructor::mark_call");
169 static void
170 reset()
172 count_ = 0;
173 throw_on_ = 0;
176 static void
177 throw_on(unsigned int count) { throw_on_ = count; }
179 private:
180 static unsigned int count_;
181 static unsigned int throw_on_;
184 // A (static) class for counting assignment operator calls and
185 // possibly throwing an exception on a desired count.
186 class assignment_operator
188 public:
189 static unsigned int
190 count() { return count_; }
192 static void
193 mark_call()
195 count_++;
196 if (count_ == throw_on_)
197 std::__throw_runtime_error("assignment_operator::mark_call");
200 static void
201 reset()
203 count_ = 0;
204 throw_on_ = 0;
207 static void
208 throw_on(unsigned int count) { throw_on_ = count; }
210 private:
211 static unsigned int count_;
212 static unsigned int throw_on_;
215 // A (static) class for tracking calls to an object's destructor.
216 class destructor
218 public:
219 static unsigned int
220 count() { return _M_count; }
222 static void
223 mark_call() { _M_count++; }
225 static void
226 reset() { _M_count = 0; }
228 private:
229 static unsigned int _M_count;
232 // An class of objects that can be used for validating various
233 // behaviours and guarantees of containers and algorithms defined in
234 // the standard library.
235 class copy_tracker
237 public:
238 // Creates a copy-tracking object with the given ID number. If
239 // "throw_on_copy" is set, an exception will be thrown if an
240 // attempt is made to copy this object.
241 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
242 : id_(id) , throw_on_copy_(throw_on_copy) { }
244 // Copy-constructs the object, marking a call to the copy
245 // constructor and forcing an exception if indicated.
246 copy_tracker(const copy_tracker& rhs)
247 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
249 if (throw_on_copy_)
250 copy_constructor::throw_on(copy_constructor::count() + 1);
251 copy_constructor::mark_call();
254 // Assigns the value of another object to this one, tracking the
255 // number of times this member function has been called and if the
256 // other object is supposed to throw an exception when it is
257 // copied, well, make it so.
258 copy_tracker&
259 operator=(const copy_tracker& rhs)
261 id_ = rhs.id();
262 if (rhs.throw_on_copy_)
263 assignment_operator::throw_on(assignment_operator::count() + 1);
264 assignment_operator::mark_call();
265 return *this;
268 ~copy_tracker()
269 { destructor::mark_call(); }
272 id() const { return id_; }
274 static void
275 reset()
277 copy_constructor::reset();
278 assignment_operator::reset();
279 destructor::reset();
282 private:
283 int id_;
284 const bool throw_on_copy_;
285 static int next_id_;
288 inline bool
289 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
290 { return lhs.id() == rhs.id(); }
292 inline bool
293 operator<(const copy_tracker& lhs, const copy_tracker& rhs)
294 { return lhs.id() < rhs.id(); }
296 // Class for checking required type conversions, implicit and
297 // explicit for given library data structures.
298 template<typename _Container>
299 struct conversion
301 typedef typename _Container::const_iterator const_iterator;
303 // Implicit conversion iterator to const_iterator.
304 static const_iterator
305 iterator_to_const_iterator()
307 _Container v;
308 const_iterator it = v.begin();
309 const_iterator end = v.end();
310 return it == end ? v.end() : it;
314 // A binary semaphore for use across multiple processes.
315 class semaphore
317 public:
318 // Creates a binary semaphore. The semaphore is initially in the
319 // unsignaled state.
320 semaphore();
322 // Destroy the semaphore.
323 ~semaphore();
325 // Signal the semaphore. If there are processes blocked in
326 // "wait", exactly one will be permitted to proceed.
327 void signal();
329 // Wait until the semaphore is signaled.
330 void wait();
332 private:
333 int sem_set_;
335 pid_t pid_;
338 // For use in 22_locale/time_get and time_put.
339 std::tm test_tm(int sec, int min, int hour, int mday, int mon,
340 int year, int wday, int yday, int isdst);
342 } // namespace __gnu_test
344 #endif // _GLIBCXX_TESTSUITE_HOOKS_H