OSDN Git Service

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