OSDN Git Service

2008-04-10 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.h
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite. 
3 //
4 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING.  If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22 //
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction.  Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License.  This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 // This file provides the following:
33 //
34 // 1)  VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
35 //   This file is included in the various testsuite programs to provide
36 //   #define(able) assert() behavior for debugging/testing. It may be
37 //   a suitable location for other furry woodland creatures as well.
38 //
39 // 2)  set_memory_limits()
40 //   set_memory_limits() uses setrlimit() to restrict dynamic memory
41 //   allocation.  We provide a default memory limit if none is passed by the
42 //   calling application.  The argument to set_memory_limits() is the
43 //   limit in megabytes (a floating-point number).  If _GLIBCXX_RES_LIMITS is
44 //   not #defined before including this header, then no limiting is attempted.
45 //
46 // 3)  counter
47 //   This is a POD with a static data member, gnu_counting_struct::count,
48 //   which starts at zero, increments on instance construction, and decrements
49 //   on instance destruction.  "assert_count(n)" can be called to VERIFY()
50 //   that the count equals N.
51 //
52 // 4)  copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
53 //   A class with nontrivial ctor/dtor that provides the ability to track the
54 //   number of copy ctors and dtors, and will throw on demand during copy.
55
56 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
57 #define _GLIBCXX_TESTSUITE_HOOKS_H
58
59 #include <bits/c++config.h>
60 #include <bits/functexcept.h>
61 #include <ctime>
62
63 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
64 #include <sys/stat.h>
65 #endif
66
67 #ifdef _GLIBCXX_ASSERT
68 # include <cassert>
69 # define VERIFY(fn) assert(fn)
70 #else
71 # define VERIFY(fn) test &= (fn)
72 #endif
73
74 #ifdef _GLIBCXX_HAVE_UNISTD_H
75 # include <unistd.h>
76 #else
77 # define unlink(x)
78 #endif
79
80 namespace __gnu_test
81 {
82   // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
83   // from c++config.h
84
85   // Set memory limits if possible, if not set to 0.
86 #ifndef _GLIBCXX_RES_LIMITS
87 #  define MEMLIMIT_MB 0
88 #else
89 # ifndef MEMLIMIT_MB
90 #  define MEMLIMIT_MB 16.0
91 # endif
92 #endif
93   extern void
94   set_memory_limits(float __size = MEMLIMIT_MB);
95
96   extern void
97   set_file_limit(unsigned long __size);
98
99   // Check mangled name demangles (using __cxa_demangle) as expected.
100   void
101   verify_demangle(const char* mangled, const char* wanted);
102
103   // 17.3.2.1.2 - Bitmask types [lib.bitmask.types]
104   // bitmask_operators
105   template<typename bitmask_type>
106     void
107     bitmask_operators(bitmask_type a = bitmask_type(),
108                       bitmask_type b = bitmask_type())
109     {
110       a | b;
111       a & b;
112       a ^ b;
113       ~b;
114       a |= b; // set
115       a &= ~b; // clear
116       a ^= b;
117     }
118
119   // Simple callback structure for variable numbers of tests (all with
120   // same signature).  Assume all unit tests are of the signature
121   // void test01(); 
122   class func_callback
123   {
124   public:
125     typedef void (*test_type) (void);
126
127   private:
128     int         _M_size;
129     test_type   _M_tests[15];
130
131     func_callback&
132     operator=(const func_callback&);
133
134     func_callback(const func_callback&);
135
136   public:
137     func_callback(): _M_size(0) { }
138
139     int
140     size() const { return _M_size; }
141
142     const test_type*
143     tests() const { return _M_tests; }
144
145     void
146     push_back(test_type test)
147     {
148       _M_tests[_M_size] = test;
149       ++_M_size;
150     }
151   };
152
153
154   // Run select unit tests after setting global locale.
155   void 
156   run_tests_wrapped_locale(const char*, const func_callback&);
157
158   // Run select unit tests after setting environment variables.
159   void 
160   run_tests_wrapped_env(const char*, const char*, const func_callback&);
161
162   // Counting.
163   struct counter
164   {
165     // Specifically and glaringly-obviously marked 'signed' so that when
166     // COUNT mistakenly goes negative, we can track the patterns of
167     // deletions more easily.
168     typedef  signed int     size_type;
169     static size_type   count;
170     counter() { ++count; }
171     counter (const counter&) { ++count; }
172     ~counter() { --count; }
173   };
174   
175 #define assert_count(n)   VERIFY(__gnu_test::counter::count == n)
176   
177   // A (static) class for counting copy constructors and possibly throwing an
178   // exception on a desired count.
179   class copy_constructor
180   {
181   public:
182     static unsigned int
183     count() { return count_; }
184     
185     static void
186     mark_call()
187     {
188       count_++;
189       if (count_ == throw_on_)
190         std::__throw_runtime_error("copy_constructor::mark_call");
191     }
192       
193     static void
194     reset()
195     {
196       count_ = 0;
197       throw_on_ = 0;
198     }
199       
200     static void
201     throw_on(unsigned int count) { throw_on_ = count; }
202
203   private:
204     static unsigned int count_;
205     static unsigned int throw_on_;
206   };
207   
208   // A (static) class for counting assignment operator calls and
209   // possibly throwing an exception on a desired count.
210   class assignment_operator
211   {
212   public:
213     static unsigned int
214     count() { return count_; }
215     
216     static void
217     mark_call()
218     {
219       count_++;
220       if (count_ == throw_on_)
221         std::__throw_runtime_error("assignment_operator::mark_call");
222     }
223
224     static void
225     reset()
226     {
227       count_ = 0;
228       throw_on_ = 0;
229     }
230
231     static void
232     throw_on(unsigned int count) { throw_on_ = count; }
233
234   private:
235     static unsigned int count_;
236     static unsigned int throw_on_;
237   };
238   
239   // A (static) class for tracking calls to an object's destructor.
240   class destructor
241   {
242   public:
243     static unsigned int
244     count() { return _M_count; }
245     
246     static void
247     mark_call() { _M_count++; }
248
249     static void
250     reset() { _M_count = 0; }
251
252   private:
253     static unsigned int _M_count;
254   };
255   
256   // An class of objects that can be used for validating various
257   // behaviours and guarantees of containers and algorithms defined in
258   // the standard library.
259   class copy_tracker
260   {
261   public:
262     // Creates a copy-tracking object with the given ID number.  If
263     // "throw_on_copy" is set, an exception will be thrown if an
264     // attempt is made to copy this object.
265     copy_tracker(int id = next_id_--, bool throw_on_copy = false)
266     : id_(id) , throw_on_copy_(throw_on_copy) { }
267
268     // Copy-constructs the object, marking a call to the copy
269     // constructor and forcing an exception if indicated.
270     copy_tracker(const copy_tracker& rhs)
271     : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
272     {
273       if (throw_on_copy_)
274         copy_constructor::throw_on(copy_constructor::count() + 1);
275       copy_constructor::mark_call();
276     }
277
278     // Assigns the value of another object to this one, tracking the
279     // number of times this member function has been called and if the
280     // other object is supposed to throw an exception when it is
281     // copied, well, make it so.
282     copy_tracker&
283     operator=(const copy_tracker& rhs)
284     { 
285       id_ = rhs.id();
286       if (rhs.throw_on_copy_)
287         assignment_operator::throw_on(assignment_operator::count() + 1);
288       assignment_operator::mark_call();
289       return *this;
290     }
291
292     ~copy_tracker()
293     { destructor::mark_call(); }
294
295     int
296     id() const { return id_; }
297
298   private:
299     int   id_;
300     const bool  throw_on_copy_;
301
302   public:
303     static void
304     reset()
305     {
306       copy_constructor::reset();
307       assignment_operator::reset();
308       destructor::reset();
309     }
310
311     // for backwards-compatibility
312     static int
313     copyCount() 
314     { return copy_constructor::count(); }
315
316     // for backwards-compatibility
317     static int
318     dtorCount() 
319     { return destructor::count(); }
320
321   private:
322     static int next_id_;
323   };
324
325   inline bool
326   operator==(const copy_tracker& lhs, const copy_tracker& rhs)
327   { return lhs.id() == rhs.id(); }
328
329   // Class for checking required type conversions, implicit and
330   // explicit for given library data structures. 
331   template<typename _Container>
332     struct conversion
333     {
334       typedef typename _Container::const_iterator const_iterator;
335       
336       // Implicit conversion iterator to const_iterator.
337       static const_iterator
338       iterator_to_const_iterator()
339       {
340         _Container v;
341         const_iterator it = v.begin();
342         const_iterator end = v.end();
343         return it == end ? v.end() : it;
344       }
345     };
346
347   // A binary semaphore for use across multiple processes.
348   class semaphore 
349   {
350   public:
351     // Creates a binary semaphore.  The semaphore is initially in the
352     // unsignaled state. 
353     semaphore();
354
355     // Destroy the semaphore.
356     ~semaphore();
357
358     // Signal the semaphore.  If there are processes blocked in
359     // "wait", exactly one will be permitted to proceed.
360     void signal();
361
362     // Wait until the semaphore is signaled.
363     void wait();
364
365   private:
366     int sem_set_;
367
368     pid_t pid_;
369   };
370
371   // For use in 22_locale/time_get and time_put.
372   std::tm test_tm(int sec, int min, int hour, int mday, int mon,
373                   int year, int wday, int yday, int isdst);
374
375 } // namespace __gnu_test
376
377 #endif // _GLIBCXX_TESTSUITE_HOOKS_H
378