OSDN Git Service

2003-07-24 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
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.
11 //
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.
16 //
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.
21 //
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.
30
31 // This file provides the following:
32 //
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.
37 //
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_MEM_LIMITS is
43 //   not #defined before including this header, then no limiting is attempted.
44 //
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.
50 //
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.
54 //
55 // 5) pod_char, pod_int, , abstract character classes and
56 //   char_traits specializations for testing instantiations.
57
58 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
59 #define _GLIBCXX_TESTSUITE_HOOKS_H
60
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>
71 #include <locale>
72 #ifdef _GLIBCXX_HAVE_UNISTD_H
73 # include <unistd.h>
74 #else
75 # define unlink(x)
76 #endif
77
78 namespace __gnu_test
79 {
80   // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
81   // from c++config.h
82
83   // Set memory limits if possible, if not set to 0.
84 #ifndef _GLIBCXX_MEM_LIMITS
85 #  define MEMLIMIT_MB 0
86 #else
87 # ifndef MEMLIMIT_MB
88 #  define MEMLIMIT_MB 16.0
89 # endif
90 #endif
91   extern void
92   set_memory_limits(float __size = MEMLIMIT_MB);
93
94
95   // Check mangled name demangles (using __cxa_demangle) as expected.
96   void
97   verify_demangle(const char* mangled, const char* wanted);
98
99
100   // Simple callback structure for variable numbers of tests (all with
101   // same signature).  Assume all unit tests are of the signature
102   // void test01(); 
103   typedef void (*test_func) (void);
104   typedef std::list<test_func> func_callback;
105
106   // Run select unit tests after setting global locale.
107   void 
108   run_tests_wrapped_locale(const char*, const func_callback&);
109
110   // Run select unit tests after setting environment variables.
111   void 
112   run_tests_wrapped_env(const char*, const char*, const func_callback&);
113
114   // Try to create a locale with the given name. If it fails, bail.
115   std::locale
116   try_named_locale(const char* name);
117
118
119   // Test data types.
120   struct pod_char
121   {
122     unsigned char c;
123   };
124   
125   struct pod_int
126   {
127     int i;
128   };
129   
130   struct state
131   {
132     unsigned long l;
133     unsigned long l2;
134   };
135
136
137   // Counting.
138   struct counter
139   {
140     // Specifically and glaringly-obviously marked 'signed' so that when
141     // COUNT mistakenly goes negative, we can track the patterns of
142     // deletions more easily.
143     typedef  signed int     size_type;
144     static size_type   count;
145     counter() { ++count; }
146     counter (const counter&) { ++count; }
147     ~counter() { --count; }
148   };
149   
150 #define assert_count(n)   VERIFY(__gnu_test::counter::count == n)
151   
152   // A (static) class for counting copy constructors and possibly throwing an
153   // exception on a desired count.
154   class copy_constructor
155   {
156   public:
157     static unsigned int
158     count() { return count_; }
159     
160     static void
161     mark_call()
162     {
163       count_++;
164       if (count_ == throw_on_)
165         __throw_exception_again "copy constructor exception";
166     }
167       
168     static void
169     reset()
170     {
171       count_ = 0;
172       throw_on_ = 0;
173     }
174       
175     static void
176     throw_on(unsigned int count) { throw_on_ = count; }
177
178   private:
179     static unsigned int count_;
180     static unsigned int throw_on_;
181   };
182   
183   // A (static) class for counting assignment operator calls and
184   // possibly throwing an exception on a desired count.
185   class assignment_operator
186   {
187   public:
188     static unsigned int
189     count() { return count_; }
190     
191     static void
192     mark_call()
193     {
194       count_++;
195       if (count_ == throw_on_)
196         __throw_exception_again "assignment operator exception";
197     }
198
199     static void
200     reset()
201     {
202       count_ = 0;
203       throw_on_ = 0;
204     }
205
206     static void
207     throw_on(unsigned int count) { throw_on_ = count; }
208
209   private:
210     static unsigned int count_;
211     static unsigned int throw_on_;
212   };
213   
214   // A (static) class for tracking calls to an object's destructor.
215   class destructor
216   {
217   public:
218     static unsigned int
219     count() { return _M_count; }
220     
221     static void
222     mark_call() { _M_count++; }
223
224     static void
225     reset() { _M_count = 0; }
226
227   private:
228     static unsigned int _M_count;
229   };
230   
231   // An class of objects that can be used for validating various
232   // behaviours and guarantees of containers and algorithms defined in
233   // the standard library.
234   class copy_tracker
235   {
236   public:
237     // Creates a copy-tracking object with the given ID number.  If
238     // "throw_on_copy" is set, an exception will be thrown if an
239     // attempt is made to copy this object.
240     copy_tracker(int id = next_id_--, bool throw_on_copy = false)
241     : id_(id) , throw_on_copy_(throw_on_copy) { }
242
243     // Copy-constructs the object, marking a call to the copy
244     // constructor and forcing an exception if indicated.
245     copy_tracker(const copy_tracker& rhs)
246     : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
247     {
248       if (throw_on_copy_)
249         copy_constructor::throw_on(copy_constructor::count() + 1);
250       copy_constructor::mark_call();
251     }
252
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)
259     { 
260       id_ = rhs.id();
261       if (rhs.throw_on_copy_)
262         assignment_operator::throw_on(assignment_operator::count() + 1);
263       assignment_operator::mark_call();
264       return *this;
265     }
266
267     ~copy_tracker()
268     { destructor::mark_call(); }
269
270     int
271     id() const { return id_; }
272
273   private:
274     int   id_;
275     const bool  throw_on_copy_;
276
277   public:
278     static void
279     reset()
280     {
281       copy_constructor::reset();
282       assignment_operator::reset();
283       destructor::reset();
284     }
285
286     // for backwards-compatibility
287     static int
288     copyCount() 
289     { return copy_constructor::count(); }
290
291     // for backwards-compatibility
292     static int
293     dtorCount() 
294     { return destructor::count(); }
295
296   private:
297     static int next_id_;
298   };
299
300   inline bool
301   operator==(const copy_tracker& lhs, const copy_tracker& rhs)
302   { return lhs.id() == rhs.id(); }
303 } // namespace __gnu_test
304
305 namespace std
306 {
307   template<class _CharT>
308     struct char_traits;
309
310   // char_traits specialization
311   template<>
312     struct char_traits<__gnu_test::pod_char>
313     {
314       typedef __gnu_test::pod_char      char_type;
315       typedef __gnu_test::pod_int       int_type;
316       typedef long                      pos_type;
317       typedef unsigned long             off_type;
318       typedef __gnu_test::state         state_type;
319       
320       static void 
321       assign(char_type& __c1, const char_type& __c2);
322
323       static bool 
324       eq(const char_type& __c1, const char_type& __c2);
325
326       static bool 
327       lt(const char_type& __c1, const char_type& __c2);
328
329       static int 
330       compare(const char_type* __s1, const char_type* __s2, size_t __n);
331
332       static size_t
333       length(const char_type* __s);
334
335       static const char_type* 
336       find(const char_type* __s, size_t __n, const char_type& __a);
337
338       static char_type* 
339       move(char_type* __s1, const char_type* __s2, size_t __n);
340
341       static char_type* 
342       copy(char_type* __s1, const char_type* __s2, size_t __n);
343
344       static char_type* 
345       assign(char_type* __s, size_t __n, char_type __a);
346
347       static char_type 
348       to_char_type(const int_type& __c);
349
350       static int_type 
351       to_int_type(const char_type& __c);
352
353       static bool 
354       eq_int_type(const int_type& __c1, const int_type& __c2);
355
356       static int_type 
357       eof();
358
359       static int_type 
360       not_eof(const int_type& __c);
361     };
362 } // namespace std
363
364 #endif // _GLIBCXX_TESTSUITE_HOOKS_H
365