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 // int_type get(); 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 = static_cast<char>(is.get()); 52 assert(!is.eof()); 53 assert(!is.fail()); 54 assert(c == ' '); 55 assert(is.gcount() == 1); 56 } 57 { 58 testbuf<char> sb(" abc"); 59 std::istream is(&sb); 60 char c = static_cast<char>(is.get()); 61 assert(!is.eof()); 62 assert(!is.fail()); 63 assert(c == ' '); 64 assert(is.gcount() == 1); 65 c = static_cast<char>(is.get()); 66 assert(!is.eof()); 67 assert(!is.fail()); 68 assert(c == 'a'); 69 assert(is.gcount() == 1); 70 c = static_cast<char>(is.get()); 71 assert(!is.eof()); 72 assert(!is.fail()); 73 assert(c == 'b'); 74 assert(is.gcount() == 1); 75 c = static_cast<char>(is.get()); 76 assert(!is.eof()); 77 assert(!is.fail()); 78 assert(c == 'c'); 79 assert(is.gcount() == 1); 80 } 81 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 82 { 83 testbuf<wchar_t> sb(L" abc"); 84 std::wistream is(&sb); 85 wchar_t c = is.get(); 86 assert(!is.eof()); 87 assert(!is.fail()); 88 assert(c == L' '); 89 assert(is.gcount() == 1); 90 c = is.get(); 91 assert(!is.eof()); 92 assert(!is.fail()); 93 assert(c == L'a'); 94 assert(is.gcount() == 1); 95 c = is.get(); 96 assert(!is.eof()); 97 assert(!is.fail()); 98 assert(c == L'b'); 99 assert(is.gcount() == 1); 100 c = is.get(); 101 assert(!is.eof()); 102 assert(!is.fail()); 103 assert(c == L'c'); 104 assert(is.gcount() == 1); 105 } 106 #endif 107 #ifndef TEST_HAS_NO_EXCEPTIONS 108 { 109 testbuf<char> sb("rrrrrrrrr"); 110 std::basic_istream<char> is(&sb); 111 is.exceptions(std::ios_base::eofbit); 112 113 bool threw = false; 114 try { 115 while (true) { 116 is.get(); 117 if (is.eof()) 118 break; 119 } 120 } catch (std::ios_base::failure const&) { 121 threw = true; 122 } 123 124 assert(!is.bad()); 125 assert( is.fail()); 126 assert( is.eof()); 127 assert(threw); 128 } 129 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 130 { 131 testbuf<wchar_t> sb(L"rrrrrrrrr"); 132 std::basic_istream<wchar_t> is(&sb); 133 is.exceptions(std::ios_base::eofbit); 134 135 bool threw = false; 136 try { 137 while (true) { 138 is.get(); 139 if (is.eof()) 140 break; 141 } 142 } catch (std::ios_base::failure const&) { 143 threw = true; 144 } 145 146 assert(!is.bad()); 147 assert( is.fail()); 148 assert( is.eof()); 149 assert(threw); 150 } 151 #endif 152 #endif // TEST_HAS_NO_EXCEPTIONS 153 154 return 0; 155 } 156