]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/ios_init.cc
* alpha.h, arc.h, arm/aout.h, avr.h, cris.h, d30v.h, dsp16xx.h,
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / ios_init.cc
CommitLineData
5de197f2
BK
1// 2001-06-05 Benjamin Kosnik <bkoz@redhat.com>
2
3a9ebf3c 3// Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5de197f2
BK
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.
10
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.
15
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.
20
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.
29
30// 27.4.2.1.6 class ios_base::init
31
32#include <fstream>
3a9ebf3c 33#include <sstream>
5de197f2 34#include <iostream>
fe413112 35#include <testsuite_hooks.h>
5de197f2
BK
36
37class gnu_filebuf: public std::filebuf
38{
39 int i;
40public:
41 gnu_filebuf(int j = 1): i(j) { }
42 ~gnu_filebuf() { --i; }
43 int get_i() { return i;}
44};
45
46const int initial = 4;
47gnu_filebuf buf(initial);
48
49// libstdc++/3045, in a vague way.
50void test01()
51{
52 bool test = true;
53 int k1;
54
55 // 1 normal
56 k1 = buf.get_i();
57 VERIFY( k1 == initial );
58 {
59 std::cout.rdbuf(&buf);
60 }
61 k1 = buf.get_i();
62 VERIFY( k1 == initial );
63
64 // 2 syncd off
65 k1 = buf.get_i();
66 VERIFY( k1 == initial );
67 {
68 std::cout.rdbuf(&buf);
69 std::ios_base::sync_with_stdio(false); // make sure doesn't clobber buf
70 }
71 k1 = buf.get_i();
72 VERIFY( k1 == initial );
73
74 // 3 callling init
75 k1 = buf.get_i();
76 VERIFY( k1 == initial );
77 {
78 std::cout.rdbuf(&buf);
79 std::ios_base::Init make_sure_initialized;
80 }
81 k1 = buf.get_i();
82 VERIFY( k1 == initial );
83}
84
3a9ebf3c 85// Non-required instantiations don't have the required facets inbued,
ac39fabb 86// by default, into the locale object.
3a9ebf3c 87// See 27.4.4.1
1bc59af5
BK
88class gnu_ios: public std::basic_ios<char> { };
89
3a9ebf3c
BK
90void test02()
91{
92 bool test = true;
93
94 // 01: Doesn't call basic_ios::init, which uses ctype<char_type>..
ac39fabb 95 // This should be unambiguously correct.
3a9ebf3c
BK
96 try
97 {
1bc59af5 98 gnu_ios gios;
3a9ebf3c
BK
99 }
100 catch(...)
101 {
102 test = false;
103 }
104
ac39fabb 105 // 02: Calls basic_ios::init, which may call ctype<char_type>...
3a9ebf3c
BK
106 try
107 {
108 std::basic_string<unsigned short> str;
109 std::basic_ostringstream<unsigned short> oss(str);
110
3a9ebf3c
BK
111 // Try each member functions for unformatted io.
112 // put
113 oss.put(324);
114
115 // write
116 const unsigned short us[4] = {1246, 433, 520, 0};
117 oss.write(us, 4);
118
119 // flush
120 oss.flush();
121 }
122 catch(const std::bad_cast& obj)
123 {
ac39fabb
BK
124 // Should be able to do the above without calling fill() and
125 // forcing a call to widen...
126 test = false;
3a9ebf3c
BK
127 }
128 catch(...)
129 {
130 test = false;
131 }
132 VERIFY( test );
133}
134
40e5b283
BK
135// libstdc++/3983
136void test03()
137{
138 using namespace std;
139 bool test = true;
140
141 // input streams
142 basic_istringstream<unsigned char> iss_uc;
143 unsigned char arr[6] = { 'a', 'b', 'c', 'd', 'e' };
144
145 // Sentry uses locale info, so have to try one formatted input.
146 try
147 {
148 int i;
149 iss_uc >> i;
150 }
151 catch (bad_cast& obj)
152 { }
153 catch (exception& obj)
154 { test = false; }
155
156 try
157 {
158 iss_uc >> arr;
159 }
160 catch (bad_cast& obj)
161 { }
162 catch (exception& obj)
163 { test = false; }
164
165 try
166 {
167 iss_uc >> ws;
168 }
169 catch (bad_cast& obj)
170 { }
171 catch (exception& obj)
172 { test = false; }
173
174 try
175 {
176 basic_string<unsigned char> s_uc(arr);
177 iss_uc >> s_uc;
178 }
179 catch (bad_cast& obj)
180 { }
181 catch (exception& obj)
182 { test = false; }
183
184 // output streams
185 basic_ostringstream<unsigned char> oss_uc;
186
187 try
188 {
189 bool b = true;
190 oss_uc << b;
191 }
192 catch (bad_cast& obj)
193 { }
194 catch (exception& obj)
195 { test = false; }
196
197 VERIFY( test );
198}
199
200// libstdc++/5268
201int test04()
202{
203 std::stringbuf b1;
204 std::cout.rdbuf( &b1 );
205 std::cout << "hello\n";
206 return 0;
207}
208
72c9b062 209#if !__GXX_WEAK__
0d223e3a
BK
210// Explicitly instantiate for systems with no COMDAT or weak support.
211template
212 std::basic_string<unsigned short>::size_type
213 std::basic_string<unsigned short>::_Rep::_S_max_size;
214
215template
216 unsigned short
217 std::basic_string<unsigned short>::_Rep::_S_terminal;
a9bb75a7
BK
218
219template
220 std::basic_string<unsigned char>::size_type
221 std::basic_string<unsigned char>::_Rep::_S_max_size;
222
223template
224 unsigned char
225 std::basic_string<unsigned char>::_Rep::_S_terminal;
72c9b062 226#endif
0d223e3a 227
5de197f2
BK
228int main()
229{
230 test01();
3a9ebf3c 231 test02();
40e5b283
BK
232 test03();
233 test04();
5de197f2
BK
234 return 0;
235}