Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_io.h
blobd370bb0f921e73320be187f439f3a93fd2441e4a
1 // -*- C++ -*-
2 // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
3 //
4 // Copyright (C) 2003, 2004, 2005, 2006, 2007 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 #ifndef _GLIBCXX_TESTSUITE_IO_H
32 #define _GLIBCXX_TESTSUITE_IO_H
34 #include <ios>
36 namespace __gnu_test
38 // Used to verify the constraints/requirements on get and put areas
39 // as defined in
40 // 27.5.1 - Stream buffer requirements: get and put areas
41 // 27.8.1.1 - Template class basic_filebuf p 3
42 // If the file is not open (ios_base::in) -> input seq. cannot be read
43 // If the file is not open (ios_base::out) -> output seq. cannot be written
44 // Joint file position
45 // 27.8.1.4 - Overridden virtual functions p9
46 // If unbuffered, pbase == pptr == NULL
47 // 27.7.1.1 - Basic_stringbuf constructors p 1
48 // 27.8.1.2 - Basic_filebuf constructors p 1
49 // ... , initializing the base class with basic_streambuf() 27.5.2.1
50 template<typename T>
51 class constraint_buf
52 : public T
54 public:
55 bool
56 write_position()
58 bool one = this->pptr() != NULL;
59 bool two = this->pptr() < this->epptr();
60 return one && two;
63 bool
64 read_position()
66 bool one = this->gptr() != NULL;
67 bool two = this->gptr() < this->egptr();
68 return one && two;
71 bool
72 unbuffered()
74 bool one = this->pbase() == NULL;
75 bool two = this->pptr() == NULL;
76 return one && two;
79 bool
80 check_pointers()
82 bool one = this->eback() == NULL;
83 bool two = this->gptr() == NULL;
84 bool three = this->egptr() == NULL;
86 bool four = this->pbase() == NULL;
87 bool five = this->pptr() == NULL;
88 bool six = this->epptr() == NULL;
89 return one && two && three && four && five && six;
93 typedef constraint_buf<std::streambuf> constraint_streambuf;
94 typedef constraint_buf<std::filebuf> constraint_filebuf;
95 typedef constraint_buf<std::stringbuf> constraint_stringbuf;
96 #ifdef _GLIBCXX_USE_WCHAR_T
97 typedef constraint_buf<std::wstreambuf> constraint_wstreambuf;
98 typedef constraint_buf<std::wfilebuf> constraint_wfilebuf;
99 typedef constraint_buf<std::wstringbuf> constraint_wstringbuf;
100 #endif
102 // Used to check if basic_streambuf::pubsync() has been called.
103 // This is useful for checking if a function creates [io]stream::sentry
104 // objects, since the sentry constructors call tie()->flush().
105 template<typename T>
106 class sync_buf
107 : public T
109 private:
110 bool m_sync_called;
112 public:
113 sync_buf()
114 : m_sync_called(false)
117 bool sync_called() const
118 { return m_sync_called; }
120 protected:
121 int sync()
123 m_sync_called = true;
124 return 0;
128 typedef sync_buf<std::streambuf> sync_streambuf;
129 #ifdef _GLIBCXX_USE_WCHAR_T
130 typedef sync_buf<std::wstreambuf> sync_wstreambuf;
131 #endif
133 // Throws on all overflow and underflow calls.
134 struct underflow_error: std::exception { };
135 struct overflow_error: std::exception { };
136 struct positioning_error: std::exception { };
138 template<typename T>
139 struct fail_buf
140 : public T
142 typedef typename T::char_type char_type;
143 typedef typename T::int_type int_type;
144 typedef typename T::off_type off_type;
145 typedef typename T::pos_type pos_type;
147 private:
148 char_type p[2];
150 public:
151 fail_buf()
153 p[0] = char_type('s');
154 p[1] = char_type();
155 setg(p, p, p + 1);
158 virtual int_type underflow()
160 throw underflow_error();
161 return int_type();
164 virtual int_type uflow()
166 throw underflow_error();
167 return int_type();
170 virtual int_type
171 overflow(int_type)
173 throw overflow_error();
174 return int_type();
177 virtual pos_type
178 seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
180 throw positioning_error();
181 return pos_type(off_type(-1));
184 virtual pos_type
185 seekpos(pos_type, std::ios_base::openmode)
187 throw positioning_error();
188 return pos_type(off_type(-1));
191 virtual int
192 sync()
194 throw positioning_error();
195 return 0;
199 typedef fail_buf<std::streambuf> fail_streambuf;
200 #ifdef _GLIBCXX_USE_WCHAR_T
201 typedef fail_buf<std::wstreambuf> fail_wstreambuf;
202 #endif
204 // Facets that throw an exception for every virtual function.
205 struct facet_error: std::exception { };
207 template<typename T>
208 class fail_num_get
209 : public std::num_get<T>
211 typedef std::ios_base ios_base;
212 typedef typename std::num_get<T>::iter_type iter_type;
214 protected:
215 iter_type
216 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const
217 { throw facet_error(); return iter_type(); }
219 virtual iter_type
220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const
221 { throw facet_error(); return iter_type(); }
223 virtual iter_type
224 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
225 unsigned short&) const
226 { throw facet_error(); return iter_type(); }
228 virtual iter_type
229 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
230 unsigned int&) const
231 { throw facet_error(); return iter_type(); }
233 virtual iter_type
234 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
235 unsigned long&) const
236 { throw facet_error(); return iter_type(); }
238 #ifdef _GLIBCXX_USE_LONG_LONG
239 virtual iter_type
240 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
241 long long&) const
242 { throw facet_error(); return iter_type(); }
244 virtual iter_type
245 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
246 unsigned long long&) const
247 { throw facet_error(); return iter_type(); }
248 #endif
250 virtual iter_type
251 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
252 float&) const
253 { throw facet_error(); return iter_type(); }
255 virtual iter_type
256 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
257 double&) const
258 { throw facet_error(); return iter_type(); }
260 virtual iter_type
261 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
262 long double&) const
263 { throw facet_error(); return iter_type(); }
265 virtual iter_type
266 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
267 void*&) const
268 { throw facet_error(); return iter_type(); }
271 typedef fail_num_get<char> fail_num_get_char;
272 #ifdef _GLIBCXX_USE_WCHAR_T
273 typedef fail_num_get<wchar_t> fail_num_get_wchar_t;
274 #endif
276 template<typename T>
277 class fail_num_put
278 : public std::num_put<T>
280 typedef std::ios_base ios_base;
281 typedef typename std::num_put<T>::iter_type iter_type;
282 typedef typename std::num_put<T>::char_type char_type;
284 protected:
285 iter_type
286 do_put(iter_type, ios_base&, char_type, bool) const
287 { throw facet_error(); return iter_type(NULL); }
289 virtual iter_type
290 do_put(iter_type, ios_base&, char_type, long) const
291 { throw facet_error(); return iter_type(NULL); }
293 virtual iter_type
294 do_put(iter_type, ios_base&, char_type, unsigned long) const
295 { throw facet_error(); return iter_type(NULL); }
297 #ifdef _GLIBCXX_USE_LONG_LONG
298 virtual iter_type
299 do_put(iter_type, ios_base&, char_type, long long) const
300 { throw facet_error(); return iter_type(NULL); }
302 virtual iter_type
303 do_put(iter_type, ios_base&, char_type, unsigned long long) const
304 { throw facet_error(); return iter_type(NULL); }
305 #endif
307 virtual iter_type
308 do_put(iter_type, ios_base&, char_type, double) const
309 { throw facet_error(); return iter_type(NULL); }
311 virtual iter_type
312 do_put(iter_type, ios_base&, char_type, long double) const
313 { throw facet_error(); return iter_type(NULL); }
315 virtual iter_type
316 do_put(iter_type, ios_base&, char_type, const void*) const
317 { throw facet_error(); return iter_type(NULL); }
320 typedef fail_num_put<char> fail_num_put_char;
321 #ifdef _GLIBCXX_USE_WCHAR_T
322 typedef fail_num_put<wchar_t> fail_num_put_wchar_t;
323 #endif
324 } // namespace __gnu_test
326 #endif // _GLIBCXX_TESTSUITE_IO_H