OSDN Git Service

2005-05-18 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / testsuite_io.h
1 // -*- C++ -*-
2 // Testing streambuf/filebuf/stringbuf for the C++ library testsuite.
3 //
4 // Copyright (C) 2003, 2004 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 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
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
31 #ifndef _GLIBCXX_TESTSUITE_IO_H
32 #define _GLIBCXX_TESTSUITE_IO_H
33
34 #include <fstream>
35 #include <sstream>
36
37 namespace __gnu_test
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
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     };
93
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
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().
106   template<typename T>
107     class sync_buf
108     : public T
109     {
110     private:
111       bool m_sync_called;
112     
113     public:
114       sync_buf()
115       : m_sync_called(false)
116       { }
117       
118       bool sync_called() const
119       { return m_sync_called; }
120
121     protected:
122       int sync()
123       {
124         m_sync_called = true;
125         return 0;
126       }
127     };
128
129   typedef  sync_buf<std::streambuf>   sync_streambuf;
130 #ifdef _GLIBCXX_USE_WCHAR_T
131   typedef  sync_buf<std::wstreambuf>  sync_wstreambuf;
132 #endif
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
139   template<typename T>
140     struct fail_buf
141     : public T
142     {
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;
147
148     private:
149       char_type p[2];
150
151     public:
152       fail_buf()
153       {
154         p[0] = char_type('s');
155         p[1] = char_type();
156         setg(p, p, p + 1); 
157       }
158
159       virtual int_type underflow() 
160       {
161         throw underflow_error();
162         return int_type();
163       }
164       
165       virtual int_type uflow() 
166       {
167         throw underflow_error();
168         return int_type();
169       }
170       
171       virtual int_type
172       overflow(int_type)
173       {
174         throw overflow_error();
175         return int_type();
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     };
199
200   typedef  fail_buf<std::streambuf>   fail_streambuf;
201 #ifdef _GLIBCXX_USE_WCHAR_T
202   typedef  fail_buf<std::wstreambuf>  fail_wstreambuf;
203 #endif
204
205   // Facets that throw an exception for every virtual function.
206   struct facet_error: std::exception { };
207
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;
214
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(); }
219
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(); }
238
239 #ifdef _GLIBCXX_USE_LONG_LONG 
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(); }
249 #endif
250
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     };
271
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
276
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); }
293
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); }
297
298 #ifdef _GLIBCXX_USE_LONG_LONG 
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); }
302
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); }
306 #endif
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); }
311
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     };
320
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
325 }; // namespace __gnu_test
326
327 #endif // _GLIBCXX_TESTSUITE_IO_H
328