2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_io.h
blobfe8827ca164ce3844af4ba79afb778d5112f60c7
1 // -*- C++ -*-
2 // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
3 //
4 // Copyright (C) 2003, 2004 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 <fstream>
35 #include <sstream>
37 namespace __gnu_test
39 // Used to verify the constraints/requirements on get and put areas
40 // as defined in
41 // 27.5.1 - Stream buffer requirements: get and put areas
42 // 27.8.1.1 - Template class basic_filebuf p 3
43 // If the file is not open (ios_base::in) -> input seq. cannot be read
44 // If the file is not open (ios_base::out) -> output seq. cannot be written
45 // Joint file position
46 // 27.8.1.4 - Overridden virtual functions p9
47 // If unbuffered, pbase == pptr == NULL
48 // 27.7.1.1 - Basic_stringbuf constructors p 1
49 // 27.8.1.2 - Basic_filebuf constructors p 1
50 // ... , initializing the base class with basic_streambuf() 27.5.2.1
51 template<typename T>
52 class constraint_buf
53 : public T
55 public:
56 bool
57 write_position()
59 bool one = this->pptr() != NULL;
60 bool two = this->pptr() < this->epptr();
61 return one && two;
64 bool
65 read_position()
67 bool one = this->gptr() != NULL;
68 bool two = this->gptr() < this->egptr();
69 return one && two;
72 bool
73 unbuffered()
75 bool one = this->pbase() == NULL;
76 bool two = this->pptr() == NULL;
77 return one && two;
80 bool
81 check_pointers()
83 bool one = this->eback() == NULL;
84 bool two = this->gptr() == NULL;
85 bool three = this->egptr() == NULL;
87 bool four = this->pbase() == NULL;
88 bool five = this->pptr() == NULL;
89 bool six = this->epptr() == NULL;
90 return one && two && three && four && five && six;
94 typedef constraint_buf<std::streambuf> constraint_streambuf;
95 typedef constraint_buf<std::filebuf> constraint_filebuf;
96 typedef constraint_buf<std::stringbuf> constraint_stringbuf;
97 #ifdef _GLIBCXX_USE_WCHAR_T
98 typedef constraint_buf<std::wstreambuf> constraint_wstreambuf;
99 typedef constraint_buf<std::wfilebuf> constraint_wfilebuf;
100 typedef constraint_buf<std::wstringbuf> constraint_wstringbuf;
101 #endif
103 // Used to check if basic_streambuf::pubsync() has been called.
104 // This is useful for checking if a function creates [io]stream::sentry
105 // objects, since the sentry constructors call tie()->flush().
106 template<typename T>
107 class sync_buf
108 : public T
110 private:
111 bool m_sync_called;
113 public:
114 sync_buf()
115 : m_sync_called(false)
118 bool sync_called() const
119 { return m_sync_called; }
121 protected:
122 int sync()
124 m_sync_called = true;
125 return 0;
129 typedef sync_buf<std::streambuf> sync_streambuf;
130 #ifdef _GLIBCXX_USE_WCHAR_T
131 typedef sync_buf<std::wstreambuf> sync_wstreambuf;
132 #endif
134 // Throws on all overflow and underflow calls.
135 struct underflow_error: std::exception { };
136 struct overflow_error: std::exception { };
137 struct positioning_error: std::exception { };
139 template<typename T>
140 struct fail_buf
141 : public T
143 typedef typename T::char_type char_type;
144 typedef typename T::int_type int_type;
145 typedef typename T::off_type off_type;
146 typedef typename T::pos_type pos_type;
148 private:
149 char_type p[2];
151 public:
152 fail_buf()
154 p[0] = char_type('s');
155 p[1] = char_type();
156 setg(p, p, p + 1);
159 virtual int_type underflow()
161 throw underflow_error();
162 return int_type();
165 virtual int_type uflow()
167 throw underflow_error();
168 return int_type();
171 virtual int_type
172 overflow(int_type)
174 throw overflow_error();
175 return int_type();
178 virtual pos_type
179 seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
181 throw positioning_error();
182 return pos_type(off_type(-1));
185 virtual pos_type
186 seekpos(pos_type, std::ios_base::openmode)
188 throw positioning_error();
189 return pos_type(off_type(-1));
192 virtual int
193 sync()
195 throw positioning_error();
196 return 0;
200 typedef fail_buf<std::streambuf> fail_streambuf;
201 #ifdef _GLIBCXX_USE_WCHAR_T
202 typedef fail_buf<std::wstreambuf> fail_wstreambuf;
203 #endif
205 // Facets that throw an exception for every virtual function.
206 struct facet_error: std::exception { };
208 template<typename T>
209 class fail_num_get
210 : public std::num_get<T>
212 typedef std::ios_base ios_base;
213 typedef typename std::num_get<T>::iter_type iter_type;
215 protected:
216 iter_type
217 do_get(iter_type a, iter_type, ios_base&, ios_base::iostate&, bool&) const
218 { throw facet_error(); return iter_type(); }
220 virtual iter_type
221 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const
222 { throw facet_error(); return iter_type(); }
224 virtual iter_type
225 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
226 unsigned short&) const
227 { throw facet_error(); return iter_type(); }
229 virtual iter_type
230 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
231 unsigned int&) const
232 { throw facet_error(); return iter_type(); }
234 virtual iter_type
235 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
236 unsigned long&) const
237 { throw facet_error(); return iter_type(); }
239 #ifdef _GLIBCXX_USE_LONG_LONG
240 virtual iter_type
241 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
242 long long&) const
243 { throw facet_error(); return iter_type(); }
245 virtual iter_type
246 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
247 unsigned long long&) const
248 { throw facet_error(); return iter_type(); }
249 #endif
251 virtual iter_type
252 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
253 float&) const
254 { throw facet_error(); return iter_type(); }
256 virtual iter_type
257 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
258 double&) const
259 { throw facet_error(); return iter_type(); }
261 virtual iter_type
262 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
263 long double&) const
264 { throw facet_error(); return iter_type(); }
266 virtual iter_type
267 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
268 void*&) const
269 { throw facet_error(); return iter_type(); }
272 typedef fail_num_get<char> fail_num_get_char;
273 #ifdef _GLIBCXX_USE_WCHAR_T
274 typedef fail_num_get<wchar_t> fail_num_get_wchar_t;
275 #endif
277 template<typename T>
278 class fail_num_put
279 : public std::num_put<T>
281 typedef std::ios_base ios_base;
282 typedef typename std::num_put<T>::iter_type iter_type;
283 typedef typename std::num_put<T>::char_type char_type;
285 protected:
286 iter_type
287 do_put(iter_type, ios_base&, char_type __fill, bool __v) const
288 { throw facet_error(); return iter_type(NULL); }
290 virtual iter_type
291 do_put(iter_type, ios_base&, char_type __fill, long __v) const
292 { throw facet_error(); return iter_type(NULL); }
294 virtual iter_type
295 do_put(iter_type, ios_base&, char_type __fill, unsigned long) const
296 { throw facet_error(); return iter_type(NULL); }
298 #ifdef _GLIBCXX_USE_LONG_LONG
299 virtual iter_type
300 do_put(iter_type, ios_base&, char_type __fill, long long __v) const
301 { throw facet_error(); return iter_type(NULL); }
303 virtual iter_type
304 do_put(iter_type, ios_base&, char_type __fill, unsigned long long) const
305 { throw facet_error(); return iter_type(NULL); }
306 #endif
308 virtual iter_type
309 do_put(iter_type, ios_base&, char_type __fill, double __v) const
310 { throw facet_error(); return iter_type(NULL); }
312 virtual iter_type
313 do_put(iter_type, ios_base&, char_type __fill, long double __v) const
314 { throw facet_error(); return iter_type(NULL); }
316 virtual iter_type
317 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const
318 { throw facet_error(); return iter_type(NULL); }
321 typedef fail_num_put<char> fail_num_put_char;
322 #ifdef _GLIBCXX_USE_WCHAR_T
323 typedef fail_num_put<wchar_t> fail_num_put_wchar_t;
324 #endif
325 }; // namespace __gnu_test
327 #endif // _GLIBCXX_TESTSUITE_IO_H