OSDN Git Service

Update comment to reflect test.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / testsuite_hooks.cc
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.
11 //
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.
16 //
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.
21 //
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.
30
31 #include <testsuite_hooks.h>
32
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>
44
45 namespace __gnu_test
46 {
47 #ifdef _GLIBCXX_MEM_LIMITS
48   void 
49   set_memory_limits(float size)
50   {
51     struct rlimit r;
52     // Cater to the absence of rlim_t.
53     __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
54
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
61
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
68
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
75
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_MEMLIMIT_AS && !defined(__hpux__)
81     getrlimit(RLIMIT_AS, &r);
82     r.rlim_cur = limit;
83     setrlimit(RLIMIT_AS, &r);
84 #endif
85   }
86
87 #else
88   void
89   set_memory_limits(float) { }
90 #endif 
91
92
93   void 
94   verify_demangle(const char* mangled, const char* wanted)
95   {
96     int status = 0;
97     const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
98     if (!s)
99       {
100         switch (status)
101           {
102           case 0:
103             s = "error code = 0: success";
104             break;
105           case -1:
106             s = "error code = -1: memory allocation failure";
107             break;
108           case -2:
109             s = "error code = -2: invalid mangled name";
110             break;
111           case -3:
112             s = "error code = -3: invalid arguments";
113             break;
114           default:
115             s = "error code unknown - who knows what happened";
116           }
117       }
118
119     std::string w(wanted);
120     if (w != s)
121       throw std::runtime_error(s);
122   }
123
124   
125   // Useful exceptions.
126   class locale_data : public std::runtime_error 
127   {
128   public:
129     explicit 
130     locale_data(const std::string&  __arg) : runtime_error(__arg) { }
131   };
132
133   class environment_variable: public std::runtime_error 
134   {
135   public:
136     explicit 
137     environment_variable(const std::string&  __arg) : runtime_error(__arg) { }
138   };
139
140   class not_found : public std::runtime_error 
141   {
142   public:
143     explicit 
144     not_found(const std::string&  __arg) : runtime_error(__arg) { }
145   };
146
147   void 
148   run_tests_wrapped_locale(const char* name, const func_callback& l)
149   {
150     using namespace std;
151     bool test = true;
152     
153     // Set the global locale. 
154     locale loc_name = try_named_locale(name);
155     locale orig = locale::global(loc_name);
156
157     const char* res = setlocale(LC_ALL, name);
158     if (res != NULL)
159       {
160         string preLC_ALL = res;
161         const func_callback::test_type* tests = l.tests();
162         for (int i = 0; i < l.size(); ++i)
163           (*tests[i])();
164         string postLC_ALL= setlocale(LC_ALL, NULL);
165         VERIFY( preLC_ALL == postLC_ALL );
166       }
167     else
168       throw environment_variable(string("LC_ALL for ") + string(name));
169   }
170   
171   void 
172   run_tests_wrapped_env(const char* name, const char* env,
173                         const func_callback& l)
174   {
175     using namespace std;
176     bool test = true;
177     
178 #ifdef _GLIBCXX_HAVE_SETENV 
179     // Set the global locale. 
180     locale loc_name = try_named_locale(name);
181     locale orig = locale::global(loc_name);
182
183     // Set environment variable env to value in name. 
184     const char* oldENV = getenv(env);
185     if (!setenv(env, name, 1))
186       {
187         const func_callback::test_type* tests = l.tests();
188         for (int i = 0; i < l.size(); ++i)
189           (*tests[i])();
190         setenv(env, oldENV ? oldENV : "", 1);
191       }
192     else
193       throw environment_variable(string(env) + string(" to ") + string(name));
194 #endif
195   }
196
197   std::locale 
198   try_named_locale(const char* name)
199   {
200     try
201       {
202         return std::locale(name);
203       }
204     catch (std::runtime_error& ex)
205       {
206         // Thrown by generic and gnu implemenation if named locale fails.
207         if (std::strstr(ex.what(), "name not valid"))
208           exit(0);
209         else
210           throw;
211       }
212   }
213
214   int
215   try_mkfifo (const char* filename, mode_t mode)
216   {
217 #if defined (_NEWLIB_VERSION) || defined (__MINGW32_VERSION)
218     /* Newlib and MinGW32 do not have mkfifo.  */
219     exit(0);
220 #else
221     return mkfifo(filename, mode);
222 #endif
223   }
224
225   counter::size_type  counter::count = 0;
226   unsigned int copy_constructor::count_ = 0;
227   unsigned int copy_constructor::throw_on_ = 0;
228   unsigned int assignment_operator::count_ = 0;
229   unsigned int assignment_operator::throw_on_ = 0;
230   unsigned int destructor::_M_count = 0;
231   int copy_tracker::next_id_ = 0;
232 }; // namespace __gnu_test
233
234 namespace std
235 {
236   // Member specializations for the existing facet classes.  
237   // NB: This isn't especially portable. Perhaps a better way would be
238   // to just specialize all of numpunct and ctype.
239   using __gnu_test::int_type;
240   using __gnu_test::value_type;
241   using __gnu_test::pod_type;
242
243   template<>
244     bool 
245     ctype<pod_type>::
246     do_is(mask, char_type) const { return true; }
247
248   template<>
249     const pod_type*
250     ctype<pod_type>::
251     do_is(const char_type* __lo, const char_type*, mask*) const
252     { return __lo; }
253
254   template<>
255     const pod_type*
256     ctype<pod_type>::
257     do_scan_is(mask, const char_type* __lo, const char_type*) const
258     { return __lo; }
259
260   template<>
261     const pod_type*
262     ctype<pod_type>::
263     do_scan_not(mask, const char_type* __lo, const char_type*) const
264     { return __lo; }
265
266   template<>
267     pod_type 
268     ctype<pod_type>::
269     do_toupper(char_type __c) const
270     { return __c; }
271
272   template<>
273     const pod_type*
274     ctype<pod_type>::
275     do_toupper(char_type*, const char_type* __hi) const
276     { return __hi; }
277
278   template<>
279     pod_type 
280     ctype<pod_type>::
281     do_tolower(char_type __c) const
282     { return __c; }
283
284   template<>
285     const pod_type*
286     ctype<pod_type>::
287     do_tolower(char_type*, const char_type* __hi) const
288     { return __hi; }
289
290   template<>
291     pod_type
292     ctype<pod_type>::
293     do_widen(char __c) const
294     { 
295       char_type ret = { value_type(__c) };
296       return ret;
297     }
298
299   template<>
300     const char*
301     ctype<pod_type>::
302     do_widen(const char* __lo, const char* __hi, char_type* __dest) const
303     {
304       while (__lo < __hi)
305         {
306           *__dest = this->do_widen(*__lo);
307           ++__lo;
308           ++__dest;
309         }
310       return __hi;
311     }
312
313   template<>
314     char
315     ctype<pod_type>::
316     do_narrow(char_type __wc, char) const
317     { return static_cast<char>(__wc.value); }
318
319   template<>
320     const pod_type*
321     ctype<pod_type>::
322     do_narrow(const pod_type* __lo, const pod_type* __hi, 
323               char, char* __dest) const
324     {
325       while (__lo < __hi)
326         {
327           *__dest = this->do_narrow(*__lo, char());
328           ++__lo;
329           ++__dest;
330         }
331       return __hi;
332     }
333
334   template<>
335     ctype<pod_type>::~ctype() { }
336
337   template<>
338     void
339     numpunct<pod_type>::_M_initialize_numpunct(__c_locale)
340     {
341       if (!_M_data)
342         _M_data = new __numpunct_cache<pod_type>;
343
344       _M_data->_M_grouping = "";
345       _M_data->_M_use_grouping = false;
346
347       _M_data->_M_decimal_point.value =  value_type('.');
348       _M_data->_M_thousands_sep.value = value_type(',');
349       
350       for (size_t i = 0; i < __num_base::_S_oend; ++i)
351         {
352           value_type v = __num_base::_S_atoms_out[i];
353           _M_data->_M_atoms_out[i].value = v;
354         }
355       _M_data->_M_atoms_out[__num_base::_S_oend] = pod_type();
356       
357       for (size_t i = 0; i < __num_base::_S_iend; ++i)
358         _M_data->_M_atoms_in[i].value = value_type(__num_base::_S_atoms_in[i]);
359       _M_data->_M_atoms_in[__num_base::_S_iend] = pod_type();
360
361       // "true"
362       pod_type* __truename = new pod_type[4 + 1];
363       __truename[0].value = value_type('t');
364       __truename[1].value = value_type('r');
365       __truename[2].value = value_type('u');
366       __truename[3].value = value_type('e');
367       __truename[4] = pod_type();
368       _M_data->_M_truename = __truename;
369
370       // "false"
371       pod_type* __falsename = new pod_type[5 + 1];
372       __falsename[0].value = value_type('f');
373       __falsename[1].value = value_type('a');
374       __falsename[2].value = value_type('l');
375       __falsename[3].value = value_type('s');
376       __falsename[4].value = value_type('e');
377       __falsename[5] = pod_type();
378       _M_data->_M_falsename = __falsename;
379     }
380
381   template<>
382     numpunct<pod_type>::~numpunct()
383     { delete _M_data; }
384 } // namespace std