| 1 | // 1999-08-16 bkoz |
| 2 | // 1999-11-01 bkoz |
| 3 | |
| 4 | // Copyright (C) 1999-2025 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 | // 27.6.2.5.4 basic_ostream character inserters |
| 22 | |
| 23 | #include <ostream> |
| 24 | #include <sstream> |
| 25 | #include <cstdio> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | class test_buffer_1 : public std::streambuf |
| 29 | { |
| 30 | public: |
| 31 | test_buffer_1(const std::string& s) : str(s), it(str.begin()) { } |
| 32 | |
| 33 | protected: |
| 34 | virtual int underflow() { return (it != str.end() ? *it : EOF); } |
| 35 | virtual int uflow() { return (it != str.end() ? *it++ : EOF); } |
| 36 | |
| 37 | private: |
| 38 | const std::string str; |
| 39 | std::string::const_iterator it; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | class test_buffer_2 : public std::streambuf |
| 44 | { |
| 45 | public: |
| 46 | test_buffer_2(const std::string& s) : str(s), it(str.begin()) { } |
| 47 | |
| 48 | protected: |
| 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()); } |
| 52 | private: |
| 53 | const std::string str; |
| 54 | std::string::const_iterator it; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | class test_buffer_3 : public std::streambuf |
| 59 | { |
| 60 | public: |
| 61 | test_buffer_3(const std::string& s) : str(s), it(str.begin()) { } |
| 62 | |
| 63 | protected: |
| 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 | } |
| 71 | private: |
| 72 | const std::string str; |
| 73 | std::string::const_iterator it; |
| 74 | }; |
| 75 | |
| 76 | class test_buffer_4 : public std::streambuf { |
| 77 | public: |
| 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 | |
| 86 | protected: |
| 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 | } |
| 93 | private: |
| 94 | const std::string str; |
| 95 | std::string::const_iterator it; |
| 96 | char buf[1]; |
| 97 | }; |
| 98 | |
| 99 | void test(const std::string& str, std::streambuf& buf) |
| 100 | { |
| 101 | std::ostringstream out; |
| 102 | std::istream in(&buf); |
| 103 | |
| 104 | out << in.rdbuf(); |
| 105 | |
| 106 | if (out.str() != str) |
| 107 | VERIFY( false ); |
| 108 | } |
| 109 | |
| 110 | // libstdc++/6745 |
| 111 | // libstdc++/8071 |
| 112 | // libstdc++/8127 |
| 113 | // Jonathan Lennox <[email protected]> |
| 114 | void test05() |
| 115 | { |
| 116 | std::string string_a("Hello, world!"); |
| 117 | std::string string_b(""); |
| 118 | |
| 119 | test_buffer_1 buf1a(string_a); |
| 120 | test_buffer_1 buf1b(string_b); |
| 121 | |
| 122 | test_buffer_2 buf2a(string_a); |
| 123 | test_buffer_2 buf2b(string_b); |
| 124 | |
| 125 | test_buffer_3 buf3a(string_a); |
| 126 | test_buffer_3 buf3b(string_b); |
| 127 | |
| 128 | test_buffer_4 buf4a(string_a); |
| 129 | test_buffer_4 buf4b(string_b); |
| 130 | |
| 131 | test(string_a, buf1a); |
| 132 | test(string_b, buf1b); |
| 133 | |
| 134 | test(string_a, buf2a); |
| 135 | test(string_b, buf2b); |
| 136 | |
| 137 | test(string_a, buf3a); |
| 138 | test(string_b, buf3b); |
| 139 | |
| 140 | test(string_a, buf4a); |
| 141 | test(string_b, buf4b); |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | main() |
| 146 | { |
| 147 | test05(); |
| 148 | return 0; |
| 149 | } |