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