xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/27_io/ostringstream_members.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
1*03a78d15Sespie // 2001-05-23 Benjamin Kosnik  <bkoz@redhat.com>
2*03a78d15Sespie 
3*03a78d15Sespie // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4*03a78d15Sespie //
5*03a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
6*03a78d15Sespie // software; you can redistribute it and/or modify it under the
7*03a78d15Sespie // terms of the GNU General Public License as published by the
8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
9*03a78d15Sespie // any later version.
10*03a78d15Sespie 
11*03a78d15Sespie // This library is distributed in the hope that it will be useful,
12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*03a78d15Sespie // GNU General Public License for more details.
15*03a78d15Sespie 
16*03a78d15Sespie // You should have received a copy of the GNU General Public License along
17*03a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19*03a78d15Sespie // USA.
20*03a78d15Sespie 
21*03a78d15Sespie // 27.7.3.2 member functions (ostringstream_members)
22*03a78d15Sespie 
23*03a78d15Sespie #include <sstream>
24*03a78d15Sespie #include <testsuite_hooks.h>
25*03a78d15Sespie 
test01()26*03a78d15Sespie void test01()
27*03a78d15Sespie {
28*03a78d15Sespie   bool test = true;
29*03a78d15Sespie   std::ostringstream os01;
30*03a78d15Sespie   const std::string str00;
31*03a78d15Sespie   const std::string str01 = "123";
32*03a78d15Sespie   std::string str02;
33*03a78d15Sespie   const int i01 = 123;
34*03a78d15Sespie   int a,b;
35*03a78d15Sespie 
36*03a78d15Sespie   std::ios_base::iostate state1, state2, statefail, stateeof;
37*03a78d15Sespie   statefail = std::ios_base::failbit;
38*03a78d15Sespie   stateeof = std::ios_base::eofbit;
39*03a78d15Sespie 
40*03a78d15Sespie   // string str() const
41*03a78d15Sespie   str02 = os01.str();
42*03a78d15Sespie   VERIFY( str00 == str02 );
43*03a78d15Sespie 
44*03a78d15Sespie   // void str(const basic_string&)
45*03a78d15Sespie   os01.str(str01);
46*03a78d15Sespie   str02 = os01.str();
47*03a78d15Sespie   VERIFY( str01 == str02 );
48*03a78d15Sespie 
49*03a78d15Sespie  #ifdef DEBUG_ASSERT
50*03a78d15Sespie   assert(test);
51*03a78d15Sespie #endif
52*03a78d15Sespie }
53*03a78d15Sespie 
54*03a78d15Sespie void
redirect_buffer(std::ios & stream,std::streambuf * new_buf)55*03a78d15Sespie redirect_buffer(std::ios& stream, std::streambuf* new_buf)
56*03a78d15Sespie { stream.rdbuf(new_buf); }
57*03a78d15Sespie 
58*03a78d15Sespie std::streambuf*
active_buffer(std::ios & stream)59*03a78d15Sespie active_buffer(std::ios& stream)
60*03a78d15Sespie { return stream.rdbuf(); }
61*03a78d15Sespie 
62*03a78d15Sespie // libstdc++/2832
test02()63*03a78d15Sespie void test02()
64*03a78d15Sespie {
65*03a78d15Sespie   bool test = true;
66*03a78d15Sespie   const char* strlit01 = "fuck war";
67*03a78d15Sespie   const char* strlit02 = "two less cars abstract riot crew, critical mass/SF";
68*03a78d15Sespie   const std::string str00;
69*03a78d15Sespie   const std::string str01(strlit01);
70*03a78d15Sespie   std::string str02;
71*03a78d15Sespie   std::stringbuf sbuf(str01);
72*03a78d15Sespie   std::streambuf* pbasebuf0 = &sbuf;
73*03a78d15Sespie 
74*03a78d15Sespie   std::ostringstream sstrm1;
75*03a78d15Sespie   VERIFY( sstrm1.str() == str00 );
76*03a78d15Sespie   // derived rdbuf() always returns original streambuf, even though
77*03a78d15Sespie   // it's no longer associated with the stream.
78*03a78d15Sespie   std::stringbuf* const buf1 = sstrm1.rdbuf();
79*03a78d15Sespie   // base rdbuf() returns the currently associated streambuf
80*03a78d15Sespie   std::streambuf* pbasebuf1 = active_buffer(sstrm1);
81*03a78d15Sespie   redirect_buffer(sstrm1, &sbuf);
82*03a78d15Sespie   std::stringbuf* const buf2 = sstrm1.rdbuf();
83*03a78d15Sespie   std::streambuf* pbasebuf2 = active_buffer(sstrm1);
84*03a78d15Sespie   VERIFY( buf1 == buf2 );
85*03a78d15Sespie   VERIFY( pbasebuf1 != pbasebuf2 );
86*03a78d15Sespie   VERIFY( pbasebuf2 == pbasebuf0 );
87*03a78d15Sespie 
88*03a78d15Sespie   // derived rdbuf() returns the original buf, so str() doesn't change.
89*03a78d15Sespie   VERIFY( sstrm1.str() != str01 );
90*03a78d15Sespie   VERIFY( sstrm1.str() == str00 );
91*03a78d15Sespie   // however, casting the active streambuf to a stringbuf shows what's up:
92*03a78d15Sespie   std::stringbuf* psbuf = dynamic_cast<std::stringbuf*>(pbasebuf2);
93*03a78d15Sespie   str02 = psbuf->str();
94*03a78d15Sespie   VERIFY( str02 == str01 );
95*03a78d15Sespie 
96*03a78d15Sespie   // How confusing and non-intuitive is this?
97*03a78d15Sespie   // These semantics are a joke, a serious defect, and incredibly lame.
98*03a78d15Sespie }
99*03a78d15Sespie 
100*03a78d15Sespie // 03: sanity checks for strings, stringbufs
101*03a78d15Sespie void
test03()102*03a78d15Sespie test03()
103*03a78d15Sespie {
104*03a78d15Sespie   bool test = false;
105*03a78d15Sespie 
106*03a78d15Sespie   // Empty string sanity check.
107*03a78d15Sespie   std::string str01;
108*03a78d15Sespie   std::string::iterator __i_start = str01.begin();
109*03a78d15Sespie   std::string::iterator __i_end = str01.end();
110*03a78d15Sespie   std::string::size_type len = str01.size();
111*03a78d15Sespie   test = __i_start == __i_end;
112*03a78d15Sespie   VERIFY( len == 0 );
113*03a78d15Sespie 
114*03a78d15Sespie   // Full string sanity check.
115*03a78d15Sespie   std::string str02("these golden days, i spend waiting for you:\n"
116*03a78d15Sespie 		    "Betty Carter on Verve with I'm Yours and You're Mine.");
117*03a78d15Sespie   __i_start = str02.begin();
118*03a78d15Sespie   __i_end = str02.end();
119*03a78d15Sespie   len = str02.size();
120*03a78d15Sespie   VERIFY( __i_start != __i_end );
121*03a78d15Sespie   VERIFY( len != 0 );
122*03a78d15Sespie 
123*03a78d15Sespie   // Test an empty ostringstream for sanity.
124*03a78d15Sespie   std::ostringstream ostrstream0;
125*03a78d15Sespie   std::string str03 = ostrstream0.str();
126*03a78d15Sespie   __i_start = str03.begin();
127*03a78d15Sespie   __i_end = str03.end();
128*03a78d15Sespie   len = str03.size();
129*03a78d15Sespie   VERIFY( __i_start == __i_end );
130*03a78d15Sespie   VERIFY( len == 0 );
131*03a78d15Sespie   VERIFY( str01 == str03 );
132*03a78d15Sespie }
133*03a78d15Sespie 
134*03a78d15Sespie // user-reported error
135*03a78d15Sespie class derived_oss: public std::ostringstream
136*03a78d15Sespie {
137*03a78d15Sespie public:
derived_oss()138*03a78d15Sespie   derived_oss() : std::ostringstream() { }
139*03a78d15Sespie };
140*03a78d15Sespie 
141*03a78d15Sespie void
test04()142*03a78d15Sespie test04()
143*03a78d15Sespie {
144*03a78d15Sespie   bool test = true;
145*03a78d15Sespie   derived_oss yy;
146*03a78d15Sespie   yy << "buena vista social club\n";
147*03a78d15Sespie   VERIFY( yy.str() == std::string("buena vista social club\n") );
148*03a78d15Sespie }
149*03a78d15Sespie 
main()150*03a78d15Sespie int main()
151*03a78d15Sespie {
152*03a78d15Sespie   test01();
153*03a78d15Sespie   test02();
154*03a78d15Sespie   test03();
155*03a78d15Sespie   test04();
156*03a78d15Sespie   return 0;
157*03a78d15Sespie }
158