| 1 | // Copyright (C) 2005-2025 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 | |
| 24 | class test_buffer_1 : public std::wstreambuf |
| 25 | { |
| 26 | public: |
| 27 | test_buffer_1(const std::wstring& s) |
| 28 | : str(s), it(str.begin()) { } |
| 29 | |
| 30 | protected: |
| 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 | |
| 39 | private: |
| 40 | const std::wstring str; |
| 41 | std::wstring::const_iterator it; |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | class test_buffer_2 : public std::wstreambuf |
| 46 | { |
| 47 | public: |
| 48 | test_buffer_2(const std::wstring& s) |
| 49 | : str(s), it(str.begin()) { } |
| 50 | |
| 51 | protected: |
| 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 | |
| 64 | private: |
| 65 | const std::wstring str; |
| 66 | std::wstring::const_iterator it; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | class test_buffer_3 : public std::wstreambuf |
| 71 | { |
| 72 | public: |
| 73 | test_buffer_3(const std::wstring& s) |
| 74 | : str(s), it(str.begin()) { } |
| 75 | |
| 76 | protected: |
| 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 | |
| 92 | private: |
| 93 | const std::wstring str; |
| 94 | std::wstring::const_iterator it; |
| 95 | }; |
| 96 | |
| 97 | class test_buffer_4 : public std::wstreambuf |
| 98 | { |
| 99 | public: |
| 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 | |
| 110 | protected: |
| 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 | |
| 126 | private: |
| 127 | const std::wstring str; |
| 128 | std::wstring::const_iterator it; |
| 129 | wchar_t buf[1]; |
| 130 | }; |
| 131 | |
| 132 | void test(const std::wstring& str, std::wstreambuf& buf) |
| 133 | { |
| 134 | std::wostringstream out; |
| 135 | std::wistream in(&buf); |
| 136 | |
| 137 | out << in.rdbuf(); |
| 138 | |
| 139 | if (out.str() != str) |
| 140 | VERIFY( false ); |
| 141 | } |
| 142 | |
| 143 | // libstdc++/6745 |
| 144 | // libstdc++/8071 |
| 145 | // libstdc++/8127 |
| 146 | // Jonathan Lennox <[email protected]> |
| 147 | void test05() |
| 148 | { |
| 149 | std::wstring string_a(L"Hello, world!"); |
| 150 | std::wstring string_b(L""); |
| 151 | |
| 152 | test_buffer_1 buf1a(string_a); |
| 153 | test_buffer_1 buf1b(string_b); |
| 154 | |
| 155 | test_buffer_2 buf2a(string_a); |
| 156 | test_buffer_2 buf2b(string_b); |
| 157 | |
| 158 | test_buffer_3 buf3a(string_a); |
| 159 | test_buffer_3 buf3b(string_b); |
| 160 | |
| 161 | test_buffer_4 buf4a(string_a); |
| 162 | test_buffer_4 buf4b(string_b); |
| 163 | |
| 164 | test(string_a, buf1a); |
| 165 | test(string_b, buf1b); |
| 166 | |
| 167 | test(string_a, buf2a); |
| 168 | test(string_b, buf2b); |
| 169 | |
| 170 | test(string_a, buf3a); |
| 171 | test(string_b, buf3b); |
| 172 | |
| 173 | test(string_a, buf4a); |
| 174 | test(string_b, buf4b); |
| 175 | } |
| 176 | |
| 177 | int |
| 178 | main() |
| 179 | { |
| 180 | test05(); |
| 181 | return 0; |
| 182 | } |