2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / testsuite / util / testsuite_io.h
blob6fcfd9a4d639a829baf0c6f4917e99e8a945ed10
1 // -*- C++ -*-
2 // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
3 //
4 // Copyright (C) 2003-2018 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 3, 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 COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
22 #ifndef _GLIBCXX_TESTSUITE_IO_H
23 #define _GLIBCXX_TESTSUITE_IO_H
25 #include <ios>
27 namespace __gnu_test
29 // Used to verify the constraints/requirements on get and put areas
30 // as defined in
31 // 27.5.1 - Stream buffer requirements: get and put areas
32 // 27.8.1.1 - Template class basic_filebuf p 3
33 // If the file is not open (ios_base::in) -> input seq. cannot be read
34 // If the file is not open (ios_base::out) -> output seq. cannot be written
35 // Joint file position
36 // 27.8.1.4 - Overridden virtual functions p9
37 // If unbuffered, pbase == pptr == NULL
38 // 27.7.1.1 - Basic_stringbuf constructors p 1
39 // 27.8.1.2 - Basic_filebuf constructors p 1
40 // ... , initializing the base class with basic_streambuf() 27.5.2.1
41 template<typename T>
42 class constraint_buf
43 : public T
45 public:
46 bool
47 write_position()
49 bool one = this->pptr() != 0;
50 bool two = this->pptr() < this->epptr();
51 return one && two;
54 bool
55 read_position()
57 bool one = this->gptr() != 0;
58 bool two = this->gptr() < this->egptr();
59 return one && two;
62 bool
63 unbuffered()
65 bool one = this->pbase() == 0;
66 bool two = this->pptr() == 0;
67 return one && two;
70 bool
71 check_pointers()
73 bool one = this->eback() == 0;
74 bool two = this->gptr() == 0;
75 bool three = this->egptr() == 0;
77 bool four = this->pbase() == 0;
78 bool five = this->pptr() == 0;
79 bool six = this->epptr() == 0;
80 return one && two && three && four && five && six;
84 typedef constraint_buf<std::streambuf> constraint_streambuf;
85 typedef constraint_buf<std::filebuf> constraint_filebuf;
86 typedef constraint_buf<std::stringbuf> constraint_stringbuf;
87 #ifdef _GLIBCXX_USE_WCHAR_T
88 typedef constraint_buf<std::wstreambuf> constraint_wstreambuf;
89 typedef constraint_buf<std::wfilebuf> constraint_wfilebuf;
90 typedef constraint_buf<std::wstringbuf> constraint_wstringbuf;
91 #endif
93 // Used to check if basic_streambuf::pubsync() has been called.
94 // This is useful for checking if a function creates [io]stream::sentry
95 // objects, since the sentry constructors call tie()->flush().
96 template<typename T>
97 class sync_buf
98 : public T
100 private:
101 bool m_sync_called;
103 public:
104 sync_buf()
105 : m_sync_called(false)
108 bool sync_called() const
109 { return m_sync_called; }
111 protected:
112 int sync()
114 m_sync_called = true;
115 return 0;
119 typedef sync_buf<std::streambuf> sync_streambuf;
120 #ifdef _GLIBCXX_USE_WCHAR_T
121 typedef sync_buf<std::wstreambuf> sync_wstreambuf;
122 #endif
124 // Throws on all overflow and underflow calls.
125 struct underflow_error: std::exception { };
126 struct overflow_error: std::exception { };
127 struct positioning_error: std::exception { };
129 template<typename T>
130 struct fail_buf
131 : public T
133 typedef typename T::char_type char_type;
134 typedef typename T::int_type int_type;
135 typedef typename T::off_type off_type;
136 typedef typename T::pos_type pos_type;
138 private:
139 char_type p[2];
141 public:
142 fail_buf()
144 p[0] = char_type('s');
145 p[1] = char_type();
146 this->setg(p, p, p + 1);
149 virtual int_type underflow()
151 throw underflow_error();
152 return int_type();
155 virtual int_type uflow()
157 throw underflow_error();
158 return int_type();
161 virtual int_type
162 overflow(int_type)
164 throw overflow_error();
165 return int_type();
168 virtual pos_type
169 seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
171 throw positioning_error();
172 return pos_type(off_type(-1));
175 virtual pos_type
176 seekpos(pos_type, std::ios_base::openmode)
178 throw positioning_error();
179 return pos_type(off_type(-1));
182 virtual int
183 sync()
185 throw positioning_error();
186 return 0;
190 typedef fail_buf<std::streambuf> fail_streambuf;
191 #ifdef _GLIBCXX_USE_WCHAR_T
192 typedef fail_buf<std::wstreambuf> fail_wstreambuf;
193 #endif
195 // Facets that throw an exception for every virtual function.
196 struct facet_error: std::exception { };
198 template<typename T>
199 class fail_num_get
200 : public std::num_get<T>
202 typedef std::ios_base ios_base;
203 typedef typename std::num_get<T>::iter_type iter_type;
205 protected:
206 iter_type
207 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const
208 { throw facet_error(); return iter_type(); }
210 virtual iter_type
211 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const
212 { throw facet_error(); return iter_type(); }
214 virtual iter_type
215 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
216 unsigned short&) const
217 { throw facet_error(); return iter_type(); }
219 virtual iter_type
220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
221 unsigned int&) const
222 { throw facet_error(); return iter_type(); }
224 virtual iter_type
225 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
226 unsigned long&) const
227 { throw facet_error(); return iter_type(); }
229 #ifdef _GLIBCXX_USE_LONG_LONG
230 virtual iter_type
231 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
232 long long&) const
233 { throw facet_error(); return iter_type(); }
235 virtual iter_type
236 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
237 unsigned long long&) const
238 { throw facet_error(); return iter_type(); }
239 #endif
241 virtual iter_type
242 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
243 float&) const
244 { throw facet_error(); return iter_type(); }
246 virtual iter_type
247 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
248 double&) const
249 { throw facet_error(); return iter_type(); }
251 virtual iter_type
252 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
253 long double&) const
254 { throw facet_error(); return iter_type(); }
256 virtual iter_type
257 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
258 void*&) const
259 { throw facet_error(); return iter_type(); }
262 typedef fail_num_get<char> fail_num_get_char;
263 #ifdef _GLIBCXX_USE_WCHAR_T
264 typedef fail_num_get<wchar_t> fail_num_get_wchar_t;
265 #endif
267 template<typename T>
268 class fail_num_put
269 : public std::num_put<T>
271 typedef std::ios_base ios_base;
272 typedef typename std::num_put<T>::iter_type iter_type;
273 typedef typename std::num_put<T>::char_type char_type;
275 protected:
276 iter_type
277 do_put(iter_type, ios_base&, char_type, bool) const
278 { throw facet_error(); return iter_type(0); }
280 virtual iter_type
281 do_put(iter_type, ios_base&, char_type, long) const
282 { throw facet_error(); return iter_type(0); }
284 virtual iter_type
285 do_put(iter_type, ios_base&, char_type, unsigned long) const
286 { throw facet_error(); return iter_type(0); }
288 #ifdef _GLIBCXX_USE_LONG_LONG
289 virtual iter_type
290 do_put(iter_type, ios_base&, char_type, long long) const
291 { throw facet_error(); return iter_type(0); }
293 virtual iter_type
294 do_put(iter_type, ios_base&, char_type, unsigned long long) const
295 { throw facet_error(); return iter_type(0); }
296 #endif
298 virtual iter_type
299 do_put(iter_type, ios_base&, char_type, double) const
300 { throw facet_error(); return iter_type(0); }
302 virtual iter_type
303 do_put(iter_type, ios_base&, char_type, long double) const
304 { throw facet_error(); return iter_type(0); }
306 virtual iter_type
307 do_put(iter_type, ios_base&, char_type, const void*) const
308 { throw facet_error(); return iter_type(0); }
311 typedef fail_num_put<char> fail_num_put_char;
312 #ifdef _GLIBCXX_USE_WCHAR_T
313 typedef fail_num_put<wchar_t> fail_num_put_wchar_t;
314 #endif
315 } // namespace __gnu_test
317 #endif // _GLIBCXX_TESTSUITE_IO_H