OSDN Git Service

2010-03-02 Paolo Carlini <paolo.carlini@oracle.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, 2008, 2009
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 3, 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 COPYING3.  If not see
20 // <http://www.gnu.org/licenses/>.
21 //
22
23 // This file provides the following:
24 //
25 // 1)  VERIFY(), via _GLIBCXX_ASSERT, from Brent Verner <brent@rcfile.org>.
26 //   This file is included in the various testsuite programs to provide
27 //   #define(able) assert() behavior for debugging/testing. It may be
28 //   a suitable location for other furry woodland creatures as well.
29 //
30 // 2)  set_memory_limits()
31 //   set_memory_limits() uses setrlimit() to restrict dynamic memory
32 //   allocation.  We provide a default memory limit if none is passed by the
33 //   calling application.  The argument to set_memory_limits() is the
34 //   limit in megabytes (a floating-point number).  If _GLIBCXX_RES_LIMITS is
35 //   not #defined before including this header, then no limiting is attempted.
36 //
37 // 3)  object_counter
38 //   This is a POD with a static data member, object_counter::count,
39 //   which starts at zero, increments on instance construction, and decrements
40 //   on instance destruction.  "assert_count(n)" can be called to VERIFY()
41 //   that the count equals N.
42 //
43 // 4)  copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
44 //   A class with nontrivial ctor/dtor that provides the ability to track the
45 //   number of copy ctors and dtors, and will throw on demand during copy.
46
47 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
48 #define _GLIBCXX_TESTSUITE_HOOKS_H
49
50 #include <bits/c++config.h>
51 #include <bits/functexcept.h>
52 #include <ctime>
53
54 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
55 #include <sys/stat.h>
56 #endif
57
58 #ifdef _GLIBCXX_ASSERT
59 # include <cassert>
60 # define VERIFY(fn) assert(fn)
61 #else
62 # define VERIFY(fn) test &= (fn)
63 #endif
64
65 #ifdef _GLIBCXX_HAVE_UNISTD_H
66 # include <unistd.h>
67 #else
68 # define unlink(x)
69 #endif
70
71 namespace __gnu_test
72 {
73   // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
74   // from c++config.h
75
76   // Set memory limits if possible, if not set to 0.
77 #ifndef _GLIBCXX_RES_LIMITS
78 #  define MEMLIMIT_MB 0
79 #else
80 # ifndef MEMLIMIT_MB
81 #  define MEMLIMIT_MB 16.0
82 # endif
83 #endif
84   extern void
85   set_memory_limits(float __size = MEMLIMIT_MB);
86
87   extern void
88   set_file_limit(unsigned long __size);
89
90   // Check mangled name demangles (using __cxa_demangle) as expected.
91   void
92   verify_demangle(const char* mangled, const char* wanted);
93
94   // Simple callback structure for variable numbers of tests (all with
95   // same signature).  Assume all unit tests are of the signature
96   // void test01(); 
97   class func_callback
98   {
99   public:
100     typedef void (*test_type) (void);
101
102   private:
103     int         _M_size;
104     test_type   _M_tests[15];
105
106     func_callback&
107     operator=(const func_callback&);
108
109     func_callback(const func_callback&);
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   // Counting.
138   struct object_counter
139   {
140     // Specifically and glaringly-obviously marked 'signed' so that
141     // when COUNT mistakenly goes negative, we can track the patterns
142     // of deletions more easily.
143     typedef  signed int     size_type;
144     static size_type   count;
145     object_counter() { ++count; }
146     object_counter (const object_counter&) { ++count; }
147     ~object_counter() { --count; }
148   };
149   
150 #define assert_count(n)   VERIFY(__gnu_test::object_counter::count == n)
151   
152   // A (static) class for counting copy constructors and possibly throwing an
153   // exception on a desired count.
154   class copy_constructor
155   {
156   public:
157     static unsigned int
158     count() { return count_; }
159     
160     static void
161     mark_call()
162     {
163       count_++;
164       if (count_ == throw_on_)
165         std::__throw_runtime_error("copy_constructor::mark_call");
166     }
167       
168     static void
169     reset()
170     {
171       count_ = 0;
172       throw_on_ = 0;
173     }
174       
175     static void
176     throw_on(unsigned int count) { throw_on_ = count; }
177
178   private:
179     static unsigned int count_;
180     static unsigned int throw_on_;
181   };
182   
183   // A (static) class for counting assignment operator calls and
184   // possibly throwing an exception on a desired count.
185   class assignment_operator
186   {
187   public:
188     static unsigned int
189     count() { return count_; }
190     
191     static void
192     mark_call()
193     {
194       count_++;
195       if (count_ == throw_on_)
196         std::__throw_runtime_error("assignment_operator::mark_call");
197     }
198
199     static void
200     reset()
201     {
202       count_ = 0;
203       throw_on_ = 0;
204     }
205
206     static void
207     throw_on(unsigned int count) { throw_on_ = count; }
208
209   private:
210     static unsigned int count_;
211     static unsigned int throw_on_;
212   };
213   
214   // A (static) class for tracking calls to an object's destructor.
215   class destructor
216   {
217   public:
218     static unsigned int
219     count() { return _M_count; }
220     
221     static void
222     mark_call() { _M_count++; }
223
224     static void
225     reset() { _M_count = 0; }
226
227   private:
228     static unsigned int _M_count;
229   };
230   
231   // An class of objects that can be used for validating various
232   // behaviours and guarantees of containers and algorithms defined in
233   // the standard library.
234   class copy_tracker
235   {
236   public:
237     // Creates a copy-tracking object with the given ID number.  If
238     // "throw_on_copy" is set, an exception will be thrown if an
239     // attempt is made to copy this object.
240     copy_tracker(int id = next_id_--, bool throw_on_copy = false)
241     : id_(id) , throw_on_copy_(throw_on_copy) { }
242
243     // Copy-constructs the object, marking a call to the copy
244     // constructor and forcing an exception if indicated.
245     copy_tracker(const copy_tracker& rhs)
246     : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
247     {
248       if (throw_on_copy_)
249         copy_constructor::throw_on(copy_constructor::count() + 1);
250       copy_constructor::mark_call();
251     }
252
253     // Assigns the value of another object to this one, tracking the
254     // number of times this member function has been called and if the
255     // other object is supposed to throw an exception when it is
256     // copied, well, make it so.
257     copy_tracker&
258     operator=(const copy_tracker& rhs)
259     { 
260       id_ = rhs.id();
261       if (rhs.throw_on_copy_)
262         assignment_operator::throw_on(assignment_operator::count() + 1);
263       assignment_operator::mark_call();
264       return *this;
265     }
266
267     ~copy_tracker()
268     { destructor::mark_call(); }
269
270     int
271     id() const { return id_; }
272
273     static void
274     reset()
275     {
276       copy_constructor::reset();
277       assignment_operator::reset();
278       destructor::reset();
279     }
280
281   private:
282     int   id_;
283     const bool  throw_on_copy_;
284     static int next_id_;
285   };
286
287   inline bool
288   operator==(const copy_tracker& lhs, const copy_tracker& rhs)
289   { return lhs.id() == rhs.id(); }
290
291   inline bool
292   operator<(const copy_tracker& lhs, const copy_tracker& rhs)
293   { return lhs.id() < rhs.id(); }
294
295   // Class for checking required type conversions, implicit and
296   // explicit for given library data structures. 
297   template<typename _Container>
298     struct conversion
299     {
300       typedef typename _Container::const_iterator const_iterator;
301       
302       // Implicit conversion iterator to const_iterator.
303       static const_iterator
304       iterator_to_const_iterator()
305       {
306         _Container v;
307         const_iterator it = v.begin();
308         const_iterator end = v.end();
309         return it == end ? v.end() : it;
310       }
311     };
312
313   // A binary semaphore for use across multiple processes.
314   class semaphore 
315   {
316   public:
317     // Creates a binary semaphore.  The semaphore is initially in the
318     // unsignaled state. 
319     semaphore();
320
321     // Destroy the semaphore.
322     ~semaphore();
323
324     // Signal the semaphore.  If there are processes blocked in
325     // "wait", exactly one will be permitted to proceed.
326     void signal();
327
328     // Wait until the semaphore is signaled.
329     void wait();
330
331   private:
332     int sem_set_;
333
334     pid_t pid_;
335   };
336
337   // For use in 22_locale/time_get and time_put.
338   std::tm test_tm(int sec, int min, int hour, int mday, int mon,
339                   int year, int wday, int yday, int isdst);
340
341 } // namespace __gnu_test
342
343 #endif // _GLIBCXX_TESTSUITE_HOOKS_H
344