FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / ios_init.cc
blob33a52de9470e43f098f80bd75d920e5fb16997c1
1 // 2001-06-05 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // 27.4.2.1.6 class ios_base::init
32 #include <fstream>
33 #include <sstream>
34 #include <iostream>
35 #include <testsuite_hooks.h>
37 // char_traits specialization
38 namespace std
40 template<>
41 struct char_traits<unsigned short>
43 typedef unsigned short char_type;
44 // Unsigned as wint_t in unsigned.
45 typedef unsigned long int_type;
46 typedef streampos pos_type;
47 typedef streamoff off_type;
48 typedef mbstate_t state_type;
50 static void
51 assign(char_type& __c1, const char_type& __c2)
52 { __c1 = __c2; }
54 static bool
55 eq(const char_type& __c1, const char_type& __c2)
56 { return __c1 == __c2; }
58 static bool
59 lt(const char_type& __c1, const char_type& __c2)
60 { return __c1 < __c2; }
62 static int
63 compare(const char_type* __s1, const char_type* __s2, size_t __n)
65 for (size_t __i = 0; __i < __n; ++__i)
66 if (!eq(__s1[__i], __s2[__i]))
67 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
68 return 0;
71 static size_t
72 length(const char_type* __s)
74 const char_type* __p = __s;
75 while (__p)
76 ++__p;
77 return (__p - __s);
80 static const char_type*
81 find(const char_type* __s, size_t __n, const char_type& __a)
83 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
84 if (*__p == __a) return __p;
85 return 0;
88 static char_type*
89 move(char_type* __s1, const char_type* __s2, size_t __n)
90 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
92 static char_type*
93 copy(char_type* __s1, const char_type* __s2, size_t __n)
94 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
96 static char_type*
97 assign(char_type* __s, size_t __n, char_type __a)
99 for (char_type* __p = __s; __p < __s + __n; ++__p)
100 assign(*__p, __a);
101 return __s;
104 static char_type
105 to_char_type(const int_type& __c)
106 { return char_type(); }
108 static int_type
109 to_int_type(const char_type& __c) { return int_type(); }
111 static bool
112 eq_int_type(const int_type& __c1, const int_type& __c2)
113 { return __c1 == __c2; }
115 static int_type
116 eof() { return static_cast<int_type>(-1); }
118 static int_type
119 not_eof(const int_type& __c)
120 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
123 template<>
124 struct char_traits<unsigned char>
126 typedef unsigned char char_type;
127 // Unsigned as wint_t in unsigned.
128 typedef unsigned long int_type;
129 typedef streampos pos_type;
130 typedef streamoff off_type;
131 typedef mbstate_t state_type;
133 static void
134 assign(char_type& __c1, const char_type& __c2)
135 { __c1 = __c2; }
137 static bool
138 eq(const char_type& __c1, const char_type& __c2)
139 { return __c1 == __c2; }
141 static bool
142 lt(const char_type& __c1, const char_type& __c2)
143 { return __c1 < __c2; }
145 static int
146 compare(const char_type* __s1, const char_type* __s2, size_t __n)
148 for (size_t __i = 0; __i < __n; ++__i)
149 if (!eq(__s1[__i], __s2[__i]))
150 return lt(__s1[__i], __s2[__i]) ? -1 : 1;
151 return 0;
154 static size_t
155 length(const char_type* __s)
157 const char_type* __p = __s;
158 while (__p && *__p)
159 ++__p;
160 return (__p - __s);
163 static const char_type*
164 find(const char_type* __s, size_t __n, const char_type& __a)
166 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
167 if (*__p == __a) return __p;
168 return 0;
171 static char_type*
172 move(char_type* __s1, const char_type* __s2, size_t __n)
173 { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
175 static char_type*
176 copy(char_type* __s1, const char_type* __s2, size_t __n)
177 { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
179 static char_type*
180 assign(char_type* __s, size_t __n, char_type __a)
182 for (char_type* __p = __s; __p < __s + __n; ++__p)
183 assign(*__p, __a);
184 return __s;
187 static char_type
188 to_char_type(const int_type& __c)
189 { return char_type(); }
191 static int_type
192 to_int_type(const char_type& __c) { return int_type(); }
194 static bool
195 eq_int_type(const int_type& __c1, const int_type& __c2)
196 { return __c1 == __c2; }
198 static int_type
199 eof() { return static_cast<int_type>(-1); }
201 static int_type
202 not_eof(const int_type& __c)
203 { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
205 } // namespace std
207 class gnu_filebuf: public std::filebuf
209 int i;
210 public:
211 gnu_filebuf(int j = 1): i(j) { }
212 ~gnu_filebuf() { --i; }
213 int get_i() { return i;}
216 const int initial = 4;
217 gnu_filebuf buf(initial);
219 // libstdc++/3045, in a vague way.
220 void test01()
222 bool test = true;
223 int k1;
225 // 1 normal
226 k1 = buf.get_i();
227 VERIFY( k1 == initial );
229 std::cout.rdbuf(&buf);
231 k1 = buf.get_i();
232 VERIFY( k1 == initial );
234 // 2 syncd off
235 k1 = buf.get_i();
236 VERIFY( k1 == initial );
238 std::cout.rdbuf(&buf);
239 std::ios_base::sync_with_stdio(false); // make sure doesn't clobber buf
241 k1 = buf.get_i();
242 VERIFY( k1 == initial );
244 // 3 callling init
245 k1 = buf.get_i();
246 VERIFY( k1 == initial );
248 std::cout.rdbuf(&buf);
249 std::ios_base::Init make_sure_initialized;
251 k1 = buf.get_i();
252 VERIFY( k1 == initial );
255 // Non-required instantiations don't have the required facets inbued,
256 // by default, into the locale object.
257 // See 27.4.4.1
258 class gnu_ios: public std::basic_ios<char> { };
260 void test02()
262 bool test = true;
264 // 01: Doesn't call basic_ios::init, which uses ctype<char_type>..
265 // This should be unambiguously correct.
268 gnu_ios gios;
270 catch(...)
272 test = false;
275 // 02: Calls basic_ios::init, which may call ctype<char_type>...
278 std::basic_string<unsigned short> str;
279 std::basic_ostringstream<unsigned short> oss(str);
281 // Try each member functions for unformatted io.
282 // put
283 oss.put(324);
285 // write
286 const unsigned short us[4] = {1246, 433, 520, 0};
287 oss.write(us, 4);
289 // flush
290 oss.flush();
292 catch(const std::bad_cast& obj)
294 // Should be able to do the above without calling fill() and
295 // forcing a call to widen...
296 test = false;
298 catch(...)
300 test = false;
302 VERIFY( test );
305 // libstdc++/3983
306 void test03()
308 using namespace std;
309 bool test = true;
311 // input streams
312 basic_istringstream<unsigned char> iss_uc;
313 unsigned char arr[6] = { 'a', 'b', 'c', 'd', 'e' };
315 // Sentry uses locale info, so have to try one formatted input.
316 try
318 int i;
319 iss_uc >> i;
321 catch (bad_cast& obj)
323 catch (exception& obj)
324 { test = false; }
326 try
328 iss_uc >> arr;
330 catch (bad_cast& obj)
332 catch (exception& obj)
333 { test = false; }
335 try
337 iss_uc >> ws;
339 catch (bad_cast& obj)
341 catch (exception& obj)
342 { test = false; }
344 try
346 basic_string<unsigned char> s_uc(arr);
347 iss_uc >> s_uc;
349 catch (bad_cast& obj)
351 catch (exception& obj)
352 { test = false; }
354 // output streams
355 basic_ostringstream<unsigned char> oss_uc;
357 try
359 bool b = true;
360 oss_uc << b;
362 catch (bad_cast& obj)
364 catch (exception& obj)
365 { test = false; }
367 VERIFY( test );
370 // libstdc++/5268
371 int test04()
373 std::stringbuf b1;
374 std::cout.rdbuf( &b1 );
375 std::cout << "hello\n";
376 return 0;
379 #if !__GXX_WEAK__
380 // Explicitly instantiate for systems with no COMDAT or weak support.
381 template
382 std::basic_string<unsigned short>::size_type
383 std::basic_string<unsigned short>::_Rep::_S_max_size;
385 template
386 unsigned short
387 std::basic_string<unsigned short>::_Rep::_S_terminal;
389 template
390 std::basic_string<unsigned char>::size_type
391 std::basic_string<unsigned char>::_Rep::_S_max_size;
393 template
394 unsigned char
395 std::basic_string<unsigned char>::_Rep::_S_terminal;
396 #endif
398 int main()
400 test01();
401 test02();
402 test03();
403 test04();
404 return 0;