[gn build] Port 0154dce8d39d
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.nonmembers / string.io / get_line_delim.pass.cpp
blob97a55fa09d14df007887b4298621870312cc5467
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 //===----------------------------------------------------------------------===//
9 // <string>
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);
16 #include <string>
17 #include <sstream>
18 #include <cassert>
20 #include "min_allocator.h"
21 #include "test_macros.h"
23 template <template <class> class Alloc>
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
31 std::istringstream in(" abc* def** ghij");
32 S s("initial text");
33 std::getline(in, s, '*');
34 assert(in.good());
35 assert(s == " abc");
36 std::getline(in, s, '*');
37 assert(in.good());
38 assert(s == " def");
39 std::getline(in, s, '*');
40 assert(in.good());
41 assert(s == "");
42 std::getline(in, s, '*');
43 assert(in.eof());
44 assert(s == " ghij");
46 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
48 std::wistringstream in(L" abc* def** ghij");
49 WS s(L"initial text");
50 std::getline(in, s, L'*');
51 assert(in.good());
52 assert(s == L" abc");
53 std::getline(in, s, L'*');
54 assert(in.good());
55 assert(s == L" def");
56 std::getline(in, s, L'*');
57 assert(in.good());
58 assert(s == L"");
59 std::getline(in, s, L'*');
60 assert(in.eof());
61 assert(s == L" ghij");
63 #endif
65 #ifndef TEST_HAS_NO_EXCEPTIONS
67 std::basic_stringbuf<char> sb("hello");
68 std::basic_istream<char> is(&sb);
69 is.exceptions(std::ios::eofbit);
71 S s;
72 bool threw = false;
73 try {
74 std::getline(is, s, '\n');
75 } catch (std::ios::failure const&) {
76 threw = true;
79 assert(!is.bad());
80 assert(!is.fail());
81 assert(is.eof());
82 assert(threw);
83 assert(s == "hello");
85 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
87 std::basic_stringbuf<wchar_t> sb(L"hello");
88 std::basic_istream<wchar_t> is(&sb);
89 is.exceptions(std::ios::eofbit);
91 WS s;
92 bool threw = false;
93 try {
94 std::getline(is, s, L'\n');
95 } catch (std::ios::failure const&) {
96 threw = true;
99 assert(!is.bad());
100 assert(!is.fail());
101 assert(is.eof());
102 assert(threw);
103 assert(s == L"hello");
105 # endif // TEST_HAS_NO_WIDE_CHARACTERS
107 std::basic_stringbuf<char> sb;
108 std::basic_istream<char> is(&sb);
109 is.exceptions(std::ios::failbit);
111 S s;
112 bool threw = false;
113 try {
114 std::getline(is, s, '\n');
115 } catch (std::ios::failure const&) {
116 threw = true;
119 assert(!is.bad());
120 assert(is.fail());
121 assert(is.eof());
122 assert(threw);
123 assert(s == "");
125 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
127 std::basic_stringbuf<wchar_t> sb;
128 std::basic_istream<wchar_t> is(&sb);
129 is.exceptions(std::ios::failbit);
131 WS s;
132 bool threw = false;
133 try {
134 std::getline(is, s, L'\n');
135 } catch (std::ios::failure const&) {
136 threw = true;
139 assert(!is.bad());
140 assert(is.fail());
141 assert(is.eof());
142 assert(threw);
143 assert(s == L"");
145 # endif // TEST_HAS_NO_WIDE_CHARACTERS
146 #endif // TEST_HAS_NO_EXCEPTIONS
149 int main(int, char**) {
150 test_string<std::allocator>();
151 #if TEST_STD_VER >= 11
152 test_string<min_allocator>();
153 #endif
154 return 0;