Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.nonmembers / string.io / get_line.pass.cpp
blob7bcb34135440bfb64d9d07bbfbcb70da5fd23144
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);
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
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");
42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
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");
56 #endif
58 #ifndef TEST_HAS_NO_EXCEPTIONS
60 std::basic_stringbuf<char> sb("hello");
61 std::basic_istream<char> is(&sb);
62 is.exceptions(std::ios_base::eofbit);
64 S s;
65 bool threw = false;
66 try {
67 std::getline(is, s);
68 } catch (std::ios::failure const&) {
69 threw = true;
72 assert(!is.bad());
73 assert(!is.fail());
74 assert(is.eof());
75 assert(threw);
76 assert(s == "hello");
78 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
80 std::basic_stringbuf<wchar_t> sb(L"hello");
81 std::basic_istream<wchar_t> is(&sb);
82 is.exceptions(std::ios_base::eofbit);
84 WS s;
85 bool threw = false;
86 try {
87 std::getline(is, s);
88 } catch (std::ios::failure const&) {
89 threw = true;
92 assert(!is.bad());
93 assert(!is.fail());
94 assert(is.eof());
95 assert(threw);
96 assert(s == L"hello");
98 # endif
101 std::basic_stringbuf<char> sb;
102 std::basic_istream<char> is(&sb);
103 is.exceptions(std::ios_base::failbit);
105 S s;
106 bool threw = false;
107 try {
108 std::getline(is, s);
109 } catch (std::ios::failure const&) {
110 threw = true;
113 assert(!is.bad());
114 assert(is.fail());
115 assert(is.eof());
116 assert(threw);
117 assert(s == "");
119 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
121 std::basic_stringbuf<wchar_t> sb;
122 std::basic_istream<wchar_t> is(&sb);
123 is.exceptions(std::ios_base::failbit);
125 WS s;
126 bool threw = false;
127 try {
128 std::getline(is, s);
129 } catch (std::ios::failure const&) {
130 threw = true;
133 assert(!is.bad());
134 assert(is.fail());
135 assert(is.eof());
136 assert(threw);
137 assert(s == L"");
139 # endif
140 #endif // TEST_HAS_NO_EXCEPTIONS
143 int main(int, char**) {
144 test_string<std::allocator>();
145 #if TEST_STD_VER >= 11
146 test_string<min_allocator>();
147 #endif
149 return 0;