2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_hooks.cc
blob2f4add63f4331fd8f1cb098f9f64ee4ec7d42d87
1 // -*- C++ -*-
2 // Utility subroutines for the C++ library testsuite.
3 //
4 // Copyright (C) 2002, 2003 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_MEM_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_MEM_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_MEMLIMIT_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_MEMLIMIT_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_MEMLIMIT_VMEM
71 getrlimit(RLIMIT_VMEM, &r);
72 r.rlim_cur = limit;
73 setrlimit(RLIMIT_VMEM, &r);
74 #endif
76 // Virtual memory.
77 #if _GLIBCXX_HAVE_MEMLIMIT_AS
78 getrlimit(RLIMIT_AS, &r);
79 r.rlim_cur = limit;
80 setrlimit(RLIMIT_AS, &r);
81 #endif
84 #else
85 void
86 set_memory_limits(float) { }
87 #endif
90 void
91 verify_demangle(const char* mangled, const char* wanted)
93 int status = 0;
94 const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
95 if (!s)
97 switch (status)
99 case 0:
100 s = "error code = 0: success";
101 break;
102 case -1:
103 s = "error code = -1: memory allocation failure";
104 break;
105 case -2:
106 s = "error code = -2: invalid mangled name";
107 break;
108 case -3:
109 s = "error code = -3: invalid arguments";
110 break;
111 default:
112 s = "error code unknown - who knows what happened";
116 std::string w(wanted);
117 if (w != s)
118 throw std::runtime_error(s);
122 // Useful exceptions.
123 class locale_data : public std::runtime_error
125 public:
126 explicit
127 locale_data(const std::string& __arg) : runtime_error(__arg) { }
130 class environment_variable: public std::runtime_error
132 public:
133 explicit
134 environment_variable(const std::string& __arg) : runtime_error(__arg) { }
137 class not_found : public std::runtime_error
139 public:
140 explicit
141 not_found(const std::string& __arg) : runtime_error(__arg) { }
144 void
145 run_tests_wrapped_locale(const char* name, const func_callback& l)
147 using namespace std;
148 bool test = true;
150 // Set the global locale.
151 locale loc_name = try_named_locale(name);
152 locale orig = locale::global(loc_name);
154 const char* res = setlocale(LC_ALL, name);
155 if (res != NULL)
157 string preLC_ALL = res;
158 const func_callback::test_type* tests = l.tests();
159 for (int i = 0; i < l.size(); ++i)
160 (*tests[i])();
161 string postLC_ALL= setlocale(LC_ALL, NULL);
162 VERIFY( preLC_ALL == postLC_ALL );
164 else
165 throw environment_variable(string("LC_ALL for ") + string(name));
168 void
169 run_tests_wrapped_env(const char* name, const char* env,
170 const func_callback& l)
172 using namespace std;
173 bool test = true;
175 #ifdef _GLIBCXX_HAVE_SETENV
176 // Set the global locale.
177 locale loc_name = try_named_locale(name);
178 locale orig = locale::global(loc_name);
180 // Set environment variable env to value in name.
181 const char* oldENV = getenv(env);
182 if (!setenv(env, name, 1))
184 const func_callback::test_type* tests = l.tests();
185 for (int i = 0; i < l.size(); ++i)
186 (*tests[i])();
187 setenv(env, oldENV ? oldENV : "", 1);
189 else
190 throw environment_variable(string(env) + string(" to ") + string(name));
191 #endif
194 std::locale
195 try_named_locale(const char* name)
199 return std::locale(name);
201 catch (std::runtime_error& ex)
203 // Thrown by generic and gnu implemenation if named locale fails.
204 if (std::strstr(ex.what(), "name not valid"))
205 exit(0);
206 else
207 throw;
211 counter::size_type counter::count = 0;
212 unsigned int copy_constructor::count_ = 0;
213 unsigned int copy_constructor::throw_on_ = 0;
214 unsigned int assignment_operator::count_ = 0;
215 unsigned int assignment_operator::throw_on_ = 0;
216 unsigned int destructor::_M_count = 0;
217 int copy_tracker::next_id_ = 0;
218 }; // namespace __gnu_test
220 namespace std
222 // Member specializations for the existing facet classes.
223 // NB: This isn't especially portable. Perhaps a better way would be
224 // to just specialize all of numpunct and ctype.
225 using __gnu_test::int_type;
226 using __gnu_test::value_type;
227 using __gnu_test::pod_type;
229 template<>
230 bool
231 ctype<pod_type>::
232 do_is(mask, char_type) const { return true; }
234 template<>
235 const pod_type*
236 ctype<pod_type>::
237 do_is(const char_type* __lo, const char_type*, mask*) const
238 { return __lo; }
240 template<>
241 const pod_type*
242 ctype<pod_type>::
243 do_scan_is(mask, const char_type* __lo, const char_type*) const
244 { return __lo; }
246 template<>
247 const pod_type*
248 ctype<pod_type>::
249 do_scan_not(mask, const char_type* __lo, const char_type*) const
250 { return __lo; }
252 template<>
253 pod_type
254 ctype<pod_type>::
255 do_toupper(char_type __c) const
256 { return __c; }
258 template<>
259 const pod_type*
260 ctype<pod_type>::
261 do_toupper(char_type*, const char_type* __hi) const
262 { return __hi; }
264 template<>
265 pod_type
266 ctype<pod_type>::
267 do_tolower(char_type __c) const
268 { return __c; }
270 template<>
271 const pod_type*
272 ctype<pod_type>::
273 do_tolower(char_type*, const char_type* __hi) const
274 { return __hi; }
276 template<>
277 pod_type
278 ctype<pod_type>::
279 do_widen(char __c) const
281 char_type ret = { value_type(__c) };
282 return ret;
285 template<>
286 const char*
287 ctype<pod_type>::
288 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
290 while (__lo < __hi)
292 *__dest = this->do_widen(*__lo);
293 ++__lo;
294 ++__dest;
296 return __hi;
299 template<>
300 char
301 ctype<pod_type>::
302 do_narrow(char_type __wc, char) const
303 { return static_cast<char>(__wc.value); }
305 template<>
306 const pod_type*
307 ctype<pod_type>::
308 do_narrow(const pod_type* __lo, const pod_type* __hi,
309 char, char* __dest) const
311 while (__lo < __hi)
313 *__dest = this->do_narrow(*__lo, char());
314 ++__lo;
315 ++__dest;
317 return __hi;
320 template<>
321 ctype<pod_type>::~ctype() { }
323 template<>
324 void
325 numpunct<pod_type>::_M_initialize_numpunct(__c_locale)
327 if (!_M_data)
328 _M_data = new __numpunct_cache<pod_type>;
330 _M_data->_M_grouping = "";
331 _M_data->_M_use_grouping = false;
333 _M_data->_M_decimal_point.value = value_type('.');
334 _M_data->_M_thousands_sep.value = value_type(',');
336 for (size_t i = 0; i < __num_base::_S_oend; ++i)
338 value_type v = __num_base::_S_atoms_out[i];
339 _M_data->_M_atoms_out[i].value = v;
341 _M_data->_M_atoms_out[__num_base::_S_oend] = pod_type();
343 for (size_t i = 0; i < __num_base::_S_iend; ++i)
344 _M_data->_M_atoms_in[i].value = value_type(__num_base::_S_atoms_in[i]);
345 _M_data->_M_atoms_in[__num_base::_S_iend] = pod_type();
347 // "true"
348 pod_type* __truename = new pod_type[4 + 1];
349 __truename[0].value = value_type('t');
350 __truename[1].value = value_type('r');
351 __truename[2].value = value_type('u');
352 __truename[3].value = value_type('e');
353 __truename[4] = pod_type();
354 _M_data->_M_truename = __truename;
356 // "false"
357 pod_type* __falsename = new pod_type[5 + 1];
358 __falsename[0].value = value_type('f');
359 __falsename[1].value = value_type('a');
360 __falsename[2].value = value_type('l');
361 __falsename[3].value = value_type('s');
362 __falsename[4].value = value_type('e');
363 __falsename[5] = pod_type();
364 _M_data->_M_falsename = __falsename;
367 template<>
368 numpunct<pod_type>::~numpunct()
369 { delete _M_data; }
370 } // namespace std