Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_hooks.cc
blob1ead3485175684c183a3f5cf29ba6fd317cdb7e7
1 // -*- C++ -*-
3 // Utility subroutines for the C++ library testsuite.
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, or (at your option)
12 // any later version.
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.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
24 #include <testsuite_hooks.h>
26 #ifdef _GLIBCXX_RES_LIMITS
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #endif
32 #include <list>
33 #include <string>
34 #include <stdexcept>
35 #include <clocale>
36 #include <cstdlib>
37 #include <locale>
38 #include <cxxabi.h>
40 // If we have <sys/types.h>, <sys/ipc.h>, and <sys/sem.h>, then assume
41 // that System V semaphores are available.
42 #if defined(_GLIBCXX_HAVE_SYS_TYPES_H) \
43 && defined(_GLIBCXX_HAVE_SYS_IPC_H) \
44 && defined(_GLIBCXX_HAVE_SYS_SEM_H)
45 #define _GLIBCXX_SYSV_SEM
46 #endif
48 #ifdef _GLIBCXX_SYSV_SEM
49 #include <sys/types.h>
50 #include <sys/ipc.h>
51 #include <sys/sem.h>
52 #endif
54 namespace __gnu_test
56 #ifdef _GLIBCXX_RES_LIMITS
57 void
58 set_memory_limits(float size)
60 struct rlimit r;
61 // Cater to the absence of rlim_t.
62 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
64 // Heap size, seems to be common.
65 #if _GLIBCXX_HAVE_LIMIT_DATA
66 getrlimit(RLIMIT_DATA, &r);
67 r.rlim_cur = limit;
68 setrlimit(RLIMIT_DATA, &r);
69 #endif
71 // Resident set size.
72 #if _GLIBCXX_HAVE_LIMIT_RSS
73 getrlimit(RLIMIT_RSS, &r);
74 r.rlim_cur = limit;
75 setrlimit(RLIMIT_RSS, &r);
76 #endif
78 // Mapped memory (brk + mmap).
79 #if _GLIBCXX_HAVE_LIMIT_VMEM
80 getrlimit(RLIMIT_VMEM, &r);
81 r.rlim_cur = limit;
82 setrlimit(RLIMIT_VMEM, &r);
83 #endif
85 // Virtual memory. On x86_64-linux, the default is -z
86 // max-page-size=0x200000 which means up to 2MB of address space
87 // are accounted for PROT_NONE mappings between text and data
88 // segments of each shared library. There are 4 shared libs
89 // involved in addition to the dynamic linker, maybe 5 if libgomp
90 // is being used as well. Use at least 20MB address space limit.
91 #if defined(__x86_64__) && defined(__linux__)
92 if (limit < 20971520)
93 limit = 20971520;
94 #endif
96 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
97 // anything less than 128MB cannot "malloc" even 1K of memory.
98 // Therefore, we skip RLIMIT_AS on HP-UX.
99 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
100 getrlimit(RLIMIT_AS, &r);
101 r.rlim_cur = limit;
102 setrlimit(RLIMIT_AS, &r);
103 #endif
106 #else
107 void
108 set_memory_limits(float) { }
109 #endif
111 #ifdef _GLIBCXX_RES_LIMITS
112 void
113 set_file_limit(unsigned long size)
115 #if _GLIBCXX_HAVE_LIMIT_FSIZE
116 struct rlimit r;
117 // Cater to the absence of rlim_t.
118 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
120 getrlimit(RLIMIT_FSIZE, &r);
121 r.rlim_cur = limit;
122 setrlimit(RLIMIT_FSIZE, &r);
123 #endif
126 #else
127 void
128 set_file_limit(unsigned long) { }
129 #endif
131 void
132 verify_demangle(const char* mangled, const char* wanted)
134 int status = 0;
135 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
136 if (!s)
138 switch (status)
140 case 0:
141 s = "error code = 0: success";
142 break;
143 case -1:
144 s = "error code = -1: memory allocation failure";
145 break;
146 case -2:
147 s = "error code = -2: invalid mangled name";
148 break;
149 case -3:
150 s = "error code = -3: invalid arguments";
151 break;
152 default:
153 s = "error code unknown - who knows what happened";
157 std::string w(wanted);
158 if (w != s)
159 std::__throw_runtime_error(s);
162 void
163 run_tests_wrapped_locale(const char* name, const func_callback& l)
165 using namespace std;
167 // Set the global locale.
168 locale loc_name = locale(name);
169 locale orig = locale::global(loc_name);
171 const char* res = setlocale(LC_ALL, name);
172 if (res)
174 string preLC_ALL = res;
175 const func_callback::test_type* tests = l.tests();
176 for (int i = 0; i < l.size(); ++i)
177 (*tests[i])();
178 string postLC_ALL= setlocale(LC_ALL, 0);
179 VERIFY( preLC_ALL == postLC_ALL );
181 else
183 string s("LC_ALL for ");
184 s += name;
185 __throw_runtime_error(s.c_str());
189 void
190 run_tests_wrapped_env(const char* name, const char* env,
191 const func_callback& l)
193 using namespace std;
195 #ifdef _GLIBCXX_HAVE_SETENV
196 // Set the global locale.
197 locale loc_name = locale(name);
198 locale orig = locale::global(loc_name);
200 // Set environment variable env to value in name.
201 const char* oldENV = getenv(env);
202 if (!setenv(env, name, 1))
204 const func_callback::test_type* tests = l.tests();
205 for (int i = 0; i < l.size(); ++i)
206 (*tests[i])();
207 setenv(env, oldENV ? oldENV : "", 1);
209 else
211 string s(env);
212 s += string(" to ");
213 s += string(name);
214 __throw_runtime_error(s.c_str());
216 #endif
219 object_counter::size_type object_counter::count = 0;
220 unsigned int copy_constructor::count_ = 0;
221 unsigned int copy_constructor::throw_on_ = 0;
222 unsigned int assignment_operator::count_ = 0;
223 unsigned int assignment_operator::throw_on_ = 0;
224 unsigned int destructor::_M_count = 0;
225 int copy_tracker::next_id_ = 0;
227 #ifdef _GLIBCXX_SYSV_SEM
228 // This union is not declared in system headers. Instead, it must
229 // be defined by user programs.
230 union semun
232 int val;
233 struct semid_ds *buf;
234 unsigned short *array;
236 #endif
238 semaphore::semaphore()
240 #ifdef _GLIBCXX_SYSV_SEM
241 // Remeber the PID for the process that created the semaphore set
242 // so that only one process will destroy the set.
243 pid_ = getpid();
245 // GLIBC does not define SEM_R and SEM_A.
246 #ifndef SEM_R
247 #define SEM_R 0400
248 #endif
250 #ifndef SEM_A
251 #define SEM_A 0200
252 #endif
254 // Get a semaphore set with one semaphore.
255 sem_set_ = semget(IPC_PRIVATE, 1, SEM_R | SEM_A);
256 if (sem_set_ == -1)
257 std::__throw_runtime_error("could not obtain semaphore set");
259 // Initialize the semaphore.
260 union semun val;
261 val.val = 0;
262 if (semctl(sem_set_, 0, SETVAL, val) == -1)
263 std::__throw_runtime_error("could not initialize semaphore");
264 #else
265 // There are no semaphores on this system. We have no way to mark
266 // a test as "unsupported" at runtime, so we just exit, pretending
267 // that the test passed.
268 exit(0);
269 #endif
272 semaphore::~semaphore()
274 #ifdef _GLIBCXX_SYSV_SEM
275 union semun val;
276 val.val = 0; // Avoid uninitialized variable warning.
277 // Destroy the semaphore set only in the process that created it.
278 if (pid_ == getpid())
279 semctl(sem_set_, 0, IPC_RMID, val);
280 #endif
283 void
284 semaphore::signal()
286 #ifdef _GLIBCXX_SYSV_SEM
287 struct sembuf op[1] =
289 { 0, 1, 0 }
291 if (semop(sem_set_, op, 1) == -1)
292 std::__throw_runtime_error("could not signal semaphore");
293 #endif
296 void
297 semaphore::wait()
299 #ifdef _GLIBCXX_SYSV_SEM
300 struct sembuf op[1] =
302 { 0, -1, SEM_UNDO }
304 if (semop(sem_set_, op, 1) == -1)
305 std::__throw_runtime_error("could not wait for semaphore");
306 #endif
309 // For use in 22_locale/time_get and time_put.
310 std::tm
311 test_tm(int sec, int min, int hour, int mday, int mon,
312 int year, int wday, int yday, int isdst)
314 static std::tm tmp;
315 tmp.tm_sec = sec;
316 tmp.tm_min = min;
317 tmp.tm_hour = hour;
318 tmp.tm_mday = mday;
319 tmp.tm_mon = mon;
320 tmp.tm_year = year;
321 tmp.tm_wday = wday;
322 tmp.tm_yday = yday;
323 tmp.tm_isdst = isdst;
324 return tmp;
326 } // namespace __gnu_test