OSDN Git Service

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