OSDN Git Service

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