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