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>& get(basic_streambuf<char_type,traits>& sb,
15// char_type delim);
16
17#include <istream>
18#include <cassert>
19#include <streambuf>
20
21#include "test_macros.h"
22
23template <class CharT>
24class testbuf
25 : public std::basic_streambuf<CharT>
26{
27 typedef std::basic_streambuf<CharT> base;
28 std::basic_string<CharT> str_;
29public:
30 testbuf()
31 {
32 }
33 testbuf(const std::basic_string<CharT>& 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 std::basic_string<CharT> str() const
42 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
43
44protected:
45
46 virtual typename base::int_type
47 overflow(typename base::int_type ch = base::traits_type::eof())
48 {
49 if (ch != base::traits_type::eof())
50 {
51 int n = static_cast<int>(str_.size());
52 str_.push_back(static_cast<CharT>(ch));
53 str_.resize(str_.capacity());
54 base::setp(const_cast<CharT*>(str_.data()),
55 const_cast<CharT*>(str_.data() + str_.size()));
56 base::pbump(n+1);
57 }
58 return ch;
59 }
60};
61
62int main(int, char**)
63{
64 {
65 testbuf<char> sb("testing*...");
66 std::istream is(&sb);
67 testbuf<char> sb2;
68 is.get(sb&: sb2, delim: '*');
69 assert(sb2.str() == "testing");
70 assert(is.good());
71 assert(is.gcount() == 7);
72 assert(is.get() == '*');
73 is.get(sb&: sb2, delim: '*');
74 assert(sb2.str() == "testing...");
75 assert(is.eof());
76 assert(!is.fail());
77 assert(is.gcount() == 3);
78 }
79#ifndef TEST_HAS_NO_WIDE_CHARACTERS
80 {
81 testbuf<wchar_t> sb(L"testing*...");
82 std::wistream is(&sb);
83 testbuf<wchar_t> sb2;
84 is.get(sb2, L'*');
85 assert(sb2.str() == L"testing");
86 assert(is.good());
87 assert(is.gcount() == 7);
88 assert(is.get() == L'*');
89 is.get(sb2, L'*');
90 assert(sb2.str() == L"testing...");
91 assert(is.eof());
92 assert(!is.fail());
93 assert(is.gcount() == 3);
94 }
95#endif // TEST_HAS_NO_WIDE_CHARACTERS
96#ifndef TEST_HAS_NO_EXCEPTIONS
97 {
98 testbuf<char> sb(" ");
99 std::basic_istream<char> is(&sb);
100 testbuf<char> sb2;
101 is.exceptions(std::ios_base::eofbit);
102 bool threw = false;
103 try {
104 is.get(sb2, '*');
105 } catch (std::ios_base::failure&) {
106 threw = true;
107 }
108 assert(threw);
109 assert(!is.bad());
110 assert( is.eof());
111 assert(!is.fail());
112 }
113#ifndef TEST_HAS_NO_WIDE_CHARACTERS
114 {
115 testbuf<wchar_t> sb(L" ");
116 std::basic_istream<wchar_t> is(&sb);
117 testbuf<wchar_t> sb2;
118 is.exceptions(std::ios_base::eofbit);
119 bool threw = false;
120 try {
121 is.get(sb2, L'*');
122 } catch (std::ios_base::failure&) {
123 threw = true;
124 }
125 assert(threw);
126 assert(!is.bad());
127 assert( is.eof());
128 assert(!is.fail());
129 }
130#endif // TEST_HAS_NO_WIDE_CHARACTERS
131
132 {
133 testbuf<char> sb;
134 std::basic_istream<char> is(&sb);
135 testbuf<char> sb2;
136 is.exceptions(std::ios_base::eofbit);
137 bool threw = false;
138 try {
139 is.get(sb2, '*');
140 } catch (std::ios_base::failure&) {
141 threw = true;
142 }
143 assert(threw);
144 assert(!is.bad());
145 assert( is.eof());
146 assert( is.fail());
147 }
148#ifndef TEST_HAS_NO_WIDE_CHARACTERS
149 {
150 testbuf<wchar_t> sb;
151 std::basic_istream<wchar_t> is(&sb);
152 testbuf<wchar_t> sb2;
153 is.exceptions(std::ios_base::eofbit);
154 bool threw = false;
155 try {
156 is.get(sb2, L'*');
157 } catch (std::ios_base::failure&) {
158 threw = true;
159 }
160 assert(threw);
161 assert(!is.bad());
162 assert( is.eof());
163 assert( is.fail());
164 }
165#endif // TEST_HAS_NO_WIDE_CHARACTERS
166#endif // TEST_HAS_NO_EXCEPTIONS
167
168 return 0;
169}
170

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