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: with_system_cxx_lib=macosx10.14
10 // XFAIL: with_system_cxx_lib=macosx10.13
11 // XFAIL: with_system_cxx_lib=macosx10.12
12 // XFAIL: with_system_cxx_lib=macosx10.11
13 // XFAIL: with_system_cxx_lib=macosx10.10
14 // XFAIL: with_system_cxx_lib=macosx10.9
15 
16 // <istream>
17 
18 // basic_istream<charT,traits>& read(char_type* s, streamsize n);
19 
20 #include <istream>
21 #include <cassert>
22 #include "test_macros.h"
23 
24 template <class CharT>
25 struct testbuf
26     : public std::basic_streambuf<CharT>
27 {
28     typedef std::basic_string<CharT> string_type;
29     typedef std::basic_streambuf<CharT> base;
30 private:
31     string_type str_;
32 public:
33 
testbuftestbuf34     testbuf() {}
testbuftestbuf35     testbuf(const string_type& str)
36         : str_(str)
37     {
38         base::setg(const_cast<CharT*>(str_.data()),
39                    const_cast<CharT*>(str_.data()),
40                    const_cast<CharT*>(str_.data()) + str_.size());
41     }
42 
ebacktestbuf43     CharT* eback() const {return base::eback();}
gptrtestbuf44     CharT* gptr() const {return base::gptr();}
egptrtestbuf45     CharT* egptr() const {return base::egptr();}
46 };
47 
main(int,char **)48 int main(int, char**)
49 {
50     {
51         testbuf<char> sb(" 123456789");
52         std::istream is(&sb);
53         char s[5];
54         is.read(s, 5);
55         assert(!is.eof());
56         assert(!is.fail());
57         assert(std::string(s, 5) == " 1234");
58         assert(is.gcount() == 5);
59         is.read(s, 5);
60         assert(!is.eof());
61         assert(!is.fail());
62         assert(std::string(s, 5) == "56789");
63         assert(is.gcount() == 5);
64         is.read(s, 5);
65         assert( is.eof());
66         assert( is.fail());
67         assert(is.gcount() == 0);
68     }
69     {
70         testbuf<wchar_t> sb(L" 123456789");
71         std::wistream is(&sb);
72         wchar_t s[5];
73         is.read(s, 5);
74         assert(!is.eof());
75         assert(!is.fail());
76         assert(std::wstring(s, 5) == L" 1234");
77         assert(is.gcount() == 5);
78         is.read(s, 5);
79         assert(!is.eof());
80         assert(!is.fail());
81         assert(std::wstring(s, 5) == L"56789");
82         assert(is.gcount() == 5);
83         is.read(s, 5);
84         assert( is.eof());
85         assert( is.fail());
86         assert(is.gcount() == 0);
87     }
88 #ifndef TEST_HAS_NO_EXCEPTIONS
89     {
90         testbuf<char> sb;
91         std::basic_istream<char> is(&sb);
92         is.exceptions(std::ios_base::eofbit);
93         char s[10];
94         bool threw = false;
95         try {
96             is.read(s, 5);
97         } catch (std::ios_base::failure&) {
98             threw = true;
99         }
100         assert(threw);
101         assert(!is.bad());
102         assert( is.eof());
103         assert( is.fail());
104     }
105     {
106         testbuf<wchar_t> sb;
107         std::basic_istream<wchar_t> is(&sb);
108         is.exceptions(std::ios_base::eofbit);
109         wchar_t s[10];
110         bool threw = false;
111         try {
112             is.read(s, 5);
113         } catch (std::ios_base::failure&) {
114             threw = true;
115         }
116         assert(threw);
117         assert(!is.bad());
118         assert( is.eof());
119         assert( is.fail());
120     }
121 #endif
122 
123     return 0;
124 }
125