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