* Makefile.in (WERROR_FLAGS): Renamed from WERROR.
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.cc
blobe12a9b41f594c235de03d5d63b5a293a15a2a779
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 #include <testsuite_hooks.h>
33 #ifdef _GLIBCXX_RES_LIMITS
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #endif
38 #include <list>
39 #include <string>
40 #include <stdexcept>
41 #include <clocale>
42 #include <locale>
43 #include <cxxabi.h>
45 namespace __gnu_test
47 #ifdef _GLIBCXX_RES_LIMITS
48 void
49 set_memory_limits(float size)
51 struct rlimit r;
52 // Cater to the absence of rlim_t.
53 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
55 // Heap size, seems to be common.
56 #if _GLIBCXX_HAVE_LIMIT_DATA
57 getrlimit(RLIMIT_DATA, &r);
58 r.rlim_cur = limit;
59 setrlimit(RLIMIT_DATA, &r);
60 #endif
62 // Resident set size.
63 #if _GLIBCXX_HAVE_LIMIT_RSS
64 getrlimit(RLIMIT_RSS, &r);
65 r.rlim_cur = limit;
66 setrlimit(RLIMIT_RSS, &r);
67 #endif
69 // Mapped memory (brk + mmap).
70 #if _GLIBCXX_HAVE_LIMIT_VMEM
71 getrlimit(RLIMIT_VMEM, &r);
72 r.rlim_cur = limit;
73 setrlimit(RLIMIT_VMEM, &r);
74 #endif
76 // Virtual memory.
77 // On HP-UX 11.23, a trivial C++ program that sets RLIMIT_AS to
78 // anything less than 128MB cannot "malloc" even 1K of memory.
79 // Therefore, we skip RLIMIT_AS on HP-UX.
80 #if _GLIBCXX_HAVE_LIMIT_AS && !defined(__hpux__)
81 getrlimit(RLIMIT_AS, &r);
82 r.rlim_cur = limit;
83 setrlimit(RLIMIT_AS, &r);
84 #endif
87 #else
88 void
89 set_memory_limits(float) { }
90 #endif
92 #ifdef _GLIBCXX_RES_LIMITS
93 void
94 set_file_limit(unsigned long size)
96 #if _GLIBCXX_HAVE_LIMIT_FSIZE
97 struct rlimit r;
98 // Cater to the absence of rlim_t.
99 __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size);
101 getrlimit(RLIMIT_FSIZE, &r);
102 r.rlim_cur = limit;
103 setrlimit(RLIMIT_FSIZE, &r);
104 #endif
107 #else
108 void
109 set_file_limit(unsigned long) { }
110 #endif
112 void
113 verify_demangle(const char* mangled, const char* wanted)
115 int status = 0;
116 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
117 if (!s)
119 switch (status)
121 case 0:
122 s = "error code = 0: success";
123 break;
124 case -1:
125 s = "error code = -1: memory allocation failure";
126 break;
127 case -2:
128 s = "error code = -2: invalid mangled name";
129 break;
130 case -3:
131 s = "error code = -3: invalid arguments";
132 break;
133 default:
134 s = "error code unknown - who knows what happened";
138 std::string w(wanted);
139 if (w != s)
140 throw std::runtime_error(s);
144 // Useful exceptions.
145 class locale_data : public std::runtime_error
147 public:
148 explicit
149 locale_data(const std::string& __arg) : runtime_error(__arg) { }
152 class environment_variable: public std::runtime_error
154 public:
155 explicit
156 environment_variable(const std::string& __arg) : runtime_error(__arg) { }
159 class not_found : public std::runtime_error
161 public:
162 explicit
163 not_found(const std::string& __arg) : runtime_error(__arg) { }
166 void
167 run_tests_wrapped_locale(const char* name, const func_callback& l)
169 using namespace std;
170 bool test = true;
172 // Set the global locale.
173 locale loc_name = try_named_locale(name);
174 locale orig = locale::global(loc_name);
176 const char* res = setlocale(LC_ALL, name);
177 if (res != NULL)
179 string preLC_ALL = res;
180 const func_callback::test_type* tests = l.tests();
181 for (int i = 0; i < l.size(); ++i)
182 (*tests[i])();
183 string postLC_ALL= setlocale(LC_ALL, NULL);
184 VERIFY( preLC_ALL == postLC_ALL );
186 else
187 throw environment_variable(string("LC_ALL for ") + string(name));
190 void
191 run_tests_wrapped_env(const char* name, const char* env,
192 const func_callback& l)
194 using namespace std;
195 bool test = true;
197 #ifdef _GLIBCXX_HAVE_SETENV
198 // Set the global locale.
199 locale loc_name = try_named_locale(name);
200 locale orig = locale::global(loc_name);
202 // Set environment variable env to value in name.
203 const char* oldENV = getenv(env);
204 if (!setenv(env, name, 1))
206 const func_callback::test_type* tests = l.tests();
207 for (int i = 0; i < l.size(); ++i)
208 (*tests[i])();
209 setenv(env, oldENV ? oldENV : "", 1);
211 else
212 throw environment_variable(string(env) + string(" to ") + string(name));
213 #endif
216 std::locale
217 try_named_locale(const char* name)
221 return std::locale(name);
223 catch (std::runtime_error& ex)
225 // Thrown by generic and gnu implemenation if named locale fails.
226 if (std::strstr(ex.what(), "name not valid"))
227 exit(0);
228 else
229 throw;
234 try_mkfifo (const char* filename, mode_t mode)
236 #if defined (_NEWLIB_VERSION) || defined (__MINGW32_VERSION)
237 /* Newlib and MinGW32 do not have mkfifo. */
238 exit(0);
239 #else
240 return mkfifo(filename, mode);
241 #endif
244 counter::size_type counter::count = 0;
245 unsigned int copy_constructor::count_ = 0;
246 unsigned int copy_constructor::throw_on_ = 0;
247 unsigned int assignment_operator::count_ = 0;
248 unsigned int assignment_operator::throw_on_ = 0;
249 unsigned int destructor::_M_count = 0;
250 int copy_tracker::next_id_ = 0;
251 }; // namespace __gnu_test
253 namespace std
255 // Member specializations for the existing facet classes.
256 // NB: This isn't especially portable. Perhaps a better way would be
257 // to just specialize all of numpunct and ctype.
258 using __gnu_test::int_type;
259 using __gnu_test::value_type;
260 using __gnu_test::pod_type;
262 template<>
263 bool
264 ctype<pod_type>::
265 do_is(mask, char_type) const { return true; }
267 template<>
268 const pod_type*
269 ctype<pod_type>::
270 do_is(const char_type* __lo, const char_type*, mask*) const
271 { return __lo; }
273 template<>
274 const pod_type*
275 ctype<pod_type>::
276 do_scan_is(mask, const char_type* __lo, const char_type*) const
277 { return __lo; }
279 template<>
280 const pod_type*
281 ctype<pod_type>::
282 do_scan_not(mask, const char_type* __lo, const char_type*) const
283 { return __lo; }
285 template<>
286 pod_type
287 ctype<pod_type>::
288 do_toupper(char_type __c) const
289 { return __c; }
291 template<>
292 const pod_type*
293 ctype<pod_type>::
294 do_toupper(char_type*, const char_type* __hi) const
295 { return __hi; }
297 template<>
298 pod_type
299 ctype<pod_type>::
300 do_tolower(char_type __c) const
301 { return __c; }
303 template<>
304 const pod_type*
305 ctype<pod_type>::
306 do_tolower(char_type*, const char_type* __hi) const
307 { return __hi; }
309 template<>
310 pod_type
311 ctype<pod_type>::
312 do_widen(char __c) const
314 char_type ret = { value_type(__c) };
315 return ret;
318 template<>
319 const char*
320 ctype<pod_type>::
321 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
323 while (__lo < __hi)
325 *__dest = this->do_widen(*__lo);
326 ++__lo;
327 ++__dest;
329 return __hi;
332 template<>
333 char
334 ctype<pod_type>::
335 do_narrow(char_type __wc, char) const
336 { return static_cast<char>(__wc.value); }
338 template<>
339 const pod_type*
340 ctype<pod_type>::
341 do_narrow(const pod_type* __lo, const pod_type* __hi,
342 char, char* __dest) const
344 while (__lo < __hi)
346 *__dest = this->do_narrow(*__lo, char());
347 ++__lo;
348 ++__dest;
350 return __hi;
353 template<>
354 ctype<pod_type>::~ctype() { }
356 template<>
357 void
358 numpunct<pod_type>::_M_initialize_numpunct(__c_locale)
360 if (!_M_data)
361 _M_data = new __numpunct_cache<pod_type>;
363 _M_data->_M_grouping = "";
364 _M_data->_M_use_grouping = false;
366 _M_data->_M_decimal_point.value = value_type('.');
367 _M_data->_M_thousands_sep.value = value_type(',');
369 for (size_t i = 0; i < __num_base::_S_oend; ++i)
371 value_type v = __num_base::_S_atoms_out[i];
372 _M_data->_M_atoms_out[i].value = v;
374 _M_data->_M_atoms_out[__num_base::_S_oend] = pod_type();
376 for (size_t j = 0; j < __num_base::_S_iend; ++j)
377 _M_data->_M_atoms_in[j].value = value_type(__num_base::_S_atoms_in[j]);
378 _M_data->_M_atoms_in[__num_base::_S_iend] = pod_type();
380 // "true"
381 pod_type* __truename = new pod_type[4 + 1];
382 __truename[0].value = value_type('t');
383 __truename[1].value = value_type('r');
384 __truename[2].value = value_type('u');
385 __truename[3].value = value_type('e');
386 __truename[4] = pod_type();
387 _M_data->_M_truename = __truename;
389 // "false"
390 pod_type* __falsename = new pod_type[5 + 1];
391 __falsename[0].value = value_type('f');
392 __falsename[1].value = value_type('a');
393 __falsename[2].value = value_type('l');
394 __falsename[3].value = value_type('s');
395 __falsename[4].value = value_type('e');
396 __falsename[5] = pod_type();
397 _M_data->_M_falsename = __falsename;
400 template<>
401 numpunct<pod_type>::~numpunct()
402 { delete _M_data; }
403 } // namespace std