]>
Commit | Line | Data |
---|---|---|
46c4e5d6 | 1 | // -*- C++ -*- |
983de0da | 2 | // Testing streambuf/filebuf/stringbuf for the C++ library testsuite. |
46c4e5d6 | 3 | // |
7adcbafe | 4 | // Copyright (C) 2003-2022 Free Software Foundation, Inc. |
46c4e5d6 BK |
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 | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
46c4e5d6 BK |
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 | |
748086b7 JJ |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
46c4e5d6 | 20 | // |
46c4e5d6 | 21 | |
3d7c150e BK |
22 | #ifndef _GLIBCXX_TESTSUITE_IO_H |
23 | #define _GLIBCXX_TESTSUITE_IO_H | |
46c4e5d6 | 24 | |
2fdff2be | 25 | #include <ios> |
46c4e5d6 | 26 | |
aecf642c | 27 | namespace __gnu_test |
f92ab29f | 28 | { |
46c4e5d6 | 29 | // Used to verify the constraints/requirements on get and put areas |
f92ab29f | 30 | // as defined in |
46c4e5d6 BK |
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 | |
983de0da PC |
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 | |
44 | { | |
45 | public: | |
46 | bool | |
f92ab29f CG |
47 | write_position() |
48 | { | |
49 | bool one = this->pptr() != 0; | |
983de0da PC |
50 | bool two = this->pptr() < this->epptr(); |
51 | return one && two; | |
52 | } | |
f92ab29f | 53 | |
983de0da PC |
54 | bool |
55 | read_position() | |
f92ab29f CG |
56 | { |
57 | bool one = this->gptr() != 0; | |
983de0da PC |
58 | bool two = this->gptr() < this->egptr(); |
59 | return one && two; | |
60 | } | |
f92ab29f | 61 | |
983de0da | 62 | bool |
f92ab29f CG |
63 | unbuffered() |
64 | { | |
65 | bool one = this->pbase() == 0; | |
66 | bool two = this->pptr() == 0; | |
983de0da PC |
67 | return one && two; |
68 | } | |
f92ab29f | 69 | |
983de0da PC |
70 | bool |
71 | check_pointers() | |
72 | { | |
8fc81078 PC |
73 | bool one = this->eback() == 0; |
74 | bool two = this->gptr() == 0; | |
75 | bool three = this->egptr() == 0; | |
f92ab29f | 76 | |
8fc81078 PC |
77 | bool four = this->pbase() == 0; |
78 | bool five = this->pptr() == 0; | |
79 | bool six = this->epptr() == 0; | |
983de0da PC |
80 | return one && two && three && four && five && six; |
81 | } | |
82 | }; | |
46c4e5d6 | 83 | |
983de0da PC |
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 | |
5681c890 PR |
92 | |
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(). | |
9b8d9ac3 PC |
96 | template<typename T> |
97 | class sync_buf | |
98 | : public T | |
99 | { | |
100 | private: | |
101 | bool m_sync_called; | |
f92ab29f | 102 | |
9b8d9ac3 PC |
103 | public: |
104 | sync_buf() | |
105 | : m_sync_called(false) | |
106 | { } | |
f92ab29f | 107 | |
9b8d9ac3 PC |
108 | bool sync_called() const |
109 | { return m_sync_called; } | |
5681c890 | 110 | |
9b8d9ac3 PC |
111 | protected: |
112 | int sync() | |
113 | { | |
114 | m_sync_called = true; | |
115 | return 0; | |
116 | } | |
117 | }; | |
5681c890 | 118 | |
9b8d9ac3 PC |
119 | typedef sync_buf<std::streambuf> sync_streambuf; |
120 | #ifdef _GLIBCXX_USE_WCHAR_T | |
121 | typedef sync_buf<std::wstreambuf> sync_wstreambuf; | |
122 | #endif | |
12841eb3 BK |
123 | |
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 { }; | |
128 | ||
9b8d9ac3 PC |
129 | template<typename T> |
130 | struct fail_buf | |
131 | : public T | |
12841eb3 | 132 | { |
9b8d9ac3 PC |
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; | |
12841eb3 | 137 | |
9b8d9ac3 PC |
138 | private: |
139 | char_type p[2]; | |
12841eb3 | 140 | |
9b8d9ac3 PC |
141 | public: |
142 | fail_buf() | |
143 | { | |
144 | p[0] = char_type('s'); | |
145 | p[1] = char_type(); | |
94df301f | 146 | this->setg(p, p, p + 1); |
9b8d9ac3 | 147 | } |
12841eb3 | 148 | |
f92ab29f | 149 | virtual int_type underflow() |
9b8d9ac3 PC |
150 | { |
151 | throw underflow_error(); | |
cfc45d90 | 152 | return int_type(); |
9b8d9ac3 | 153 | } |
f92ab29f CG |
154 | |
155 | virtual int_type uflow() | |
9b8d9ac3 PC |
156 | { |
157 | throw underflow_error(); | |
cfc45d90 | 158 | return int_type(); |
9b8d9ac3 | 159 | } |
f92ab29f | 160 | |
9b8d9ac3 PC |
161 | virtual int_type |
162 | overflow(int_type) | |
163 | { | |
164 | throw overflow_error(); | |
cfc45d90 | 165 | return int_type(); |
9b8d9ac3 | 166 | } |
f92ab29f CG |
167 | |
168 | virtual pos_type | |
9b8d9ac3 | 169 | seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) |
f92ab29f | 170 | { |
9b8d9ac3 | 171 | throw positioning_error(); |
f92ab29f CG |
172 | return pos_type(off_type(-1)); |
173 | } | |
174 | ||
175 | virtual pos_type | |
9b8d9ac3 | 176 | seekpos(pos_type, std::ios_base::openmode) |
f92ab29f | 177 | { |
9b8d9ac3 | 178 | throw positioning_error(); |
f92ab29f CG |
179 | return pos_type(off_type(-1)); |
180 | } | |
181 | ||
182 | virtual int | |
183 | sync() | |
184 | { | |
9b8d9ac3 | 185 | throw positioning_error(); |
f92ab29f | 186 | return 0; |
9b8d9ac3 PC |
187 | } |
188 | }; | |
12841eb3 | 189 | |
9b8d9ac3 PC |
190 | typedef fail_buf<std::streambuf> fail_streambuf; |
191 | #ifdef _GLIBCXX_USE_WCHAR_T | |
192 | typedef fail_buf<std::wstreambuf> fail_wstreambuf; | |
193 | #endif | |
12841eb3 BK |
194 | |
195 | // Facets that throw an exception for every virtual function. | |
196 | struct facet_error: std::exception { }; | |
197 | ||
9b8d9ac3 PC |
198 | template<typename T> |
199 | class fail_num_get | |
200 | : public std::num_get<T> | |
201 | { | |
202 | typedef std::ios_base ios_base; | |
203 | typedef typename std::num_get<T>::iter_type iter_type; | |
12841eb3 | 204 | |
9b8d9ac3 | 205 | protected: |
f92ab29f | 206 | iter_type |
8b5bc374 | 207 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const |
9b8d9ac3 | 208 | { throw facet_error(); return iter_type(); } |
12841eb3 | 209 | |
f92ab29f | 210 | virtual iter_type |
9b8d9ac3 PC |
211 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const |
212 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
213 | |
214 | virtual iter_type | |
215 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
216 | unsigned short&) const |
217 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
218 | |
219 | virtual iter_type | |
220 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
221 | unsigned int&) const |
222 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
223 | |
224 | virtual iter_type | |
225 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
226 | unsigned long&) const |
227 | { throw facet_error(); return iter_type(); } | |
12841eb3 | 228 | |
f92ab29f CG |
229 | #ifdef _GLIBCXX_USE_LONG_LONG |
230 | virtual iter_type | |
231 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
232 | long long&) const |
233 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
234 | |
235 | virtual iter_type | |
236 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
237 | unsigned long long&) const |
238 | { throw facet_error(); return iter_type(); } | |
12841eb3 BK |
239 | #endif |
240 | ||
f92ab29f CG |
241 | virtual iter_type |
242 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
243 | float&) const |
244 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
245 | |
246 | virtual iter_type | |
247 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
248 | double&) const |
249 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
250 | |
251 | virtual iter_type | |
252 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
253 | long double&) const |
254 | { throw facet_error(); return iter_type(); } | |
f92ab29f CG |
255 | |
256 | virtual iter_type | |
257 | do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, | |
9b8d9ac3 PC |
258 | void*&) const |
259 | { throw facet_error(); return iter_type(); } | |
260 | }; | |
12841eb3 | 261 | |
9b8d9ac3 PC |
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 | |
12841eb3 | 266 | |
9b8d9ac3 PC |
267 | template<typename T> |
268 | class fail_num_put | |
269 | : public std::num_put<T> | |
270 | { | |
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; | |
274 | ||
275 | protected: | |
f92ab29f | 276 | iter_type |
8b5bc374 | 277 | do_put(iter_type, ios_base&, char_type, bool) const |
8fc81078 | 278 | { throw facet_error(); return iter_type(0); } |
f92ab29f CG |
279 | |
280 | virtual iter_type | |
8b5bc374 | 281 | do_put(iter_type, ios_base&, char_type, long) const |
8fc81078 | 282 | { throw facet_error(); return iter_type(0); } |
12841eb3 | 283 | |
f92ab29f | 284 | virtual iter_type |
8b5bc374 | 285 | do_put(iter_type, ios_base&, char_type, unsigned long) const |
8fc81078 | 286 | { throw facet_error(); return iter_type(0); } |
12841eb3 | 287 | |
f92ab29f CG |
288 | #ifdef _GLIBCXX_USE_LONG_LONG |
289 | virtual iter_type | |
8b5bc374 | 290 | do_put(iter_type, ios_base&, char_type, long long) const |
8fc81078 | 291 | { throw facet_error(); return iter_type(0); } |
12841eb3 | 292 | |
f92ab29f | 293 | virtual iter_type |
8b5bc374 | 294 | do_put(iter_type, ios_base&, char_type, unsigned long long) const |
8fc81078 | 295 | { throw facet_error(); return iter_type(0); } |
12841eb3 | 296 | #endif |
f92ab29f CG |
297 | |
298 | virtual iter_type | |
8b5bc374 | 299 | do_put(iter_type, ios_base&, char_type, double) const |
8fc81078 | 300 | { throw facet_error(); return iter_type(0); } |
12841eb3 | 301 | |
f92ab29f | 302 | virtual iter_type |
8b5bc374 | 303 | do_put(iter_type, ios_base&, char_type, long double) const |
8fc81078 | 304 | { throw facet_error(); return iter_type(0); } |
f92ab29f CG |
305 | |
306 | virtual iter_type | |
8b5bc374 | 307 | do_put(iter_type, ios_base&, char_type, const void*) const |
8fc81078 | 308 | { throw facet_error(); return iter_type(0); } |
9b8d9ac3 | 309 | }; |
12841eb3 | 310 | |
9b8d9ac3 PC |
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 | |
799a6e36 | 315 | } // namespace __gnu_test |
46c4e5d6 | 316 | |
3d7c150e | 317 | #endif // _GLIBCXX_TESTSUITE_IO_H |
46c4e5d6 | 318 |