OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [testsuite/] [util/] [testsuite_hooks.h] - Blame information for rev 746

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.