Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / input.streams / istream.unformatted / get_streambuf.pass.cpp
blob331e38caefe4a691f189b29b9f084320f7a5a5c8
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 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14}}
11 // <istream>
13 // basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb);
15 #include <istream>
16 #include <cassert>
18 #include "test_macros.h"
20 template <class CharT>
21 class testbuf
22 : public std::basic_streambuf<CharT>
24 typedef std::basic_streambuf<CharT> base;
25 std::basic_string<CharT> str_;
26 public:
27 testbuf()
30 testbuf(const std::basic_string<CharT>& str)
31 : str_(str)
33 base::setg(const_cast<CharT*>(str_.data()),
34 const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data() + str_.size()));
38 std::basic_string<CharT> str() const
39 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
41 protected:
43 virtual typename base::int_type
44 overflow(typename base::int_type ch = base::traits_type::eof())
46 if (ch != base::traits_type::eof())
48 int n = static_cast<int>(str_.size());
49 str_.push_back(static_cast<CharT>(ch));
50 str_.resize(str_.capacity());
51 base::setp(const_cast<CharT*>(str_.data()),
52 const_cast<CharT*>(str_.data() + str_.size()));
53 base::pbump(n+1);
55 return ch;
59 int main(int, char**)
62 testbuf<char> sb("testing\n...");
63 std::istream is(&sb);
64 testbuf<char> sb2;
65 is.get(sb2);
66 assert(sb2.str() == "testing");
67 assert(is.good());
68 assert(is.gcount() == 7);
69 assert(is.get() == '\n');
70 is.get(sb2);
71 assert(sb2.str() == "testing...");
72 assert(is.eof());
73 assert(!is.fail());
74 assert(is.gcount() == 3);
76 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78 testbuf<wchar_t> sb(L"testing\n...");
79 std::wistream is(&sb);
80 testbuf<wchar_t> sb2;
81 is.get(sb2);
82 assert(sb2.str() == L"testing");
83 assert(is.good());
84 assert(is.gcount() == 7);
85 assert(is.get() == L'\n');
86 is.get(sb2);
87 assert(sb2.str() == L"testing...");
88 assert(is.eof());
89 assert(!is.fail());
90 assert(is.gcount() == 3);
92 #endif
93 #ifndef TEST_HAS_NO_EXCEPTIONS
95 testbuf<char> sb(" ");
96 std::basic_istream<char> is(&sb);
97 testbuf<char> sb2;
98 is.exceptions(std::ios_base::eofbit);
99 bool threw = false;
100 try {
101 is.get(sb2);
102 } catch (std::ios_base::failure&) {
103 threw = true;
105 assert(threw);
106 assert(!is.bad());
107 assert( is.eof());
108 assert(!is.fail());
110 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
112 testbuf<wchar_t> sb(L" ");
113 std::basic_istream<wchar_t> is(&sb);
114 testbuf<wchar_t> sb2;
115 is.exceptions(std::ios_base::eofbit);
116 bool threw = false;
117 try {
118 is.get(sb2);
119 } catch (std::ios_base::failure&) {
120 threw = true;
122 assert(threw);
123 assert(!is.bad());
124 assert( is.eof());
125 assert(!is.fail());
127 #endif
130 testbuf<char> sb;
131 std::basic_istream<char> is(&sb);
132 testbuf<char> sb2;
133 is.exceptions(std::ios_base::eofbit);
134 bool threw = false;
135 try {
136 is.get(sb2);
137 } catch (std::ios_base::failure&) {
138 threw = true;
140 assert(threw);
141 assert(!is.bad());
142 assert( is.eof());
143 assert( is.fail());
145 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
147 testbuf<wchar_t> sb;
148 std::basic_istream<wchar_t> is(&sb);
149 testbuf<wchar_t> sb2;
150 is.exceptions(std::ios_base::eofbit);
151 bool threw = false;
152 try {
153 is.get(sb2);
154 } catch (std::ios_base::failure&) {
155 threw = true;
157 assert(threw);
158 assert(!is.bad());
159 assert( is.eof());
160 assert( is.fail());
162 #endif
163 #endif // TEST_HAS_NO_EXCEPTIONS
165 return 0;