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