PR tree-optimization/86650 - -Warray-bounds missing inlining context
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.h
blob36b53ac23f7136c987d9ef0446ede7d9450713a0
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2000-2018 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 3, or (at your option)
10 // any later version.
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.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 // This file provides the following:
24 // 1) VERIFY()
26 // 2) set_memory_limits()
27 // set_memory_limits() uses setrlimit() to restrict dynamic memory
28 // allocation. We provide a default memory limit if none is passed by the
29 // calling application. The argument to set_memory_limits() is the
30 // limit in megabytes (a floating-point number). If _GLIBCXX_RES_LIMITS is
31 // not #defined before including this header, then no limiting is attempted.
33 // 3) object_counter
34 // This is a POD with a static data member, object_counter::count,
35 // which starts at zero, increments on instance construction, and decrements
36 // on instance destruction. "assert_count(n)" can be called to VERIFY()
37 // that the count equals N.
39 // 4) copy_tracker, from Stephen M. Webb <stephen@bregmasoft.com>.
40 // A class with nontrivial ctor/dtor that provides the ability to track the
41 // number of copy ctors and dtors, and will throw on demand during copy.
43 #ifndef _GLIBCXX_TESTSUITE_HOOKS_H
44 #define _GLIBCXX_TESTSUITE_HOOKS_H
46 #include <bits/c++config.h>
47 #include <bits/functexcept.h>
48 #include <ctime>
50 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
51 #include <sys/stat.h>
52 #endif
54 #define VERIFY(fn) \
55 do \
56 { \
57 if (! (fn)) \
58 { \
59 __builtin_printf("%s:%d: %s: Assertion '%s' failed.\n", \
60 __FILE__, __LINE__, __PRETTY_FUNCTION__, #fn); \
61 __builtin_abort(); \
62 } \
63 } while (false)
65 #ifdef _GLIBCXX_HAVE_UNISTD_H
66 # include <unistd.h>
67 #else
68 # define unlink(x)
69 #endif
71 #if defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__
72 # define ISO_8859(part,langTERR) #langTERR ".ISO8859-" #part
73 #else
74 # define ISO_8859(part,langTERR) ((part) == 15 ?\
75 #langTERR ".ISO8859-" #part "@euro" : #langTERR ".ISO8859-" #part)
76 #endif
78 #if __cplusplus < 201103L
79 # define THROW(X) throw(X)
80 #else
81 # define THROW(X) noexcept(false)
82 #endif
84 #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT || _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL
85 // Correct order of thread_local destruction needs __cxa_thread_atexit_impl
86 // or similar support from libc.
87 # define CORRECT_THREAD_LOCAL_DTORS 1
88 #endif
90 namespace __gnu_test
92 // All macros are defined in GLIBCXX_CONFIGURE_TESTSUITE and imported
93 // from c++config.h
95 // Set memory limits if possible, if not set to 0.
96 #ifndef _GLIBCXX_RES_LIMITS
97 # define MEMLIMIT_MB 0
98 #else
99 # ifndef MEMLIMIT_MB
100 # define MEMLIMIT_MB 16.0
101 # endif
102 #endif
103 extern void
104 set_memory_limits(float __size = MEMLIMIT_MB);
106 extern void
107 set_file_limit(unsigned long __size);
109 // Check mangled name demangles (using __cxa_demangle) as expected.
110 void
111 verify_demangle(const char* mangled, const char* wanted);
113 // Simple callback structure for variable numbers of tests (all with
114 // same signature). Assume all unit tests are of the signature
115 // void test01();
116 class func_callback
118 public:
119 typedef void (*test_type) (void);
121 private:
122 int _M_size;
123 test_type _M_tests[15];
125 func_callback&
126 operator=(const func_callback&);
128 func_callback(const func_callback&);
130 public:
131 func_callback(): _M_size(0) { }
134 size() const { return _M_size; }
136 const test_type*
137 tests() const { return _M_tests; }
139 void
140 push_back(test_type test)
142 _M_tests[_M_size] = test;
143 ++_M_size;
148 // Run select unit tests after setting global locale.
149 void
150 run_tests_wrapped_locale(const char*, const func_callback&);
152 // Run select unit tests after setting environment variables.
153 void
154 run_tests_wrapped_env(const char*, const char*, const func_callback&);
156 // Counting.
157 struct object_counter
159 // Specifically and glaringly-obviously marked 'signed' so that
160 // when COUNT mistakenly goes negative, we can track the patterns
161 // of deletions more easily.
162 typedef signed int size_type;
163 static size_type count;
164 object_counter() { ++count; }
165 object_counter (const object_counter&) { ++count; }
166 ~object_counter() { --count; }
169 #define assert_count(n) VERIFY(__gnu_test::object_counter::count == n)
171 // A (static) class for counting copy constructors and possibly throwing an
172 // exception on a desired count.
173 class copy_constructor
175 public:
176 static unsigned int
177 count() { return count_; }
179 static void
180 mark_call()
182 count_++;
183 if (count_ == throw_on_)
184 std::__throw_runtime_error("copy_constructor::mark_call");
187 static void
188 reset()
190 count_ = 0;
191 throw_on_ = 0;
194 static void
195 throw_on(unsigned int count) { throw_on_ = count; }
197 private:
198 static unsigned int count_;
199 static unsigned int throw_on_;
202 // A (static) class for counting assignment operator calls and
203 // possibly throwing an exception on a desired count.
204 class assignment_operator
206 public:
207 static unsigned int
208 count() { return count_; }
210 static void
211 mark_call()
213 count_++;
214 if (count_ == throw_on_)
215 std::__throw_runtime_error("assignment_operator::mark_call");
218 static void
219 reset()
221 count_ = 0;
222 throw_on_ = 0;
225 static void
226 throw_on(unsigned int count) { throw_on_ = count; }
228 private:
229 static unsigned int count_;
230 static unsigned int throw_on_;
233 // A (static) class for tracking calls to an object's destructor.
234 class destructor
236 public:
237 static unsigned int
238 count() { return _M_count; }
240 static void
241 mark_call() { _M_count++; }
243 static void
244 reset() { _M_count = 0; }
246 private:
247 static unsigned int _M_count;
250 // A class of objects that can be used for validating various
251 // behaviors and guarantees of containers and algorithms defined in
252 // the standard library.
253 class copy_tracker
255 public:
256 // Creates a copy-tracking object with the given ID number. If
257 // "throw_on_copy" is set, an exception will be thrown if an
258 // attempt is made to copy this object.
259 copy_tracker(int id = next_id_--, bool throw_on_copy = false)
260 : id_(id) , throw_on_copy_(throw_on_copy) { }
262 // Copy-constructs the object, marking a call to the copy
263 // constructor and forcing an exception if indicated.
264 copy_tracker(const copy_tracker& rhs)
265 : id_(rhs.id()), throw_on_copy_(rhs.throw_on_copy_)
267 if (throw_on_copy_)
268 copy_constructor::throw_on(copy_constructor::count() + 1);
269 copy_constructor::mark_call();
272 // Assigns the value of another object to this one, tracking the
273 // number of times this member function has been called and if the
274 // other object is supposed to throw an exception when it is
275 // copied, well, make it so.
276 copy_tracker&
277 operator=(const copy_tracker& rhs)
279 id_ = rhs.id();
280 if (rhs.throw_on_copy_)
281 assignment_operator::throw_on(assignment_operator::count() + 1);
282 assignment_operator::mark_call();
283 return *this;
286 ~copy_tracker()
287 { destructor::mark_call(); }
290 id() const { return id_; }
292 static void
293 reset()
295 copy_constructor::reset();
296 assignment_operator::reset();
297 destructor::reset();
300 private:
301 int id_;
302 const bool throw_on_copy_;
303 static int next_id_;
306 inline bool
307 operator==(const copy_tracker& lhs, const copy_tracker& rhs)
308 { return lhs.id() == rhs.id(); }
310 inline bool
311 operator<(const copy_tracker& lhs, const copy_tracker& rhs)
312 { return lhs.id() < rhs.id(); }
314 // Class for checking required type conversions, implicit and
315 // explicit for given library data structures.
316 template<typename _Container>
317 struct conversion
319 typedef typename _Container::const_iterator const_iterator;
321 // Implicit conversion iterator to const_iterator.
322 static const_iterator
323 iterator_to_const_iterator()
325 _Container v;
326 const_iterator it = v.begin();
327 const_iterator end = v.end();
328 return it == end ? v.end() : it;
332 // A binary semaphore for use across multiple processes.
333 class semaphore
335 public:
336 // Creates a binary semaphore. The semaphore is initially in the
337 // unsignaled state.
338 semaphore();
340 // Destroy the semaphore.
341 ~semaphore();
343 // Signal the semaphore. If there are processes blocked in
344 // "wait", exactly one will be permitted to proceed.
345 void signal();
347 // Wait until the semaphore is signaled.
348 void wait();
350 private:
351 int sem_set_;
353 pid_t pid_;
356 // For use in 22_locale/time_get and time_put.
357 std::tm test_tm(int sec, int min, int hour, int mday, int mon,
358 int year, int wday, int yday, int isdst);
360 } // namespace __gnu_test
362 #endif // _GLIBCXX_TESTSUITE_HOOKS_H