| 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 | // template <class charT, class traits = char_traits<charT> > |
| 15 | // class basic_istream; |
| 16 | |
| 17 | // basic_istream<charT,traits>& operator>>(basic_streambuf<charT,traits>* sb); |
| 18 | |
| 19 | #include <istream> |
| 20 | #include <cassert> |
| 21 | #include <streambuf> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | |
| 25 | template <class CharT> |
| 26 | class testbuf |
| 27 | : public std::basic_streambuf<CharT> |
| 28 | { |
| 29 | typedef std::basic_streambuf<CharT> base; |
| 30 | std::basic_string<CharT> str_; |
| 31 | public: |
| 32 | testbuf() |
| 33 | { |
| 34 | } |
| 35 | testbuf(const std::basic_string<CharT>& str) |
| 36 | : str_(str) |
| 37 | { |
| 38 | base::setg(const_cast<CharT*>(str_.data()), |
| 39 | const_cast<CharT*>(str_.data()), |
| 40 | const_cast<CharT*>(str_.data() + str_.size())); |
| 41 | } |
| 42 | |
| 43 | std::basic_string<CharT> str() const |
| 44 | {return std::basic_string<CharT>(base::pbase(), base::pptr());} |
| 45 | |
| 46 | protected: |
| 47 | |
| 48 | virtual typename base::int_type |
| 49 | overflow(typename base::int_type ch = base::traits_type::eof()) |
| 50 | { |
| 51 | if (ch != base::traits_type::eof()) |
| 52 | { |
| 53 | std::size_t n = str_.size(); |
| 54 | str_.push_back(static_cast<CharT>(ch)); |
| 55 | str_.resize(str_.capacity()); |
| 56 | base::setp(const_cast<CharT*>(str_.data()), |
| 57 | const_cast<CharT*>(str_.data() + str_.size())); |
| 58 | base::pbump(static_cast<int>(n+1)); |
| 59 | } |
| 60 | return ch; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | int main(int, char**) |
| 65 | { |
| 66 | { |
| 67 | testbuf<char> sb("testing..." ); |
| 68 | std::istream is(&sb); |
| 69 | testbuf<char> sb2; |
| 70 | is >> &sb2; |
| 71 | assert(sb2.str() == "testing..." ); |
| 72 | assert(is.gcount() == 10); |
| 73 | } |
| 74 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 75 | { |
| 76 | testbuf<char> sb(" " ); |
| 77 | std::basic_istream<char> is(&sb); |
| 78 | testbuf<char> sb2; |
| 79 | is.exceptions(std::istream::eofbit); |
| 80 | bool threw = false; |
| 81 | try { |
| 82 | is >> &sb2; |
| 83 | } catch (std::ios_base::failure&) { |
| 84 | threw = true; |
| 85 | } |
| 86 | assert(threw); |
| 87 | assert(!is.bad()); |
| 88 | assert( is.eof()); |
| 89 | assert(!is.fail()); |
| 90 | } |
| 91 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 92 | { |
| 93 | testbuf<wchar_t> sb(L" " ); |
| 94 | std::basic_istream<wchar_t> is(&sb); |
| 95 | testbuf<wchar_t> sb2; |
| 96 | is.exceptions(std::istream::eofbit); |
| 97 | bool threw = false; |
| 98 | try { |
| 99 | is >> &sb2; |
| 100 | } catch (std::ios_base::failure&) { |
| 101 | threw = true; |
| 102 | } |
| 103 | assert(threw); |
| 104 | assert(!is.bad()); |
| 105 | assert( is.eof()); |
| 106 | assert(!is.fail()); |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | { |
| 111 | testbuf<char> sb; |
| 112 | std::basic_istream<char> is(&sb); |
| 113 | testbuf<char> sb2; |
| 114 | is.exceptions(std::istream::failbit); |
| 115 | bool threw = false; |
| 116 | try { |
| 117 | is >> &sb2; |
| 118 | } catch (std::ios_base::failure&) { |
| 119 | threw = true; |
| 120 | } |
| 121 | assert(threw); |
| 122 | assert(!is.bad()); |
| 123 | assert( is.eof()); |
| 124 | assert( is.fail()); |
| 125 | } |
| 126 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 127 | { |
| 128 | testbuf<wchar_t> sb; |
| 129 | std::basic_istream<wchar_t> is(&sb); |
| 130 | testbuf<wchar_t> sb2; |
| 131 | is.exceptions(std::istream::failbit); |
| 132 | bool threw = false; |
| 133 | try { |
| 134 | is >> &sb2; |
| 135 | } catch (std::ios_base::failure&) { |
| 136 | threw = true; |
| 137 | } |
| 138 | assert(threw); |
| 139 | assert(!is.bad()); |
| 140 | assert( is.eof()); |
| 141 | assert( is.fail()); |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | { |
| 146 | testbuf<char> sb; |
| 147 | std::basic_istream<char> is(&sb); |
| 148 | is.exceptions(std::istream::failbit); |
| 149 | bool threw = false; |
| 150 | try { |
| 151 | is >> static_cast<testbuf<char>*>(0); |
| 152 | } catch (std::ios_base::failure&) { |
| 153 | threw = true; |
| 154 | } |
| 155 | assert(threw); |
| 156 | assert(!is.bad()); |
| 157 | assert(!is.eof()); |
| 158 | assert( is.fail()); |
| 159 | } |
| 160 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 161 | { |
| 162 | testbuf<wchar_t> sb; |
| 163 | std::basic_istream<wchar_t> is(&sb); |
| 164 | is.exceptions(std::istream::failbit); |
| 165 | bool threw = false; |
| 166 | try { |
| 167 | is >> static_cast<testbuf<wchar_t>*>(0); |
| 168 | } catch (std::ios_base::failure&) { |
| 169 | threw = true; |
| 170 | } |
| 171 | assert(threw); |
| 172 | assert(!is.bad()); |
| 173 | assert(!is.eof()); |
| 174 | assert( is.fail()); |
| 175 | } |
| 176 | #endif |
| 177 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |