OSDN Git Service

2002-11-25 Stephen M. Webb <stephen@bregmasoft.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / testsuite_hooks.h
1 // Utility subroutines for the C++ library testsuite.
2 //
3 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 //
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // This file provides the following:
31 //
32 // 1)  VERIFY(), via DEBUG_ASSERT, from Brent Verner <brent@rcfile.org>.
33 //   This file is included in the various testsuite programs to provide
34 //   #define(able) assert() behavior for debugging/testing. It may be
35 //   a suitable location for other furry woodland creatures as well.
36 //
37 // 2)  __set_testsuite_memlimit()
38 //   __set_testsuite_memlimit() uses setrlimit() to restrict dynamic memory
39 //   allocation.  We provide a default memory limit if none is passed by the
40 //   calling application.  The argument to __set_testsuite_memlimit() is the
41 //   limit in megabytes (a floating-point number).  If _GLIBCPP_MEM_LIMITS is
42 //   not #defined before including this header, then no limiting is attempted.
43 //
44 // 3)  gnu_counting_struct
45 //   This is a POD with a static data member, gnu_counting_struct::count,
46 //   which starts at zero, increments on instance construction, and decrements
47 //   on instance destruction.  "assert_count(n)" can be called to VERIFY()
48 //   that the count equals N.
49 //
50 // 4)  gnu_copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
51 //   A class with nontrivial ctor/dtor that provides the ability to track the
52 //   number of copy ctors and dtors, and will throw on demand during copy.
53 //
54 // 5) gnu_char, gnu_char_traits, abstract character classes and
55 // char_traits specializations for testing instantiations.
56
57 #ifndef _GLIBCPP_TESTSUITE_HOOKS_H
58 #define _GLIBCPP_TESTSUITE_HOOKS_H
59
60 #include <bits/c++config.h>
61 #include <bits/functexcept.h>
62 #include <cstddef>
63
64 #ifdef DEBUG_ASSERT
65 # include <cassert>
66 # define VERIFY(fn) assert(fn)
67 #else
68 # define VERIFY(fn) test &= (fn)
69 #endif
70
71 // Defined in GLIBCPP_CONFIGURE_TESTSUITE.
72 #ifndef _GLIBCPP_MEM_LIMITS
73 // Don't do memory limits.
74 extern void
75 __set_testsuite_memlimit(float x = 0);
76
77 #else
78
79 // Do memory limits.
80 #ifndef MEMLIMIT_MB
81 #define MEMLIMIT_MB 16.0
82 #endif
83
84 extern void
85 __set_testsuite_memlimit(float __size = MEMLIMIT_MB);
86 #endif
87
88
89 struct gnu_counting_struct
90 {
91     // Specifically and glaringly-obviously marked 'signed' so that when
92     // COUNT mistakenly goes negative, we can track the patterns of
93     // deletions more easily.
94     typedef  signed int     size_type;
95     static size_type   count;
96     gnu_counting_struct() { ++count; }
97     gnu_counting_struct (const gnu_counting_struct&) { ++count; }
98     ~gnu_counting_struct() { --count; }
99 };
100
101 #define assert_count(n)   VERIFY(gnu_counting_struct::count == n)
102
103 // A (static) class for counting copy constructors and possibly throwing an
104 // exception on a desired count.
105 class gnu_copy_constructor
106 {
107 public:
108   static unsigned int
109   count()
110   { return count_; }
111
112   static void
113   mark_call()
114   {
115     count_++;
116     if (count_ == throw_on_)
117     {
118       __throw_exception_again "copy constructor exception";
119     }
120   }
121
122   static void
123   reset()
124   {
125     count_ = 0;
126     throw_on_ = 0;
127   }
128
129   static void
130   throw_on(unsigned int count)
131   { throw_on_ = count; }
132
133 private:
134   static unsigned int count_;
135   static unsigned int throw_on_;
136 };
137
138 // A (static) class for counting assignment operator calls and possibly
139 // throwing an exception on a desired count.
140 class gnu_assignment_operator
141 {
142 public:
143   static unsigned int
144   count()
145   { return count_; }
146
147   static void
148   mark_call()
149   {
150     count_++;
151     if (count_ == throw_on_)
152     {
153       __throw_exception_again "assignment operator exception";
154     }
155   }
156
157   static void
158   reset()
159   {
160     count_ = 0;
161     throw_on_ = 0;
162   }
163
164   static void
165   throw_on(unsigned int count)
166   { throw_on_ = count; }
167
168 private:
169   static unsigned int count_;
170   static unsigned int throw_on_;
171 };
172
173 // A (static) class for tracking calls to an object's destructor.
174 class gnu_destructor
175 {
176 public:
177   static unsigned int
178   count()
179   { return count_; }
180
181   static void
182   mark_call()
183   { count_++; }
184
185   static void
186   reset()
187   { count_ = 0; }
188
189 private:
190   static unsigned int count_;
191 };
192
193 // An class of objects that can be used for validating various behaviours and
194 // guarantees of containers and algorithms defined in the standard library.
195 class gnu_copy_tracker
196 {
197   public:
198     // Creates a copy-tracking object with the given ID number.
199     // If "throw_on_copy" is set, an exception will be thrown if
200     // an attempt is made to copy this object.
201     gnu_copy_tracker(int id = next_id_--, bool throw_on_copy = false)
202     : id_(id)
203     , throw_on_copy_(throw_on_copy)
204     {
205     }
206
207     // Copy-constructs the object, marking a call to the copy
208     // constructor and forcing an exception if indicated.
209     gnu_copy_tracker(const gnu_copy_tracker& rhs)
210     : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
211     {
212       int kkk = throw_on_copy_;
213       if (throw_on_copy_)
214       {
215         gnu_copy_constructor::throw_on(gnu_copy_constructor::count() + 1);
216       }
217       gnu_copy_constructor::mark_call();
218     }
219
220     // Assigns the value of another object to this one, tracking the
221     // number of times this member function has been called and if the
222     // other object is supposed to throw an exception when it is
223     // copied, well, make it so.
224     gnu_copy_tracker&
225     operator=(const gnu_copy_tracker& rhs)
226     { 
227       id_ = rhs.id();
228       if (rhs.throw_on_copy_)
229       {
230         gnu_assignment_operator::throw_on(gnu_assignment_operator::count() 
231                                           + 1);
232       }
233       gnu_assignment_operator::mark_call();
234     }
235
236     ~gnu_copy_tracker()
237     { gnu_destructor::mark_call(); }
238
239     int
240     id() const
241     { return id_; }
242
243   private:
244     int   id_;
245     const bool  throw_on_copy_;
246
247   public:
248     static void
249     reset()
250     {
251       gnu_copy_constructor::reset();
252       gnu_assignment_operator::reset();
253       gnu_destructor::reset();
254     }
255
256     // for backwards-compatibility
257     static int
258     copyCount() 
259     { return gnu_copy_constructor::count(); }
260
261     // for backwards-compatibility
262     static int
263     dtorCount() 
264     { return gnu_destructor::count(); }
265
266   private:
267     static int next_id_;
268 };
269
270 inline bool
271 operator==(const gnu_copy_tracker& lhs, const gnu_copy_tracker& rhs)
272 { return lhs.id() == rhs.id(); }
273
274 struct gnu_char
275 {
276   unsigned long c;
277 };
278
279 struct gnu_int
280 {
281   unsigned long i;
282 };
283
284 struct gnu_state
285 {
286   unsigned long l;
287   unsigned long l2;
288 };
289
290 // char_traits specialization
291 namespace std
292 {
293   template<class _CharT>
294     struct char_traits;
295
296   template<>
297     struct char_traits<gnu_char>
298     {
299       typedef gnu_char          char_type;
300       typedef gnu_int           int_type;
301       typedef long              pos_type;
302       typedef unsigned long     off_type;
303       typedef gnu_state         state_type;
304       
305       static void 
306       assign(char_type& __c1, const char_type& __c2);
307
308       static bool 
309       eq(const char_type& __c1, const char_type& __c2);
310
311       static bool 
312       lt(const char_type& __c1, const char_type& __c2);
313
314       static int 
315       compare(const char_type* __s1, const char_type* __s2, size_t __n);
316
317       static size_t
318       length(const char_type* __s);
319
320       static const char_type* 
321       find(const char_type* __s, size_t __n, const char_type& __a);
322
323       static char_type* 
324       move(char_type* __s1, const char_type* __s2, size_t __n);
325
326       static char_type* 
327       copy(char_type* __s1, const char_type* __s2, size_t __n);
328
329       static char_type* 
330       assign(char_type* __s, size_t __n, char_type __a);
331
332       static char_type 
333       to_char_type(const int_type& __c);
334
335       static int_type 
336       to_int_type(const char_type& __c);
337
338       static bool 
339       eq_int_type(const int_type& __c1, const int_type& __c2);
340
341       static int_type 
342       eof();
343
344       static int_type 
345       not_eof(const int_type& __c);
346     };
347 } // namespace std
348
349 #endif // _GLIBCPP_TESTSUITE_HOOKS_H
350