1// 1999-08-16 bkoz 2// 1999-11-01 bkoz 3 4// Copyright (C) 1999, 2000, 2001, 2002, 2003 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20// USA. 21 22// 27.6.2.5.4 basic_ostream character inserters 23 24#include <ostream> 25#include <sstream> 26#include <testsuite_hooks.h> 27 28class test_buffer_1 : public std::streambuf 29{ 30public: 31 test_buffer_1(const std::string& s) : str(s), it(str.begin()) { } 32 33protected: 34 virtual int underflow() { return (it != str.end() ? *it : EOF); } 35 virtual int uflow() { return (it != str.end() ? *it++ : EOF); } 36 37private: 38 const std::string str; 39 std::string::const_iterator it; 40}; 41 42 43class test_buffer_2 : public std::streambuf 44{ 45public: 46 test_buffer_2(const std::string& s) : str(s), it(str.begin()) { } 47 48protected: 49 virtual int underflow() { return (it != str.end() ? *it : EOF); } 50 virtual int uflow() { return (it != str.end() ? *it++ : EOF); } 51 virtual std::streamsize showmanyc() { return std::distance(it, str.end()); } 52private: 53 const std::string str; 54 std::string::const_iterator it; 55}; 56 57 58class test_buffer_3 : public std::streambuf 59{ 60public: 61 test_buffer_3(const std::string& s) : str(s), it(str.begin()) { } 62 63protected: 64 virtual int underflow() { return (it != str.end() ? *it : EOF); } 65 virtual int uflow() { return (it != str.end() ? *it++ : EOF); } 66 virtual std::streamsize showmanyc() 67 { 68 std::streamsize ret = std::distance(it, str.end()); 69 return ret > 0 ? ret : -1; 70 } 71private: 72 const std::string str; 73 std::string::const_iterator it; 74}; 75 76class test_buffer_4 : public std::streambuf { 77public: 78 test_buffer_4(const std::string& s) : str(s), it(str.begin()) 79 { 80 if (it != str.end()) { 81 buf[0] = *it++; 82 setg(buf, buf, buf+1); 83 } 84 } 85 86protected: 87 virtual int underflow() { return (it != str.end() ? *it : EOF); } 88 virtual int uflow() { return (it != str.end() ? *it++ : EOF); } 89 virtual std::streamsize showmanyc() { 90 std::streamsize ret = std::distance(it, str.end()); 91 return ret > 0 ? ret : -1; 92 } 93private: 94 const std::string str; 95 std::string::const_iterator it; 96 char buf[1]; 97}; 98 99void test(const std::string& str, std::streambuf& buf) 100{ 101 bool test __attribute__((unused)) = true; 102 103 std::ostringstream out; 104 std::istream in(&buf); 105 106 out << in.rdbuf(); 107 108 if (out.str() != str) 109 VERIFY( false ); 110} 111 112// libstdc++/6745 113// libstdc++/8071 114// libstdc++/8127 115// Jonathan Lennox <lennox@cs.columbia.edu> 116void test05() 117{ 118 std::string string_a("Hello, world!"); 119 std::string string_b(""); 120 121 test_buffer_1 buf1a(string_a); 122 test_buffer_1 buf1b(string_b); 123 124 test_buffer_2 buf2a(string_a); 125 test_buffer_2 buf2b(string_b); 126 127 test_buffer_3 buf3a(string_a); 128 test_buffer_3 buf3b(string_b); 129 130 test_buffer_4 buf4a(string_a); 131 test_buffer_4 buf4b(string_b); 132 133 test(string_a, buf1a); 134 test(string_b, buf1b); 135 136 test(string_a, buf2a); 137 test(string_b, buf2b); 138 139 test(string_a, buf3a); 140 test(string_b, buf3b); 141 142 test(string_a, buf4a); 143 test(string_b, buf4b); 144} 145 146int 147main() 148{ 149 test05(); 150 return 0; 151} 152