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