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 // <string>
10
11 // template<class charT, class traits, class Allocator>
12 // basic_istream<charT,traits>&
13 // getline(basic_istream<charT,traits>& is,
14 // basic_string<charT,traits,Allocator>& str, charT delim);
15
16 #include <string>
17 #include <sstream>
18 #include <cassert>
19
20 #include "min_allocator.h"
21 #include "test_macros.h"
22
main(int,char **)23 int main(int, char**)
24 {
25 {
26 std::istringstream in(" abc* def** ghij");
27 std::string s("initial text");
28 getline(in, s, '*');
29 assert(in.good());
30 assert(s == " abc");
31 getline(in, s, '*');
32 assert(in.good());
33 assert(s == " def");
34 getline(in, s, '*');
35 assert(in.good());
36 assert(s == "");
37 getline(in, s, '*');
38 assert(in.eof());
39 assert(s == " ghij");
40 }
41 {
42 std::wistringstream in(L" abc* def** ghij");
43 std::wstring s(L"initial text");
44 getline(in, s, L'*');
45 assert(in.good());
46 assert(s == L" abc");
47 getline(in, s, L'*');
48 assert(in.good());
49 assert(s == L" def");
50 getline(in, s, L'*');
51 assert(in.good());
52 assert(s == L"");
53 getline(in, s, L'*');
54 assert(in.eof());
55 assert(s == L" ghij");
56 }
57 #if TEST_STD_VER >= 11
58 {
59 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
60 std::istringstream in(" abc* def** ghij");
61 S s("initial text");
62 getline(in, s, '*');
63 assert(in.good());
64 assert(s == " abc");
65 getline(in, s, '*');
66 assert(in.good());
67 assert(s == " def");
68 getline(in, s, '*');
69 assert(in.good());
70 assert(s == "");
71 getline(in, s, '*');
72 assert(in.eof());
73 assert(s == " ghij");
74 }
75 {
76 typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
77 std::wistringstream in(L" abc* def** ghij");
78 S s(L"initial text");
79 getline(in, s, L'*');
80 assert(in.good());
81 assert(s == L" abc");
82 getline(in, s, L'*');
83 assert(in.good());
84 assert(s == L" def");
85 getline(in, s, L'*');
86 assert(in.good());
87 assert(s == L"");
88 getline(in, s, L'*');
89 assert(in.eof());
90 assert(s == L" ghij");
91 }
92 #endif
93 #ifndef TEST_HAS_NO_EXCEPTIONS
94 {
95 std::basic_stringbuf<char> sb("hello");
96 std::basic_istream<char> is(&sb);
97 is.exceptions(std::ios::eofbit);
98
99 std::basic_string<char> s;
100 bool threw = false;
101 try {
102 std::getline(is, s, '\n');
103 } catch (std::ios::failure const&) {
104 threw = true;
105 }
106
107 assert(!is.bad());
108 assert(!is.fail());
109 assert( is.eof());
110 assert(threw);
111 assert(s == "hello");
112 }
113 {
114 std::basic_stringbuf<wchar_t> sb(L"hello");
115 std::basic_istream<wchar_t> is(&sb);
116 is.exceptions(std::ios::eofbit);
117
118 std::basic_string<wchar_t> s;
119 bool threw = false;
120 try {
121 std::getline(is, s, L'\n');
122 } catch (std::ios::failure const&) {
123 threw = true;
124 }
125
126 assert(!is.bad());
127 assert(!is.fail());
128 assert( is.eof());
129 assert(threw);
130 assert(s == L"hello");
131 }
132 {
133 std::basic_stringbuf<char> sb;
134 std::basic_istream<char> is(&sb);
135 is.exceptions(std::ios::failbit);
136
137 std::basic_string<char> s;
138 bool threw = false;
139 try {
140 std::getline(is, s, '\n');
141 } catch (std::ios::failure const&) {
142 threw = true;
143 }
144
145 assert(!is.bad());
146 assert( is.fail());
147 assert( is.eof());
148 assert(threw);
149 assert(s == "");
150 }
151 {
152 std::basic_stringbuf<wchar_t> sb;
153 std::basic_istream<wchar_t> is(&sb);
154 is.exceptions(std::ios::failbit);
155
156 std::basic_string<wchar_t> s;
157 bool threw = false;
158 try {
159 std::getline(is, s, L'\n');
160 } catch (std::ios::failure const&) {
161 threw = true;
162 }
163
164 assert(!is.bad());
165 assert( is.fail());
166 assert( is.eof());
167 assert(threw);
168 assert(s == L"");
169 }
170 #endif // TEST_HAS_NO_EXCEPTIONS
171
172 return 0;
173 }
174