Debian Codegraph @

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

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