]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/stringstream.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / stringstream.cc
CommitLineData
b2dad0e3
BK
1// 981015 bkoz
2// i,o,stringstream usage
3
4// Copyright (C) 1997-1999 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
b2dad0e3
BK
31#include <vector>
32#include <string>
33#include <sstream>
aa1b2f7d 34#include <debug_assert.h>
b2dad0e3
BK
35
36// 01: sanity checks for strings, stringbufs
aa1b2f7d
BV
37std::string
38test01()
b2dad0e3
BK
39{
40 bool test = false;
41
42 // Empty string sanity check.
43 std::string str01;
44 std::string::iterator __i_start = str01.begin();
b2dad0e3 45 std::string::iterator __i_end = str01.end();
b2dad0e3 46 std::string::size_type len = str01.size();
97e0a05a 47 test = __i_start == __i_end;
aa1b2f7d 48 VERIFY( len == 0 );
b2dad0e3
BK
49
50 // Full string sanity check.
04d930e6 51 // { dg-warning "string literals" "" { xfail *-*-* } 52 }
b2dad0e3
BK
52 std::string str02("these golden days, i spend waiting for you:\n
53 Betty Carter on Verve with I'm Yours and You're Mine.");
54 __i_start = str02.begin();
b2dad0e3 55 __i_end = str02.end();
b2dad0e3 56 len = str02.size();
aa1b2f7d
BV
57 VERIFY( __i_start != __i_end );
58 VERIFY( len != 0 );
b2dad0e3
BK
59
60 // Test an empty ostring stream for sanity.
61 std::ostringstream ostrstream0;
62 std::string str03 = ostrstream0.str();
63 __i_start = str03.begin();
b2dad0e3 64 __i_end = str03.end();
b2dad0e3 65 len = str03.size();
aa1b2f7d
BV
66 VERIFY( __i_start == __i_end );
67 VERIFY( len == 0 );
68 VERIFY( str01 == str03 );
b2dad0e3
BK
69
70 return str02;
71}
72
73
aa1b2f7d
BV
74int
75test02()
76{
b2dad0e3
BK
77 bool test = true;
78
79 //
80 // 1: Automatic formatting of a compound string
81 //
82 int i = 1024;
83 int *pi = &i;
84 double d = 3.14159;
85 double *pd = &d;
86 std::string blank;
87 std::ostringstream ostrst01;
88 std::ostringstream ostrst02(blank);
89
f60ded13 90 // No buffer,so should be created.
b2dad0e3
BK
91 ostrst01 << "i: " << i << " i's address: " << pi << "\n"
92 << "d: " << d << " d's address: " << pd << std::endl;
f60ded13 93 // Buffer, so existing buffer should be overwritten.
b2dad0e3
BK
94 ostrst02 << "i: " << i << " i's address: " << pi << "\n"
95 << "d: " << d << " d's address: " << pd << std::endl;
96
97 std::string msg01 = ostrst01.str();
98 std::string msg02 = ostrst02.str();
f60ded13 99 VERIFY( msg01 == msg02 );
aa1b2f7d 100 VERIFY( msg02 != blank );
b2dad0e3
BK
101
102 //
103 // 2: istringstream
104 //
105 // extracts the stored ascii values, placing them in turn in the four vars
106#if 0
107 int i2 = 0;
108 int *pi2 = &i2;
109 double d2 = 0.0;
110 double *pd2 = &d2;
111 std::istringstream istrst01(ostrst02.str());
112
113 istrst01 >> i2 >> pi2 >> d2 >> pd2;
114 //istrst01 >> i2;
115 //istrst01 >> pi2;
aa1b2f7d
BV
116 VERIFY( i2 == i );
117 VERIFY( d2 == d );
118 VERIFY( pd2 == pd );
119 VERIFY( pi2 == pi );
b2dad0e3
BK
120#endif
121
122 // stringstream
123 std::string str1("");
124 std::string str3("this is a somewhat string");
125 std::stringstream ss1(str1, std::ios_base::in|std::ios_base::out);
126 std::stringstream ss2(str3, std::ios_base::in|std::ios_base::out);
127
aa1b2f7d 128 return 0;
b2dad0e3
BK
129}
130
131// user-reported error
132class derived_oss: public std::ostringstream
133{
134public:
135 derived_oss() : std::ostringstream() {}
136};
137
aa1b2f7d
BV
138int
139test03()
b2dad0e3
BK
140{
141 bool test = true;
142 derived_oss yy;
143 yy << "buena vista social club\n";
aa1b2f7d 144 VERIFY( yy.str() == std::string("buena vista social club\n") );
b2dad0e3
BK
145
146#ifdef DEBUG_ASSERT
147 assert(test);
148#endif
149
aa1b2f7d 150 return 0;
b2dad0e3
BK
151}
152
aa1b2f7d
BV
153int
154main()
b2dad0e3
BK
155{
156 test01();
157 test02();
158 test03();
aa1b2f7d
BV
159
160 return 0;
b2dad0e3 161}