2005-04-15 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.cc
blobe8618fb71df01ea43749076cb7c2a9a931bf843e
1 // -*- C++ -*-
3 // Utility subroutines for the C++ library testsuite.
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
32 #include <testsuite_hooks.h>
34 #ifdef _GLIBCXX_RES_LIMITS
35 #include <unistd.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #endif
39 #include <list>
40 #include <string>
41 #include <stdexcept>
42 #include <clocale>
43 #include <locale>
44 #include <cxxabi.h>
46 // If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
47 // that System V semaphores are available.
48 #if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
49 && defined(_GLIBCXX_HAVE_SYS_IPC_H) \
50 && defined(_GLIBCXX_HAVE_SYS_SEM_H)
51 #define _GLIBCXX_SYSV_SEM
52 #endif
54 #ifdef _GLIBCXX_SYSV_SEM
55 #include <sys/types.h>
56 #include <sys/ipc.h>
57 #include <sys/sem.h>
58 #endif
60 namespace __gnu_test
62 #ifdef _GLIBCXX_RES_LIMITS
63 void
64 set_memory_limits(float size)
66 struct rlimit r;
67 // Cater to the absence of rlim_t.
68 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
70 // Heap size, seems to be common.
71 #if _GLIBCXX_HAVE_LIMIT_DATA
72 getrlimit(RLIMIT_DATA, &r);
73 r.rlim_cur = limit;
74 setrlimit(RLIMIT_DATA, &r);
75 #endif
77 // Resident set size.
78 #if _GLIBCXX_HAVE_LIMIT_RSS
79 getrlimit(RLIMIT_RSS, &r);
80 r.rlim_cur = limit;
81 setrlimit(RLIMIT_RSS, &r);
82 #endif
84 // Mapped memory (brk + mmap).
85 #if _GLIBCXX_HAVE_LIMIT_VMEM
86 getrlimit(RLIMIT_VMEM, &r);
87 r.rlim_cur = limit;
88 setrlimit(RLIMIT_VMEM, &r);
89 #endif
91 // Virtual memory.
92 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
93 // anything less than 128MB cannot "malloc" even 1K of memory.
94 // Therefore, we skip RLIMIT_AS on HP-UX.
95 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
96 getrlimit(RLIMIT_AS, &r);
97 r.rlim_cur = limit;
98 setrlimit(RLIMIT_AS, &r);
99 #endif
102 #else
103 void
104 set_memory_limits(float) { }
105 #endif
107 #ifdef _GLIBCXX_RES_LIMITS
108 void
109 set_file_limit(unsigned long size)
111 #if _GLIBCXX_HAVE_LIMIT_FSIZE
112 struct rlimit r;
113 // Cater to the absence of rlim_t.
114 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
116 getrlimit(RLIMIT_FSIZE, &r);
117 r.rlim_cur = limit;
118 setrlimit(RLIMIT_FSIZE, &r);
119 #endif
122 #else
123 void
124 set_file_limit(unsigned long) { }
125 #endif
127 void
128 verify_demangle(const char* mangled, const char* wanted)
130 int status = 0;
131 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
132 if (!s)
134 switch (status)
136 case 0:
137 s = "error code = 0: success";
138 break;
139 case -1:
140 s = "error code = -1: memory allocation failure";
141 break;
142 case -2:
143 s = "error code = -2: invalid mangled name";
144 break;
145 case -3:
146 s = "error code = -3: invalid arguments";
147 break;
148 default:
149 s = "error code unknown - who knows what happened";
153 std::string w(wanted);
154 if (w != s)
155 std::__throw_runtime_error(s);
158 void
159 run_tests_wrapped_locale(const char* name, const func_callback& l)
161 using namespace std;
162 bool test = true;
164 // Set the global locale.
165 locale loc_name = try_named_locale(name);
166 locale orig = locale::global(loc_name);
168 const char* res = setlocale(LC_ALL, name);
169 if (res != NULL)
171 string preLC_ALL = res;
172 const func_callback::test_type* tests = l.tests();
173 for (int i = 0; i < l.size(); ++i)
174 (*tests[i])();
175 string postLC_ALL= setlocale(LC_ALL, NULL);
176 VERIFY( preLC_ALL == postLC_ALL );
178 else
180 string s("LC_ALL for ");
181 s += name;
182 __throw_runtime_error(s.c_str());
186 void
187 run_tests_wrapped_env(const char* name, const char* env,
188 const func_callback& l)
190 using namespace std;
191 bool test = true;
193 #ifdef _GLIBCXX_HAVE_SETENV
194 // Set the global locale.
195 locale loc_name = try_named_locale(name);
196 locale orig = locale::global(loc_name);
198 // Set environment variable env to value in name.
199 const char* oldENV = getenv(env);
200 if (!setenv(env, name, 1))
202 const func_callback::test_type* tests = l.tests();
203 for (int i = 0; i < l.size(); ++i)
204 (*tests[i])();
205 setenv(env, oldENV ? oldENV : "", 1);
207 else
209 string s(env);
210 s += string(" to ");
211 s += string(name);
212 __throw_runtime_error(s.c_str());
214 #endif
217 std::locale
218 try_named_locale(const char* name)
222 return std::locale(name);
224 #ifdef __EXCEPTIONS
225 catch (std::runtime_error& ex)
227 // Thrown by generic and gnu implemenation if named locale fails.
228 if (std::strstr(ex.what(), "name not valid"))
229 exit(0);
230 else
231 throw;
233 #endif
237 try_mkfifo (const char* filename, mode_t mode)
239 #if defined (_NEWLIB_VERSION) || defined (__MINGW32_VERSION)
240 /* Newlib and MinGW32 do not have mkfifo. */
241 exit(0);
242 #else
243 return mkfifo(filename, mode);
244 #endif
247 counter::size_type counter::count = 0;
248 unsigned int copy_constructor::count_ = 0;
249 unsigned int copy_constructor::throw_on_ = 0;
250 unsigned int assignment_operator::count_ = 0;
251 unsigned int assignment_operator::throw_on_ = 0;
252 unsigned int destructor::_M_count = 0;
253 int copy_tracker::next_id_ = 0;
255 #ifdef _GLIBCXX_SYSV_SEM
256 // This union is not declared in system headers. Instead, it must
257 // be defined by user programs.
258 union semun
260 int val;
261 struct semid_ds *buf;
262 unsigned short *array;
264 #endif
266 semaphore::semaphore()
268 #ifdef _GLIBCXX_SYSV_SEM
269 // Remeber the PID for the process that created the semaphore set
270 // so that only one process will destroy the set.
271 pid_ = getpid();
273 // GLIBC does not define SEM_R and SEM_A.
274 #ifndef SEM_R
275 #define SEM_R 0400
276 #endif
278 #ifndef SEM_A
279 #define SEM_A 0200
280 #endif
282 // Get a semaphore set with one semaphore.
283 sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
284 if (sem_set_ == -1)
285 std::__throw_runtime_error("could not obtain semaphore set");
287 // Initialize the semaphore.
288 union semun val;
289 val.val = 0;
290 if (semctl(sem_set_, 0, SETVAL, val) == -1)
291 std::__throw_runtime_error("could not initialize semaphore");
292 #else
293 // There are no semaphores on this system. We have no way to mark
294 // a test as "unsupported" at runtime, so we just exit, pretending
295 // that the test passed.
296 exit(0);
297 #endif
300 semaphore::~semaphore()
302 #ifdef _GLIBCXX_SYSV_SEM
303 union semun val;
304 // Destroy the semaphore set only in the process that created it.
305 if (pid_ == getpid())
306 semctl(sem_set_, 0, IPC_RMID, val);
307 #endif
310 void
311 semaphore::signal()
313 #ifdef _GLIBCXX_SYSV_SEM
314 struct sembuf op[1] =
316 { 0, 1, 0 }
318 if (semop(sem_set_, op, 1) == -1)
319 std::__throw_runtime_error("could not signal semaphore");
320 #endif
323 void
324 semaphore::wait()
326 #ifdef _GLIBCXX_SYSV_SEM
327 struct sembuf op[1] =
329 { 0, -1, SEM_UNDO }
331 if (semop(sem_set_, op, 1) == -1)
332 std::__throw_runtime_error("could not wait for semaphore");
333 #endif
335 }; // namespace __gnu_test