]>
Commit | Line | Data |
---|---|---|
46c4e5d6 | 1 | // -*- C++ -*- |
983de0da | 2 | // Testing streambuf/filebuf/stringbuf for the C++ library testsuite. |
46c4e5d6 | 3 | // |
983de0da | 4 | // Copyright (C) 2003, 2004 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 | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
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 | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
46c4e5d6 BK |
20 | // USA. |
21 | // | |
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. | |
30 | ||
3d7c150e BK |
31 | #ifndef _GLIBCXX_TESTSUITE_IO_H |
32 | #define _GLIBCXX_TESTSUITE_IO_H | |
46c4e5d6 BK |
33 | |
34 | #include <fstream> | |
983de0da | 35 | #include <sstream> |
46c4e5d6 | 36 | |
aecf642c | 37 | namespace __gnu_test |
46c4e5d6 BK |
38 | { |
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 | |
983de0da PC |
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 | |
54 | { | |
55 | public: | |
56 | bool | |
57 | write_position() | |
58 | { | |
59 | bool one = this->pptr() != NULL; | |
60 | bool two = this->pptr() < this->epptr(); | |
61 | return one && two; | |
62 | } | |
63 | ||
64 | bool | |
65 | read_position() | |
66 | { | |
67 | bool one = this->gptr() != NULL; | |
68 | bool two = this->gptr() < this->egptr(); | |
69 | return one && two; | |
70 | } | |
71 | ||
72 | bool | |
73 | unbuffered() | |
74 | { | |
75 | bool one = this->pbase() == NULL; | |
76 | bool two = this->pptr() == NULL; | |
77 | return one && two; | |
78 | } | |
79 | ||
80 | bool | |
81 | check_pointers() | |
82 | { | |
83 | bool one = this->eback() == NULL; | |
84 | bool two = this->gptr() == NULL; | |
85 | bool three = this->egptr() == NULL; | |
86 | ||
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; | |
91 | } | |
92 | }; | |
46c4e5d6 | 93 | |
983de0da PC |
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 | |
5681c890 PR |
102 | |
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(). | |
9b8d9ac3 PC |
106 | template<typename T> |
107 | class sync_buf | |
108 | : public T | |
109 | { | |
110 | private: | |
111 | bool m_sync_called; | |
5681c890 | 112 | |
9b8d9ac3 PC |
113 | public: |
114 | sync_buf() | |
115 | : m_sync_called(false) | |
116 | { } | |
117 | ||
118 | bool sync_called() const | |
119 | { return m_sync_called; } | |
5681c890 | 120 | |
9b8d9ac3 PC |
121 | protected: |
122 | int sync() | |
123 | { | |
124 | m_sync_called = true; | |
125 | return 0; | |
126 | } | |
127 | }; | |
5681c890 | 128 | |
9b8d9ac3 PC |
129 | typedef sync_buf<std::streambuf> sync_streambuf; |
130 | #ifdef _GLIBCXX_USE_WCHAR_T | |
131 | typedef sync_buf<std::wstreambuf> sync_wstreambuf; | |
132 | #endif | |
12841eb3 BK |
133 | |
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 { }; | |
138 | ||
9b8d9ac3 PC |
139 | template<typename T> |
140 | struct fail_buf | |
141 | : public T | |
12841eb3 | 142 | { |
9b8d9ac3 PC |
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; | |
12841eb3 | 147 | |
9b8d9ac3 PC |
148 | private: |
149 | char_type p[2]; | |
12841eb3 | 150 | |
9b8d9ac3 PC |
151 | public: |
152 | fail_buf() | |
153 | { | |
154 | p[0] = char_type('s'); | |
155 | p[1] = char_type(); | |
156 | setg(p, p, p + 1); | |
157 | } | |
12841eb3 | 158 | |
9b8d9ac3 PC |
159 | virtual int_type underflow() |
160 | { | |
161 | throw underflow_error(); | |
cfc45d90 | 162 | return int_type(); |
9b8d9ac3 PC |
163 | } |
164 | ||
165 | virtual int_type uflow() | |
166 | { | |
167 | throw underflow_error(); | |
cfc45d90 | 168 | return int_type(); |
9b8d9ac3 PC |
169 | } |
170 | ||
171 | virtual int_type | |
172 | overflow(int_type) | |
173 | { | |
174 | throw overflow_error(); | |
cfc45d90 | 175 | return int_type(); |
9b8d9ac3 PC |
176 | } |
177 | ||
178 | virtual pos_type | |
179 | seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) | |
180 | { | |
181 | throw positioning_error(); | |
182 | return pos_type(off_type(-1)); | |
183 | } | |
184 | ||
185 | virtual pos_type | |
186 | seekpos(pos_type, std::ios_base::openmode) | |
187 | { | |
188 | throw positioning_error(); | |
189 | return pos_type(off_type(-1)); | |
190 | } | |
191 | ||
192 | virtual int | |
193 | sync() | |
194 | { | |
195 | throw positioning_error(); | |
196 | return 0; | |
197 | } | |
198 | }; | |
12841eb3 | 199 | |
9b8d9ac3 PC |
200 | typedef fail_buf<std::streambuf> fail_streambuf; |
201 | #ifdef _GLIBCXX_USE_WCHAR_T | |
202 | typedef fail_buf<std::wstreambuf> fail_wstreambuf; | |
203 | #endif | |
12841eb3 BK |
204 | |
205 | // Facets that throw an exception for every virtual function. | |
206 | struct facet_error: std::exception { }; | |
207 | ||
9b8d9ac3 PC |
208 | template<typename T> |
209 | class fail_num_get | |
210 | : public std::num_get<T> | |
211 | { | |
212 | typedef std::ios_base ios_base; | |
213 | typedef typename std::num_get<T>::iter_type iter_type; | |
12841eb3 | 214 | |
9b8d9ac3 PC |
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(); } | |
12841eb3 | 219 | |
9b8d9ac3 PC |
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(); } | |
223 | ||
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(); } | |
228 | ||
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(); } | |
233 | ||
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(); } | |
12841eb3 BK |
238 | |
239 | #ifdef _GLIBCXX_USE_LONG_LONG | |
9b8d9ac3 PC |
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(); } | |
244 | ||
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(); } | |
12841eb3 BK |
249 | #endif |
250 | ||
9b8d9ac3 PC |
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(); } | |
255 | ||
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(); } | |
260 | ||
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(); } | |
265 | ||
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(); } | |
270 | }; | |
12841eb3 | 271 | |
9b8d9ac3 PC |
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 | |
12841eb3 | 276 | |
9b8d9ac3 PC |
277 | template<typename T> |
278 | class fail_num_put | |
279 | : public std::num_put<T> | |
280 | { | |
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; | |
284 | ||
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); } | |
289 | ||
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); } | |
12841eb3 | 293 | |
9b8d9ac3 PC |
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); } | |
12841eb3 BK |
297 | |
298 | #ifdef _GLIBCXX_USE_LONG_LONG | |
9b8d9ac3 PC |
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); } | |
12841eb3 | 302 | |
9b8d9ac3 PC |
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); } | |
12841eb3 | 306 | #endif |
9b8d9ac3 PC |
307 | |
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); } | |
12841eb3 | 311 | |
9b8d9ac3 PC |
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); } | |
315 | ||
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); } | |
319 | }; | |
12841eb3 | 320 | |
9b8d9ac3 PC |
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 | |
aecf642c | 325 | }; // namespace __gnu_test |
46c4e5d6 | 326 | |
3d7c150e | 327 | #endif // _GLIBCXX_TESTSUITE_IO_H |
46c4e5d6 | 328 |