]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/util/testsuite_io.h
re PR target/32889 (ICE in delete_output_reload, at reload1.c:7926)
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_io.h
CommitLineData
46c4e5d6 1// -*- C++ -*-
983de0da 2// Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
46c4e5d6 3//
2fdff2be 4// Copyright (C) 2003, 2004, 2005, 2006, 2007 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 33
2fdff2be 34#include <ios>
46c4e5d6 35
aecf642c 36namespace __gnu_test
46c4e5d6
BK
37{
38 // Used to verify the constraints/requirements on get and put areas
39 // as defined in
40 // 27.5.1 - Stream buffer requirements: get and put areas
41 // 27.8.1.1 - Template class basic_filebuf p 3
42 // If the file is not open (ios_base::in) -> input seq. cannot be read
43 // If the file is not open (ios_base::out) -> output seq. cannot be written
44 // Joint file position
45 // 27.8.1.4 - Overridden virtual functions p9
46 // If unbuffered, pbase == pptr == NULL
983de0da
PC
47 // 27.7.1.1 - Basic_stringbuf constructors p 1
48 // 27.8.1.2 - Basic_filebuf constructors p 1
49 // ... , initializing the base class with basic_streambuf() 27.5.2.1
50 template<typename T>
51 class constraint_buf
52 : public T
53 {
54 public:
55 bool
56 write_position()
57 {
58 bool one = this->pptr() != NULL;
59 bool two = this->pptr() < this->epptr();
60 return one && two;
61 }
62
63 bool
64 read_position()
65 {
66 bool one = this->gptr() != NULL;
67 bool two = this->gptr() < this->egptr();
68 return one && two;
69 }
70
71 bool
72 unbuffered()
73 {
74 bool one = this->pbase() == NULL;
75 bool two = this->pptr() == NULL;
76 return one && two;
77 }
78
79 bool
80 check_pointers()
81 {
82 bool one = this->eback() == NULL;
83 bool two = this->gptr() == NULL;
84 bool three = this->egptr() == NULL;
85
86 bool four = this->pbase() == NULL;
87 bool five = this->pptr() == NULL;
88 bool six = this->epptr() == NULL;
89 return one && two && three && four && five && six;
90 }
91 };
46c4e5d6 92
983de0da
PC
93 typedef constraint_buf<std::streambuf> constraint_streambuf;
94 typedef constraint_buf<std::filebuf> constraint_filebuf;
95 typedef constraint_buf<std::stringbuf> constraint_stringbuf;
96#ifdef _GLIBCXX_USE_WCHAR_T
97 typedef constraint_buf<std::wstreambuf> constraint_wstreambuf;
98 typedef constraint_buf<std::wfilebuf> constraint_wfilebuf;
99 typedef constraint_buf<std::wstringbuf> constraint_wstringbuf;
100#endif
5681c890
PR
101
102 // Used to check if basic_streambuf::pubsync() has been called.
103 // This is useful for checking if a function creates [io]stream::sentry
104 // objects, since the sentry constructors call tie()->flush().
9b8d9ac3
PC
105 template<typename T>
106 class sync_buf
107 : public T
108 {
109 private:
110 bool m_sync_called;
5681c890 111
9b8d9ac3
PC
112 public:
113 sync_buf()
114 : m_sync_called(false)
115 { }
116
117 bool sync_called() const
118 { return m_sync_called; }
5681c890 119
9b8d9ac3
PC
120 protected:
121 int sync()
122 {
123 m_sync_called = true;
124 return 0;
125 }
126 };
5681c890 127
9b8d9ac3
PC
128 typedef sync_buf<std::streambuf> sync_streambuf;
129#ifdef _GLIBCXX_USE_WCHAR_T
130 typedef sync_buf<std::wstreambuf> sync_wstreambuf;
131#endif
12841eb3
BK
132
133 // Throws on all overflow and underflow calls.
134 struct underflow_error: std::exception { };
135 struct overflow_error: std::exception { };
136 struct positioning_error: std::exception { };
137
9b8d9ac3
PC
138 template<typename T>
139 struct fail_buf
140 : public T
12841eb3 141 {
9b8d9ac3
PC
142 typedef typename T::char_type char_type;
143 typedef typename T::int_type int_type;
144 typedef typename T::off_type off_type;
145 typedef typename T::pos_type pos_type;
12841eb3 146
9b8d9ac3
PC
147 private:
148 char_type p[2];
12841eb3 149
9b8d9ac3
PC
150 public:
151 fail_buf()
152 {
153 p[0] = char_type('s');
154 p[1] = char_type();
155 setg(p, p, p + 1);
156 }
12841eb3 157
9b8d9ac3
PC
158 virtual int_type underflow()
159 {
160 throw underflow_error();
cfc45d90 161 return int_type();
9b8d9ac3
PC
162 }
163
164 virtual int_type uflow()
165 {
166 throw underflow_error();
cfc45d90 167 return int_type();
9b8d9ac3
PC
168 }
169
170 virtual int_type
171 overflow(int_type)
172 {
173 throw overflow_error();
cfc45d90 174 return int_type();
9b8d9ac3
PC
175 }
176
177 virtual pos_type
178 seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode)
179 {
180 throw positioning_error();
181 return pos_type(off_type(-1));
182 }
183
184 virtual pos_type
185 seekpos(pos_type, std::ios_base::openmode)
186 {
187 throw positioning_error();
188 return pos_type(off_type(-1));
189 }
190
191 virtual int
192 sync()
193 {
194 throw positioning_error();
195 return 0;
196 }
197 };
12841eb3 198
9b8d9ac3
PC
199 typedef fail_buf<std::streambuf> fail_streambuf;
200#ifdef _GLIBCXX_USE_WCHAR_T
201 typedef fail_buf<std::wstreambuf> fail_wstreambuf;
202#endif
12841eb3
BK
203
204 // Facets that throw an exception for every virtual function.
205 struct facet_error: std::exception { };
206
9b8d9ac3
PC
207 template<typename T>
208 class fail_num_get
209 : public std::num_get<T>
210 {
211 typedef std::ios_base ios_base;
212 typedef typename std::num_get<T>::iter_type iter_type;
12841eb3 213
9b8d9ac3
PC
214 protected:
215 iter_type
8b5bc374 216 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const
9b8d9ac3 217 { throw facet_error(); return iter_type(); }
12841eb3 218
9b8d9ac3
PC
219 virtual iter_type
220 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const
221 { throw facet_error(); return iter_type(); }
222
223 virtual iter_type
8b5bc374 224 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
225 unsigned short&) const
226 { throw facet_error(); return iter_type(); }
227
228 virtual iter_type
8b5bc374 229 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
230 unsigned int&) const
231 { throw facet_error(); return iter_type(); }
232
233 virtual iter_type
8b5bc374 234 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
235 unsigned long&) const
236 { throw facet_error(); return iter_type(); }
12841eb3
BK
237
238#ifdef _GLIBCXX_USE_LONG_LONG
9b8d9ac3 239 virtual iter_type
8b5bc374 240 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
241 long long&) const
242 { throw facet_error(); return iter_type(); }
243
244 virtual iter_type
8b5bc374 245 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
246 unsigned long long&) const
247 { throw facet_error(); return iter_type(); }
12841eb3
BK
248#endif
249
9b8d9ac3 250 virtual iter_type
8b5bc374 251 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
252 float&) const
253 { throw facet_error(); return iter_type(); }
254
255 virtual iter_type
8b5bc374 256 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
257 double&) const
258 { throw facet_error(); return iter_type(); }
259
260 virtual iter_type
8b5bc374 261 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
262 long double&) const
263 { throw facet_error(); return iter_type(); }
264
265 virtual iter_type
8b5bc374 266 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&,
9b8d9ac3
PC
267 void*&) const
268 { throw facet_error(); return iter_type(); }
269 };
12841eb3 270
9b8d9ac3
PC
271 typedef fail_num_get<char> fail_num_get_char;
272#ifdef _GLIBCXX_USE_WCHAR_T
273 typedef fail_num_get<wchar_t> fail_num_get_wchar_t;
274#endif
12841eb3 275
9b8d9ac3
PC
276 template<typename T>
277 class fail_num_put
278 : public std::num_put<T>
279 {
280 typedef std::ios_base ios_base;
281 typedef typename std::num_put<T>::iter_type iter_type;
282 typedef typename std::num_put<T>::char_type char_type;
283
284 protected:
285 iter_type
8b5bc374 286 do_put(iter_type, ios_base&, char_type, bool) const
9b8d9ac3
PC
287 { throw facet_error(); return iter_type(NULL); }
288
289 virtual iter_type
8b5bc374 290 do_put(iter_type, ios_base&, char_type, long) const
9b8d9ac3 291 { throw facet_error(); return iter_type(NULL); }
12841eb3 292
9b8d9ac3 293 virtual iter_type
8b5bc374 294 do_put(iter_type, ios_base&, char_type, unsigned long) const
9b8d9ac3 295 { throw facet_error(); return iter_type(NULL); }
12841eb3
BK
296
297#ifdef _GLIBCXX_USE_LONG_LONG
9b8d9ac3 298 virtual iter_type
8b5bc374 299 do_put(iter_type, ios_base&, char_type, long long) const
9b8d9ac3 300 { throw facet_error(); return iter_type(NULL); }
12841eb3 301
9b8d9ac3 302 virtual iter_type
8b5bc374 303 do_put(iter_type, ios_base&, char_type, unsigned long long) const
9b8d9ac3 304 { throw facet_error(); return iter_type(NULL); }
12841eb3 305#endif
9b8d9ac3
PC
306
307 virtual iter_type
8b5bc374 308 do_put(iter_type, ios_base&, char_type, double) const
9b8d9ac3 309 { throw facet_error(); return iter_type(NULL); }
12841eb3 310
9b8d9ac3 311 virtual iter_type
8b5bc374 312 do_put(iter_type, ios_base&, char_type, long double) const
9b8d9ac3
PC
313 { throw facet_error(); return iter_type(NULL); }
314
315 virtual iter_type
8b5bc374 316 do_put(iter_type, ios_base&, char_type, const void*) const
9b8d9ac3
PC
317 { throw facet_error(); return iter_type(NULL); }
318 };
12841eb3 319
9b8d9ac3
PC
320 typedef fail_num_put<char> fail_num_put_char;
321#ifdef _GLIBCXX_USE_WCHAR_T
322 typedef fail_num_put<wchar_t> fail_num_put_wchar_t;
323#endif
aecf642c 324}; // namespace __gnu_test
46c4e5d6 325
3d7c150e 326#endif // _GLIBCXX_TESTSUITE_IO_H
46c4e5d6 327