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