2 // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
4 // Copyright (C) 2003-2025 Free Software Foundation, Inc.
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)
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
29 // Used to verify the constraints/requirements on get and put areas
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
49 bool one
= this->pptr() != 0;
50 bool two
= this->pptr() < this->epptr();
57 bool one
= this->gptr() != 0;
58 bool two
= this->gptr() < this->egptr();
65 bool one
= this->pbase() == 0;
66 bool two
= this->pptr() == 0;
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
;
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().
105 : m_sync_called(false)
108 bool sync_called() const
109 { return m_sync_called
; }
114 m_sync_called
= true;
119 typedef sync_buf
<std::streambuf
> sync_streambuf
;
120 #ifdef _GLIBCXX_USE_WCHAR_T
121 typedef sync_buf
<std::wstreambuf
> sync_wstreambuf
;
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
{ };
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
;
144 p
[0] = char_type('s');
146 this->setg(p
, p
, p
+ 1);
149 virtual int_type
underflow()
151 throw underflow_error();
155 virtual int_type
uflow()
157 throw underflow_error();
164 throw overflow_error();
169 seekoff(off_type
, std::ios_base::seekdir
, std::ios_base::openmode
)
171 throw positioning_error();
172 return pos_type(off_type(-1));
176 seekpos(pos_type
, std::ios_base::openmode
)
178 throw positioning_error();
179 return pos_type(off_type(-1));
185 throw positioning_error();
190 typedef fail_buf
<std::streambuf
> fail_streambuf
;
191 #ifdef _GLIBCXX_USE_WCHAR_T
192 typedef fail_buf
<std::wstreambuf
> fail_wstreambuf
;
195 // Facets that throw an exception for every virtual function.
196 struct facet_error
: std::exception
{ };
200 : public std::num_get
<T
>
202 typedef std::ios_base ios_base
;
203 typedef typename
std::num_get
<T
>::iter_type iter_type
;
207 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&, bool&) const
208 { throw facet_error(); return iter_type(); }
211 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&, long&) const
212 { throw facet_error(); return 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(); }
220 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
222 { throw facet_error(); return 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
231 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
233 { throw facet_error(); return 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(); }
242 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
244 { throw facet_error(); return iter_type(); }
247 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
249 { throw facet_error(); return iter_type(); }
252 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
254 { throw facet_error(); return iter_type(); }
257 do_get(iter_type
, iter_type
, ios_base
&, ios_base::iostate
&,
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
;
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
;
277 do_put(iter_type
, ios_base
&, char_type
, bool) const
278 { throw facet_error(); return iter_type(0); }
281 do_put(iter_type
, ios_base
&, char_type
, long) const
282 { throw facet_error(); return iter_type(0); }
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
290 do_put(iter_type
, ios_base
&, char_type
, long long) const
291 { throw facet_error(); return iter_type(0); }
294 do_put(iter_type
, ios_base
&, char_type
, unsigned long long) const
295 { throw facet_error(); return iter_type(0); }
299 do_put(iter_type
, ios_base
&, char_type
, double) const
300 { throw facet_error(); return iter_type(0); }
303 do_put(iter_type
, ios_base
&, char_type
, long double) const
304 { throw facet_error(); return iter_type(0); }
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
;
315 } // namespace __gnu_test
317 #endif // _GLIBCXX_TESTSUITE_IO_H