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
testbuftestbuf29 testbuf() {}
testbuftestbuf30 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
ebacktestbuf38 CharT* eback() const {return base::eback();}
gptrtestbuf39 CharT* gptr() const {return base::gptr();}
egptrtestbuf40 CharT* egptr() const {return base::egptr();}
41 };
42
main(int,char **)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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
65 {
66 testbuf<wchar_t> sb(L" 123456789");
67 std::wistream is(&sb);
68 wchar_t s[5];
69 is.read(s, 5);
70 assert(!is.eof());
71 assert(!is.fail());
72 assert(std::wstring(s, 5) == L" 1234");
73 assert(is.gcount() == 5);
74 is.read(s, 5);
75 assert(!is.eof());
76 assert(!is.fail());
77 assert(std::wstring(s, 5) == L"56789");
78 assert(is.gcount() == 5);
79 is.read(s, 5);
80 assert( is.eof());
81 assert( is.fail());
82 assert(is.gcount() == 0);
83 }
84 #endif
85 #ifndef TEST_HAS_NO_EXCEPTIONS
86 {
87 testbuf<char> sb;
88 std::basic_istream<char> is(&sb);
89 is.exceptions(std::ios_base::eofbit);
90 char s[10];
91 bool threw = false;
92 try {
93 is.read(s, 5);
94 } catch (std::ios_base::failure&) {
95 threw = true;
96 }
97 assert(threw);
98 assert(!is.bad());
99 assert( is.eof());
100 assert( is.fail());
101 }
102 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
103 {
104 testbuf<wchar_t> sb;
105 std::basic_istream<wchar_t> is(&sb);
106 is.exceptions(std::ios_base::eofbit);
107 wchar_t s[10];
108 bool threw = false;
109 try {
110 is.read(s, 5);
111 } catch (std::ios_base::failure&) {
112 threw = true;
113 }
114 assert(threw);
115 assert(!is.bad());
116 assert( is.eof());
117 assert( is.fail());
118 }
119 #endif
120 #endif // TEST_HAS_NO_EXCEPTIONS
121
122 return 0;
123 }
124