annotate libstdc++-v3/testsuite/util/testsuite_hooks.h @ 155:da32f4b04d38

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