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(char_type& c); 15 16 #include <istream> 17 #include <cassert> 18 #include <streambuf> 19 20 #include "test_macros.h" 21 22 template <class CharT> 23 struct testbuf 24 : public std::basic_streambuf<CharT> 25 { 26 typedef std::basic_string<CharT> string_type; 27 typedef std::basic_streambuf<CharT> base; 28 private: 29 string_type str_; 30 public: 31 32 testbuf() {} 33 testbuf(const string_type& str) 34 : str_(str) 35 { 36 base::setg(const_cast<CharT*>(str_.data()), 37 const_cast<CharT*>(str_.data()), 38 const_cast<CharT*>(str_.data()) + str_.size()); 39 } 40 41 CharT* eback() const {return base::eback();} 42 CharT* gptr() const {return base::gptr();} 43 CharT* egptr() const {return base::egptr();} 44 }; 45 46 int main(int, char**) 47 { 48 { 49 testbuf<char> sb(" "); 50 std::istream is(&sb); 51 char c; 52 is.get(c); 53 assert(!is.eof()); 54 assert(!is.fail()); 55 assert(c == ' '); 56 assert(is.gcount() == 1); 57 } 58 { 59 testbuf<char> sb(" abc"); 60 std::istream is(&sb); 61 char c; 62 is.get(c); 63 assert(!is.eof()); 64 assert(!is.fail()); 65 assert(c == ' '); 66 assert(is.gcount() == 1); 67 is.get(c); 68 assert(!is.eof()); 69 assert(!is.fail()); 70 assert(c == 'a'); 71 assert(is.gcount() == 1); 72 is.get(c); 73 assert(!is.eof()); 74 assert(!is.fail()); 75 assert(c == 'b'); 76 assert(is.gcount() == 1); 77 is.get(c); 78 assert(!is.eof()); 79 assert(!is.fail()); 80 assert(c == 'c'); 81 assert(is.gcount() == 1); 82 } 83 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 84 { 85 testbuf<wchar_t> sb(L" abc"); 86 std::wistream is(&sb); 87 wchar_t c; 88 is.get(c); 89 assert(!is.eof()); 90 assert(!is.fail()); 91 assert(c == L' '); 92 assert(is.gcount() == 1); 93 is.get(c); 94 assert(!is.eof()); 95 assert(!is.fail()); 96 assert(c == L'a'); 97 assert(is.gcount() == 1); 98 is.get(c); 99 assert(!is.eof()); 100 assert(!is.fail()); 101 assert(c == L'b'); 102 assert(is.gcount() == 1); 103 is.get(c); 104 assert(!is.eof()); 105 assert(!is.fail()); 106 assert(c == L'c'); 107 assert(is.gcount() == 1); 108 } 109 #endif // TEST_HAS_NO_WIDE_CHARACTERS 110 #ifndef TEST_HAS_NO_EXCEPTIONS 111 { 112 testbuf<char> sb("rrrrrrrrr"); 113 std::basic_istream<char> is(&sb); 114 is.exceptions(std::ios_base::eofbit); 115 116 bool threw = false; 117 try { 118 while (true) { 119 char c; 120 is.get(c); 121 if (is.eof()) 122 break; 123 } 124 } catch (std::ios_base::failure const&) { 125 threw = true; 126 } 127 128 assert(!is.bad()); 129 assert( is.fail()); 130 assert( is.eof()); 131 assert(threw); 132 } 133 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 134 { 135 testbuf<wchar_t> sb(L"rrrrrrrrr"); 136 std::basic_istream<wchar_t> is(&sb); 137 is.exceptions(std::ios_base::eofbit); 138 139 bool threw = false; 140 try { 141 while (true) { 142 wchar_t c; 143 is.get(c); 144 if (is.eof()) 145 break; 146 } 147 } catch (std::ios_base::failure const&) { 148 threw = true; 149 } 150 151 assert(!is.bad()); 152 assert( is.fail()); 153 assert( is.eof()); 154 assert(threw); 155 } 156 #endif // TEST_HAS_NO_WIDE_CHARACTERS 157 #endif // TEST_HAS_NO_EXCEPTIONS 158 159 return 0; 160 } 161