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// basic_istream<charT,traits>& read(char_type* s, streamsize n);
15
16#include <istream>
17#include <cassert>
18#include <streambuf>
19
20#include "test_macros.h"
21
22template <class CharT>
23struct testbuf
24 : public std::basic_streambuf<CharT>
25{
26 typedef std::basic_string<CharT> string_type;
27 typedef std::basic_streambuf<CharT> base;
28private:
29 string_type str_;
30public:
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
46int main(int, char**)
47{
48 {
49 testbuf<char> sb(" 123456789");
50 std::istream is(&sb);
51 char s[5];
52 is.read(s: s, n: 5);
53 assert(!is.eof());
54 assert(!is.fail());
55 assert(std::string(s, 5) == " 1234");
56 assert(is.gcount() == 5);
57 is.read(s: s, n: 5);
58 assert(!is.eof());
59 assert(!is.fail());
60 assert(std::string(s, 5) == "56789");
61 assert(is.gcount() == 5);
62 is.read(s: s, n: 5);
63 assert( is.eof());
64 assert( is.fail());
65 assert(is.gcount() == 0);
66 }
67#ifndef TEST_HAS_NO_WIDE_CHARACTERS
68 {
69 testbuf<wchar_t> sb(L" 123456789");
70 std::wistream is(&sb);
71 wchar_t s[5];
72 is.read(s: s, n: 5);
73 assert(!is.eof());
74 assert(!is.fail());
75 assert(std::wstring(s, 5) == L" 1234");
76 assert(is.gcount() == 5);
77 is.read(s: s, n: 5);
78 assert(!is.eof());
79 assert(!is.fail());
80 assert(std::wstring(s, 5) == L"56789");
81 assert(is.gcount() == 5);
82 is.read(s: s, n: 5);
83 assert( is.eof());
84 assert( is.fail());
85 assert(is.gcount() == 0);
86 }
87#endif
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#ifndef TEST_HAS_NO_WIDE_CHARACTERS
106 {
107 testbuf<wchar_t> sb;
108 std::basic_istream<wchar_t> is(&sb);
109 is.exceptions(std::ios_base::eofbit);
110 wchar_t s[10];
111 bool threw = false;
112 try {
113 is.read(s, 5);
114 } catch (std::ios_base::failure&) {
115 threw = true;
116 }
117 assert(threw);
118 assert(!is.bad());
119 assert( is.eof());
120 assert( is.fail());
121 }
122#endif
123#endif // TEST_HAS_NO_EXCEPTIONS
124
125 return 0;
126}
127

source code of libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/read.pass.cpp