xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/27_io/ostream_inserter_other.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
1 // 1999-08-16 bkoz
2 // 1999-11-01 bkoz
3 
4 // Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
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 // 27.6.2.5.4 basic_ostream character inserters
23 // @require@ %-*.tst %-*.txt
24 // @diff@ %-*.tst %-*.txt
25 
26 #include <ostream>
27 #include <sstream>
28 #include <fstream>
29 #include <testsuite_hooks.h>
30 
31 const int size = 1000;
32 const char name_01[] = "ostream_inserter_other-1.tst";
33 const char name_02[] = "ostream_inserter_other-1.txt";
34 const char name_03[] = "ostream_inserter_other-2.tst";
35 const char name_04[] = "ostream_inserter_other-2.txt";
36 
37 
38 // stringstream
39 int
test01()40 test01()
41 {
42   bool test = true;
43 #ifdef DEBUG_ASSERT
44   assert(test);
45 #endif
46   return 0;
47 }
48 
49 // fstream
50 int
test02()51 test02()
52 {
53   typedef std::ios_base::iostate iostate;
54   bool test = true;
55 
56   // basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sb)
57   // filebuf-> NULL
58   std::ifstream f_in1(name_01);
59   std::ofstream f_out1(name_02);
60   std::stringbuf* strbuf01 = NULL;
61   iostate state01 = f_in1.rdstate();
62   f_in1 >> strbuf01;
63   iostate state02 = f_in1.rdstate();
64   VERIFY( state01 != state02 );
65   VERIFY( (state02 & std::ios_base::failbit) != 0 );
66   state01 = f_out1.rdstate();
67   f_out1 << strbuf01;
68   state02 = f_out1.rdstate();
69   VERIFY( state01 != state02 );
70   VERIFY( (state02 & std::ios_base::badbit) != 0 );
71 
72   // filebuf->filebuf
73   std::ifstream f_in(name_01);
74   std::ofstream f_out(name_02);
75   f_out << f_in.rdbuf();
76   f_in.close();
77   f_out.close();
78 
79   // filebuf->stringbuf->filebuf
80   std::ifstream f_in2(name_03);
81   std::ofstream f_out2(name_04); // should be different name
82   std::stringbuf strbuf02;
83   f_in2 >> &strbuf02;
84   f_out2 << &strbuf02;
85   f_in2.close();
86   f_out2.close();
87 
88   // no characters inserted
89 
90 #ifdef DEBUG_ASSERT
91   assert(test);
92 #endif
93 
94   return 0;
95 }
96 
97 // via Brent Verner <brent@rcfile.org>
98 // http://gcc.gnu.org/ml/libstdc++/2000-06/msg00005.html
99 int
test03(void)100 test03(void)
101 {
102   using namespace std;
103 
104   typedef ios::pos_type 	pos_type;
105 
106   const char* TEST_IN = "ostream_inserter_other_in";
107   const char* TEST_OUT = "ostream_inserter_other_out";
108   pos_type i_read, i_wrote, rs, ws;
109   double tf_size = BUFSIZ * 2.5;
110   ofstream testfile(TEST_IN);
111 
112   for (int i = 0; i < tf_size; ++i)
113     testfile.put('.');
114   testfile.close();
115 
116   ifstream in(TEST_IN);
117   ofstream out(TEST_OUT);
118   out << in.rdbuf();
119   in.seekg(0,ios_base::beg);
120   out.seekp(0,ios_base::beg);
121   rs = in.tellg();
122   ws = out.tellp();
123   in.seekg(0,ios_base::end);
124   out.seekp(0,ios_base::end);
125   i_read = in.tellg() - rs;
126   i_wrote = out.tellp() - ws;
127   in.close();
128   out.close();
129 
130 #ifdef DEBUG_ASSERT
131   assert(i_read == i_wrote);
132 #endif
133 
134   return 0;
135 }
136 
137 // libstdc++/3272
test04()138 void test04()
139 {
140   using namespace std;
141   bool test = true;
142   istringstream istr("inside betty carter");
143   ostringstream ostr;
144   ostr << istr.rdbuf() << endl;
145 
146   if (ostr.rdstate() & ios_base::eofbit)
147     test = false;
148 
149   VERIFY( test );
150 }
151 
152 
153 class test_buffer_1 : public std::streambuf
154 {
155 public:
test_buffer_1(const std::string & s)156   test_buffer_1(const std::string& s) : str(s), it(str.begin()) { }
157 
158 protected:
underflow()159   virtual int underflow() { return (it != str.end() ? *it : EOF); }
uflow()160   virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
161 
162 private:
163   const std::string str;
164   std::string::const_iterator it;
165 };
166 
167 
168 class test_buffer_2 : public std::streambuf
169 {
170 public:
test_buffer_2(const std::string & s)171   test_buffer_2(const std::string& s) : str(s), it(str.begin()) { }
172 
173 protected:
underflow()174   virtual int underflow() { return (it != str.end() ? *it : EOF); }
uflow()175   virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
showmanyc()176   virtual std::streamsize showmanyc() { return std::distance(it, str.end()); }
177 private:
178   const std::string str;
179   std::string::const_iterator it;
180 };
181 
182 
183 class test_buffer_3 : public std::streambuf
184 {
185 public:
test_buffer_3(const std::string & s)186   test_buffer_3(const std::string& s) : str(s), it(str.begin()) { }
187 
188 protected:
underflow()189   virtual int underflow() { return (it != str.end() ? *it : EOF); }
uflow()190   virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
showmanyc()191   virtual std::streamsize showmanyc()
192   {
193     std::streamsize ret = std::distance(it, str.end());
194     return ret > 0 ? ret : -1;
195   }
196 private:
197   const std::string str;
198   std::string::const_iterator it;
199 };
200 
201 class test_buffer_4 : public std::streambuf {
202 public:
test_buffer_4(const std::string & s)203   test_buffer_4(const std::string& s) : str(s), it(str.begin())
204   {
205     if (it != str.end()) {
206       buf[0] = *it++;
207       setg(buf, buf, buf+1);
208     }
209   }
210 
211 protected:
underflow()212   virtual int underflow() { return (it != str.end() ? *it : EOF); }
uflow()213   virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
showmanyc()214   virtual std::streamsize showmanyc() {
215     std::streamsize ret = std::distance(it, str.end());
216     return ret > 0 ? ret : -1;
217   }
218 private:
219   const std::string str;
220   std::string::const_iterator it;
221   char buf[1];
222 };
223 
test(const std::string & str,std::streambuf & buf)224 void test(const std::string& str, std::streambuf& buf)
225 {
226   bool test = true;
227 
228   std::ostringstream out;
229   std::istream in(&buf);
230 
231   out << in.rdbuf();
232 
233   if (out.str() != str)
234     VERIFY( false );
235 }
236 
237 // libstdc++/6745
238 // libstdc++/8071
239 // libstdc++/8127
240 // Jonathan Lennox  <lennox@cs.columbia.edu>
test05()241 void test05()
242 {
243   std::string string_a("Hello, world!");
244   std::string string_b("");
245 
246   test_buffer_1 buf1a(string_a);
247   test_buffer_1 buf1b(string_b);
248 
249   test_buffer_2 buf2a(string_a);
250   test_buffer_2 buf2b(string_b);
251 
252   test_buffer_3 buf3a(string_a);
253   test_buffer_3 buf3b(string_b);
254 
255   test_buffer_4 buf4a(string_a);
256   test_buffer_4 buf4b(string_b);
257 
258   test(string_a, buf1a);
259   test(string_b, buf1b);
260 
261   test(string_a, buf2a);
262   test(string_b, buf2b);
263 
264   test(string_a, buf3a);
265   test(string_b, buf3b);
266 
267   test(string_a, buf4a);
268   test(string_b, buf4b);
269 }
270 
271 int
main()272 main()
273 {
274   test01();
275   test02();
276   test03();
277   test04();
278 
279   test05();
280   return 0;
281 }
282