* java/util/Properties.java (load): Only skip line if the first
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
blobd13e86c75186e8693abc24d757202c83184e11fa
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 DEBUG_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 _GLIBCPP_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 _GLIBCPP_TESTSUITE_HOOKS_H
59 #define _GLIBCPP_TESTSUITE_HOOKS_H
61 #include <bits/c++config.h>
62 #include <bits/functexcept.h>
63 #include <cstddef>
64 #ifdef DEBUG_ASSERT
65 # include <cassert>
66 # define VERIFY(fn) assert(fn)
67 #else
68 # define VERIFY(fn) test &= (fn)
69 #endif
70 #include <list>
72 namespace __gnu_cxx_test
74 // All macros are defined in GLIBCPP_CONFIGURE_TESTSUITE and imported
75 // from c++config.h
77 // Set memory limits if possible, if not set to 0.
78 #ifndef _GLIBCPP_MEM_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);
89 // Check mangled name demangles (using __cxa_demangle) as expected.
90 void
91 verify_demangle(const char* mangled, const char* wanted);
94 // Simple callback structure for variable numbers of tests (all with
95 // same signature). Assume all unit tests are of the signature
96 // void test01();
97 typedef void (*test_func) (void);
98 typedef std::list<test_func> func_callback;
100 // Run select unit tests after setting global locale.
101 void
102 run_tests_wrapped_locale(const char*, const func_callback&);
104 // Run select unit tests after setting environment variables.
105 void
106 run_tests_wrapped_env(const char*, const char*, const func_callback&);
109 // Test data types.
110 struct pod_char
112 unsigned char c;
115 struct pod_int
117 int i;
120 struct pod_unsigned_int
122 unsigned int i;
125 struct pod_long
127 unsigned long i;
130 struct state
132 unsigned long l;
133 unsigned long l2;
136 // Counting.
137 struct counter
139 // Specifically and glaringly-obviously marked 'signed' so that when
140 // COUNT mistakenly goes negative, we can track the patterns of
141 // deletions more easily.
142 typedef signed int size_type;
143 static size_type count;
144 counter() { ++count; }
145 counter (const counter&) { ++count; }
146 ~counter() { --count; }
149 #define assert_count(n) VERIFY(__gnu_cxx_test::counter::count == n)
151 // A (static) class for counting copy constructors and possibly throwing an
152 // exception on a desired count.
153 class copy_constructor
155 public:
156 static unsigned int
157 count() { return count_; }
159 static void
160 mark_call()
162 count_++;
163 if (count_ == throw_on_)
164 __throw_exception_again "copy constructor exception";
167 static void
168 reset()
170 count_ = 0;
171 throw_on_ = 0;
174 static void
175 throw_on(unsigned int count) { throw_on_ = count; }
177 private:
178 static unsigned int count_;
179 static unsigned int throw_on_;
182 // A (static) class for counting assignment operator calls and
183 // possibly throwing an exception on a desired count.
184 class assignment_operator
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 "assignment operator 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 tracking calls to an object's destructor.
214 class destructor
216 public:
217 static unsigned int
218 count() { return _M_count; }
220 static void
221 mark_call() { _M_count++; }
223 static void
224 reset() { _M_count = 0; }
226 private:
227 static unsigned int _M_count;
230 // An class of objects that can be used for validating various
231 // behaviours and guarantees of containers and algorithms defined in
232 // the standard library.
233 class copy_tracker
235 public:
236 // Creates a copy-tracking object with the given ID number. If
237 // "throw_on_copy" is set, an exception will be thrown if an
238 // attempt is made to copy this object.
239 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
240 : id_(id) , throw_on_copy_(throw_on_copy) { }
242 // Copy-constructs the object, marking a call to the copy
243 // constructor and forcing an exception if indicated.
244 copy_tracker(const copy_tracker& rhs)
245 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
247 int kkk = throw_on_copy_;
248 if (throw_on_copy_)
249 copy_constructor::throw_on(copy_constructor::count() + 1);
250 copy_constructor::mark_call();
253 // Assigns the value of another object to this one, tracking the
254 // number of times this member function has been called and if the
255 // other object is supposed to throw an exception when it is
256 // copied, well, make it so.
257 copy_tracker&
258 operator=(const copy_tracker& rhs)
260 id_ = rhs.id();
261 if (rhs.throw_on_copy_)
262 assignment_operator::throw_on(assignment_operator::count() + 1);
263 assignment_operator::mark_call();
266 ~copy_tracker()
267 { destructor::mark_call(); }
270 id() const { return id_; }
272 private:
273 int id_;
274 const bool throw_on_copy_;
276 public:
277 static void
278 reset()
280 copy_constructor::reset();
281 assignment_operator::reset();
282 destructor::reset();
285 // for backwards-compatibility
286 static int
287 copyCount()
288 { return copy_constructor::count(); }
290 // for backwards-compatibility
291 static int
292 dtorCount()
293 { return destructor::count(); }
295 private:
296 static int next_id_;
299 inline bool
300 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
301 { return lhs.id() == rhs.id(); }
302 }; // namespace __gnu_cxx_test
304 namespace std
306 template<class _CharT>
307 struct char_traits;
309 // char_traits specialization
310 template<>
311 struct char_traits<__gnu_cxx_test::pod_char>
313 typedef __gnu_cxx_test::pod_char char_type;
314 typedef __gnu_cxx_test::pod_int int_type;
315 typedef long pos_type;
316 typedef unsigned long off_type;
317 typedef __gnu_cxx_test::state state_type;
319 static void
320 assign(char_type& __c1, const char_type& __c2);
322 static bool
323 eq(const char_type& __c1, const char_type& __c2);
325 static bool
326 lt(const char_type& __c1, const char_type& __c2);
328 static int
329 compare(const char_type* __s1, const char_type* __s2, size_t __n);
331 static size_t
332 length(const char_type* __s);
334 static const char_type*
335 find(const char_type* __s, size_t __n, const char_type& __a);
337 static char_type*
338 move(char_type* __s1, const char_type* __s2, size_t __n);
340 static char_type*
341 copy(char_type* __s1, const char_type* __s2, size_t __n);
343 static char_type*
344 assign(char_type* __s, size_t __n, char_type __a);
346 static char_type
347 to_char_type(const int_type& __c);
349 static int_type
350 to_int_type(const char_type& __c);
352 static bool
353 eq_int_type(const int_type& __c1, const int_type& __c2);
355 static int_type
356 eof();
358 static int_type
359 not_eof(const int_type& __c);
361 } // namespace std
363 #endif // _GLIBCPP_TESTSUITE_HOOKS_H