OSDN Git Service

4e714fa6cbaf501c238c79fc99cb7432f58c07f2
[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
44 namespace __gnu_cxx_test
45 {
46 #ifdef _GLIBCPP_MEM_LIMITS
47   void 
48   set_memory_limits(float size)
49   {
50     struct rlimit r;
51     // Cater to the absence of rlim_t.
52     __typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur))(size * 1048576);
53
54     // Heap size, seems to be common.
55 #if _GLIBCPP_HAVE_MEMLIMIT_DATA
56     getrlimit(RLIMIT_DATA, &r);
57     r.rlim_cur = limit;
58     setrlimit(RLIMIT_DATA, &r);
59 #endif
60
61     // Resident set size.
62 #if _GLIBCPP_HAVE_MEMLIMIT_RSS
63     getrlimit(RLIMIT_RSS, &r);
64     r.rlim_cur = limit;
65     setrlimit(RLIMIT_RSS, &r);
66 #endif
67
68     // Mapped memory (brk + mmap).
69 #if _GLIBCPP_HAVE_MEMLIMIT_VMEM
70     getrlimit(RLIMIT_VMEM, &r);
71     r.rlim_cur = limit;
72     setrlimit(RLIMIT_VMEM, &r);
73 #endif
74
75     // Virtual memory.
76 #if _GLIBCPP_HAVE_MEMLIMIT_AS
77     getrlimit(RLIMIT_AS, &r);
78     r.rlim_cur = limit;
79     setrlimit(RLIMIT_AS, &r);
80 #endif
81   }
82
83 #else
84   void
85   set_memory_limits(float) { }
86 #endif 
87
88   // Useful exceptions.
89   class locale_data : public std::runtime_error 
90   {
91   public:
92     explicit 
93     locale_data(const std::string&  __arg) : runtime_error(__arg) { }
94   };
95
96   class environment_variable: public std::runtime_error 
97   {
98   public:
99     explicit 
100     environment_variable(const std::string&  __arg) : runtime_error(__arg) { }
101   };
102
103   class not_found : public std::runtime_error 
104   {
105   public:
106     explicit 
107     not_found(const std::string&  __arg) : runtime_error(__arg) { }
108   };
109
110   void 
111   run_tests_wrapped_locale(const char* name, const func_callback& l)
112   {
113     using namespace std;
114     bool test = true;
115     
116     // Set the global locale. 
117     locale loc_name(name);
118     locale orig = locale::global(loc_name);
119     
120     const char* res = setlocale(LC_ALL, name);
121     if (res != NULL)
122       {
123         string preLC_ALL = res;
124         for (func_callback::const_iterator i = l.begin(); i != l.end(); ++i)
125           (*i)();
126         string postLC_ALL= setlocale(LC_ALL, NULL);
127         VERIFY( preLC_ALL == postLC_ALL );
128       }
129     else
130       throw environment_variable(string("LC_ALL for") + string(name));
131   }
132   
133   void 
134   run_tests_wrapped_env(const char* name, const char* env,
135                         const func_callback& l)
136   {
137     using namespace std;
138     bool test = true;
139     
140 #ifdef _GLIBCPP_HAVE_SETENV 
141     // Set the global locale. 
142     locale loc_name(name);
143     locale orig = locale::global(loc_name);
144
145     // Set environment variable env to value in name. 
146     const char* oldENV = getenv(env);
147     if (!setenv(env, name, 1))
148       {
149         for (func_callback::const_iterator i = l.begin(); i != l.end(); ++i)
150           (*i)();
151         setenv(env, oldENV ? oldENV : "", 1);
152       }
153     else
154       throw environment_variable(string(env) + string(" to ") + string(name));
155 #else
156     throw not_found("setenv");
157 #endif
158   }
159
160   counter::size_type  counter::count = 0;
161   unsigned int copy_constructor::count_ = 0;
162   unsigned int copy_constructor::throw_on_ = 0;
163   unsigned int assignment_operator::count_ = 0;
164   unsigned int assignment_operator::throw_on_ = 0;
165   unsigned int destructor::_M_count = 0;
166   int copy_tracker::next_id_ = 0;
167 }; // namespace __cxx_test