[X86] Better handling of impossibly large stack frames (#124217)
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / input.streams / istream.unformatted / read.pass.cpp
blobc7cca7bf7b41750ff9acc7a21a7ada77e01348f7
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 //===----------------------------------------------------------------------===//
9 // Requires 396145d in the built library.
10 // XFAIL: using-built-library-before-llvm-9
12 // <istream>
14 // basic_istream<charT,traits>& read(char_type* s, streamsize n);
16 #include <istream>
17 #include <cassert>
18 #include <streambuf>
20 #include "test_macros.h"
22 template <class CharT>
23 struct testbuf
24 : public std::basic_streambuf<CharT>
26 typedef std::basic_string<CharT> string_type;
27 typedef std::basic_streambuf<CharT> base;
28 private:
29 string_type str_;
30 public:
32 testbuf() {}
33 testbuf(const string_type& str)
34 : str_(str)
36 base::setg(const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data()),
38 const_cast<CharT*>(str_.data()) + str_.size());
41 CharT* eback() const {return base::eback();}
42 CharT* gptr() const {return base::gptr();}
43 CharT* egptr() const {return base::egptr();}
46 int main(int, char**)
49 testbuf<char> sb(" 123456789");
50 std::istream is(&sb);
51 char s[5];
52 is.read(s, 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, 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, 5);
63 assert( is.eof());
64 assert( is.fail());
65 assert(is.gcount() == 0);
67 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69 testbuf<wchar_t> sb(L" 123456789");
70 std::wistream is(&sb);
71 wchar_t s[5];
72 is.read(s, 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, 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, 5);
83 assert( is.eof());
84 assert( is.fail());
85 assert(is.gcount() == 0);
87 #endif
88 #ifndef TEST_HAS_NO_EXCEPTIONS
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;
100 assert(threw);
101 assert(!is.bad());
102 assert( is.eof());
103 assert( is.fail());
105 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
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;
117 assert(threw);
118 assert(!is.bad());
119 assert( is.eof());
120 assert( is.fail());
122 #endif
123 #endif // TEST_HAS_NO_EXCEPTIONS
125 return 0;