1// Copyright (C) 2005-2015 Free Software Foundation, Inc. 2// 3// This file is part of the GNU ISO C++ Library. This library is free 4// software; you can redistribute it and/or modify it under the 5// terms of the GNU General Public License as published by the 6// Free Software Foundation; either version 3, or (at your option) 7// any later version. 8 9// This library is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU General Public License for more details. 13 14// You should have received a copy of the GNU General Public License along 15// with this library; see the file COPYING3. If not see 16// <http://www.gnu.org/licenses/>. 17 18// 27.6.2.5.4 basic_ostream character inserters 19 20#include <ostream> 21#include <sstream> 22#include <testsuite_hooks.h> 23 24class test_buffer_1 : public std::wstreambuf 25{ 26public: 27 test_buffer_1(const std::wstring& s) 28 : str(s), it(str.begin()) { } 29 30protected: 31 virtual int_type 32 underflow() 33 { return (it != str.end() ? *it : WEOF); } 34 35 virtual int_type 36 uflow() 37 { return (it != str.end() ? *it++ : WEOF); } 38 39private: 40 const std::wstring str; 41 std::wstring::const_iterator it; 42}; 43 44 45class test_buffer_2 : public std::wstreambuf 46{ 47public: 48 test_buffer_2(const std::wstring& s) 49 : str(s), it(str.begin()) { } 50 51protected: 52 virtual int_type 53 underflow() 54 { return (it != str.end() ? *it : WEOF); } 55 56 virtual int_type 57 uflow() 58 { return (it != str.end() ? *it++ : WEOF); } 59 60 virtual std::streamsize 61 showmanyc() 62 { return std::distance(it, str.end()); } 63 64private: 65 const std::wstring str; 66 std::wstring::const_iterator it; 67}; 68 69 70class test_buffer_3 : public std::wstreambuf 71{ 72public: 73 test_buffer_3(const std::wstring& s) 74 : str(s), it(str.begin()) { } 75 76protected: 77 virtual int_type 78 underflow() 79 { return (it != str.end() ? *it : WEOF); } 80 81 virtual int_type 82 uflow() 83 { return (it != str.end() ? *it++ : WEOF); } 84 85 virtual std::streamsize 86 showmanyc() 87 { 88 std::streamsize ret = std::distance(it, str.end()); 89 return ret > 0 ? ret : -1; 90 } 91 92private: 93 const std::wstring str; 94 std::wstring::const_iterator it; 95}; 96 97class test_buffer_4 : public std::wstreambuf 98{ 99public: 100 test_buffer_4(const std::wstring& s) 101 : str(s), it(str.begin()) 102 { 103 if (it != str.end()) 104 { 105 buf[0] = *it++; 106 setg(buf, buf, buf+1); 107 } 108 } 109 110protected: 111 virtual int_type 112 underflow() 113 { return (it != str.end() ? *it : WEOF); } 114 115 virtual int_type 116 uflow() 117 { return (it != str.end() ? *it++ : WEOF); } 118 119 virtual std::streamsize 120 showmanyc() 121 { 122 std::streamsize ret = std::distance(it, str.end()); 123 return ret > 0 ? ret : -1; 124 } 125 126private: 127 const std::wstring str; 128 std::wstring::const_iterator it; 129 wchar_t buf[1]; 130}; 131 132void test(const std::wstring& str, std::wstreambuf& buf) 133{ 134 bool test __attribute__((unused)) = true; 135 136 std::wostringstream out; 137 std::wistream in(&buf); 138 139 out << in.rdbuf(); 140 141 if (out.str() != str) 142 VERIFY( false ); 143} 144 145// libstdc++/6745 146// libstdc++/8071 147// libstdc++/8127 148// Jonathan Lennox <lennox@cs.columbia.edu> 149void test05() 150{ 151 std::wstring string_a(L"Hello, world!"); 152 std::wstring string_b(L""); 153 154 test_buffer_1 buf1a(string_a); 155 test_buffer_1 buf1b(string_b); 156 157 test_buffer_2 buf2a(string_a); 158 test_buffer_2 buf2b(string_b); 159 160 test_buffer_3 buf3a(string_a); 161 test_buffer_3 buf3b(string_b); 162 163 test_buffer_4 buf4a(string_a); 164 test_buffer_4 buf4b(string_b); 165 166 test(string_a, buf1a); 167 test(string_b, buf1b); 168 169 test(string_a, buf2a); 170 test(string_b, buf2b); 171 172 test(string_a, buf3a); 173 test(string_b, buf3b); 174 175 test(string_a, buf4a); 176 test(string_b, buf4b); 177} 178 179int 180main() 181{ 182 test05(); 183 return 0; 184} 185