Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / libstdc++-v3 / testsuite / testsuite_hooks.cc
blobb649e9ba7d7e9b2e3725ea0f294891672b4234cc
1 // -*- C++ -*-
3 // Utility subroutines for the C++ library testsuite.
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 x86_64-linux, the default is -z max-page-size=0x200000
93 // which means up to 2MB of address space are accounted for
94 // PROT_NONE mappings between text and data segments of
95 // each shared library. There are 4 shared libs involved
96 // in addition to the dynamic linker. Use at least 16MB address space
97 // limit.
98 #if defined(__x86_64__) && defined(__linux__)
99 if (limit < 16777216)
100 limit = 16777216;
101 #endif
102 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
103 // anything less than 128MB cannot "malloc" even 1K of memory.
104 // Therefore, we skip RLIMIT_AS on HP-UX.
105 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
106 getrlimit(RLIMIT_AS, &r);
107 r.rlim_cur = limit;
108 setrlimit(RLIMIT_AS, &r);
109 #endif
112 #else
113 void
114 set_memory_limits(float) { }
115 #endif
117 #ifdef _GLIBCXX_RES_LIMITS
118 void
119 set_file_limit(unsigned long size)
121 #if _GLIBCXX_HAVE_LIMIT_FSIZE
122 struct rlimit r;
123 // Cater to the absence of rlim_t.
124 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
126 getrlimit(RLIMIT_FSIZE, &r);
127 r.rlim_cur = limit;
128 setrlimit(RLIMIT_FSIZE, &r);
129 #endif
132 #else
133 void
134 set_file_limit(unsigned long) { }
135 #endif
137 void
138 verify_demangle(const char* mangled, const char* wanted)
140 int status = 0;
141 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
142 if (!s)
144 switch (status)
146 case 0:
147 s = "error code = 0: success";
148 break;
149 case -1:
150 s = "error code = -1: memory allocation failure";
151 break;
152 case -2:
153 s = "error code = -2: invalid mangled name";
154 break;
155 case -3:
156 s = "error code = -3: invalid arguments";
157 break;
158 default:
159 s = "error code unknown - who knows what happened";
163 std::string w(wanted);
164 if (w != s)
165 std::__throw_runtime_error(s);
168 void
169 run_tests_wrapped_locale(const char* name, const func_callback& l)
171 using namespace std;
172 bool test = true;
174 // Set the global locale.
175 locale loc_name = locale(name);
176 locale orig = locale::global(loc_name);
178 const char* res = setlocale(LC_ALL, name);
179 if (res != NULL)
181 string preLC_ALL = res;
182 const func_callback::test_type* tests = l.tests();
183 for (int i = 0; i < l.size(); ++i)
184 (*tests[i])();
185 string postLC_ALL= setlocale(LC_ALL, NULL);
186 VERIFY( preLC_ALL == postLC_ALL );
188 else
190 string s("LC_ALL for ");
191 s += name;
192 __throw_runtime_error(s.c_str());
196 void
197 run_tests_wrapped_env(const char* name, const char* env,
198 const func_callback& l)
200 using namespace std;
201 bool test = true;
203 #ifdef _GLIBCXX_HAVE_SETENV
204 // Set the global locale.
205 locale loc_name = locale(name);
206 locale orig = locale::global(loc_name);
208 // Set environment variable env to value in name.
209 const char* oldENV = getenv(env);
210 if (!setenv(env, name, 1))
212 const func_callback::test_type* tests = l.tests();
213 for (int i = 0; i < l.size(); ++i)
214 (*tests[i])();
215 setenv(env, oldENV ? oldENV : "", 1);
217 else
219 string s(env);
220 s += string(" to ");
221 s += string(name);
222 __throw_runtime_error(s.c_str());
224 #endif
227 counter::size_type counter::count = 0;
228 unsigned int copy_constructor::count_ = 0;
229 unsigned int copy_constructor::throw_on_ = 0;
230 unsigned int assignment_operator::count_ = 0;
231 unsigned int assignment_operator::throw_on_ = 0;
232 unsigned int destructor::_M_count = 0;
233 int copy_tracker::next_id_ = 0;
235 #ifdef _GLIBCXX_SYSV_SEM
236 // This union is not declared in system headers. Instead, it must
237 // be defined by user programs.
238 union semun
240 int val;
241 struct semid_ds *buf;
242 unsigned short *array;
244 #endif
246 semaphore::semaphore()
248 #ifdef _GLIBCXX_SYSV_SEM
249 // Remeber the PID for the process that created the semaphore set
250 // so that only one process will destroy the set.
251 pid_ = getpid();
253 // GLIBC does not define SEM_R and SEM_A.
254 #ifndef SEM_R
255 #define SEM_R 0400
256 #endif
258 #ifndef SEM_A
259 #define SEM_A 0200
260 #endif
262 // Get a semaphore set with one semaphore.
263 sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
264 if (sem_set_ == -1)
265 std::__throw_runtime_error("could not obtain semaphore set");
267 // Initialize the semaphore.
268 union semun val;
269 val.val = 0;
270 if (semctl(sem_set_, 0, SETVAL, val) == -1)
271 std::__throw_runtime_error("could not initialize semaphore");
272 #else
273 // There are no semaphores on this system. We have no way to mark
274 // a test as "unsupported" at runtime, so we just exit, pretending
275 // that the test passed.
276 exit(0);
277 #endif
280 semaphore::~semaphore()
282 #ifdef _GLIBCXX_SYSV_SEM
283 union semun val;
284 // Destroy the semaphore set only in the process that created it.
285 if (pid_ == getpid())
286 semctl(sem_set_, 0, IPC_RMID, val);
287 #endif
290 void
291 semaphore::signal()
293 #ifdef _GLIBCXX_SYSV_SEM
294 struct sembuf op[1] =
296 { 0, 1, 0 }
298 if (semop(sem_set_, op, 1) == -1)
299 std::__throw_runtime_error("could not signal semaphore");
300 #endif
303 void
304 semaphore::wait()
306 #ifdef _GLIBCXX_SYSV_SEM
307 struct sembuf op[1] =
309 { 0, -1, SEM_UNDO }
311 if (semop(sem_set_, op, 1) == -1)
312 std::__throw_runtime_error("could not wait for semaphore");
313 #endif
316 // For use in 22_locale/time_get and time_put.
318 test_tm(int sec, int min, int hour, int mday, int mon,
319 int year, int wday, int yday, int isdst)
321 static tm tmp;
322 tmp.tm_sec = sec;
323 tmp.tm_min = min;
324 tmp.tm_hour = hour;
325 tmp.tm_mday = mday;
326 tmp.tm_mon = mon;
327 tmp.tm_year = year;
328 tmp.tm_wday = wday;
329 tmp.tm_yday = yday;
330 tmp.tm_isdst = isdst;
331 return tmp;
333 }; // namespace __gnu_test