| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Requires 396145d in the built library. |
| 10 | // XFAIL: using-built-library-before-llvm-9 |
| 11 | |
| 12 | // <istream> |
| 13 | |
| 14 | // basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb); |
| 15 | |
| 16 | #include <istream> |
| 17 | #include <cassert> |
| 18 | #include <streambuf> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | template <class CharT> |
| 23 | class testbuf |
| 24 | : public std::basic_streambuf<CharT> |
| 25 | { |
| 26 | typedef std::basic_streambuf<CharT> base; |
| 27 | std::basic_string<CharT> str_; |
| 28 | public: |
| 29 | testbuf() |
| 30 | { |
| 31 | } |
| 32 | testbuf(const std::basic_string<CharT>& str) |
| 33 | : str_(str) |
| 34 | { |
| 35 | base::setg(const_cast<CharT*>(str_.data()), |
| 36 | const_cast<CharT*>(str_.data()), |
| 37 | const_cast<CharT*>(str_.data() + str_.size())); |
| 38 | } |
| 39 | |
| 40 | std::basic_string<CharT> str() const |
| 41 | {return std::basic_string<CharT>(base::pbase(), base::pptr());} |
| 42 | |
| 43 | protected: |
| 44 | |
| 45 | virtual typename base::int_type |
| 46 | overflow(typename base::int_type ch = base::traits_type::eof()) |
| 47 | { |
| 48 | if (ch != base::traits_type::eof()) |
| 49 | { |
| 50 | int n = static_cast<int>(str_.size()); |
| 51 | str_.push_back(static_cast<CharT>(ch)); |
| 52 | str_.resize(str_.capacity()); |
| 53 | base::setp(const_cast<CharT*>(str_.data()), |
| 54 | const_cast<CharT*>(str_.data() + str_.size())); |
| 55 | base::pbump(n+1); |
| 56 | } |
| 57 | return ch; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | int main(int, char**) |
| 62 | { |
| 63 | { |
| 64 | testbuf<char> sb("testing\n..."); |
| 65 | std::istream is(&sb); |
| 66 | testbuf<char> sb2; |
| 67 | is.get(sb&: sb2); |
| 68 | assert(sb2.str() == "testing"); |
| 69 | assert(is.good()); |
| 70 | assert(is.gcount() == 7); |
| 71 | assert(is.get() == '\n'); |
| 72 | is.get(sb&: sb2); |
| 73 | assert(sb2.str() == "testing..."); |
| 74 | assert(is.eof()); |
| 75 | assert(!is.fail()); |
| 76 | assert(is.gcount() == 3); |
| 77 | } |
| 78 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 79 | { |
| 80 | testbuf<wchar_t> sb(L"testing\n..."); |
| 81 | std::wistream is(&sb); |
| 82 | testbuf<wchar_t> sb2; |
| 83 | is.get(sb2); |
| 84 | assert(sb2.str() == L"testing"); |
| 85 | assert(is.good()); |
| 86 | assert(is.gcount() == 7); |
| 87 | assert(is.get() == L'\n'); |
| 88 | is.get(sb2); |
| 89 | assert(sb2.str() == L"testing..."); |
| 90 | assert(is.eof()); |
| 91 | assert(!is.fail()); |
| 92 | assert(is.gcount() == 3); |
| 93 | } |
| 94 | #endif |
| 95 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 96 | { |
| 97 | testbuf<char> sb(" "); |
| 98 | std::basic_istream<char> is(&sb); |
| 99 | testbuf<char> sb2; |
| 100 | is.exceptions(std::ios_base::eofbit); |
| 101 | bool threw = false; |
| 102 | try { |
| 103 | is.get(sb2); |
| 104 | } catch (std::ios_base::failure&) { |
| 105 | threw = true; |
| 106 | } |
| 107 | assert(threw); |
| 108 | assert(!is.bad()); |
| 109 | assert( is.eof()); |
| 110 | assert(!is.fail()); |
| 111 | } |
| 112 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 113 | { |
| 114 | testbuf<wchar_t> sb(L" "); |
| 115 | std::basic_istream<wchar_t> is(&sb); |
| 116 | testbuf<wchar_t> sb2; |
| 117 | is.exceptions(std::ios_base::eofbit); |
| 118 | bool threw = false; |
| 119 | try { |
| 120 | is.get(sb2); |
| 121 | } catch (std::ios_base::failure&) { |
| 122 | threw = true; |
| 123 | } |
| 124 | assert(threw); |
| 125 | assert(!is.bad()); |
| 126 | assert( is.eof()); |
| 127 | assert(!is.fail()); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | { |
| 132 | testbuf<char> sb; |
| 133 | std::basic_istream<char> is(&sb); |
| 134 | testbuf<char> sb2; |
| 135 | is.exceptions(std::ios_base::eofbit); |
| 136 | bool threw = false; |
| 137 | try { |
| 138 | is.get(sb2); |
| 139 | } catch (std::ios_base::failure&) { |
| 140 | threw = true; |
| 141 | } |
| 142 | assert(threw); |
| 143 | assert(!is.bad()); |
| 144 | assert( is.eof()); |
| 145 | assert( is.fail()); |
| 146 | } |
| 147 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 148 | { |
| 149 | testbuf<wchar_t> sb; |
| 150 | std::basic_istream<wchar_t> is(&sb); |
| 151 | testbuf<wchar_t> sb2; |
| 152 | is.exceptions(std::ios_base::eofbit); |
| 153 | bool threw = false; |
| 154 | try { |
| 155 | is.get(sb2); |
| 156 | } catch (std::ios_base::failure&) { |
| 157 | threw = true; |
| 158 | } |
| 159 | assert(threw); |
| 160 | assert(!is.bad()); |
| 161 | assert( is.eof()); |
| 162 | assert( is.fail()); |
| 163 | } |
| 164 | #endif |
| 165 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 |
