]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-07-01 bkoz |
2 | ||
7adcbafe | 3 | // Copyright (C) 1999-2022 Free Software Foundation, Inc. |
b2dad0e3 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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
19 | |
20 | // 21.3.7.9 inserters and extractors | |
21 | ||
22 | // NB: This file is predicated on sstreams, istreams, and ostreams | |
23 | // working, not to mention other major details like char_traits, and | |
24 | // all of the string class. | |
25 | ||
26 | #include <string> | |
27 | #include <stdexcept> | |
28 | #include <sstream> | |
29 | #include <fstream> | |
30 | #include <iostream> | |
fe413112 | 31 | #include <testsuite_hooks.h> |
b2dad0e3 | 32 | |
118c8424 | 33 | void test01(void) |
b2dad0e3 | 34 | { |
b2dad0e3 BK |
35 | typedef std::string::size_type csize_type; |
36 | typedef std::string::const_reference cref; | |
37 | typedef std::string::reference ref; | |
b2dad0e3 | 38 | |
7edc73b4 ZW |
39 | const std::string str01("sailing grand traverse bay\n" |
40 | "\t\t\t from Elk Rapids to the point reminds me of miles"); | |
b2dad0e3 BK |
41 | const std::string str02("sailing"); |
42 | const std::string str03("grand"); | |
43 | const std::string str04("traverse"); | |
44 | const std::string str05; | |
45 | std::string str10; | |
46 | ||
47 | // istream& operator>>(istream&, string&) | |
48 | std::istringstream istrs01(str01); | |
49 | istrs01 >> str10; | |
aa1b2f7d | 50 | VERIFY( str10 == str02 ); |
36ce7daa BK |
51 | try |
52 | { | |
53 | std::istringstream::int_type i01 = istrs01.peek(); //a-boo | |
54 | VERIFY( std::istringstream::traits_type::to_char_type(i01) == ' ' ); | |
55 | } | |
56 | catch(std::exception& fail) | |
57 | { | |
58 | VERIFY( false ); // shouldn't throw | |
59 | } | |
60 | ||
61 | istrs01.clear(); | |
b2dad0e3 | 62 | istrs01 >> str10; |
aa1b2f7d | 63 | VERIFY( str10 == str03 ); |
36ce7daa | 64 | istrs01.clear(); |
b2dad0e3 | 65 | istrs01 >> str10; |
aa1b2f7d | 66 | VERIFY( str10 == str04 ); // sentry picks out the white spaces. . |
b2dad0e3 BK |
67 | |
68 | std::istringstream istrs02(str05); // empty | |
69 | istrs02 >> str10; | |
aa1b2f7d | 70 | VERIFY( str10 == str04 ); |
b2dad0e3 BK |
71 | |
72 | // istream& getline(istream&, string&, char) | |
73 | // istream& getline(istream&, string&) | |
36ce7daa BK |
74 | try |
75 | { | |
76 | istrs01.clear(); | |
77 | getline(istrs01, str10); | |
78 | VERIFY( !istrs01.fail() ); | |
79 | VERIFY( !istrs01.eof() ); | |
80 | VERIFY( istrs01.good() ); | |
81 | VERIFY( str10 == " bay" ); | |
82 | } | |
83 | catch(std::exception& fail) | |
84 | { | |
85 | VERIFY( false ); // shouldn't throw | |
86 | } | |
87 | ||
88 | try | |
89 | { | |
90 | istrs01.clear(); | |
91 | getline(istrs01, str10,'\t'); | |
92 | VERIFY( !istrs01.fail() ); | |
93 | VERIFY( !istrs01.eof() ); | |
94 | VERIFY( istrs01.good() ); | |
95 | VERIFY( str10 == str05 ); | |
96 | } | |
97 | catch(std::exception& fail) | |
98 | { | |
99 | VERIFY( false ); // shouldn't throw | |
100 | } | |
101 | ||
102 | try | |
103 | { | |
104 | istrs01.clear(); | |
105 | getline(istrs01, str10,'\t'); | |
106 | VERIFY( !istrs01.fail() ); | |
107 | VERIFY( !istrs01.eof() ); | |
108 | VERIFY( istrs01.good() ); | |
109 | VERIFY( str10 == str05 ); | |
110 | } | |
111 | catch(std::exception& fail) | |
112 | { | |
113 | VERIFY( false ); // shouldn't throw | |
114 | } | |
115 | ||
116 | try | |
117 | { | |
118 | istrs01.clear(); | |
119 | getline(istrs01, str10, '.'); | |
120 | VERIFY( !istrs01.fail() ); | |
121 | VERIFY( istrs01.eof() ); | |
122 | VERIFY( !istrs01.good() ); | |
123 | VERIFY( str10 == "\t from Elk Rapids to the point reminds me of miles" ); | |
124 | } | |
125 | catch(std::exception& fail) | |
126 | { | |
127 | VERIFY( false ); // shouldn't throw | |
128 | } | |
129 | ||
130 | try | |
131 | { | |
132 | getline(istrs02, str10); | |
133 | VERIFY( istrs02.fail() ); | |
134 | VERIFY( istrs02.eof() ); | |
135 | VERIFY( str10 =="\t from Elk Rapids to the point reminds me of miles" ); | |
136 | } | |
137 | catch(std::exception& fail) | |
138 | { | |
139 | VERIFY( false ); // shouldn't throw | |
140 | } | |
141 | ||
b2dad0e3 BK |
142 | // ostream& operator<<(ostream&, const basic_string&) |
143 | std::ostringstream ostrs01; | |
36ce7daa BK |
144 | try |
145 | { | |
146 | ostrs01 << str01; | |
147 | VERIFY( ostrs01.str() == str01 ); | |
148 | } | |
149 | catch(std::exception& fail) | |
150 | { | |
151 | VERIFY( false ); | |
152 | } | |
153 | ||
b2dad0e3 BK |
154 | std::string hello_world; |
155 | std::cout << hello_world; | |
b2dad0e3 BK |
156 | } |
157 | ||
b2dad0e3 BK |
158 | int main() |
159 | { | |
160 | test01(); | |
b2dad0e3 BK |
161 | return 0; |
162 | } |