OSDN Git Service

2004-08-15 Paolo Carlini <pcarlini@suse.de>
[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, 2004 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_RES_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 #include <locale>
65 #include <ext/pod_char_traits.h>
66 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
67 #include <sys/stat.h>
68 #endif
69
70 #ifdef _GLIBCXX_ASSERT
71 # include <cassert>
72 # define VERIFY(fn) assert(fn)
73 #else
74 # define VERIFY(fn) test &= (fn)
75 #endif
76
77 #ifdef _GLIBCXX_HAVE_UNISTD_H
78 # include <unistd.h>
79 #else
80 # define unlink(x)
81 #endif
82
83 namespace __gnu_test
84 {
85   // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
86   // from c++config.h
87
88   // Set memory limits if possible, if not set to 0.
89 #ifndef _GLIBCXX_RES_LIMITS
90 #  define MEMLIMIT_MB 0
91 #else
92 # ifndef MEMLIMIT_MB
93 #  define MEMLIMIT_MB 16.0
94 # endif
95 #endif
96   extern void
97   set_memory_limits(float __size = MEMLIMIT_MB);
98
99   extern void
100   set_file_limit(unsigned long __size);
101
102   // Check mangled name demangles (using __cxa_demangle) as expected.
103   void
104   verify_demangle(const char* mangled, const char* wanted);
105
106   // Simple callback structure for variable numbers of tests (all with
107   // same signature).  Assume all unit tests are of the signature
108   // void test01(); 
109   class func_callback
110   {
111   public:
112     typedef void (*test_type) (void);
113
114   private:
115     int         _M_size;
116     test_type   _M_tests[15];
117
118     func_callback&
119     operator=(const func_callback&);
120
121     func_callback(const func_callback&);
122
123   public:
124     func_callback(): _M_size(0) { };
125
126     int
127     size() const { return _M_size; }
128
129     const test_type*
130     tests() const { return _M_tests; }
131
132     void
133     push_back(test_type test)
134     {
135       _M_tests[_M_size] = test;
136       ++_M_size;
137     }
138   };
139
140
141   // Run select unit tests after setting global locale.
142   void 
143   run_tests_wrapped_locale(const char*, const func_callback&);
144
145   // Run select unit tests after setting environment variables.
146   void 
147   run_tests_wrapped_env(const char*, const char*, const func_callback&);
148
149   // Try to create a locale with the given name. If it fails, bail.
150   std::locale
151   try_named_locale(const char* name);
152
153   int
154   try_mkfifo (const char* filename, mode_t mode);
155
156   // Test data types.
157   struct pod_char
158   {
159     unsigned char c;
160   };
161
162   inline bool
163   operator==(const pod_char& lhs, const pod_char& rhs)
164   { return lhs.c == rhs.c; }
165   
166   struct pod_int
167   {
168     int i;
169   };
170   
171   struct state
172   {
173     unsigned long l;
174     unsigned long l2;
175   };
176
177   typedef unsigned short                                value_type;
178   typedef unsigned int                                  int_type;
179   typedef __gnu_cxx::character<value_type, int_type>    pod_type;
180
181
182   // Counting.
183   struct counter
184   {
185     // Specifically and glaringly-obviously marked 'signed' so that when
186     // COUNT mistakenly goes negative, we can track the patterns of
187     // deletions more easily.
188     typedef  signed int     size_type;
189     static size_type   count;
190     counter() { ++count; }
191     counter (const counter&) { ++count; }
192     ~counter() { --count; }
193   };
194   
195 #define assert_count(n)   VERIFY(__gnu_test::counter::count == n)
196   
197   // A (static) class for counting copy constructors and possibly throwing an
198   // exception on a desired count.
199   class copy_constructor
200   {
201   public:
202     static unsigned int
203     count() { return count_; }
204     
205     static void
206     mark_call()
207     {
208       count_++;
209       if (count_ == throw_on_)
210         __throw_exception_again "copy constructor exception";
211     }
212       
213     static void
214     reset()
215     {
216       count_ = 0;
217       throw_on_ = 0;
218     }
219       
220     static void
221     throw_on(unsigned int count) { throw_on_ = count; }
222
223   private:
224     static unsigned int count_;
225     static unsigned int throw_on_;
226   };
227   
228   // A (static) class for counting assignment operator calls and
229   // possibly throwing an exception on a desired count.
230   class assignment_operator
231   {
232   public:
233     static unsigned int
234     count() { return count_; }
235     
236     static void
237     mark_call()
238     {
239       count_++;
240       if (count_ == throw_on_)
241         __throw_exception_again "assignment operator exception";
242     }
243
244     static void
245     reset()
246     {
247       count_ = 0;
248       throw_on_ = 0;
249     }
250
251     static void
252     throw_on(unsigned int count) { throw_on_ = count; }
253
254   private:
255     static unsigned int count_;
256     static unsigned int throw_on_;
257   };
258   
259   // A (static) class for tracking calls to an object's destructor.
260   class destructor
261   {
262   public:
263     static unsigned int
264     count() { return _M_count; }
265     
266     static void
267     mark_call() { _M_count++; }
268
269     static void
270     reset() { _M_count = 0; }
271
272   private:
273     static unsigned int _M_count;
274   };
275   
276   // An class of objects that can be used for validating various
277   // behaviours and guarantees of containers and algorithms defined in
278   // the standard library.
279   class copy_tracker
280   {
281   public:
282     // Creates a copy-tracking object with the given ID number.  If
283     // "throw_on_copy" is set, an exception will be thrown if an
284     // attempt is made to copy this object.
285     copy_tracker(int id = next_id_--, bool throw_on_copy = false)
286     : id_(id) , throw_on_copy_(throw_on_copy) { }
287
288     // Copy-constructs the object, marking a call to the copy
289     // constructor and forcing an exception if indicated.
290     copy_tracker(const copy_tracker& rhs)
291     : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
292     {
293       if (throw_on_copy_)
294         copy_constructor::throw_on(copy_constructor::count() + 1);
295       copy_constructor::mark_call();
296     }
297
298     // Assigns the value of another object to this one, tracking the
299     // number of times this member function has been called and if the
300     // other object is supposed to throw an exception when it is
301     // copied, well, make it so.
302     copy_tracker&
303     operator=(const copy_tracker& rhs)
304     { 
305       id_ = rhs.id();
306       if (rhs.throw_on_copy_)
307         assignment_operator::throw_on(assignment_operator::count() + 1);
308       assignment_operator::mark_call();
309       return *this;
310     }
311
312     ~copy_tracker()
313     { destructor::mark_call(); }
314
315     int
316     id() const { return id_; }
317
318   private:
319     int   id_;
320     const bool  throw_on_copy_;
321
322   public:
323     static void
324     reset()
325     {
326       copy_constructor::reset();
327       assignment_operator::reset();
328       destructor::reset();
329     }
330
331     // for backwards-compatibility
332     static int
333     copyCount() 
334     { return copy_constructor::count(); }
335
336     // for backwards-compatibility
337     static int
338     dtorCount() 
339     { return destructor::count(); }
340
341   private:
342     static int next_id_;
343   };
344
345   inline bool
346   operator==(const copy_tracker& lhs, const copy_tracker& rhs)
347   { return lhs.id() == rhs.id(); }
348
349   // Class for checking required type conversions, implicit and
350   // explicit for given library data structures. 
351   template<typename _Container>
352     struct conversion
353     {
354       typedef typename _Container::const_iterator const_iterator;
355       
356       // Implicit conversion iterator to const_iterator.
357       static const_iterator
358       iterator_to_const_iterator()
359       {
360         _Container v;
361         const_iterator it = v.begin();
362         const_iterator end = v.end();
363         return it == end ? v.end() : it;
364       }
365     };
366 } // namespace __gnu_test
367
368 namespace std
369 {
370   template<class _CharT>
371     struct char_traits;
372
373   // char_traits specialization
374   template<>
375     struct char_traits<__gnu_test::pod_char>
376     {
377       typedef __gnu_test::pod_char      char_type;
378       typedef __gnu_test::pod_int       int_type;
379       typedef __gnu_test::state         state_type;
380       typedef fpos<state_type>          pos_type;
381       typedef streamoff                 off_type;
382       
383       static void 
384       assign(char_type& c1, const char_type& c2)
385       { c1.c = c2.c; }
386
387       static bool 
388       eq(const char_type& c1, const char_type& c2)
389       { return c1.c == c2.c; }
390
391       static bool 
392       lt(const char_type& c1, const char_type& c2)
393       { return c1.c < c2.c; }
394
395       static int 
396       compare(const char_type* s1, const char_type* s2, size_t n)
397       { return memcmp(s1, s2, n); }
398
399       static size_t
400       length(const char_type* s)
401       { return strlen(reinterpret_cast<const char*>(s)); }
402
403       static const char_type* 
404       find(const char_type* s, size_t n, const char_type& a)
405       { return static_cast<const char_type*>(memchr(s, a.c, n)); }
406
407       static char_type* 
408       move(char_type* s1, const char_type* s2, size_t n)
409       {
410         memmove(s1, s2, n);
411         return s1;
412       }
413
414       static char_type* 
415       copy(char_type* s1, const char_type* s2, size_t n)
416       {
417         memcpy(s1, s2, n);
418         return s1;
419       }
420
421       static char_type* 
422       assign(char_type* s, size_t n, char_type a)
423       {
424         memset(s, a.c, n);
425         return s;
426       }
427
428       static char_type 
429       to_char_type(const int_type& c)
430       {
431         char_type ret;
432         ret.c = static_cast<unsigned char>(c.i);
433         return ret;
434       }
435
436       static int_type 
437       to_int_type(const char_type& c)
438       {
439         int_type ret;
440         ret.i = c.c;
441         return ret;
442       }
443
444       static bool 
445       eq_int_type(const int_type& c1, const int_type& c2)
446       { return c1.i == c2.i; }
447
448       static int_type 
449       eof()
450       {
451         int_type n;
452         n.i = -10;
453         return n;
454       }
455
456       static int_type 
457       not_eof(const int_type& c)
458       {
459         if (eq_int_type(c, eof()))
460           return int_type();
461         return c;
462       }
463     };
464 } // namespace std
465
466 #endif // _GLIBCXX_TESTSUITE_HOOKS_H
467