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