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