OSDN Git Service

* testsuite/testsuite_hooks.h (run_tests_wrapped_env): Do not
[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 _GLIBCPP_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_cxx_test
46 {
47 #ifdef _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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 #if _GLIBCPP_HAVE_MEMLIMIT_AS
78     getrlimit(RLIMIT_AS, &r);
79     r.rlim_cur = limit;
80     setrlimit(RLIMIT_AS, &r);
81 #endif
82   }
83
84 #else
85   void
86   set_memory_limits(float) { }
87 #endif 
88
89
90   void 
91   verify_demangle(const char* mangled, const char* wanted)
92   {
93     int status = 0;
94     const char* s = abi::__cxa_demangle(mangled, 0, 0, &status);
95     if (!s)
96       {
97         switch (status)
98           {
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";
113           }
114       }
115
116     std::string w(wanted);
117     if (w != s)
118       throw std::runtime_error(s);
119   }
120
121   
122   // Useful exceptions.
123   class locale_data : public std::runtime_error 
124   {
125   public:
126     explicit 
127     locale_data(const std::string&  __arg) : runtime_error(__arg) { }
128   };
129
130   class environment_variable: public std::runtime_error 
131   {
132   public:
133     explicit 
134     environment_variable(const std::string&  __arg) : runtime_error(__arg) { }
135   };
136
137   class not_found : public std::runtime_error 
138   {
139   public:
140     explicit 
141     not_found(const std::string&  __arg) : runtime_error(__arg) { }
142   };
143
144   void 
145   run_tests_wrapped_locale(const char* name, const func_callback& l)
146   {
147     using namespace std;
148     bool test = true;
149     
150     // Set the global locale. 
151     try
152       {
153         locale loc_name(name);
154         locale orig = locale::global(loc_name);
155       }
156     catch (std::runtime_error& ex)
157       {
158         if (std::strstr (ex.what(), "unhandled name in generic implementation"))
159           return;
160         else
161           throw;
162       }
163
164     const char* res = setlocale(LC_ALL, name);
165     if (res != NULL)
166       {
167         string preLC_ALL = res;
168         for (func_callback::const_iterator i = l.begin(); i != l.end(); ++i)
169           (*i)();
170         string postLC_ALL= setlocale(LC_ALL, NULL);
171         VERIFY( preLC_ALL == postLC_ALL );
172       }
173     else
174       throw environment_variable(string("LC_ALL for") + string(name));
175   }
176   
177   void 
178   run_tests_wrapped_env(const char* name, const char* env,
179                         const func_callback& l)
180   {
181     using namespace std;
182     bool test = true;
183     
184 #ifdef _GLIBCPP_HAVE_SETENV 
185     // Set the global locale. 
186     try
187       {
188         locale loc_name(name);
189         locale orig = locale::global(loc_name);
190       }
191     catch (std::runtime_error& ex)
192       {
193         if (std::strstr (ex.what(), "unhandled name in generic implementation"))
194           return;
195         else
196           throw;
197       }
198     // Set environment variable env to value in name. 
199     const char* oldENV = getenv(env);
200     if (!setenv(env, name, 1))
201       {
202         for (func_callback::const_iterator i = l.begin(); i != l.end(); ++i)
203           (*i)();
204         setenv(env, oldENV ? oldENV : "", 1);
205       }
206     else
207       throw environment_variable(string(env) + string(" to ") + string(name));
208 #endif
209   }
210
211   void 
212   run_test_wrapped_generic_locale_exception_catcher(const test_func f)
213   {
214     try
215       {
216         f();
217       }
218     catch (std::runtime_error& ex)
219       {
220         if (std::strstr (ex.what(), "unhandled name in generic implementation"))
221           return;
222         else
223           throw;
224       }
225   }
226
227   counter::size_type  counter::count = 0;
228   unsigned int copy_constructor::count_ = 0;
229   unsigned int copy_constructor::throw_on_ = 0;
230   unsigned int assignment_operator::count_ = 0;
231   unsigned int assignment_operator::throw_on_ = 0;
232   unsigned int destructor::_M_count = 0;
233   int copy_tracker::next_id_ = 0;
234 }; // namespace __cxx_test