]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_hooks.cc
testsuite_allocator.h, [...]: Remove semi-colons after namespace declarations.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.cc
1 // -*- C++ -*-
2
3 // Utility subroutines for the C++ library testsuite.
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
21 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // USA.
23 //
24 // As a special exception, you may use this file as part of a free software
25 // library without restriction. Specifically, if other files instantiate
26 // templates or use macros or inline functions from this file, or you compile
27 // this file and link it with other files to produce an executable, this
28 // file does not by itself cause the resulting executable to be covered by
29 // the GNU General Public License. This exception does not however
30 // invalidate any other reasons why the executable file might be covered by
31 // the GNU General Public License.
32
33 #include <testsuite_hooks.h>
34
35 #ifdef _GLIBCXX_RES_LIMITS
36 #include <unistd.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #endif
40
41 #include <list>
42 #include <string>
43 #include <stdexcept>
44 #include <cstddef>
45 #include <clocale>
46 #include <cstdlib>
47 #include <locale>
48 #include <cxxabi.h>
49
50 // If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
51 // that System V semaphores are available.
52 #if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
53 && defined(_GLIBCXX_HAVE_SYS_IPC_H) \
54 && defined(_GLIBCXX_HAVE_SYS_SEM_H)
55 #define _GLIBCXX_SYSV_SEM
56 #endif
57
58 #ifdef _GLIBCXX_SYSV_SEM
59 #include <sys/types.h>
60 #include <sys/ipc.h>
61 #include <sys/sem.h>
62 #endif
63
64 namespace __gnu_test
65 {
66 #ifdef _GLIBCXX_RES_LIMITS
67 void
68 set_memory_limits(float size)
69 {
70 struct rlimit r;
71 // Cater to the absence of rlim_t.
72 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
73
74 // Heap size, seems to be common.
75 #if _GLIBCXX_HAVE_LIMIT_DATA
76 getrlimit(RLIMIT_DATA, &r);
77 r.rlim_cur = limit;
78 setrlimit(RLIMIT_DATA, &r);
79 #endif
80
81 // Resident set size.
82 #if _GLIBCXX_HAVE_LIMIT_RSS
83 getrlimit(RLIMIT_RSS, &r);
84 r.rlim_cur = limit;
85 setrlimit(RLIMIT_RSS, &r);
86 #endif
87
88 // Mapped memory (brk + mmap).
89 #if _GLIBCXX_HAVE_LIMIT_VMEM
90 getrlimit(RLIMIT_VMEM, &r);
91 r.rlim_cur = limit;
92 setrlimit(RLIMIT_VMEM, &r);
93 #endif
94
95 // Virtual memory. On x86_64-linux, the default is -z
96 // max-page-size=0x200000 which means up to 2MB of address space
97 // are accounted for PROT_NONE mappings between text and data
98 // segments of each shared library. There are 4 shared libs
99 // involved in addition to the dynamic linker, maybe 5 if libgomp
100 // is being used as well. Use at least 20MB address space limit.
101 #if defined(__x86_64__) && defined(__linux__)
102 if (limit < 20971520)
103 limit = 20971520;
104 #endif
105
106 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
107 // anything less than 128MB cannot "malloc" even 1K of memory.
108 // Therefore, we skip RLIMIT_AS on HP-UX.
109 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
110 getrlimit(RLIMIT_AS, &r);
111 r.rlim_cur = limit;
112 setrlimit(RLIMIT_AS, &r);
113 #endif
114 }
115
116 #else
117 void
118 set_memory_limits(float) { }
119 #endif
120
121 #ifdef _GLIBCXX_RES_LIMITS
122 void
123 set_file_limit(unsigned long size)
124 {
125 #if _GLIBCXX_HAVE_LIMIT_FSIZE
126 struct rlimit r;
127 // Cater to the absence of rlim_t.
128 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
129
130 getrlimit(RLIMIT_FSIZE, &r);
131 r.rlim_cur = limit;
132 setrlimit(RLIMIT_FSIZE, &r);
133 #endif
134 }
135
136 #else
137 void
138 set_file_limit(unsigned long) { }
139 #endif
140
141 void
142 verify_demangle(const char* mangled, const char* wanted)
143 {
144 int status = 0;
145 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
146 if (!s)
147 {
148 switch (status)
149 {
150 case 0:
151 s = "error code = 0: success";
152 break;
153 case -1:
154 s = "error code = -1: memory allocation failure";
155 break;
156 case -2:
157 s = "error code = -2: invalid mangled name";
158 break;
159 case -3:
160 s = "error code = -3: invalid arguments";
161 break;
162 default:
163 s = "error code unknown - who knows what happened";
164 }
165 }
166
167 std::string w(wanted);
168 if (w != s)
169 std::__throw_runtime_error(s);
170 }
171
172 void
173 run_tests_wrapped_locale(const char* name, const func_callback& l)
174 {
175 using namespace std;
176 bool test = true;
177
178 // Set the global locale.
179 locale loc_name = locale(name);
180 locale orig = locale::global(loc_name);
181
182 const char* res = setlocale(LC_ALL, name);
183 if (res != NULL)
184 {
185 string preLC_ALL = res;
186 const func_callback::test_type* tests = l.tests();
187 for (int i = 0; i < l.size(); ++i)
188 (*tests[i])();
189 string postLC_ALL= setlocale(LC_ALL, NULL);
190 VERIFY( preLC_ALL == postLC_ALL );
191 }
192 else
193 {
194 string s("LC_ALL for ");
195 s += name;
196 __throw_runtime_error(s.c_str());
197 }
198 }
199
200 void
201 run_tests_wrapped_env(const char* name, const char* env,
202 const func_callback& l)
203 {
204 using namespace std;
205 bool test = true;
206
207 #ifdef _GLIBCXX_HAVE_SETENV
208 // Set the global locale.
209 locale loc_name = locale(name);
210 locale orig = locale::global(loc_name);
211
212 // Set environment variable env to value in name.
213 const char* oldENV = getenv(env);
214 if (!setenv(env, name, 1))
215 {
216 const func_callback::test_type* tests = l.tests();
217 for (int i = 0; i < l.size(); ++i)
218 (*tests[i])();
219 setenv(env, oldENV ? oldENV : "", 1);
220 }
221 else
222 {
223 string s(env);
224 s += string(" to ");
225 s += string(name);
226 __throw_runtime_error(s.c_str());
227 }
228 #endif
229 }
230
231 counter::size_type counter::count = 0;
232 unsigned int copy_constructor::count_ = 0;
233 unsigned int copy_constructor::throw_on_ = 0;
234 unsigned int assignment_operator::count_ = 0;
235 unsigned int assignment_operator::throw_on_ = 0;
236 unsigned int destructor::_M_count = 0;
237 int copy_tracker::next_id_ = 0;
238
239 #ifdef _GLIBCXX_SYSV_SEM
240 // This union is not declared in system headers. Instead, it must
241 // be defined by user programs.
242 union semun
243 {
244 int val;
245 struct semid_ds *buf;
246 unsigned short *array;
247 };
248 #endif
249
250 semaphore::semaphore()
251 {
252 #ifdef _GLIBCXX_SYSV_SEM
253 // Remeber the PID for the process that created the semaphore set
254 // so that only one process will destroy the set.
255 pid_ = getpid();
256
257 // GLIBC does not define SEM_R and SEM_A.
258 #ifndef SEM_R
259 #define SEM_R 0400
260 #endif
261
262 #ifndef SEM_A
263 #define SEM_A 0200
264 #endif
265
266 // Get a semaphore set with one semaphore.
267 sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
268 if (sem_set_ == -1)
269 std::__throw_runtime_error("could not obtain semaphore set");
270
271 // Initialize the semaphore.
272 union semun val;
273 val.val = 0;
274 if (semctl(sem_set_, 0, SETVAL, val) == -1)
275 std::__throw_runtime_error("could not initialize semaphore");
276 #else
277 // There are no semaphores on this system. We have no way to mark
278 // a test as "unsupported" at runtime, so we just exit, pretending
279 // that the test passed.
280 exit(0);
281 #endif
282 }
283
284 semaphore::~semaphore()
285 {
286 #ifdef _GLIBCXX_SYSV_SEM
287 union semun val;
288 // Destroy the semaphore set only in the process that created it.
289 if (pid_ == getpid())
290 semctl(sem_set_, 0, IPC_RMID, val);
291 #endif
292 }
293
294 void
295 semaphore::signal()
296 {
297 #ifdef _GLIBCXX_SYSV_SEM
298 struct sembuf op[1] =
299 {
300 { 0, 1, 0 }
301 };
302 if (semop(sem_set_, op, 1) == -1)
303 std::__throw_runtime_error("could not signal semaphore");
304 #endif
305 }
306
307 void
308 semaphore::wait()
309 {
310 #ifdef _GLIBCXX_SYSV_SEM
311 struct sembuf op[1] =
312 {
313 { 0, -1, SEM_UNDO }
314 };
315 if (semop(sem_set_, op, 1) == -1)
316 std::__throw_runtime_error("could not wait for semaphore");
317 #endif
318 }
319
320 // For use in 22_locale/time_get and time_put.
321 std::tm
322 test_tm(int sec, int min, int hour, int mday, int mon,
323 int year, int wday, int yday, int isdst)
324 {
325 static std::tm tmp;
326 tmp.tm_sec = sec;
327 tmp.tm_min = min;
328 tmp.tm_hour = hour;
329 tmp.tm_mday = mday;
330 tmp.tm_mon = mon;
331 tmp.tm_year = year;
332 tmp.tm_wday = wday;
333 tmp.tm_yday = yday;
334 tmp.tm_isdst = isdst;
335 return tmp;
336 }
337 } // namespace __gnu_test