OSDN Git Service

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