[SandboxVec][Utils] Implement Utils::verifyFunction() (#124356)
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / input.streams / istream.unformatted / get_streambuf.pass.cpp
blob7988b2f42cb2429ee2b55adeaf43a13f029d7c7e
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>& get(basic_streambuf<char_type,traits>& sb);
16 #include <istream>
17 #include <cassert>
18 #include <streambuf>
20 #include "test_macros.h"
22 template <class CharT>
23 class testbuf
24 : public std::basic_streambuf<CharT>
26 typedef std::basic_streambuf<CharT> base;
27 std::basic_string<CharT> str_;
28 public:
29 testbuf()
32 testbuf(const std::basic_string<CharT>& str)
33 : str_(str)
35 base::setg(const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data() + str_.size()));
40 std::basic_string<CharT> str() const
41 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
43 protected:
45 virtual typename base::int_type
46 overflow(typename base::int_type ch = base::traits_type::eof())
48 if (ch != base::traits_type::eof())
50 int n = static_cast<int>(str_.size());
51 str_.push_back(static_cast<CharT>(ch));
52 str_.resize(str_.capacity());
53 base::setp(const_cast<CharT*>(str_.data()),
54 const_cast<CharT*>(str_.data() + str_.size()));
55 base::pbump(n+1);
57 return ch;
61 int main(int, char**)
64 testbuf<char> sb("testing\n...");
65 std::istream is(&sb);
66 testbuf<char> sb2;
67 is.get(sb2);
68 assert(sb2.str() == "testing");
69 assert(is.good());
70 assert(is.gcount() == 7);
71 assert(is.get() == '\n');
72 is.get(sb2);
73 assert(sb2.str() == "testing...");
74 assert(is.eof());
75 assert(!is.fail());
76 assert(is.gcount() == 3);
78 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
80 testbuf<wchar_t> sb(L"testing\n...");
81 std::wistream is(&sb);
82 testbuf<wchar_t> sb2;
83 is.get(sb2);
84 assert(sb2.str() == L"testing");
85 assert(is.good());
86 assert(is.gcount() == 7);
87 assert(is.get() == L'\n');
88 is.get(sb2);
89 assert(sb2.str() == L"testing...");
90 assert(is.eof());
91 assert(!is.fail());
92 assert(is.gcount() == 3);
94 #endif
95 #ifndef TEST_HAS_NO_EXCEPTIONS
97 testbuf<char> sb(" ");
98 std::basic_istream<char> is(&sb);
99 testbuf<char> sb2;
100 is.exceptions(std::ios_base::eofbit);
101 bool threw = false;
102 try {
103 is.get(sb2);
104 } catch (std::ios_base::failure&) {
105 threw = true;
107 assert(threw);
108 assert(!is.bad());
109 assert( is.eof());
110 assert(!is.fail());
112 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
114 testbuf<wchar_t> sb(L" ");
115 std::basic_istream<wchar_t> is(&sb);
116 testbuf<wchar_t> sb2;
117 is.exceptions(std::ios_base::eofbit);
118 bool threw = false;
119 try {
120 is.get(sb2);
121 } catch (std::ios_base::failure&) {
122 threw = true;
124 assert(threw);
125 assert(!is.bad());
126 assert( is.eof());
127 assert(!is.fail());
129 #endif
132 testbuf<char> sb;
133 std::basic_istream<char> is(&sb);
134 testbuf<char> sb2;
135 is.exceptions(std::ios_base::eofbit);
136 bool threw = false;
137 try {
138 is.get(sb2);
139 } catch (std::ios_base::failure&) {
140 threw = true;
142 assert(threw);
143 assert(!is.bad());
144 assert( is.eof());
145 assert( is.fail());
147 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
149 testbuf<wchar_t> sb;
150 std::basic_istream<wchar_t> is(&sb);
151 testbuf<wchar_t> sb2;
152 is.exceptions(std::ios_base::eofbit);
153 bool threw = false;
154 try {
155 is.get(sb2);
156 } catch (std::ios_base::failure&) {
157 threw = true;
159 assert(threw);
160 assert(!is.bad());
161 assert( is.eof());
162 assert( is.fail());
164 #endif
165 #endif // TEST_HAS_NO_EXCEPTIONS
167 return 0;