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 // basic_istream<charT,traits>& read(char_type* s, streamsize n); 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(" 123456789"); 47 std::istream is(&sb); 48 char s[5]; 49 is.read(s, 5); 50 assert(!is.eof()); 51 assert(!is.fail()); 52 assert(std::string(s, 5) == " 1234"); 53 assert(is.gcount() == 5); 54 is.read(s, 5); 55 assert(!is.eof()); 56 assert(!is.fail()); 57 assert(std::string(s, 5) == "56789"); 58 assert(is.gcount() == 5); 59 is.read(s, 5); 60 assert( is.eof()); 61 assert( is.fail()); 62 assert(is.gcount() == 0); 63 } 64 { 65 testbuf<wchar_t> sb(L" 123456789"); 66 std::wistream is(&sb); 67 wchar_t s[5]; 68 is.read(s, 5); 69 assert(!is.eof()); 70 assert(!is.fail()); 71 assert(std::wstring(s, 5) == L" 1234"); 72 assert(is.gcount() == 5); 73 is.read(s, 5); 74 assert(!is.eof()); 75 assert(!is.fail()); 76 assert(std::wstring(s, 5) == L"56789"); 77 assert(is.gcount() == 5); 78 is.read(s, 5); 79 assert( is.eof()); 80 assert( is.fail()); 81 assert(is.gcount() == 0); 82 } 83 #ifndef TEST_HAS_NO_EXCEPTIONS 84 { 85 testbuf<char> sb; 86 std::basic_istream<char> is(&sb); 87 is.exceptions(std::ios_base::eofbit); 88 char s[10]; 89 bool threw = false; 90 try { 91 is.read(s, 5); 92 } catch (std::ios_base::failure&) { 93 threw = true; 94 } 95 assert(threw); 96 assert(!is.bad()); 97 assert( is.eof()); 98 assert( is.fail()); 99 } 100 { 101 testbuf<wchar_t> sb; 102 std::basic_istream<wchar_t> is(&sb); 103 is.exceptions(std::ios_base::eofbit); 104 wchar_t s[10]; 105 bool threw = false; 106 try { 107 is.read(s, 5); 108 } catch (std::ios_base::failure&) { 109 threw = true; 110 } 111 assert(threw); 112 assert(!is.bad()); 113 assert( is.eof()); 114 assert( is.fail()); 115 } 116 #endif 117 118 return 0; 119 } 120